Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omartinma committed Nov 22, 2023
1 parent 4938c12 commit 34ada5d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
29 changes: 23 additions & 6 deletions packages/api_client/lib/src/resources/questions_resource.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';
import 'dart:io';

import 'package:api_client/api_client.dart';
import 'package:cross_file/cross_file.dart';

/// {@template questions_resource}
/// An api resource to get response to question from the VERTEX api
Expand All @@ -21,22 +21,39 @@ class QuestionsResource {
///
/// Returns a [List<String>].
Future<VertexResponse> getVertexResponse(String query) async {
String body;
if (_realApiEnabled) {
final response = await _apiClient.get(
// TODO(oscar): update with real API once is enabled.
// TODO(oscar): update with real API once is enabled
// and add possible failures.
'google.es',
queryParameters: {
'query': query,
},
);
final json = jsonDecode(response.body);
return VertexResponse.fromJson(json as Map<String, dynamic>);
if (response.statusCode != 200) {
throw ApiClientError(
'GET getVertexResponse with query=$query '
'returned status ${response.statusCode} '
'with the following response: "${response.body}"',
StackTrace.current,
);
}
body = response.body;
} else {
const path = 'lib/src/resources/fake_response.json';
final fakeResponse = await File(path).readAsString();
body = await XFile(path).readAsString();
}

final json = jsonDecode(fakeResponse) as Map<String, dynamic>;
try {
final json = jsonDecode(body) as Map<String, dynamic>;
return VertexResponse.fromJson(json);
} catch (e) {
throw ApiClientError(
'GET getVertexResponse with query=$query '
'returned invalid response "$body"',
StackTrace.current,
);
}
}
}
1 change: 1 addition & 0 deletions packages/api_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
sdk: ">=3.1.0 <4.0.0"

dependencies:
cross_file: ^0.3.3+6
equatable: ^2.0.5
http: ^1.1.0
json_annotation: ^4.8.1
Expand Down
62 changes: 43 additions & 19 deletions packages/api_client/test/src/resources/questions_resource_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,51 @@ void main() {
});

group('getVertexResponse', () {
test('returns a VertexResponse for api enabled', () async {
when(() => response.statusCode).thenReturn(200);
final vertexResponse = VertexResponse(
summary: '',
documents: const [
VertexDocument(
metadata: VertexMetadata(url: '', title: '', description: ''),
),
],
);
when(() => response.body).thenReturn(
jsonEncode(vertexResponse.toJson()),
);
final result = await questionsResourceApiEnabled.getVertexResponse('');
expect(result, equals(vertexResponse));
group('realApiEnabled=true', () {
test('returns a VertexResponse if success', () async {
when(() => response.statusCode).thenReturn(200);
final vertexResponse = VertexResponse(
summary: '',
documents: const [
VertexDocument(
metadata: VertexMetadata(url: '', title: '', description: ''),
),
],
);
when(() => response.body).thenReturn(
jsonEncode(vertexResponse.toJson()),
);
final result =
await questionsResourceApiEnabled.getVertexResponse('');
expect(result, equals(vertexResponse));
});

test('throws ApiClientError when request fails', () async {
when(() => response.statusCode).thenReturn(500);
when(() => response.body).thenReturn('Ops');
await expectLater(
questionsResourceApiEnabled.getVertexResponse(''),
throwsA(isA<ApiClientError>()),
);
});

test('throws ApiClientError when request success but wrong body',
() async {
when(() => response.statusCode).thenReturn(200);
when(() => response.body).thenReturn('Ops');
await expectLater(
questionsResourceApiEnabled.getVertexResponse(''),
throwsA(isA<ApiClientError>()),
);
});
});

test('returns a VertexResponse for api disabled', () async {
final result =
await questionsResourceApiNotEnabled.getVertexResponse('');
expect(result, isA<VertexResponse>());
group('realApiEnabled=false', () {
test('returns a VertexResponse', () async {
final result =
await questionsResourceApiNotEnabled.getVertexResponse('');
expect(result, isA<VertexResponse>());
});
});
});
});
Expand Down

0 comments on commit 34ada5d

Please sign in to comment.