Skip to content

Commit

Permalink
feat: add test for inscribe_verify
Browse files Browse the repository at this point in the history
  • Loading branch information
yubing744 committed Sep 2, 2024
1 parent 9f5b577 commit e6ca471
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
37 changes: 37 additions & 0 deletions examples/bitseed_generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Inscribe Generate Contract

A CosmWasm smart contract for generating and verifying custom attributes for digital assets or NFTs.

## Features

- Custom attribute generation rules (e.g., range, list)
- Deterministic generation based on seed and user input
- Attribute verification
- Extensible design

## Build and Test

### Prerequisites

- Rust and Cargo (latest stable version)

### Build

```
cargo build
```

### Test

```
cargo test
```

## Usage

Deploy to a CosmWasm-compatible blockchain and interact using:
- `InscribeGenerate`: Generate attributes
- `InscribeVerify`: Verify attributes
- `IndexerGenerate`: Generate for indexing

Refer to the contract's query messages for input/output structures.
107 changes: 106 additions & 1 deletion examples/bitseed_generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,109 @@ fn hash_str_uint32(str: &str) -> u32 {
}

hash
}
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::from_json;

#[test]
fn test_inscribe_generate() {
// Step 1: Set up the test environment
let deps = mock_dependencies();
let env = mock_env();

// Step 2: Prepare test data
let input = InputData {
seed: "test_seed".to_string(),
user_input: "test_user_input".to_string(),
attributes: vec![
AttributeGenerateRule {
key: "test_range".to_string(),
rule_type: AttributeRuleType::Range,
parameters: vec!["1".to_string(), "10".to_string()],
},
AttributeGenerateRule {
key: "test_list".to_string(),
rule_type: AttributeRuleType::List,
parameters: vec!["A".to_string(), "B".to_string(), "C".to_string()],
},
],
};

// Step 3: Call the query function
let query_msg = QueryMsg::InscribeGenerate(input);
let binary_response = query(deps.as_ref(), env, query_msg).unwrap();

// Step 4: Parse the response
let output: OutputData = from_json(&binary_response).unwrap();

// Step 5: Validate the results
assert_eq!(output.amount, 1000);
assert_eq!(output.attributes.len(), 3); // id + test_range + test_list

// Validate id attribute
assert_eq!(output.attributes[0].key, "id");
assert_eq!(output.attributes[0].value, "test_user_input");

// Validate test_range attribute
assert_eq!(output.attributes[1].key, "test_range");
let range_value: u32 = output.attributes[1].value.parse().unwrap();
assert!(range_value >= 1 && range_value <= 10);

// Validate test_list attribute
assert_eq!(output.attributes[2].key, "test_list");
assert!(["A", "B", "C"].contains(&output.attributes[2].value.as_str()));

// Verify that content is empty
assert_eq!(output.content, Binary::default());
}

#[test]
fn test_inscribe_verify() {
// Set up dependencies and environment
let deps = mock_dependencies();
let env = mock_env();

// Prepare test data
let input = InputData {
seed: "test_seed".to_string(),
user_input: "test_user_input".to_string(),
attributes: vec![
AttributeGenerateRule {
key: "test_range".to_string(),
rule_type: AttributeRuleType::Range,
parameters: vec!["1".to_string(), "10".to_string()],
},
],
};

// Generate expected output
let expected_output = query_inscribe_generate(deps.as_ref(), input.clone()).unwrap();

// Test verification with correct output
let query_msg = QueryMsg::InscribeVerify {
input: input.clone(),
output: expected_output.clone(),
};
let binary_response = query(deps.as_ref(), env.clone(), query_msg).unwrap();
let result: bool = from_json(&binary_response).unwrap();
assert!(result);

// Test verification with incorrect output
let incorrect_output = OutputData {
amount: 999, // Incorrect amount
..expected_output
};
let query_msg = QueryMsg::InscribeVerify {
input,
output: incorrect_output,
};
let binary_response = query(deps.as_ref(), env, query_msg).unwrap();
let result: bool = from_json(&binary_response).unwrap();
assert!(!result);
}

}

0 comments on commit e6ca471

Please sign in to comment.