Skip to content

Commit

Permalink
wip: add fuzz-loop for tests using fake
Browse files Browse the repository at this point in the history
  • Loading branch information
cylewitruk committed Mar 4, 2024
1 parent 9f2e9e9 commit d1be1f7
Showing 1 changed file with 99 additions and 85 deletions.
184 changes: 99 additions & 85 deletions clarity/src/vm/database/tests/clarity_db_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,131 +6,145 @@ use crate::vm::{contracts::Contract, database::{clarity_store::ContractCommitmen

#[test]
fn insert_contract() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();

let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();

db.insert_contract(&contract_id, contract)
.expect("failed to insert contract into backing store");

let exists = sql_exists(
&mut store,
&format!(
"SELECT * FROM metadata_table WHERE key LIKE '%{}%'",
contract_id.to_string()
)
);
assert!(!exists);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();

let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();

db.insert_contract(&contract_id, contract)
.expect("failed to insert contract into backing store");

let exists = sql_exists(
&mut store,
&format!(
"SELECT * FROM metadata_table WHERE key LIKE '%{}%'",
contract_id.to_string()
)
);
assert!(!exists);
}
}

#[test]
fn get_contract() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();
db.begin();

let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();
let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();

db.insert_contract(&contract_id, contract.clone())
.expect("failed to insert contract into backing store");
db.insert_contract(&contract_id, contract.clone())
.expect("failed to insert contract into backing store");

let retrieved_contract = db.get_contract(&contract_id)
.expect("failed to retrieve contract from backing store");
let retrieved_contract = db.get_contract(&contract_id)
.expect("failed to retrieve contract from backing store");

assert_eq!(contract, retrieved_contract);
assert_eq!(contract, retrieved_contract);
}
}

#[test]
fn insert_contract_without_begin_should_fail() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();
let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();

db.insert_contract(&contract_id, contract)
.expect_err("inserting contract without a begin should fail");
db.insert_contract(&contract_id, contract)
.expect_err("inserting contract without a begin should fail");
}
}

#[test]
fn insert_contract_with_commit_should_exist_in_backing_store() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();
db.begin();

let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();
let contract: Contract = Faker.fake();
let contract_id = contract.contract_context.contract_identifier.clone();

db.insert_contract(&contract_id, contract.clone())
.expect("failed to insert contract into backing store");
db.insert_contract(&contract_id, contract.clone())
.expect("failed to insert contract into backing store");

db.commit().expect("failed to commit to backing store");
db.commit().expect("failed to commit to backing store");

let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'",
format!("clr-meta::{}::vm-metadata::9::contract", contract_id.to_string())
)
);
let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'",
format!("clr-meta::{}::vm-metadata::9::contract", contract_id.to_string())
)
);

assert_eq!(1, count);
assert_eq!(1, count);
}
}

#[test]
fn put_data_no_commit() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();
db.begin();

db.put("hello", &ContractCommitment {
block_height: 1,
hash: Sha512Trunc256Sum::from_data(&[1, 2, 3, 4])
}).expect("failed to put data");
db.put("hello", &ContractCommitment {
block_height: 1,
hash: Sha512Trunc256Sum::from_data(&[1, 2, 3, 4])
}).expect("failed to put data");

let count = sql_query_u32(&mut store, "SELECT COUNT(*) FROM data_table");
assert_eq!(0, count);
let count = sql_query_u32(&mut store, "SELECT COUNT(*) FROM data_table");
assert_eq!(0, count);
}
}

#[test]
fn put_data_with_commit_should_exist_in_backing_store() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect("failed to put data");

db.commit().expect("failed to commit to backing store");

let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM data_table where key = '{}'", key)
);
assert_eq!(1, count);
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

db.begin();

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect("failed to put data");

db.commit().expect("failed to commit to backing store");

let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM data_table where key = '{}'", key)
);
assert_eq!(1, count);
}
}

#[test]
fn put_data_without_begin_fails() {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect_err("expected not-nested error");
for _ in 0..1000 {
let mut store = MemoryBackingStore::new();
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect_err("expected not-nested error");
}
}

/// Executes the provided SQL query, which is expected to return a positive
Expand Down

0 comments on commit d1be1f7

Please sign in to comment.