1- use graph:: blockchain:: block_stream:: FirehoseCursor ;
1+ use graph:: blockchain:: block_stream:: { EntityWithType , FirehoseCursor } ;
22use graph:: data:: subgraph:: schema:: DeploymentCreate ;
33use graph:: data:: value:: Word ;
44use graph:: data_source:: CausalityRegion ;
55use graph:: schema:: { EntityKey , EntityType , InputSchema } ;
66use lazy_static:: lazy_static;
7- use std:: collections:: BTreeSet ;
7+ use std:: collections:: { BTreeMap , BTreeSet } ;
88use std:: marker:: PhantomData ;
99use std:: ops:: Range ;
1010use test_store:: * ;
@@ -137,49 +137,40 @@ async fn insert_count(
137137 deployment : & DeploymentLocator ,
138138 block : u8 ,
139139 count : u8 ,
140- counter_type : & EntityType ,
141- id : & str ,
140+ immutable : bool ,
142141) {
143- let count_key_local = |id : & str | counter_type. parse_key ( id) . unwrap ( ) ;
142+ let count_key_local = |counter_type : & EntityType , id : & str | counter_type. parse_key ( id) . unwrap ( ) ;
144143 let data = entity ! { TEST_SUBGRAPH_SCHEMA =>
145- id: id ,
146- count : count as i32 ,
144+ id: "1" ,
145+ count: count as i32
147146 } ;
148- let entity_op = EntityOperation :: Set {
149- key : count_key_local ( & data. get ( "id" ) . unwrap ( ) . to_string ( ) ) ,
150- data,
147+ let entity_op = if ( block != 3 && block != 5 && block != 7 ) || !immutable {
148+ EntityOperation :: Set {
149+ key : count_key_local ( & COUNTER_TYPE , & data. get ( "id" ) . unwrap ( ) . to_string ( ) ) ,
150+ data,
151+ }
152+ } else {
153+ EntityOperation :: Remove {
154+ key : count_key_local ( & COUNTER_TYPE , & data. get ( "id" ) . unwrap ( ) . to_string ( ) ) ,
155+ }
151156 } ;
152- transact_entity_operations ( store, deployment, block_pointer ( block) , vec ! [ entity_op] )
157+ let mut ops = vec ! [ entity_op] ;
158+ if immutable && block < 6 {
159+ let data = entity ! { TEST_SUBGRAPH_SCHEMA =>
160+ id: & block. to_string( ) ,
161+ count : count as i32 ,
162+ } ;
163+ let entity_op = EntityOperation :: Set {
164+ key : count_key_local ( & COUNTER2_TYPE , & data. get ( "id" ) . unwrap ( ) . to_string ( ) ) ,
165+ data,
166+ } ;
167+ ops. push ( entity_op) ;
168+ }
169+ transact_entity_operations ( store, deployment, block_pointer ( block) , ops)
153170 . await
154171 . unwrap ( ) ;
155172}
156173
157- async fn insert_count_mutable (
158- store : & Arc < DieselSubgraphStore > ,
159- deployment : & DeploymentLocator ,
160- block : u8 ,
161- count : u8 ,
162- ) {
163- insert_count ( store, deployment, block, count, & COUNTER_TYPE , "1" ) . await ;
164- }
165-
166- async fn insert_count_immutable (
167- store : & Arc < DieselSubgraphStore > ,
168- deployment : & DeploymentLocator ,
169- block : u8 ,
170- count : u8 ,
171- ) {
172- insert_count (
173- store,
174- deployment,
175- block,
176- count,
177- & COUNTER2_TYPE ,
178- & ( block / 2 ) . to_string ( ) ,
179- )
180- . await ;
181- }
182-
183174async fn pause_writer ( deployment : & DeploymentLocator ) {
184175 flush ( deployment) . await . unwrap ( ) ;
185176 writable:: allow_steps ( deployment, 0 ) . await ;
@@ -205,13 +196,13 @@ where
205196 }
206197
207198 for count in 1 ..4 {
208- insert_count_mutable ( & subgraph_store, & deployment, count, count) . await ;
199+ insert_count ( & subgraph_store, & deployment, count, count, false ) . await ;
209200 }
210201
211202 // Test reading back with pending writes to the same entity
212203 pause_writer ( & deployment) . await ;
213204 for count in 4 ..7 {
214- insert_count_mutable ( & subgraph_store, & deployment, count, count) . await ;
205+ insert_count ( & subgraph_store, & deployment, count, count, false ) . await ;
215206 }
216207 assert_eq ! ( 6 , read_count( ) ) ;
217208
@@ -220,7 +211,7 @@ where
220211
221212 // Test reading back with pending writes and a pending revert
222213 for count in 7 ..10 {
223- insert_count_mutable ( & subgraph_store, & deployment, count, count) . await ;
214+ insert_count ( & subgraph_store, & deployment, count, count, false ) . await ;
224215 }
225216 writable
226217 . revert_block_operations ( block_pointer ( 2 ) , FirehoseCursor :: None )
@@ -370,12 +361,22 @@ async fn read_range(
370361
371362#[ test]
372363fn read_range_test ( ) {
364+ let result_entities = vec ! [
365+ r#"(1, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(2), id: String("1") }, vid: 1 }])"# ,
366+ r#"(2, [EntityWithType { entity_op: Modify, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(4), id: String("2") }, vid: 2 }])"# ,
367+ r#"(3, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(4), id: String("1") }, vid: 2 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(6), id: String("3") }, vid: 3 }])"# ,
368+ r#"(4, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(8), id: String("4") }, vid: 4 }])"# ,
369+ r#"(5, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(8), id: String("1") }, vid: 3 }, EntityWithType { entity_op: Create, entity_type: EntityType(Counter2), entity: Entity { count: Int(10), id: String("5") }, vid: 5 }])"# ,
370+ r#"(6, [EntityWithType { entity_op: Create, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"# ,
371+ r#"(7, [EntityWithType { entity_op: Delete, entity_type: EntityType(Counter), entity: Entity { count: Int(12), id: String("1") }, vid: 4 }])"# ,
372+ ] ;
373+
373374 run_test (
374375 |store, writable, sourceable : Arc < dyn SourceableStore > , deployment| async move {
375376 let subgraph_store = store. subgraph_store ( ) ;
376377 writable. deployment_synced ( ) . unwrap ( ) ;
377378
378- // Test individual types with sourceable store
379+ // First test sourceable store with individual types
379380 let mutable_count = read_range (
380381 store. clone ( ) ,
381382 writable. clone ( ) ,
@@ -395,12 +396,38 @@ fn read_range_test() {
395396 assert_eq ! ( mutable_count, 4 ) ; // Fixed: range is half-open
396397 assert_eq ! ( immutable_count, 4 ) ; // Fixed: range is half-open
397398
398- // Test combined query with writable store
399- let br: Range < BlockNumber > = 4 ..8 ;
399+ // Then test writable store with the detailed entity checks
400+ for count in 1 ..=5 {
401+ insert_count ( & subgraph_store, & deployment, count, 2 * count, true ) . await ;
402+ }
403+ writable. flush ( ) . await . unwrap ( ) ;
400404 writable. deployment_synced ( ) . unwrap ( ) ;
405+
406+ let br: Range < BlockNumber > = 0 ..18 ;
401407 let entity_types = vec ! [ COUNTER_TYPE . clone( ) , COUNTER2_TYPE . clone( ) ] ;
402- let e = writable. get_range ( entity_types, br) . unwrap ( ) ;
403- assert_eq ! ( e. len( ) , 4 ) // Fixed: value changed from 5 to 4 as range is half-open
408+ let e: BTreeMap < i32 , Vec < EntityWithType > > = writable
409+ . get_range ( entity_types. clone ( ) , br. clone ( ) )
410+ . unwrap ( ) ;
411+ assert_eq ! ( e. len( ) , 5 ) ;
412+ for en in & e {
413+ let index = * en. 0 - 1 ;
414+ let a = result_entities[ index as usize ] ;
415+ assert_eq ! ( a, format!( "{:?}" , en) ) ;
416+ }
417+
418+ for count in 6 ..=7 {
419+ insert_count ( & subgraph_store, & deployment, count, 2 * count, true ) . await ;
420+ }
421+ writable. flush ( ) . await . unwrap ( ) ;
422+ writable. deployment_synced ( ) . unwrap ( ) ;
423+
424+ let e: BTreeMap < i32 , Vec < EntityWithType > > = writable. get_range ( entity_types, br) . unwrap ( ) ;
425+ assert_eq ! ( e. len( ) , 7 ) ;
426+ for en in & e {
427+ let index = * en. 0 - 1 ;
428+ let a = result_entities[ index as usize ] ;
429+ assert_eq ! ( a, format!( "{:?}" , en) ) ;
430+ }
404431 } ,
405432 )
406- }
433+ }
0 commit comments