forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrent-conditions.ts
160 lines (140 loc) · 4.75 KB
/
current-conditions.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
import { assertError, assertSuccess } from '@chainlink/ea-test-helpers'
import { AdapterRequest } from '@chainlink/types'
import {
mockAWCurrentConditionsResponseError,
mockAWCurrentConditionsResponseSuccess,
mockAWCurrentConditionsResponseSuccessMalformed1,
mockAWCurrentConditionsResponseSuccessMalformed2,
mockAWCurrentConditionsResponseSuccessMalformed3,
} from './fixtures'
import { Unit } from '../../src/endpoint/current-conditions'
import { SuiteContext } from './adapter.test'
export function currentConditionsTests(context: SuiteContext): void {
const id = '1'
describe('error calls', () => {
const locationKey = 123456
describe('when unsuccessfully requests accuweather API', () => {
it('should throw an exception', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey,
units: Unit.METRIC,
},
}
mockAWCurrentConditionsResponseError(locationKey)
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
assertError({ expected: 500, actual: response.body.providerStatusCode }, response.body, id)
})
})
describe('when successfully requests accuweather API', () => {
it('should throw an exception if the response data format is malformed', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey,
units: Unit.METRIC,
},
}
mockAWCurrentConditionsResponseSuccessMalformed1(locationKey)
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(500)
assertError({ expected: 500, actual: response.status }, response.body, id)
})
it('should throw an exception if the response is missing data', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey,
units: Unit.METRIC,
},
}
mockAWCurrentConditionsResponseSuccessMalformed2(locationKey)
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(500)
assertError({ expected: 500, actual: response.status }, response.body, id)
})
it('should throw an exception if there is a NaN condition', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey,
units: Unit.METRIC,
},
}
mockAWCurrentConditionsResponseSuccessMalformed3(locationKey)
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(500)
assertError({ expected: 500, actual: response.status }, response.body, id)
})
})
})
describe('success calls', () => {
it('returns the result encoded', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey: 2097720,
units: Unit.METRIC,
},
}
mockAWCurrentConditionsResponseSuccess()
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
assertSuccess({ expected: 200, actual: response.statusCode }, response.body, id)
expect(response.body).toMatchSnapshot()
})
it('returns the result as JSON', async () => {
const data: AdapterRequest = {
id,
data: {
endpoint: 'current-conditions',
locationKey: 2097720,
units: Unit.METRIC,
encodeResult: false,
},
}
mockAWCurrentConditionsResponseSuccess()
const response = await context.req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
assertSuccess({ expected: 200, actual: response.statusCode }, response.body, id)
expect(response.body).toMatchSnapshot()
})
})
}