forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnba.test.ts
178 lines (162 loc) · 5.52 KB
/
nba.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { AdapterRequest } from '@chainlink/types'
import { server as startServer } from '../../src'
import nock from 'nock'
import request, { SuperTest, Test } from 'supertest'
import { ethers } from 'ethers'
import { AddressInfo } from 'net'
import { mockBooleanOnlyGalaxisApiResp, mockNonBooleanOnlyGalaxisApiResp } from './fixtures'
import { Interface } from 'ethers/lib/utils'
import { BATCH_WRITER_ABI, EC_REGISTRY_ABI, TRAIT_IMPLEMENTER_ABI } from '../../src/abis'
const fakeBooleanOnlyEndpoint = 'http://galaxis.com/achievements'
const fakeNonBooleanOnlyEndpoint = 'http://galaxis-fake-non-boolean.com/achievements'
const mockECRegistryInterface = new Interface(EC_REGISTRY_ABI)
const mockTraitImplementerInterface = new Interface(TRAIT_IMPLEMENTER_ABI)
const mockBatchWriterInterface = new Interface(BATCH_WRITER_ABI)
const mockImplementerAddress = 'test-implementer-address'
const mockBatchWriterAddress = 'test-batch-writer-address'
jest.mock('ethers', () => {
const actualModule = jest.requireActual('ethers')
const getMockContractInterface = (address: string): ethers.ContractInterface => {
switch (address) {
case mockImplementerAddress:
return mockTraitImplementerInterface
case mockBatchWriterAddress:
return mockBatchWriterInterface
default:
return mockECRegistryInterface
}
}
return {
...actualModule,
ethers: {
...actualModule.ethers,
providers: {
JsonRpcProvider: function (_: string): ethers.provider.JsonRpcProvider {
return {}
},
},
Contract: function (address: string) {
let estimateGasIncrement = 20000
const numTimesGasEstimated = 0
const initialGasEstimate = 90000
return {
address,
estimateGas: {
estimate: jest.fn().mockImplementation(() => {
const estimatedGas = numTimesGasEstimated * estimateGasIncrement + initialGasEstimate
estimateGasIncrement++
return estimatedGas
}),
},
interface: getMockContractInterface(address),
LastDataRecordId: jest.fn().mockReturnValue(0),
traits: jest.fn().mockReturnValue({ implementer: mockImplementerAddress }),
addressCanModifyTrait: jest.fn().mockImplementation((_, achievementID: number) => {
return achievementID === 2 || achievementID === 15
}),
getData: jest.fn().mockReturnValue([]),
playerCount: jest.fn().mockReturnValue(300),
getTeams: jest.fn().mockReturnValue([
{
id: 1,
name: 'team-1',
city: 'Seattle',
tricode: 'SEA',
real_id: 1610612737,
},
{
id: 2,
name: 'team-1',
city: 'Seattle',
tricode: 'SEA',
real_id: 1610612738,
},
]),
getPlayers: jest.fn().mockReturnValue([
{
id: 3,
team_id: 1,
real_id: 203991,
real_team_id: 1610612738,
name: 'Lebron James',
},
{
id: 4,
team_id: 1,
real_id: 203991,
real_team_id: 1610612738,
name: 'Steph Curry',
},
]),
}
},
},
}
})
let oldEnv: NodeJS.ProcessEnv
let fastify: FastifyInstance
let req: SuperTest<Test>
const jobID = '1'
describe('nba', () => {
beforeAll(async () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
fastify = await startServer()
req = request(`localhost:${(fastify.server.address() as AddressInfo).port}`)
process.env.POLYGON_RPC_URL = process.env.POLYGON_RPC_URL || 'fake-polygon-rpc-url'
process.env.CACHE_ENABLED = 'false'
process.env.CHAIN_BATCH_WRITE_ADAPTER_ADDRESS = mockBatchWriterAddress
if (process.env.RECORD) {
nock.recorder.rec()
}
})
afterAll((done) => {
process.env = oldEnv
fastify.close(done)
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
})
afterEach(() => {
delete process.env.API_ENDPOINT
nock.cleanAll()
})
describe('successful call for successful calls', () => {
const date = '2021-05-22'
it('return success when fetching the achievements and returns the correct result for boolean only achievements', async () => {
process.env.API_ENDPOINT = process.env.API_ENDPOINT || fakeBooleanOnlyEndpoint
mockBooleanOnlyGalaxisApiResp(fakeBooleanOnlyEndpoint, date)
const data: AdapterRequest = {
id: jobID,
data: {
date,
},
}
const response = await req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(response.body).toMatchSnapshot()
})
it('return success when fetching the achievements and returns the correct result for non boolean only achievements', async () => {
process.env.API_ENDPOINT = process.env.API_ENDPOINT || fakeNonBooleanOnlyEndpoint
mockNonBooleanOnlyGalaxisApiResp(fakeNonBooleanOnlyEndpoint, date)
const data: AdapterRequest = {
id: jobID,
data: {
date,
},
}
const response = await req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(response.body).toMatchSnapshot()
})
})
})