-
Notifications
You must be signed in to change notification settings - Fork 25
Tests counties endpoint #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TarekkMA
wants to merge
2
commits into
vostpt:develop
Choose a base branch
from
TarekkMA:tests-counties-endpoint
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'package:vost/constants.dart'; | ||
| import 'package:vost/data/remote/endpoints/county_endpoints.dart'; | ||
|
|
||
| import '../../../utils/dioadapter/_base_dio_adapter.dart'; | ||
|
|
||
| void main() { | ||
| group("CountyEndpoints getCounties()", () { | ||
| test('200 Response Valid Json Test', () async { | ||
| final mockDio = | ||
| BaseDioAdapter.success(path: pathCounties, response: okResponseValid) | ||
| .getMockedDioClient(); | ||
| final countyEndpoints = CountyEndpoints(mockDio); | ||
|
|
||
| final counties = await countyEndpoints.getCounties(); | ||
|
|
||
| expect(counties.statusCode, 200); | ||
| expect(counties.data, jsonDecode(okResponseValid)); | ||
| }); | ||
|
|
||
| test('200 Response Invalid Json Test', () async { | ||
| final mockDio = BaseDioAdapter.success( | ||
| path: pathCounties, response: okResponseInvalid) | ||
| .getMockedDioClient(); | ||
| final countyEndpoints = CountyEndpoints(mockDio); | ||
|
|
||
| dynamic error; | ||
| try { | ||
| await countyEndpoints.getCounties(); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| expect(error, TypeMatcher<DioError>()); | ||
| //check if the underlying of DioError error is FormatException | ||
| expect(error.error, TypeMatcher<FormatException>()); | ||
| }); | ||
|
|
||
| test('500 Response Test', () async { | ||
| final mockDio = | ||
| BaseDioAdapter.failure(path: pathCounties,statusCode: 500,response: okResponseValid).getMockedDioClient(); | ||
| final countyEndpoints = CountyEndpoints(mockDio); | ||
|
|
||
| dynamic error; | ||
| try { | ||
| await countyEndpoints.getCounties(); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| expect(error, TypeMatcher<DioError>()); | ||
| expect(error.response.statusCode,500); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| final okResponseValid = """ | ||
| { | ||
| "links": { | ||
| "first": "https://api.vost.pt/v1/counties?page=1", | ||
| "last": "https://api.vost.pt/v1/counties?page=308", | ||
| "next": "https://api.vost.pt/v1/counties?page=2" | ||
| }, | ||
| "data": [ | ||
| { | ||
| "type": "counties", | ||
| "id": "270", | ||
| "attributes": { | ||
| "code": "181600", | ||
| "name": "SÃO PEDRO DO SUL", | ||
| "created_at": "2019-07-02 13:31:05", | ||
| "updated_at": "2019-07-02 13:31:05" | ||
| }, | ||
| "relationships": { | ||
| "district": { | ||
| "data": null | ||
| } | ||
| }, | ||
| "links": { | ||
| "self": "https://api.vost.pt/v1/counties/270" | ||
| } | ||
| } | ||
| ], | ||
| "meta": { | ||
| "items": 1, | ||
| "total": 308 | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| final okResponseInvalid = """ | ||
| { <<INVALID CHARACTERS>> | ||
| "links": { | ||
| "first": "https://api.vost.pt/v1/counties?page=1", | ||
| "last": "https://api.vost.pt/v1/counties?page=308", | ||
| "next": "https://api.vost.pt/v1/counties?page=2" | ||
| }, | ||
| "data": [ | ||
| { | ||
| "type": "counties", | ||
| "id": "270", | ||
| "attributes": { | ||
| "code": "181600", | ||
| "name": "SÃO PEDRO DO SUL", | ||
| "created_at": "2019-07-02 13:31:05", | ||
| "updated_at": "2019-07-02 13:31:05" | ||
| }, | ||
| "relationships": { | ||
| "district": { | ||
| "data": null | ||
| } | ||
| }, | ||
| "links": { | ||
| "self": "https://api.vost.pt/v1/counties/270" | ||
| } | ||
| } | ||
| ], | ||
| "meta": { | ||
| "items": 1, | ||
| "total": 308 | ||
| } | ||
| } | ||
| """; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'package:vost/constants.dart'; | ||
| import 'package:vost/data/remote/endpoints/county_endpoints.dart'; | ||
| import 'package:vost/data/remote/models/response/base_list_response.dart'; | ||
| import 'package:vost/data/remote/services/county_service.dart'; | ||
|
|
||
| import '../../../utils/dioadapter/_base_dio_adapter.dart'; | ||
|
TarekkMA marked this conversation as resolved.
|
||
|
|
||
| class MockCountyEndpoint extends Mock implements CountyEndpoints {} | ||
|
|
||
| void main() { | ||
| group("CountyService getCounties()", () { | ||
| test('200 Response Valid Json Test', () async { | ||
| final mockEndpoint = MockCountyEndpoint(); | ||
| final baseListResponse = BaseListResponse.fromJson((okResponseValid)); | ||
| final mockResponse = Response(statusCode: 200, data: (okResponseValid)); | ||
| when(mockEndpoint.getCounties()) | ||
| .thenAnswer((_) => Future.value(mockResponse)); | ||
| final service = CountyService(mockEndpoint); | ||
|
|
||
| final counties = service.getCounties(); | ||
|
|
||
| verify(mockEndpoint.getCounties()).called(1); | ||
| await expectLater(counties, emits(baseListResponse)); | ||
| }); | ||
|
|
||
| test('Faield Request', () async { | ||
| final mockEndpoint = MockCountyEndpoint(); | ||
| when(mockEndpoint.getCounties()).thenThrow(DioError(message: "error")); | ||
| final service = CountyService(mockEndpoint); | ||
|
|
||
| dynamic error; | ||
| try { | ||
| final counties = service.getCounties(); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| expect(error, TypeMatcher<DioError>()); | ||
| expect(error.message,"error"); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| final okResponseValid = """ | ||
| { | ||
| "links": { | ||
| "first": "https://api.vost.pt/v1/counties?page=1", | ||
| "last": "https://api.vost.pt/v1/counties?page=308", | ||
| "next": "https://api.vost.pt/v1/counties?page=2" | ||
| }, | ||
| "data": [ | ||
| { | ||
| "type": "counties", | ||
| "id": "270", | ||
| "attributes": { | ||
| "code": "181600", | ||
| "name": "SÃO PEDRO DO SUL", | ||
| "created_at": "2019-07-02 13:31:05", | ||
| "updated_at": "2019-07-02 13:31:05" | ||
| }, | ||
| "relationships": { | ||
| "district": null | ||
| }, | ||
| "links": { | ||
| "self": "https://api.vost.pt/v1/counties/270" | ||
| } | ||
| } | ||
| ], | ||
| "meta": { | ||
| "items": 1, | ||
| "total": 308 | ||
| } | ||
| } | ||
| """; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:vost/constants.dart'; | ||
|
|
||
| class BaseDioAdapter extends HttpClientAdapter { | ||
| final DefaultHttpClientAdapter _defaultHttpClientAdapter = | ||
| DefaultHttpClientAdapter(); | ||
|
|
||
| final String path; | ||
| final String response; | ||
| final int statusCode; | ||
|
|
||
| BaseDioAdapter(this.path, this.response, this.statusCode); | ||
|
|
||
| BaseDioAdapter.success({this.path, this.response}) : statusCode = 200; | ||
|
|
||
| BaseDioAdapter.failure({this.path, this.response, this.statusCode = 500}); | ||
|
|
||
| @override | ||
| Future<ResponseBody> fetch(RequestOptions options, | ||
| Stream<List<int>> requestStream, Future cancelFuture) async { | ||
| Uri uri = options.uri; | ||
| if (uri.host == mockHost) { | ||
| if(uri.path == "/$path"){ | ||
| return ResponseBody.fromString( | ||
| response, | ||
| statusCode, | ||
| DioHttpHeaders.fromMap({ | ||
| HttpHeaders.contentTypeHeader: ContentType.json, | ||
| }), | ||
| ); | ||
| }else{ | ||
| return ResponseBody.fromString("", 404, DioHttpHeaders()); | ||
| } | ||
| } | ||
| return _defaultHttpClientAdapter.fetch( | ||
| options, requestStream, cancelFuture); | ||
| } | ||
|
|
||
| Dio getMockedDioClient(){ | ||
| final dio = Dio(); | ||
| dio.options.baseUrl = mockBase; | ||
| dio.httpClientAdapter = this; | ||
| return dio; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.