Skip to content

Commit 921c014

Browse files
fix(scaffold): correct entity ID format in generated tests (#2078)
* fix(scaffold): correct entity ID format in generated tests When generating test files with --index-events, the entity ID format was using the old string concatenation format ('-1') instead of the format produced by concatI32() ('01000000'). This caused all generated tests to fail out-of-the-box. Changes: - Update entity ID in test assertions from '-1' to '01000000' format - Improve comment to explain concatI32() encoding with little-endian bytes - Correct terminology from "default address" to "default transaction hash" This fixes a bug introduced in commit 34aee49 (Oct 2022) when Bytes as ID with @immutable was implemented. The mapping and schema templates were updated but the test template was missed. * add changeset * update test * add proper healthcheck to docker compose and wait * lint * pin postgres --------- Co-authored-by: YaroShkvorets <[email protected]>
1 parent 8455ab9 commit 921c014

File tree

8 files changed

+51
-24
lines changed

8 files changed

+51
-24
lines changed

.changeset/metal-lights-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Fixed entity id format in generated tests

examples/ethereum-basic-event-handlers/build/schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
type NewGravatar @entity {
1+
type NewGravatar @entity(immutable: false) {
22
id: ID!
33
owner: Bytes!
44
displayName: String!
55
imageUrl: String!
66
}
77

8-
type UpdatedGravatar @entity {
8+
type UpdatedGravatar @entity(immutable: false) {
99
id: ID!
1010
owner: Bytes!
1111
displayName: String!

examples/ethereum-basic-event-handlers/build/subgraph.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dataSources:
88
name: Gravity
99
network: test
1010
source:
11-
address: "0x8f86403A4DE0BB5791fa46B8e795C547942fE4Cf"
11+
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3"
1212
abi: Gravity
1313
mapping:
1414
kind: ethereum/events

examples/ethereum-basic-event-handlers/docker-compose.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ services:
55
- '8545:8545'
66
graph-node:
77
image: graphprotocol/graph-node:latest
8+
platform: linux/amd64
89
ports:
910
- '8000:8000'
1011
- '8001:8001'
1112
- '8020:8020'
1213
- '8030:8030'
1314
- '8040:8040'
1415
depends_on:
15-
- hardhat
16-
- ipfs
17-
- postgres
16+
hardhat:
17+
condition: service_started
18+
ipfs:
19+
condition: service_started
20+
postgres:
21+
condition: service_healthy
1822
environment:
1923
postgres_host: postgres
2024
postgres_user: graph-node
@@ -24,24 +28,36 @@ services:
2428
ethereum: 'test:http://hardhat:8545'
2529
GRAPH_LOG: info
2630
GRAPH_ALLOW_NON_DETERMINISTIC_IPFS: 1
31+
healthcheck:
32+
test: ['CMD-SHELL', 'nc -z localhost 8030 || exit 1']
33+
interval: 5s
34+
timeout: 5s
35+
retries: 30
36+
start_period: 10s
2737
ipfs:
2838
image: ipfs/kubo:v0.19.1
2939
ports:
3040
- '5001:5001'
3141
volumes:
3242
- ipfs-data:/data/ipfs
3343
postgres:
34-
image: postgres
44+
image: postgres:17
3545
ports:
3646
- '5432:5432'
37-
command: ['postgres', '-cshared_preload_libraries=pg_stat_statements']
47+
command: ['postgres', '-c', 'shared_preload_libraries=pg_stat_statements']
3848
environment:
3949
POSTGRES_USER: graph-node
4050
POSTGRES_PASSWORD: let-me-in
4151
POSTGRES_DB: graph-node
4252
POSTGRES_INITDB_ARGS: '-E UTF8 --locale=C'
4353
volumes:
4454
- postgres-data:/var/lib/postgresql/data
55+
healthcheck:
56+
test: ['CMD-SHELL', 'pg_isready -U graph-node']
57+
interval: 5s
58+
timeout: 5s
59+
retries: 30
60+
start_period: 5s
4561

4662
volumes:
4763
ipfs-data:

examples/ethereum-basic-event-handlers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"codegen": "graph codegen",
1414
"create-test": "graph create test/basic-event-handlers --node http://localhost:8020/",
1515
"deploy-test": "graph deploy test/basic-event-handlers --version-label v0.0.1 --ipfs http://localhost:5001 --node http://localhost:8020/",
16-
"test": "docker compose up -d && sleep 30 && hardhat test --network localhost && docker compose down",
16+
"test": "docker compose up -d --wait --wait-timeout 50 && hardhat test --network localhost && docker compose down",
1717
"test:hardhat": "hardhat test --network localhost"
1818
},
1919
"dependencies": {

examples/ethereum-basic-event-handlers/subgraph.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dataSources:
88
name: Gravity
99
network: test
1010
source:
11-
address: '0x8f86403A4DE0BB5791fa46B8e795C547942fE4Cf'
11+
address: '0x5FbDB2315678afecb367f032d93F642f64180aa3'
1212
abi: Gravity
1313
mapping:
1414
kind: ethereum/events

packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,34 +315,36 @@ describe("Describe entity assertions", () => {
315315
test("ExampleEvent created and stored", () => {
316316
assert.entityCount("ExampleEvent", 1)
317317
318-
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
318+
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default transaction hash used in newMockEvent() function
319+
// When indexEvents is true, entity ID uses concatI32() which appends log index as little-endian bytes
320+
// Default log index 1 becomes: 01000000 (4 bytes: 0x01 0x00 0x00 0x00)
319321
assert.fieldEquals(
320322
"ExampleEvent",
321-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
323+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
322324
"a",
323325
"234"
324326
)
325327
assert.fieldEquals(
326328
"ExampleEvent",
327-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
329+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
328330
"b",
329331
"[1234567890]"
330332
)
331333
assert.fieldEquals(
332334
"ExampleEvent",
333-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
335+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
334336
"param2",
335337
"Example string value"
336338
)
337339
assert.fieldEquals(
338340
"ExampleEvent",
339-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
341+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
340342
"c",
341343
"ethereum.Tuple Not implemented"
342344
)
343345
assert.fieldEquals(
344346
"ExampleEvent",
345-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
347+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
346348
"d",
347349
"Example string value"
348350
)
@@ -482,34 +484,36 @@ describe("Describe entity assertions", () => {
482484
test("ExampleEvent created and stored", () => {
483485
assert.entityCount("ExampleEvent", 1)
484486
485-
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
487+
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default transaction hash used in newMockEvent() function
488+
// When indexEvents is true, entity ID uses concatI32() which appends log index as little-endian bytes
489+
// Default log index 1 becomes: 01000000 (4 bytes: 0x01 0x00 0x00 0x00)
486490
assert.fieldEquals(
487491
"ExampleEvent",
488-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
492+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
489493
"a",
490494
"234"
491495
)
492496
assert.fieldEquals(
493497
"ExampleEvent",
494-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
498+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
495499
"b",
496500
"[1234567890]"
497501
)
498502
assert.fieldEquals(
499503
"ExampleEvent",
500-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
504+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
501505
"param2",
502506
"Example string value"
503507
)
504508
assert.fieldEquals(
505509
"ExampleEvent",
506-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
510+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
507511
"c",
508512
"ethereum.Tuple Not implemented"
509513
)
510514
assert.fieldEquals(
511515
"ExampleEvent",
512-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
516+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
513517
"d",
514518
"Example string value"
515519
)

packages/cli/src/scaffold/tests.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const generateFieldsAssertions = (entity: string, eventInputs: any[], indexEvent
9191
(input, index) =>
9292
`assert.fieldEquals(
9393
"${entity}",
94-
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a${indexEvents ? '-1' : ''}",
94+
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a${indexEvents ? '01000000' : ''}",
9595
"${input.name || `param${index}`}",
9696
"${expectedValue(ascTypeForEthereum(input.type))}"
9797
)`,
@@ -171,7 +171,9 @@ const generateExampleTest = (
171171
test("${entity} created and stored", () => {
172172
assert.entityCount('${entity}', 1)
173173
174-
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
174+
// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default transaction hash used in newMockEvent() function
175+
// When indexEvents is true, entity ID uses concatI32() which appends log index as little-endian bytes
176+
// Default log index 1 becomes: 01000000 (4 bytes: 0x01 0x00 0x00 0x00)
175177
${generateFieldsAssertions(entity, eventInputs, indexEvents)}
176178
177179
// More assert options:

0 commit comments

Comments
 (0)