-
Notifications
You must be signed in to change notification settings - Fork 44
/
open_meteo_api_client_test.dart
202 lines (185 loc) · 6.24 KB
/
open_meteo_api_client_test.dart
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// ignore_for_file: prefer_const_constructors
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';
import 'package:open_meteo_api/open_meteo_api.dart';
import 'package:test/test.dart';
class MockHttpClient extends Mock implements http.Client {}
class MockResponse extends Mock implements http.Response {}
class FakeUri extends Fake implements Uri {}
void main() {
group('OpenMeteoApiClient', () {
late http.Client httpClient;
late OpenMeteoApiClient apiClient;
setUpAll(() {
registerFallbackValue(FakeUri());
});
setUp(() {
httpClient = MockHttpClient();
apiClient = OpenMeteoApiClient(httpClient: httpClient);
});
group('constructor', () {
test('does not require an httpClient', () {
expect(OpenMeteoApiClient(), isNotNull);
});
});
group('locationSearch', () {
const query = 'mock-query';
test('makes correct http request', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('{}');
when(() => httpClient.get(any())).thenAnswer((_) async => response);
try {
await apiClient.locationSearch(query);
} catch (_) {}
verify(
() => httpClient.get(
Uri.https(
'geocoding-api.open-meteo.com',
'/v1/search',
{'name': query, 'count': '1'},
),
),
).called(1);
});
test('throws LocationRequestFailure on non-200 response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(400);
when(() => httpClient.get(any())).thenAnswer((_) async => response);
expect(
() async => apiClient.locationSearch(query),
throwsA(isA<LocationRequestFailure>()),
);
});
test('throws LocationNotFoundFailure on error response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('{}');
when(() => httpClient.get(any())).thenAnswer((_) async => response);
await expectLater(
apiClient.locationSearch(query),
throwsA(isA<LocationNotFoundFailure>()),
);
});
test('throws LocationNotFoundFailure on empty response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('{"results": []}');
when(() => httpClient.get(any())).thenAnswer((_) async => response);
await expectLater(
apiClient.locationSearch(query),
throwsA(isA<LocationNotFoundFailure>()),
);
});
test('returns Location on valid response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn(
'''
{
"results": [
{
"id": 4887398,
"name": "Chicago",
"latitude": 41.85003,
"longitude": -87.65005
}
]
}''',
);
when(() => httpClient.get(any())).thenAnswer((_) async => response);
final actual = await apiClient.locationSearch(query);
expect(
actual,
isA<Location>()
.having((l) => l.name, 'name', 'Chicago')
.having((l) => l.id, 'id', 4887398)
.having((l) => l.latitude, 'latitude', 41.85003)
.having((l) => l.longitude, 'longitude', -87.65005),
);
});
});
group('getWeather', () {
const latitude = 41.85003;
const longitude = -87.6500;
test('makes correct http request', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('{}');
when(() => httpClient.get(any())).thenAnswer((_) async => response);
try {
await apiClient.getWeather(latitude: latitude, longitude: longitude);
} catch (_) {}
verify(
() => httpClient.get(
Uri.https('api.open-meteo.com', 'v1/forecast', {
'latitude': '$latitude',
'longitude': '$longitude',
'current_weather': 'true'
}),
),
).called(1);
});
test('throws WeatherRequestFailure on non-200 response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(400);
when(() => httpClient.get(any())).thenAnswer((_) async => response);
expect(
() async => apiClient.getWeather(
latitude: latitude,
longitude: longitude,
),
throwsA(isA<WeatherRequestFailure>()),
);
});
test('throws WeatherNotFoundFailure on empty response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('{}');
when(() => httpClient.get(any())).thenAnswer((_) async => response);
expect(
() async => apiClient.getWeather(
latitude: latitude,
longitude: longitude,
),
throwsA(isA<WeatherNotFoundFailure>()),
);
});
test('returns weather on valid response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn(
'''
{
"latitude": 43,
"longitude": -87.875,
"generationtime_ms": 0.2510547637939453,
"utc_offset_seconds": 0,
"timezone": "GMT",
"timezone_abbreviation": "GMT",
"elevation": 189,
"current_weather": {
"temperature": 15.3,
"windspeed": 25.8,
"winddirection": 310,
"weathercode": 63,
"time": "2022-09-12T01:00"
}
}
''',
);
when(() => httpClient.get(any())).thenAnswer((_) async => response);
final actual = await apiClient.getWeather(
latitude: latitude,
longitude: longitude,
);
expect(
actual,
isA<Weather>()
.having((w) => w.temperature, 'temperature', 15.3)
.having((w) => w.weatherCode, 'weatherCode', 63.0),
);
});
});
});
}