Skip to content

Commit 122650a

Browse files
authored
Merge pull request #33 from kleros/chore/light-item-tests
chore: litem-tests
2 parents 19f6385 + ef5f25e commit 122650a

File tree

10 files changed

+368
-3649
lines changed

10 files changed

+368
-3649
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,9 @@ generated/
114114

115115
.env
116116

117-
subgraph.yaml
117+
subgraph.yaml
118+
119+
# tests
120+
.docker
121+
.latest.json
122+
.bin

matchstick.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testsFolder: ./tests
2+
manifestPath: ./subgraph.yaml

package.json

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"lint:eslint": "eslint . --ext .ts,.tsx",
2424
"lint:prettier": "prettier --config .prettierrc 'src/**/*.ts' --check",
2525
"lint": "yarn lint:eslint && yarn lint:prettier && yarn lint:secrets",
26-
"format": "prettier --config .prettierrc 'src/**/*.ts' --write && eslint . --ext .ts,.tsx --fix"
26+
"format": "prettier --config .prettierrc 'src/**/*.ts' --write && eslint . --ext .ts,.tsx --fix",
27+
"test": "graph test"
2728
},
2829
"repository": {
2930
"type": "git",
@@ -39,33 +40,22 @@
3940
"@graphprotocol/graph-ts": "^0.35.1"
4041
},
4142
"devDependencies": {
42-
"@assemblyscript/loader": "^0.14.11",
43-
"@assemblyscript/node": "github:AssemblyScript/node",
4443
"@gnosis.pm/truffle-nice-tools": "^1.3.1",
4544
"@goldskycom/cli": "^11.3.0",
4645
"@graphprotocol/graph-cli": "^0.71.0",
47-
"@kleros/erc-792": "3.0.0",
48-
"@kleros/gtcr-encoder": "^1.1.3",
49-
"@kleros/tcr": "^2.0.0",
50-
"@truffle/contract": "^4.2.22",
51-
"@truffle/hdwallet-provider": "^1.1.0",
5246
"@typescript-eslint/eslint-plugin": "^4.28.2",
5347
"@typescript-eslint/parser": "^4.28.2",
54-
"assemblyscript": "^0.14.11",
55-
"delay": "^4.4.0",
5648
"dotenv-safe": "^8.2.0",
5749
"eslint": "^7.30.0",
5850
"eslint-config-prettier": "^8.3.0",
5951
"eslint-plugin-prettier": "^3.4.0",
60-
"ethers": "^5.0.14",
6152
"fs-extra": "^9.0.1",
53+
"matchstick-as": "^0.6.0",
6254
"mocha": "^8.1.3",
6355
"mocha-steps": "^1.3.0",
6456
"mustache": "^4.0.1",
6557
"replace-in-file": "^6.1.0",
66-
"should": "^13.2.3",
67-
"typescript": "^4.3.5",
68-
"wait-on": "^5.2.0"
58+
"typescript": "^4.3.5"
6959
},
7060
"resolutions": {
7161
"ejs": "^3.1.10"

tests/ipfs/item.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"columns": [
3+
{
4+
"label": "Github Repository URL",
5+
"description": "The URL of the repository containing the function that returns the Contract Tags. The repository name must be in the kebab case (hyphen-case). ",
6+
"type": "link",
7+
"isIdentifier": true
8+
},
9+
{
10+
"label": "Commit hash",
11+
"description": "The hash of the specific commit for this repository to be referenced.",
12+
"type": "text",
13+
"isIdentifier": true
14+
},
15+
{
16+
"label": "Chain ID",
17+
"description": "The EVM integer Chain ID of the chain that the contracts being retrieved by the function in this module are on. This is informational only and not used as input for the functions.",
18+
"type": "number",
19+
"isIdentifier": true
20+
},
21+
{
22+
"label": "Available on The Graph’s hosted service",
23+
"description": "Boolean value indicating if the function can be called in an unauthenticated manner using The Graph’s hosted (unauthenticated) service.",
24+
"type": "boolean",
25+
"isIdentifier": true
26+
},
27+
{
28+
"label": "Available on the decentralized Graph Network",
29+
"description": "Boolean value indicating if the function can make use of indexed data from the decentralized Graph Network.",
30+
"type": "boolean",
31+
"isIdentifier": true
32+
},
33+
{
34+
"label": "Description",
35+
"description": "A field used to describe the range of contracts being curated here, specifying (if applicable) the version, type and purpose of the contracts that are returned. ",
36+
"type": "long text",
37+
"isIdentifier": false
38+
}
39+
],
40+
"values": {
41+
"Github Repository URL": "https://github.com/gmkung/balancer-v2-pools-atq-module.git",
42+
"Commit hash": " 628491a",
43+
"Chain ID": "137",
44+
"Available on The Graph’s hosted service": "true",
45+
"Available on the decentralized Graph Network": "false",
46+
"Description": "A module to retrieve address tags describing Balancer V2 pools on Polygon."
47+
}
48+
}

tests/litem.test.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {
2+
Address,
3+
Bytes,
4+
DataSourceContext,
5+
ethereum,
6+
} from '@graphprotocol/graph-ts';
7+
import {
8+
afterAll,
9+
assert,
10+
beforeAll,
11+
clearStore,
12+
createMockedFunction,
13+
dataSourceMock,
14+
describe,
15+
readFile,
16+
test,
17+
} from 'matchstick-as/assembly/index';
18+
import { ZERO } from '../src/utils';
19+
import { createNewItemEvent } from './utils/litem-utils';
20+
import { handleNewGTCR } from '../src/LightGTCRFactoryMapping';
21+
import { handleNewItem } from '../src/LightGeneralizedTCRMapping';
22+
import { handleLItemMetadata } from '../src/fileHandlers/LItemMetadataHandler';
23+
import {
24+
GRAPH_ITEM_ID,
25+
IPFS_HASH,
26+
ITEM_DATA,
27+
ITEM_ID,
28+
REGISTRY_ADDRESS,
29+
} from './utils/mockValues';
30+
import { createNewGTCREvent } from './utils/lregistry-utils';
31+
32+
// mock getItemInfo function, called inside handleNewItem
33+
createMockedFunction(
34+
Address.fromString(REGISTRY_ADDRESS),
35+
'getItemInfo',
36+
'getItemInfo(bytes32):(uint8,uint256,uint256)',
37+
)
38+
.withArgs([ethereum.Value.fromFixedBytes(Bytes.fromHexString(ITEM_ID))])
39+
.returns([
40+
ethereum.Value.fromI32(0),
41+
ethereum.Value.fromUnsignedBigInt(ZERO),
42+
ethereum.Value.fromUnsignedBigInt(ZERO),
43+
]);
44+
45+
describe('Testing LItem creation', () => {
46+
beforeAll(() => {
47+
const newGTCREvent = createNewGTCREvent(REGISTRY_ADDRESS);
48+
49+
handleNewGTCR(newGTCREvent);
50+
51+
assert.fieldEquals(
52+
'LRegistry',
53+
REGISTRY_ADDRESS,
54+
'id',
55+
REGISTRY_ADDRESS,
56+
'LRegistry not created',
57+
);
58+
});
59+
60+
test('Should create LItem entity', () => {
61+
const newItemEvent = createNewItemEvent(
62+
REGISTRY_ADDRESS,
63+
ITEM_ID,
64+
ITEM_DATA,
65+
);
66+
67+
handleNewItem(newItemEvent);
68+
69+
assert.fieldEquals('LItem', GRAPH_ITEM_ID, 'id', GRAPH_ITEM_ID);
70+
});
71+
72+
test('Should create LItemMetadata datasource', () => {
73+
// previous test should have created this datasource
74+
assert.dataSourceExists('LItemMetadata', IPFS_HASH);
75+
76+
const context = new DataSourceContext();
77+
context.setString('graphItemID', GRAPH_ITEM_ID);
78+
context.setString('address', REGISTRY_ADDRESS);
79+
dataSourceMock.setReturnValues(IPFS_HASH, 'arbitrum-one', context);
80+
81+
const content = readFile('tests/ipfs/item.json');
82+
83+
handleLItemMetadata(content);
84+
85+
const metadataEntityId = `${IPFS_HASH}-${GRAPH_ITEM_ID}`;
86+
87+
assert.fieldEquals(
88+
'LItemMetadata',
89+
metadataEntityId,
90+
'id',
91+
metadataEntityId,
92+
);
93+
dataSourceMock.resetValues();
94+
});
95+
96+
afterAll(() => {
97+
clearStore();
98+
});
99+
});

tests/utils/litem-utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Address, Bytes, ethereum } from '@graphprotocol/graph-ts';
2+
import { NewItem } from '../../generated/templates/LightGeneralizedTCR/LightGeneralizedTCR';
3+
import { newMockEvent } from 'matchstick-as';
4+
5+
export function createNewItemEvent(
6+
registry: string,
7+
itemID: string,
8+
data: string,
9+
): NewItem {
10+
const mockEvent = newMockEvent();
11+
12+
const newNewItemEvent = new NewItem(
13+
Address.fromString(registry),
14+
mockEvent.logIndex,
15+
mockEvent.transactionLogIndex,
16+
mockEvent.logType,
17+
mockEvent.block,
18+
mockEvent.transaction,
19+
mockEvent.parameters,
20+
mockEvent.receipt,
21+
);
22+
newNewItemEvent.parameters = [];
23+
24+
const itemIDParam = new ethereum.EventParam(
25+
'_itemID',
26+
ethereum.Value.fromBytes(Bytes.fromHexString(itemID)),
27+
);
28+
const dataParam = new ethereum.EventParam(
29+
'_data',
30+
ethereum.Value.fromString(data),
31+
);
32+
const addedDirectlyParam = new ethereum.EventParam(
33+
'_addedDirectly',
34+
ethereum.Value.fromBoolean(false),
35+
);
36+
37+
newNewItemEvent.parameters.push(itemIDParam);
38+
newNewItemEvent.parameters.push(dataParam);
39+
newNewItemEvent.parameters.push(addedDirectlyParam);
40+
41+
return newNewItemEvent;
42+
}

tests/utils/lregistry-utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Address, ethereum } from '@graphprotocol/graph-ts';
2+
import { newMockEvent } from 'matchstick-as';
3+
import { NewGTCR } from '../../generated/LightGTCRFactory/LightGTCRFactory';
4+
5+
export function createNewGTCREvent(address: string): NewGTCR {
6+
const mockEvent = newMockEvent();
7+
8+
const newGTCREvent = new NewGTCR(
9+
mockEvent.address,
10+
mockEvent.logIndex,
11+
mockEvent.transactionLogIndex,
12+
mockEvent.logType,
13+
mockEvent.block,
14+
mockEvent.transaction,
15+
mockEvent.parameters,
16+
mockEvent.receipt,
17+
);
18+
newGTCREvent.parameters = [];
19+
20+
const addressParam = new ethereum.EventParam(
21+
'_address',
22+
ethereum.Value.fromAddress(Address.fromString(address)),
23+
);
24+
25+
newGTCREvent.parameters.push(addressParam);
26+
27+
return newGTCREvent;
28+
}

tests/utils/mockValues.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const REGISTRY_ADDRESS = '0x08e58bc26cfb0d346babd253a1799866f269805a';
2+
export const ITEM_ID =
3+
'0x9caa5e1ac3fac10c7953a80f022f78bce895bff8e6c4eeca0aab987e1f5aa103';
4+
export const GRAPH_ITEM_ID = `${ITEM_ID}@${REGISTRY_ADDRESS}`;
5+
export const IPFS_HASH =
6+
'QmfNNjdL5P2J9gM3yk8qoFUixqzsSF2Yzo1gYrfZ6QBgw6/item.json';
7+
export const ITEM_DATA = `/ipfs/${IPFS_HASH}`;

tsconfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"resolveJsonModule": true,
1616
"experimentalDecorators": true
1717
},
18-
"include": ["src"],
19-
"exclude": ["node_modules"]
18+
"include": [
19+
"src",
20+
"tests"
21+
],
22+
"exclude": [
23+
"node_modules"
24+
]
2025
}

0 commit comments

Comments
 (0)