Skip to content

Commit 90c342c

Browse files
committed
tests: Update subgraph composition integration tests to work with immutable entities
1 parent 97f01a0 commit 90c342c

File tree

10 files changed

+57
-137
lines changed

10 files changed

+57
-137
lines changed

tests/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: '3'
22
services:
33
ipfs:
4-
image: docker.io/ipfs/kubo:v0.17.0
4+
image: docker.io/ipfs/kubo:v0.34.1
55
ports:
66
- '127.0.0.1:3001:5001'
77
postgres:
@@ -20,7 +20,7 @@ services:
2020
POSTGRES_DB: graph-node
2121
POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
2222
anvil:
23-
image: ghcr.io/foundry-rs/foundry:latest
23+
image: ghcr.io/foundry-rs/foundry:stable
2424
ports:
2525
- '3021:8545'
2626
command: "'anvil --host 0.0.0.0 --gas-limit 100000000000 --base-fee 1 --block-time 5 --mnemonic \"test test test test test test test test test test test junk\"'"
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import { dataSource, EntityTrigger, log } from '@graphprotocol/graph-ts'
22
import { AggregatedData } from '../generated/schema'
3-
import { SourceAData } from '../generated/subgraph-QmPWnNsD4m8T9EEF1ec5d8wetFxrMebggLj1efFHzdnZhx'
4-
import { SourceBData } from '../generated/subgraph-Qma4Rk2D1w6mFiP15ZtHHx7eWkqFR426RWswreLiDanxej'
3+
import { SourceAData } from '../generated/subgraph-QmYHp1bPEf7EoYBpEtJUpZv1uQHYQfWE4AhvR6frjB1Huj'
4+
import { SourceBData } from '../generated/subgraph-QmYBEzastJi7bsa722ac78tnZa6xNnV9vvweerY4kVyJtq'
55

6-
export function handleSourceAData(data: EntityTrigger<SourceAData>): void {
7-
let aggregated = AggregatedData.load(data.data.id)
8-
if (!aggregated) {
9-
aggregated = new AggregatedData(data.data.id)
10-
aggregated.sourceA = data.data.data
11-
aggregated.first = 'sourceA'
12-
} else {
13-
aggregated.sourceA = data.data.data
14-
}
6+
7+
// We know this handler will run first since its defined first in the manifest
8+
// So we dont need to check if the Aggregated data exists
9+
export function handleSourceAData(data: SourceAData): void {
10+
let aggregated = new AggregatedData(data.id)
11+
aggregated.sourceA = data.data
12+
aggregated.first = 'sourceA'
1513
aggregated.save()
1614
}
1715

18-
export function handleSourceBData(data: EntityTrigger<SourceBData>): void {
19-
let aggregated = AggregatedData.load(data.data.id)
16+
export function handleSourceBData(data: SourceBData): void {
17+
let aggregated = AggregatedData.load(data.id)
2018
if (!aggregated) {
21-
aggregated = new AggregatedData(data.data.id)
22-
aggregated.sourceB = data.data.data
19+
aggregated = new AggregatedData(data.id)
20+
aggregated.sourceB = data.data
2321
aggregated.first = 'sourceB'
2422
} else {
25-
aggregated.sourceB = data.data.data
23+
aggregated.sourceB = data.data
2624
}
2725
aggregated.save()
2826
}

tests/integration-tests/multiple-subgraph-datasources/subgraph.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dataSources:
66
name: SourceA
77
network: test
88
source:
9-
address: 'QmPWnNsD4m8T9EEF1ec5d8wetFxrMebggLj1efFHzdnZhx'
9+
address: 'QmYHp1bPEf7EoYBpEtJUpZv1uQHYQfWE4AhvR6frjB1Huj'
1010
startBlock: 0
1111
mapping:
1212
apiVersion: 0.0.7
@@ -22,7 +22,7 @@ dataSources:
2222
name: SourceB
2323
network: test
2424
source:
25-
address: 'Qma4Rk2D1w6mFiP15ZtHHx7eWkqFR426RWswreLiDanxej'
25+
address: 'QmYBEzastJi7bsa722ac78tnZa6xNnV9vvweerY4kVyJtq'
2626
startBlock: 0
2727
mapping:
2828
apiVersion: 0.0.7

tests/integration-tests/source-subgraph-a/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type SourceAData @entity {
1+
type SourceAData @entity(immutable: true) {
22
id: ID!
33
data: String!
44
blockNumber: BigInt!

tests/integration-tests/source-subgraph-b/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type SourceBData @entity {
1+
type SourceBData @entity(immutable: true) {
22
id: ID!
33
data: String!
44
blockNumber: BigInt!

tests/integration-tests/source-subgraph/schema.graphql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
type Block @entity {
1+
type Block @entity(immutable: true) {
22
id: ID!
33
number: BigInt!
44
hash: Bytes!
5-
testMessage: String
65
}
76

8-
type Block2 @entity {
7+
type Block2 @entity(immutable: true) {
98
id: ID!
109
number: BigInt!
1110
hash: Bytes!
Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ethereum, log, store } from '@graphprotocol/graph-ts';
22
import { Block, Block2 } from '../generated/schema';
3-
import { BigInt } from '@graphprotocol/graph-ts';
43

54
export function handleBlock(block: ethereum.Block): void {
65
log.info('handleBlock {}', [block.number.toString()]);
@@ -21,37 +20,6 @@ export function handleBlock(block: ethereum.Block): void {
2120
let blockEntity3 = new Block2(id3);
2221
blockEntity3.number = block.number;
2322
blockEntity3.hash = block.hash;
23+
blockEntity3.testMessage = block.number.toString().concat('-message');
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-
}
5725
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { Entity, log, store, BigInt, EntityTrigger, EntityOp } from '@graphprotocol/graph-ts';
2-
import { Block } from '../generated/subgraph-QmVz1Pt7NhgCkz4gfavmNrMhojnMT9hW81QDqVjy56ZMUP';
1+
import { log, store } from '@graphprotocol/graph-ts';
2+
import { Block, Block2 } from '../generated/subgraph-QmWi3H11QFE2PiWx6WcQkZYZdA5UasaBptUJqGn54MFux5';
33
import { MirrorBlock } from '../generated/schema';
44

5-
export function handleEntity(trigger: EntityTrigger<Block>): void {
6-
let blockEntity = trigger.data;
7-
let id = blockEntity.id;
5+
export function handleEntity(block: Block): void {
6+
let id = block.id;
87

9-
if (trigger.operation === EntityOp.Remove) {
10-
log.info('Removing block entity with id: {}', [id]);
11-
store.remove('MirrorBlock', id);
12-
return;
13-
}
8+
let blockEntity = loadOrCreateMirrorBlock(id);
9+
blockEntity.number = block.number;
10+
blockEntity.hash = block.hash;
1411

15-
let block = loadOrCreateMirrorBlock(id);
16-
block.number = blockEntity.number;
17-
block.hash = blockEntity.hash;
18-
19-
if (blockEntity.testMessage) {
20-
block.testMessage = blockEntity.testMessage;
21-
}
12+
blockEntity.save();
13+
}
14+
15+
export function handleEntity2(block: Block2): void {
16+
let id = block.id;
17+
18+
let blockEntity = loadOrCreateMirrorBlock(id);
19+
blockEntity.number = block.number;
20+
blockEntity.hash = block.hash;
21+
blockEntity.testMessage = block.testMessage;
2222

23-
block.save();
23+
blockEntity.save();
2424
}
2525

2626
export function loadOrCreateMirrorBlock(id: string): MirrorBlock {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dataSources:
66
name: Contract
77
network: test
88
source:
9-
address: 'QmVz1Pt7NhgCkz4gfavmNrMhojnMT9hW81QDqVjy56ZMUP'
9+
address: 'QmWi3H11QFE2PiWx6WcQkZYZdA5UasaBptUJqGn54MFux5'
1010
startBlock: 0
1111
mapping:
1212
apiVersion: 0.0.7
@@ -16,6 +16,6 @@ dataSources:
1616
handlers:
1717
- handler: handleEntity
1818
entity: Block
19-
- handler: handleEntity
19+
- handler: handleEntity2
2020
entity: Block2
2121
file: ./src/mapping.ts

tests/tests/integration_tests.rs

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -523,79 +523,34 @@ async fn subgraph_data_sources(ctx: TestContext) -> anyhow::Result<()> {
523523
assert!(subgraph.healthy);
524524
let expected_response = json!({
525525
"mirrorBlocks": [
526-
{ "id": "1-v1", "number": "1" },
527-
{ "id": "1-v2", "number": "1" },
528-
{ "id": "1-v3", "number": "1" },
529-
{ "id": "2-v1", "number": "2" },
530-
{ "id": "2-v2", "number": "2" },
531-
{ "id": "2-v3", "number": "2" },
532-
{ "id": "3-v1", "number": "3" },
533-
{ "id": "3-v2", "number": "3" },
534-
{ "id": "3-v3", "number": "3" },
535-
{ "id": "4-v1", "number": "4" },
536-
{ "id": "4-v2", "number": "4" },
537-
{ "id": "4-v3", "number": "4" },
538-
{ "id": "5-v1", "number": "5" },
539-
{ "id": "5-v2", "number": "5" },
540-
{ "id": "5-v3", "number": "5" },
541-
{ "id": "6-v1", "number": "6" },
542-
{ "id": "6-v2", "number": "6" },
543-
{ "id": "6-v3", "number": "6" },
544-
{ "id": "7-v1", "number": "7" },
545-
{ "id": "7-v2", "number": "7" },
546-
{ "id": "7-v3", "number": "7" },
547-
{ "id": "8-v1", "number": "8" },
548-
{ "id": "8-v2", "number": "8" },
549-
{ "id": "8-v3", "number": "8" },
550-
{ "id": "9-v1", "number": "9" },
551-
{ "id": "9-v2", "number": "9" },
552-
{ "id": "9-v3", "number": "9" },
553-
{ "id": "10-v1", "number": "10" },
554-
{ "id": "10-v2", "number": "10" },
555-
{ "id": "10-v3", "number": "10" },
526+
{ "id": "1-v1", "number": "1", "testMessage": null },
527+
{ "id": "1-v2", "number": "1", "testMessage": null },
528+
{ "id": "1-v3", "number": "1", "testMessage": "1-message" },
529+
{ "id": "2-v1", "number": "2", "testMessage": null },
530+
{ "id": "2-v2", "number": "2", "testMessage": null },
531+
{ "id": "2-v3", "number": "2", "testMessage": "2-message" },
532+
{ "id": "3-v1", "number": "3", "testMessage": null },
533+
{ "id": "3-v2", "number": "3", "testMessage": null },
534+
{ "id": "3-v3", "number": "3", "testMessage": "3-message" },
556535
]
557536
});
558537

559538
query_succeeds(
560-
"Blocks should be right",
539+
"Query all blocks with testMessage",
561540
&subgraph,
562-
"{ mirrorBlocks(where: {number_lte: 10}, orderBy: number) { id, number } }",
541+
"{ mirrorBlocks(where: {number_lte: 3}, orderBy: number) { id, number, testMessage } }",
563542
expected_response,
564543
)
565544
.await?;
566545

567546
let expected_response = json!({
568-
"mirrorBlock": { "id": "TEST", "number": "1", "testMessage": "Created at block 1" },
547+
"mirrorBlock": { "id": "1-v3", "number": "1", "testMessage": "1-message" },
569548
});
570549

571550
query_succeeds(
572-
"Blocks should be right",
551+
"Query specific block with testMessage",
573552
&subgraph,
574-
"{ mirrorBlock(id: \"TEST\", block: {number: 1}) { id, number, testMessage } }",
575-
expected_response,
576-
)
577-
.await?;
578-
579-
let expected_response = json!({
580-
"mirrorBlock": { "id": "TEST", "number": "1", "testMessage": "Updated at block 2" },
581-
});
582-
583-
query_succeeds(
584-
"Blocks should be right",
585-
&subgraph,
586-
"{ mirrorBlock(id: \"TEST\", block: {number: 2}) { id, number, testMessage } }",
587-
expected_response,
588-
)
589-
.await?;
590-
591-
let expected_response = json!({
592-
"mirrorBlock": null,
593-
});
594-
595-
query_succeeds(
596-
"Blocks should be right",
597-
&subgraph,
598-
"{ mirrorBlock(id: \"TEST\", block: {number: 3}) { id, number, testMessage } }",
553+
"{ mirrorBlock(id: \"1-v3\") { id, number, testMessage } }",
599554
expected_response,
600555
)
601556
.await?;

0 commit comments

Comments
 (0)