Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed Nov 18, 2024
1 parent 330efe3 commit 3d9e05f
Showing 1 changed file with 46 additions and 51 deletions.
97 changes: 46 additions & 51 deletions store/test-store/tests/graph/entity_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use std::sync::Arc;
use web3::types::H256;

use graph_store_postgres::SubgraphStore as DieselSubgraphStore;
use test_store::store::filter_vid;
use test_store::*;

lazy_static! {
Expand Down Expand Up @@ -208,13 +207,13 @@ fn insert_modifications() {
let store = Arc::new(store);
let mut cache = EntityCache::new(store);

let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai"};
let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai" };
let mogwai_key = make_band_key("mogwai");
cache
.set(mogwai_key.clone(), EntityV::new(mogwai_data.clone(), 0))
.unwrap();

let sigurros_data = entity! { SCHEMA => id: "sigurros", name: "Sigur Ros"};
let sigurros_data = entity! { SCHEMA => id: "sigurros", name: "Sigur Ros" };
let sigurros_key = make_band_key("sigurros");
cache
.set(sigurros_key.clone(), EntityV::new(sigurros_data.clone(), 0))
Expand Down Expand Up @@ -254,7 +253,7 @@ fn overwrite_modifications() {
let store = Arc::new(store);
let mut cache = EntityCache::new(store);

let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai", founded: 1995};
let mogwai_data = entity! { SCHEMA => id: "mogwai", name: "Mogwai", founded: 1995 };
let mogwai_key = make_band_key("mogwai");
cache
.set(mogwai_key.clone(), EntityV::new(mogwai_data.clone(), 0))
Expand All @@ -281,9 +280,8 @@ fn consecutive_modifications() {
// Pre-populate the store with data so that we can test setting a field to
// `Value::Null`.
let store = {
let entities = vec![
entity! { SCHEMA => id: "mogwai", name: "Mogwai", label: "Chemikal Underground", vid: 0i64 },
];
let entities =
vec![entity! { SCHEMA => id: "mogwai", name: "Mogwai", label: "Chemikal Underground" }];

MockStore::new(entity_version_map("Band", entities))
};
Expand Down Expand Up @@ -311,7 +309,7 @@ fn consecutive_modifications() {
sort_by_entity_key(result.unwrap().modifications),
sort_by_entity_key(vec![EntityModification::overwrite(
update_key,
entity! { SCHEMA => id: "mogwai", name: "Mogwai", founded: 1995, vid: 0i64 },
entity! { SCHEMA => id: "mogwai", name: "Mogwai", founded: 1995 },
0,
0
)])
Expand Down Expand Up @@ -477,13 +475,12 @@ fn create_account_entity(id: &str, name: &str, email: &str, age: i32, vid: i64)
}
}

fn create_wallet_entity(id: &str, account_id: &Id, balance: i32, vid: i64) -> EntityV {
fn create_wallet_entity(id: &str, account_id: &Id, balance: i32) -> Entity {
let account_id = Value::from(account_id.clone());
let e = entity! { LOAD_RELATED_SUBGRAPH => id: id, account: account_id, balance: balance};
EntityV::new(e, vid)
entity! { LOAD_RELATED_SUBGRAPH => id: id, account: account_id, balance: balance }
}
fn create_wallet_operation(id: &str, account_id: &Id, balance: i32, vid: i64) -> EntityOperation {
let test_wallet = create_wallet_entity(id, account_id, balance, vid);
let test_wallet = EntityV::new(create_wallet_entity(id, account_id, balance), vid);
EntityOperation::Set {
key: WALLET_TYPE.parse_key(id).unwrap(),
data: test_wallet,
Expand All @@ -501,12 +498,12 @@ fn check_for_account_with_multiple_wallets() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_1 = create_wallet_entity("1", &account_id, 67_i32, 0).e;
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32, 1).e;
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32, 2).e;
let wallet_1 = create_wallet_entity("1", &account_id, 67_i32);
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32);
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32);
let expeted_vec = vec![wallet_1, wallet_2, wallet_3];

assert_eq!(result, filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

Expand All @@ -521,10 +518,10 @@ fn check_for_account_with_single_wallet() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_1 = create_wallet_entity("4", &account_id, 32_i32, 1).e;
let wallet_1 = create_wallet_entity("4", &account_id, 32_i32);
let expeted_vec = vec![wallet_1];

assert_eq!(result, filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

Expand Down Expand Up @@ -605,15 +602,14 @@ fn check_for_insert_async_store() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_1 = create_wallet_entity("4", &account_id, 32_i32, 21).e;
let wallet_2 = create_wallet_entity("5", &account_id, 79_i32, 22).e;
let wallet_3 = create_wallet_entity("6", &account_id, 200_i32, 23).e;
let wallet_1 = create_wallet_entity("4", &account_id, 32_i32);
let wallet_2 = create_wallet_entity("5", &account_id, 79_i32);
let wallet_3 = create_wallet_entity("6", &account_id, 200_i32);
let expeted_vec = vec![wallet_1, wallet_2, wallet_3];

assert_eq!(filter_vid(result), filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

#[test]
fn check_for_insert_async_not_related() {
run_store_test(|mut cache, store, deployment, _writable| async move {
Expand All @@ -638,12 +634,12 @@ fn check_for_insert_async_not_related() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_1 = create_wallet_entity("1", &account_id, 67_i32, 1).e;
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32, 2).e;
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32, 3).e;
let wallet_1 = create_wallet_entity("1", &account_id, 67_i32);
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32);
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32);
let expeted_vec = vec![wallet_1, wallet_2, wallet_3];

assert_eq!(result, filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

Expand All @@ -657,9 +653,8 @@ fn check_for_update_async_related() {
let new_data = match wallet_entity_update {
EntityOperation::Set { ref data, .. } => data.clone(),
_ => unreachable!(),
}
.e;
assert_ne!(writable.get(&entity_key).unwrap().unwrap(), new_data);
};
assert_ne!(writable.get(&entity_key).unwrap().unwrap(), new_data.e);
// insert a new wallet
transact_entity_operations(
&store,
Expand All @@ -677,11 +672,11 @@ fn check_for_update_async_related() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32, 12).e;
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32, 13).e;
let expeted_vec = vec![new_data, wallet_2, wallet_3];
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32);
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32);
let expeted_vec = vec![new_data.e, wallet_2, wallet_3];

assert_eq!(filter_vid(result), filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

Expand All @@ -707,55 +702,55 @@ fn check_for_delete_async_related() {
causality_region: CausalityRegion::ONCHAIN,
};
let result = cache.load_related(&request).unwrap();
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32, 2).e;
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32, 3).e;
let wallet_2 = create_wallet_entity("2", &account_id, 92_i32);
let wallet_3 = create_wallet_entity("3", &account_id, 192_i32);
let expeted_vec = vec![wallet_2, wallet_3];

assert_eq!(result, filter_vid(expeted_vec));
assert_eq!(result, expeted_vec);
});
}

#[test]
fn scoped_get() {
run_store_test(|mut cache, _store, _deployment, _writable| async move {
// Key for an existing entity that is in the store
let account1 = ACCOUNT_TYPE.parse_id("1").unwrap();
let key1 = WALLET_TYPE.parse_key("1").unwrap();
let wallet1 = create_wallet_entity("1", &account1, 67, 1);
let wallet1 = create_wallet_entity("1", &account1, 67);

// Create a new entity that is not in the store
let account5 = ACCOUNT_TYPE.parse_id("5").unwrap();
let wallet5 = create_wallet_entity("5", &account5, 100, 5);
let wallet5 = create_wallet_entity("5", &account5, 100);
let key5 = WALLET_TYPE.parse_key("5").unwrap();
cache.set(key5.clone(), wallet5.clone()).unwrap();
cache
.set(key5.clone(), EntityV::new(wallet5.clone(), 5))
.unwrap();

// For the new entity, we can retrieve it with either scope
let act5 = cache.get(&key5, GetScope::InBlock).unwrap();
assert_eq!(Some(&wallet5.e), act5.as_ref().map(|e| e.as_ref()));
assert_eq!(Some(&wallet5), act5.as_ref().map(|e| e.as_ref()));
let act5 = cache.get(&key5, GetScope::Store).unwrap();
assert_eq!(Some(&wallet5.e), act5.as_ref().map(|e| e.as_ref()));
assert_eq!(Some(&wallet5), act5.as_ref().map(|e| e.as_ref()));

// For an entity in the store, we can not get it `InBlock` but with
// `Store`
let act1 = cache.get(&key1, GetScope::InBlock).unwrap();
assert_eq!(None, act1);
let act1 = cache.get(&key1, GetScope::Store).unwrap();
assert_eq!(
filter_vid(vec![wallet1.e.clone()]),
vec![act1.as_ref().map(|e| e.as_ref()).unwrap().clone()]
);
assert_eq!(Some(&wallet1), act1.as_ref().map(|e| e.as_ref()));
// Even after reading from the store, the entity is not visible with
// `InBlock`
let act1 = cache.get(&key1, GetScope::InBlock).unwrap();
assert_eq!(None, act1);
// But if it gets updated, it becomes visible with either scope
let mut wallet1 = wallet1;
wallet1.e.set("balance", 70).unwrap();
cache.set(key1.clone(), wallet1.clone()).unwrap();
wallet1.set("balance", 70).unwrap();
cache
.set(key1.clone(), EntityV::new(wallet1.clone(), 1))
.unwrap();
let act1 = cache.get(&key1, GetScope::InBlock).unwrap();
assert_eq!(Some(&wallet1.e), act1.as_ref().map(|e| e.as_ref()));
assert_eq!(Some(&wallet1), act1.as_ref().map(|e| e.as_ref()));
let act1 = cache.get(&key1, GetScope::Store).unwrap();
assert_eq!(Some(&wallet1.e), act1.as_ref().map(|e| e.as_ref()));
assert_eq!(Some(&wallet1), act1.as_ref().map(|e| e.as_ref()));
})
}

Expand Down

0 comments on commit 3d9e05f

Please sign in to comment.