-
Notifications
You must be signed in to change notification settings - Fork 308
/
location.test.ts
132 lines (120 loc) · 3.42 KB
/
location.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
import { AdapterError, Requester } from '@chainlink/ea-bootstrap'
import { assertError } from '@chainlink/ea-test-helpers'
import { AdapterRequest } from '@chainlink/ea-bootstrap'
import { makeExecute } from '../../src/adapter'
import { TInputParameters } from '../../src/endpoint'
import {
encodeLocationResult,
getLocationResult,
Location,
LocationResult,
noLocationResult,
} from '../../src/endpoint/location'
describe('validation error', () => {
const jobID = '1'
const execute = makeExecute()
process.env.API_KEY = 'test_api_key'
const requests = [
{
name: 'lat not supplied',
id: '1',
testData: {
data: {
endpoint: 'location',
lon: -7.77,
lat: '',
},
},
},
{
name: 'lon not supplied',
id: '1',
testData: {
data: {
endpoint: 'location',
lat: 42.42,
},
},
},
]
requests.forEach((req) => {
it(`${req.name}`, async () => {
try {
await execute(req.testData as AdapterRequest<TInputParameters>, {})
} catch (error) {
const errorResp = Requester.errored(jobID, error as AdapterError)
assertError({ expected: 400, actual: errorResp.statusCode }, errorResp, jobID)
}
})
})
})
describe('getLocationResult()', () => {
const testCasesError = [
{
name: 'locations has more than one item',
testData: ['item1', 'item2'],
},
{
name: 'location key is not a number',
testData: [
{
Key: 'LINK',
EnglishName: 'Chainlink',
Country: {
ID: 'CL',
},
},
],
},
{
name: 'location is missing data',
testData: [
{
Key: '777',
EnglishName: 'Chainlink',
Country: {},
},
],
},
]
testCasesError.forEach((testCase) => {
it(`throws an error when ${testCase.name}`, async () => {
expect(() => getLocationResult(testCase.testData as Location[])).toThrow()
})
})
it('returns the noLocationResult object', () => {
const locationResult = getLocationResult([])
expect(locationResult).toEqual(noLocationResult)
})
it('parses the API response data', () => {
const locations: Location[] = [
{
Key: '777',
EnglishName: 'Chainlink',
Country: { ID: 'CL' },
},
]
const locationResult = getLocationResult(locations)
const expectedLocationResult: LocationResult = {
locationFound: true,
locationKey: 777,
name: 'Chainlink',
countryCode: '0x434c',
}
expect(locationResult).toEqual(expectedLocationResult)
})
})
describe('encodeLocationResult()', () => {
it('encodes the endpoint result', () => {
const locationResult: LocationResult = {
locationFound: true,
locationKey: 777,
name: 'Chainlink',
countryCode: '0x434c',
}
const encodedResult = encodeLocationResult(locationResult)
const expectedEncodedResult =
'0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000003090000000000000000000000000000000000000000000000000000000000000060434c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009436861696e6c696e6b0000000000000000000000000000000000000000000000'
expect(encodedResult).toBe(expectedEncodedResult)
})
})