Skip to content

Commit 625300d

Browse files
committed
tests: added for pact query query
1 parent 710a5d5 commit 625300d

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { gql } from 'graphql-request';
2+
3+
interface PactData {
4+
key: string;
5+
value: string;
6+
}
7+
8+
interface PactQueryItem {
9+
chainId: string;
10+
code: string;
11+
data?: PactData[];
12+
}
13+
14+
interface PactQueryParams {
15+
pactQuery: PactQueryItem[];
16+
}
17+
18+
export const getPactQueryBuilder = (params: PactQueryParams): string => {
19+
if (Object.keys(params).length === 0) {
20+
throw new Error('No parameters provided to getPactQueryBuilder.');
21+
}
22+
23+
if (Object.keys(params).length > 1 || !params.pactQuery) {
24+
throw new Error('Only pactQuery parameter is allowed in getPactQueryBuilder.');
25+
}
26+
27+
const formattedParams = Object.entries(params)
28+
.map(([key, value]) => {
29+
if (Array.isArray(value)) {
30+
const formattedArray = value.map(item => {
31+
const { chainId, code, data } = item;
32+
const dataString = data
33+
? `, data: [${data.map((d: PactData) => `{ key: ${JSON.stringify(d.key)}, value: ${JSON.stringify(d.value)} }`).join(', ')}]`
34+
: '';
35+
return `{ chainId: ${JSON.stringify(chainId)}, code: ${JSON.stringify(code)}${dataString} }`;
36+
});
37+
return `${key}: [${formattedArray.join(', ')}]`;
38+
}
39+
40+
throw new Error('Invalid parameter in getPactQueryBuilder.');
41+
})
42+
.join(', ');
43+
44+
const queryGql = gql`
45+
query {
46+
pactQuery(${formattedParams}) {
47+
chainId
48+
code
49+
error
50+
result
51+
status
52+
}
53+
}
54+
`;
55+
56+
return queryGql;
57+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const pactQueryFixture001 = {
2+
data: {
3+
pactQuery: [
4+
{
5+
chainId: '1',
6+
code: "(coin.details (read-msg 'account))",
7+
error: null,
8+
result:
9+
'{"guard":{"pred":"keys-all","keys":["55c6d18b855820af917b49d49a60e944728a0691a4a9c414a7c0d6b6e31de0ea"]},"balance":1.20342,"account":"test"}',
10+
status: 'success',
11+
},
12+
],
13+
},
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const pactQueryFixture002 = {
2+
data: {
3+
pactQuery: [
4+
{
5+
chainId: '1',
6+
code: '(coin.details "test")',
7+
error: null,
8+
result:
9+
'{"guard":{"pred":"keys-all","keys":["55c6d18b855820af917b49d49a60e944728a0691a4a9c414a7c0d6b6e31de0ea"]},"balance":1.20342,"account":"test"}',
10+
status: 'success',
11+
},
12+
],
13+
},
14+
};

0 commit comments

Comments
 (0)