Skip to content

Commit a6eb97e

Browse files
committed
tests: udpate subgraph composition integration test cases to handle entity ops
1 parent ff19081 commit a6eb97e

File tree

7 files changed

+96
-7
lines changed

7 files changed

+96
-7
lines changed

graph/src/runtime/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,9 @@ pub enum IndexForAscTypeId {
368368
// ...
369369
// LastStarknetType = 4499,
370370

371-
372371
// Subgraph Data Source types
373372
AscEntityTrigger = 4500,
374373

375-
376374
// Reserved discriminant space for a future blockchain type IDs: [4,500, 5,499]
377375
//
378376
// Generated with the following shell script:
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
21
type Block @entity {
32
id: ID!
43
number: BigInt!
54
hash: Bytes!
5+
testMessage: String
66
}
77

88
type Block2 @entity {
99
id: ID!
1010
number: BigInt!
1111
hash: Bytes!
12+
testMessage: String
1213
}

tests/integration-tests/source-subgraph/src/mapping.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ethereum, log } from '@graphprotocol/graph-ts';
1+
import { ethereum, log, store } from '@graphprotocol/graph-ts';
22
import { Block, Block2 } from '../generated/schema';
33
import { BigInt } from '@graphprotocol/graph-ts';
44

@@ -22,4 +22,36 @@ export function handleBlock(block: ethereum.Block): void {
2222
blockEntity3.number = block.number;
2323
blockEntity3.hash = block.hash;
2424
blockEntity3.save();
25+
26+
if (block.number.equals(BigInt.fromI32(1))) {
27+
let id = 'TEST';
28+
let entity = new Block(id);
29+
entity.number = block.number;
30+
entity.hash = block.hash;
31+
entity.testMessage = 'Created at block 1';
32+
log.info('Created entity at block 1', []);
33+
entity.save();
34+
}
35+
36+
if (block.number.equals(BigInt.fromI32(2))) {
37+
let id = 'TEST';
38+
let blockEntity1 = Block.load(id);
39+
if (blockEntity1) {
40+
// Update the block entity
41+
blockEntity1.testMessage = 'Updated at block 2';
42+
log.info('Updated entity at block 2', []);
43+
blockEntity1.save();
44+
}
45+
}
46+
47+
if (block.number.equals(BigInt.fromI32(3))) {
48+
let id = 'TEST';
49+
let blockEntity1 = Block.load(id);
50+
if (blockEntity1) {
51+
blockEntity1.testMessage = 'Deleted at block 3';
52+
log.info('Deleted entity at block 3', []);
53+
blockEntity1.save();
54+
store.remove('Block', id);
55+
}
56+
}
2557
}

tests/integration-tests/subgraph-data-sources/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ type MirrorBlock @entity {
22
id: String!
33
number: BigInt!
44
hash: Bytes!
5+
testMessage: String
56
}

tests/integration-tests/subgraph-data-sources/src/mapping.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, log } from '@graphprotocol/graph-ts';
1+
import { Entity, log, store } from '@graphprotocol/graph-ts';
22
import { MirrorBlock } from '../generated/schema';
33

44
export class EntityTrigger {
@@ -14,12 +14,33 @@ export function handleEntity(trigger: EntityTrigger): void {
1414
let blockEntity = trigger.entity;
1515
let blockNumber = blockEntity.getBigInt('number');
1616
let blockHash = blockEntity.getBytes('hash');
17+
let testMessage = blockEntity.get('testMessage');
1718
let id = blockEntity.getString('id');
1819

1920
log.info('Block number: {}', [blockNumber.toString()]);
2021

21-
let block = new MirrorBlock(id);
22+
if (trigger.entityOp == 2) {
23+
log.info('Removing block entity with id: {}', [id]);
24+
store.remove('MirrorBlock', id);
25+
return;
26+
}
27+
28+
let block = loadOrCreateMirrorBlock(id);
2229
block.number = blockNumber;
2330
block.hash = blockHash;
31+
if (testMessage) {
32+
block.testMessage = testMessage.toString();
33+
}
34+
2435
block.save();
2536
}
37+
38+
export function loadOrCreateMirrorBlock(id: string): MirrorBlock {
39+
let block = MirrorBlock.load(id);
40+
if (!block) {
41+
log.info('Creating new block entity with id: {}', [id]);
42+
block = new MirrorBlock(id);
43+
}
44+
45+
return block;
46+
}

tests/integration-tests/subgraph-data-sources/subgraph.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dataSources:
66
name: Contract
77
network: test
88
source:
9-
address: 'QmeZhEiJuBusu7GxCe6AytvqSsgwV8QxkbSYx5ojSFB28a'
9+
address: 'Qmaqf8cRxfxbduZppSHKG9DMuX5JZPMoGuwGb2DQuo48sq'
1010
startBlock: 0
1111
mapping:
1212
apiVersion: 0.0.7

tests/tests/integration_tests.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,42 @@ async fn subgraph_data_sources(ctx: TestContext) -> anyhow::Result<()> {
563563
)
564564
.await?;
565565

566+
let expected_response = json!({
567+
"mirrorBlock": { "id": "TEST", "number": "1", "testMessage": "Created at block 1" },
568+
});
569+
570+
query_succeeds(
571+
"Blocks should be right",
572+
&subgraph,
573+
"{ mirrorBlock(id: \"TEST\", block: {number: 1}) { id, number, testMessage } }",
574+
expected_response,
575+
)
576+
.await?;
577+
578+
let expected_response = json!({
579+
"mirrorBlock": { "id": "TEST", "number": "1", "testMessage": "Updated at block 2" },
580+
});
581+
582+
query_succeeds(
583+
"Blocks should be right",
584+
&subgraph,
585+
"{ mirrorBlock(id: \"TEST\", block: {number: 2}) { id, number, testMessage } }",
586+
expected_response,
587+
)
588+
.await?;
589+
590+
let expected_response = json!({
591+
"mirrorBlock": null,
592+
});
593+
594+
query_succeeds(
595+
"Blocks should be right",
596+
&subgraph,
597+
"{ mirrorBlock(id: \"TEST\", block: {number: 3}) { id, number, testMessage } }",
598+
expected_response,
599+
)
600+
.await?;
601+
566602
Ok(())
567603
}
568604

0 commit comments

Comments
 (0)