Skip to content

Commit ae26254

Browse files
refactor tests to use snapshots (#1824)
1 parent f3c94f1 commit ae26254

File tree

8 files changed

+1562
-697
lines changed

8 files changed

+1562
-697
lines changed

packages/cli/src/protocols/ethereum/codegen/__snapshots__/abi.test.ts.snap

Lines changed: 961 additions & 0 deletions
Large diffs are not rendered by default.

packages/cli/src/protocols/ethereum/codegen/abi.test.ts

Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import path from 'node:path';
22
import fs from 'fs-extra';
3-
import immutable from 'immutable';
43
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
5-
import * as ts from '../../../codegen/typescript.js';
64
import ABI from '../abi.js';
75
import AbiCodeGenerator from './abi.js';
86

97
let tempdir: string;
108
let abi: ABI;
11-
let generatedTypes: any[];
9+
let generatedTypes: any[] = [];
1210

1311
describe.concurrent('ABI code generation', () => {
1412
beforeAll(async () => {
@@ -179,153 +177,10 @@ describe.concurrent('ABI code generation', () => {
179177
});
180178

181179
describe('Generated types', () => {
182-
test('All expected types are generated', () => {
183-
expect(generatedTypes.map(type => type.name)).toEqual([
184-
'Contract__getProposalResultValue0Struct',
185-
'Contract__getProposalInputParam1Struct',
186-
'Contract__getProposalInputParam1BarStruct',
187-
'Contract__getProposalsResultValue1Struct',
188-
'Contract__getProposalsResult',
189-
'Contract',
190-
]);
191-
});
192-
});
193-
194-
describe('Contract class', () => {
195-
test('Exists', () => {
196-
expect(generatedTypes.find(type => type.name === 'Contract')).toBeDefined();
197-
});
198-
199-
test('Has methods', () => {
200-
const contract = generatedTypes.find(type => type.name === 'Contract');
201-
expect(contract.methods).toBeInstanceOf(Array);
202-
});
203-
204-
test('Has `bind` method', () => {
205-
const contract = generatedTypes.find(type => type.name === 'Contract');
206-
expect(contract.methods.find((method: any) => method.name === 'bind')).toBeDefined();
207-
});
208-
209-
test('Has methods for all callable functions', () => {
210-
const contract = generatedTypes.find(type => type.name === 'Contract');
211-
expect(contract.methods.map((method: any) => method.name)).toContain('getProposal');
212-
});
213-
});
214-
215-
describe('Methods for callable functions', () => {
216-
test('Have correct parameters', () => {
217-
const contract = generatedTypes.find(type => type.name === 'Contract');
218-
expect(contract.methods.map((method: any) => [method.name, method.params])).toEqual([
219-
['bind', immutable.List([ts.param('address', 'Address')])],
220-
['read', immutable.List()],
221-
['try_read', immutable.List()],
222-
[
223-
'getProposal',
224-
immutable.List([
225-
ts.param('proposalId', 'BigInt'),
226-
ts.param('param1', 'Contract__getProposalInputParam1Struct'),
227-
]),
228-
],
229-
[
230-
'try_getProposal',
231-
immutable.List([
232-
ts.param('proposalId', 'BigInt'),
233-
ts.param('param1', 'Contract__getProposalInputParam1Struct'),
234-
]),
235-
],
236-
['getProposals', immutable.List()],
237-
['try_getProposals', immutable.List()],
238-
['overloaded', immutable.List([ts.param('param0', 'string')])],
239-
['try_overloaded', immutable.List([ts.param('param0', 'string')])],
240-
['overloaded1', immutable.List([ts.param('param0', 'BigInt')])],
241-
['try_overloaded1', immutable.List([ts.param('param0', 'BigInt')])],
242-
['overloaded2', immutable.List([ts.param('param0', 'Bytes')])],
243-
['try_overloaded2', immutable.List([ts.param('param0', 'Bytes')])],
244-
]);
245-
});
246-
247-
test('Have correct return types', () => {
248-
const contract = generatedTypes.find(type => type.name === 'Contract');
249-
expect(contract.methods.map((method: any) => [method.name, method.returnType])).toEqual([
250-
['bind', ts.namedType('Contract')],
251-
['read', ts.namedType('Bytes')],
252-
['try_read', 'ethereum.CallResult<Bytes>'],
253-
['getProposal', ts.namedType('Contract__getProposalResultValue0Struct')],
254-
['try_getProposal', 'ethereum.CallResult<Contract__getProposalResultValue0Struct>'],
255-
['getProposals', ts.namedType('Contract__getProposalsResult')],
256-
['try_getProposals', 'ethereum.CallResult<Contract__getProposalsResult>'],
257-
['overloaded', ts.namedType('string')],
258-
['try_overloaded', 'ethereum.CallResult<string>'],
259-
['overloaded1', ts.namedType('string')],
260-
['try_overloaded1', 'ethereum.CallResult<string>'],
261-
['overloaded2', ts.namedType('string')],
262-
['try_overloaded2', 'ethereum.CallResult<string>'],
263-
]);
264-
});
265-
});
266-
267-
describe('Tuples', () => {
268-
test('Tuple types exist for function parameters', () => {
269-
let tupleType = generatedTypes.find(
270-
type => type.name === 'Contract__getProposalInputParam1Struct',
271-
);
272-
273-
// Verify that the tuple type has methods
274-
expect(tupleType.methods).toBeDefined();
275-
276-
// Verify that the tuple type has getters for all tuple fields with
277-
// the right return types
278-
expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([
279-
['get foo', 'i32'],
280-
['get bar', 'Contract__getProposalInputParam1BarStruct'],
281-
]);
282-
283-
// Inner tuple:
284-
tupleType = generatedTypes.find(
285-
type => type.name === 'Contract__getProposalInputParam1BarStruct',
286-
);
287-
288-
// Verify that the tuple type has methods
289-
expect(tupleType.methods).toBeDefined();
290-
291-
// Verify that the tuple type has getters for all tuple fields with
292-
// the right return types
293-
expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([
294-
['get baz', 'Address'],
295-
]);
296-
});
297-
298-
test('Tuple types exist for function return values', () => {
299-
const tupleType = generatedTypes.find(
300-
type => type.name === 'Contract__getProposalResultValue0Struct',
301-
);
302-
303-
// Verify that the tuple type has methods
304-
expect(tupleType.methods).toBeDefined();
305-
306-
// Verify that the tuple type has getters for all tuple fields with
307-
// the right return types
308-
expect(tupleType.methods.map((method: any) => [method.name, method.returnType])).toEqual([
309-
['get result', 'i32'],
310-
['get target', 'Address'],
311-
['get data', 'Bytes'],
312-
['get proposer', 'Address'],
313-
['get feeRecipient', 'Address'],
314-
['get fee', 'BigInt'],
315-
['get startTime', 'BigInt'],
316-
['get yesCount', 'BigInt'],
317-
['get noCount', 'BigInt'],
318-
]);
319-
});
320-
321-
test('Function bodies are generated correctly for tuple arrays', () => {
322-
const contract = generatedTypes.find(type => type.name === 'Contract');
323-
const getter = contract.methods.find((method: any) => method.name === 'getProposals');
324-
325-
expect(getter.body).not.toContain('toTupleArray<undefined>');
326-
expect(getter.body).toContain(
327-
'result[1].toTupleArray<Contract__getProposalsResultValue1Struct>()',
328-
);
180+
test(`Type test`, () => {
181+
for (const generatedType of generatedTypes) {
182+
expect(generatedType).toMatchSnapshot();
183+
}
329184
});
330185
});
331186
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Cosmos subgraph scaffolding > Manifest 1`] = `
4+
"specVersion: 1.0.0
5+
indexerHints:
6+
prune: auto
7+
schema:
8+
file: ./schema.graphql
9+
dataSources:
10+
- kind: cosmos
11+
name: CosmosHub
12+
network: cosmoshub-4
13+
source:
14+
startBlock: 0
15+
mapping:
16+
apiVersion: 0.0.5
17+
language: wasm/assemblyscript
18+
entities:
19+
- ExampleEntity
20+
blockHandlers:
21+
- handler: handleBlock
22+
file: ./src/contract.ts
23+
"
24+
`;
25+
26+
exports[`Cosmos subgraph scaffolding > Mapping (default) 1`] = `
27+
"import { cosmos, BigInt } from "@graphprotocol/graph-ts"
28+
import { ExampleEntity } from "../generated/schema"
29+
30+
export function handleBlock(block: cosmos.Block): void {
31+
// Entities can be loaded from the store using a string ID; this ID
32+
// needs to be unique across all entities of the same type
33+
let entity = ExampleEntity.load(block.header.hash.toHex())
34+
35+
// Entities only exist after they have been saved to the store;
36+
// \`null\` checks allow to create entities on demand
37+
if (!entity) {
38+
entity = new ExampleEntity(block.header.hash.toHex())
39+
40+
// Entity fields can be set using simple assignments
41+
entity.count = BigInt.fromI32(0)
42+
}
43+
44+
// BigInt and BigDecimal math are supported
45+
entity.count = entity.count + BigInt.fromI32(1)
46+
47+
// Entity fields can be set based on receipt information
48+
entity.height = block.header.height
49+
50+
// Entities can be written to the store with \`.save()\`
51+
entity.save()
52+
53+
// Note: If a handler doesn't require existing field values, it is faster
54+
// _not_ to load the entity from the store. Instead, create it fresh with
55+
// \`new Entity(...)\`, set the fields that should be updated and save the
56+
// entity back to the store. Fields that were not set or unset remain
57+
// unchanged, allowing for partial updates to be applied.
58+
}
59+
"
60+
`;
61+
62+
exports[`Cosmos subgraph scaffolding > Schema (default) 1`] = `
63+
"type ExampleEntity @entity {
64+
id: ID!
65+
block: Bytes!
66+
count: BigInt!
67+
}
68+
"
69+
`;

0 commit comments

Comments
 (0)