Skip to content

Commit 97c6305

Browse files
author
Zoran Cvetkov
committed
cleanup
1 parent d4e5740 commit 97c6305

File tree

11 files changed

+55
-78
lines changed

11 files changed

+55
-78
lines changed

graph/src/data/store/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,21 @@ impl Entity {
913913
Id::try_from(self.get("id").unwrap().clone()).expect("the id is set to a valid value")
914914
}
915915

916-
// TODO: try to use it only for tests!
917-
// #[cfg(debug_assertions)]
916+
/// Return the VID of this entity and if its missing or of a type different than
917+
/// i64 it panics.
918918
pub fn vid(&self) -> i64 {
919919
self.get("vid")
920920
.expect("the vid is set")
921-
// .unwrap_or(&Value::Int8(0))
921+
.as_int8()
922+
.expect("the vid is set to a valid value")
923+
}
924+
925+
/// This version of the function returns 0 if the VID is not set. It should be
926+
/// used only in the testing code for more lenient definition of entities.
927+
#[cfg(debug_assertions)]
928+
pub fn vid_or_default(&self) -> i64 {
929+
self.get("vid")
930+
.unwrap_or(&Value::Int8(0))
922931
.as_int8()
923932
.expect("the vid is set to a valid value")
924933
}

runtime/test/src/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,8 @@ async fn test_entity_store(api_version: Version) {
10011001

10021002
let schema = store.input_schema(&deployment.hash).unwrap();
10031003

1004-
let alex = entity! { schema => id: "alex", name: "Alex", vid: 0i64};
1005-
let steve = entity! { schema => id: "steve", name: "Steve", vid: 1i64};
1004+
let alex = entity! { schema => id: "alex", name: "Alex", vid: 0i64 };
1005+
let steve = entity! { schema => id: "steve", name: "Steve", vid: 1i64 };
10061006
let user_type = schema.entity_type("User").unwrap();
10071007
test_store::insert_entities(
10081008
&deployment,

store/test-store/src/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ pub async fn insert_entities(
424424
entities: Vec<(EntityType, Entity)>,
425425
) -> Result<(), StoreError> {
426426
let insert_ops = entities.into_iter().map(|(entity_type, data)| {
427-
let vid = data.vid();
427+
let vid = data.vid_or_default();
428428
EntityOperation::Set {
429429
key: entity_type.key(data.id()),
430430
data: EntityV::new(data, vid),

store/test-store/tests/chain/ethereum/manifest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ specVersion: 0.0.2
272272
.unwrap();
273273

274274
// Adds an example entity.
275-
let thing = entity! { schema => id: "datthing", vid : 0i64 };
275+
let thing = entity! { schema => id: "datthing" };
276276
test_store::insert_entities(
277277
&deployment,
278278
vec![(schema.entity_type("Thing").unwrap(), thing)],
@@ -372,7 +372,7 @@ specVersion: 0.0.2
372372
msg
373373
);
374374

375-
let thing = entity! { schema => id: "datthing", vid : 1i64 };
375+
let thing = entity! { schema => id: "datthing", vid: 1i64 };
376376
test_store::insert_entities(
377377
&deployment,
378378
vec![(schema.entity_type("Thing").unwrap(), thing)],

store/test-store/tests/core/interfaces.rs

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async fn one_interface_one_entity() {
6969
type Animal implements Legged @entity { id: ID!, legs: Int }";
7070
let schema = InputSchema::raw(document, subgraph_id);
7171

72-
let entity = ("Animal", entity! { schema => id: "1", legs: 3, vid: 0i64 });
72+
let entity = ("Animal", entity! { schema => id: "1", legs: 3 });
7373

7474
// Collection query.
7575
let query = "query { leggeds(first: 100) { legs } }";
@@ -97,7 +97,7 @@ async fn one_interface_one_entity_typename() {
9797
type Animal implements Legged @entity { id: ID!, legs: Int }";
9898
let schema = InputSchema::raw(document, subgraph_id);
9999

100-
let entity = ("Animal", entity! { schema => id: "1", legs: 3, vid: 0i64 });
100+
let entity = ("Animal", entity! { schema => id: "1", legs: 3 });
101101

102102
let query = "query { leggeds(first: 100) { __typename } }";
103103

@@ -118,11 +118,8 @@ async fn one_interface_multiple_entities() {
118118
";
119119
let schema = InputSchema::raw(document, subgraph_id);
120120

121-
let animal = ("Animal", entity! { schema => id: "1", legs: 3, vid: 0i64 });
122-
let furniture = (
123-
"Furniture",
124-
entity! { schema => id: "2", legs: 4, vid: 0i64 },
125-
);
121+
let animal = ("Animal", entity! { schema => id: "1", legs: 3 });
122+
let furniture = ("Furniture", entity! { schema => id: "2", legs: 4 });
126123

127124
let query = "query { leggeds(first: 100, orderBy: legs) { legs } }";
128125

@@ -153,8 +150,8 @@ async fn reference_interface() {
153150

154151
let query = "query { leggeds(first: 100) { leg { id } } }";
155152

156-
let leg = ("Leg", entity! { schema => id: "1", vid: 0i64 });
157-
let animal = ("Animal", entity! { schema => id: "1", leg: 1, vid: 0i64 });
153+
let leg = ("Leg", entity! { schema => id: "1" });
154+
let animal = ("Animal", entity! { schema => id: "1", leg: 1 });
158155

159156
let res = insert_and_query(subgraph_id, document, vec![leg, animal], query)
160157
.await
@@ -209,11 +206,11 @@ async fn reference_interface_derived() {
209206
let sell2 = ("SellEvent", entity! { schema => id: "sell2", vid: 1i64 });
210207
let gift = (
211208
"GiftEvent",
212-
entity! { schema => id: "gift", transaction: "txn", vid: 0i64 },
209+
entity! { schema => id: "gift", transaction: "txn" },
213210
);
214211
let txn = (
215212
"Transaction",
216-
entity! { schema => id: "txn", buyEvent: "buy", sellEvents: vec!["sell1", "sell2"], vid: 0i64 },
213+
entity! { schema => id: "txn", buyEvent: "buy", sellEvents: vec!["sell1", "sell2"] },
217214
);
218215

219216
let entities = vec![buy, sell1, sell2, gift, txn];
@@ -308,11 +305,8 @@ async fn conflicting_implementors_id() {
308305
";
309306
let schema = InputSchema::raw(document, subgraph_id);
310307

311-
let animal = ("Animal", entity! { schema => id: "1", legs: 3, vid: 0i64 });
312-
let furniture = (
313-
"Furniture",
314-
entity! { schema => id: "1", legs: 3, vid: 0i64 },
315-
);
308+
let animal = ("Animal", entity! { schema => id: "1", legs: 3 });
309+
let furniture = ("Furniture", entity! { schema => id: "1", legs: 3 });
316310

317311
let query = "query { leggeds(first: 100) { legs } }";
318312

@@ -340,11 +334,8 @@ async fn derived_interface_relationship() {
340334
";
341335
let schema = InputSchema::raw(document, subgraph_id);
342336

343-
let forest = ("Forest", entity! { schema => id: "1", vid: 0i64 });
344-
let animal = (
345-
"Animal",
346-
entity! { schema => id: "1", forest: "1", vid: 0i64 },
347-
);
337+
let forest = ("Forest", entity! { schema => id: "1" });
338+
let animal = ("Animal", entity! { schema => id: "1", forest: "1" });
348339

349340
let query = "query { forests(first: 100) { dwellers(first: 100) { id } } }";
350341

@@ -371,12 +362,9 @@ async fn two_interfaces() {
371362
";
372363
let schema = InputSchema::raw(document, subgraph_id);
373364

374-
let a = ("A", entity! { schema => id: "1", foo: "bla", vid: 0i64 });
375-
let b = ("B", entity! { schema => id: "1", bar: 100, vid: 0i64 });
376-
let ab = (
377-
"AB",
378-
entity! { schema => id: "2", foo: "ble", bar: 200, vid: 0i64 },
379-
);
365+
let a = ("A", entity! { schema => id: "1", foo: "bla" });
366+
let b = ("B", entity! { schema => id: "1", bar: 100 });
367+
let ab = ("AB", entity! { schema => id: "2", foo: "ble", bar: 200 });
380368

381369
let query = "query {
382370
ibars(first: 100, orderBy: bar) { bar }
@@ -402,7 +390,7 @@ async fn interface_non_inline_fragment() {
402390

403391
let entity = (
404392
"Animal",
405-
entity! { schema => id: "1", name: "cow", legs: 3, vid: 0i64 },
393+
entity! { schema => id: "1", name: "cow", legs: 3 },
406394
);
407395

408396
// Query only the fragment.
@@ -434,12 +422,9 @@ async fn interface_inline_fragment() {
434422

435423
let animal = (
436424
"Animal",
437-
entity! { schema => id: "1", name: "cow", legs: 4, vid: 0i64 },
438-
);
439-
let bird = (
440-
"Bird",
441-
entity! { schema => id: "2", airspeed: 24, legs: 2, vid: 0i64 },
425+
entity! { schema => id: "1", name: "cow", legs: 4 },
442426
);
427+
let bird = ("Bird", entity! { schema => id: "2", airspeed: 24, legs: 2 });
443428

444429
let query =
445430
"query { leggeds(orderBy: legs) { ... on Animal { name } ...on Bird { airspeed } } }";
@@ -867,14 +852,8 @@ async fn merge_fields_not_in_interface() {
867852
}
868853
}";
869854

870-
let animal = (
871-
"Animal",
872-
entity! { schema => id: "cow", human: "fred", vid: 0i64 },
873-
);
874-
let human = (
875-
"Human",
876-
entity! { schema => id: "fred", animal: "cow", vid: 0i64 },
877-
);
855+
let animal = ("Animal", entity! { schema => id: "cow", human: "fred" });
856+
let human = ("Human", entity! { schema => id: "fred", animal: "cow" });
878857

879858
let res = insert_and_query(subgraph_id, document, vec![animal, human], query)
880859
.await
@@ -947,15 +926,15 @@ async fn nested_interface_fragments() {
947926
}
948927
}";
949928

950-
let foo = ("Foo", entity! { schema => id: "foo", vid: 0i64 });
951-
let one = ("One", entity! { schema => id: "1", foo1: "foo", vid: 0i64 });
929+
let foo = ("Foo", entity! { schema => id: "foo" });
930+
let one = ("One", entity! { schema => id: "1", foo1: "foo" });
952931
let two = (
953932
"Two",
954-
entity! { schema => id: "2", foo1: "foo", foo2: "foo", vid: 0i64 },
933+
entity! { schema => id: "2", foo1: "foo", foo2: "foo" },
955934
);
956935
let three = (
957936
"Three",
958-
entity! { schema => id: "3", foo1: "foo", foo2: "foo", foo3: "foo", vid: 0i64 },
937+
entity! { schema => id: "3", foo1: "foo", foo2: "foo", foo3: "foo" },
959938
);
960939

961940
let res = insert_and_query(subgraph_id, document, vec![foo, one, two, three], query)
@@ -1028,9 +1007,9 @@ async fn nested_interface_fragments_overlapping() {
10281007
}
10291008
}";
10301009

1031-
let foo = ("Foo", entity! { schema => id: "foo", vid: 0i64 });
1032-
let one = ("One", entity! { schema => id: "1", foo1: "foo", vid: 0i64 });
1033-
let two = ("Two", entity! { schema => id: "2", foo1: "foo", vid: 0i64 });
1010+
let foo = ("Foo", entity! { schema => id: "foo" });
1011+
let one = ("One", entity! { schema => id: "1", foo1: "foo" });
1012+
let two = ("Two", entity! { schema => id: "2", foo1: "foo" });
10341013
let res = insert_and_query(subgraph_id, document, vec![foo, one, two], query)
10351014
.await
10361015
.unwrap();
@@ -1305,13 +1284,10 @@ async fn mixed_mutability() {
13051284
let query = "query { events { id } }";
13061285

13071286
let entities = vec![
1308-
(
1309-
"Mutable",
1310-
entity! { schema => id: "mut0", name: "mut0", vid: 0i64 },
1311-
),
1287+
("Mutable", entity! { schema => id: "mut0", name: "mut0" }),
13121288
(
13131289
"Immutable",
1314-
entity! { schema => id: "immo0", name: "immo0", vid: 0i64 },
1290+
entity! { schema => id: "immo0", name: "immo0" },
13151291
),
13161292
];
13171293

@@ -1362,15 +1338,9 @@ async fn derived_interface_bytes() {
13621338
let query = "query { pools { trades { id } } }";
13631339

13641340
let entities = vec![
1365-
("Pool", entity! { schema => id: b("0xf001"), vid: 0i64 }),
1366-
(
1367-
"Sell",
1368-
entity! { schema => id: b("0xc0"), pool: "0xf001", vid: 0i64 },
1369-
),
1370-
(
1371-
"Buy",
1372-
entity! { schema => id: b("0xb0"), pool: "0xf001", vid: 0i64 },
1373-
),
1341+
("Pool", entity! { schema => id: b("0xf001") }),
1342+
("Sell", entity! { schema => id: b("0xc0"), pool: "0xf001" }),
1343+
("Buy", entity! { schema => id: b("0xb0"), pool: "0xf001" }),
13741344
];
13751345

13761346
let res = insert_and_query(subgraph_id, document, entities, query)

store/test-store/tests/graphql/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ async fn insert_test_entities(
426426
.map(|(typename, entities)| {
427427
let entity_type = schema.entity_type(typename).unwrap();
428428
entities.into_iter().map(move |data| {
429-
let vid = data.vid();
429+
let vid = data.vid_or_default();
430430
EntityOperation::Set {
431431
key: entity_type.key(data.id()),
432432
data: EntityV::new(data, vid),

store/test-store/tests/postgres/aggregation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub async fn insert(
8383
.map(|data| {
8484
let data_type = schema.entity_type("Data").unwrap();
8585
let key = data_type.key(data.id());
86-
let vid = data.vid();
86+
let vid = data.vid_or_default();
8787
EntityOperation::Set {
8888
data: EntityV::new(data, vid),
8989
key,

store/test-store/tests/postgres/relational.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,11 @@ lazy_static! {
205205
bigInt: big_int.clone(),
206206
bigIntArray: vec![big_int.clone(), (big_int + 1.into())],
207207
color: "yellow",
208-
vid: 0i64,
209208
}
210209
};
211210
static ref EMPTY_NULLABLESTRINGS_ENTITY: Entity = {
212211
entity! { THINGS_SCHEMA =>
213212
id: "one",
214-
vid: 0i64,
215213
}
216214
};
217215
static ref SCALAR_TYPE: EntityType = THINGS_SCHEMA.entity_type("Scalar").unwrap();

store/test-store/tests/postgres/relational_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn row_group_update(
8484
) -> RowGroup {
8585
let mut group = RowGroup::new(entity_type.clone(), false);
8686
for (key, data) in data {
87-
let vid = data.vid();
87+
let vid = data.vid_or_default();
8888
group
8989
.push(EntityModification::overwrite(key, data, block, vid), block)
9090
.unwrap();
@@ -99,7 +99,7 @@ pub fn row_group_insert(
9999
) -> RowGroup {
100100
let mut group = RowGroup::new(entity_type.clone(), false);
101101
for (key, data) in data {
102-
let vid = data.vid();
102+
let vid = data.vid_or_default();
103103
group
104104
.push(EntityModification::insert(key, data, block, vid), block)
105105
.unwrap();

store/test-store/tests/postgres/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ fn entity_changes_are_fired_and_forwarded_to_subscriptions() {
13161316
.iter()
13171317
.map(|(id, data)| EntityOperation::Set {
13181318
key: USER_TYPE.parse_key(id.as_str()).unwrap(),
1319-
data: EntityV::new(data.clone(), data.vid()),
1319+
data: EntityV::new(data.clone(), data.vid_or_default()),
13201320
})
13211321
.collect(),
13221322
)

0 commit comments

Comments
 (0)