diff --git a/lib/data/core/constants.dart b/lib/data/core/constants.dart index 43e07b0..9cd3b3e 100644 --- a/lib/data/core/constants.dart +++ b/lib/data/core/constants.dart @@ -8,5 +8,5 @@ const List successStatus = [ 206, 207, 208, - 226 + 226, ]; diff --git a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_actions_body.dart b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_actions_body.dart index 6bcdfe3..ad0ab4a 100644 --- a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_actions_body.dart +++ b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_actions_body.dart @@ -1,22 +1,19 @@ class LaravelRestApiActionsBody { final List fields; - LaravelRestApiActionsBody({ - required this.fields, - }); + LaravelRestApiActionsBody({required this.fields}); factory LaravelRestApiActionsBody.fromJson(Map json) { return LaravelRestApiActionsBody( - fields: (json['fields'] as List) - .map((e) => Action.fromJson(e as Map)) - .toList(), + fields: + (json['fields'] as List) + .map((e) => Action.fromJson(e as Map)) + .toList(), ); } Map toJson() { - return { - 'fields': fields.map((action) => action.toJson()).toList(), - }; + return {'fields': fields.map((action) => action.toJson()).toList()}; } } @@ -24,22 +21,13 @@ class Action { final String name; final String value; - Action({ - required this.name, - required this.value, - }); + Action({required this.name, required this.value}); factory Action.fromJson(Map json) { - return Action( - name: json['name'] as String, - value: json['value'] as String, - ); + return Action(name: json['name'] as String, value: json['value'] as String); } Map toJson() { - return { - 'name': name, - 'value': value, - }; + return {'name': name, 'value': value}; } } diff --git a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_mutate_body.dart b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_mutate_body.dart index cbb6cf2..5c6b6db 100644 --- a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_mutate_body.dart +++ b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_mutate_body.dart @@ -1,9 +1,7 @@ class LaravelRestApiMutateBody { List mutate; - LaravelRestApiMutateBody({ - required this.mutate, - }); + LaravelRestApiMutateBody({required this.mutate}); factory LaravelRestApiMutateBody.fromJson(Map json) { return LaravelRestApiMutateBody(mutate: json["mutate"]); @@ -19,11 +17,7 @@ class Mutation { Map? attributes; dynamic key; - Mutation({ - required this.operation, - this.attributes, - this.key, - }); + Mutation({required this.operation, this.attributes, this.key}); factory Mutation.fromJson(Map json) { return Mutation( @@ -42,11 +36,4 @@ class Mutation { } } -enum MutationOperation { - create, - update, - attach, - detach, - toggle, - sync, -} +enum MutationOperation { create, update, attach, detach, toggle, sync } diff --git a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_search_body.dart b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_search_body.dart index c5b93f0..93d2984 100644 --- a/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_search_body.dart +++ b/lib/data/core/models/laravel_rest_api/body/laravel_rest_api_search_body.dart @@ -28,27 +28,34 @@ class LaravelRestApiSearchBody { factory LaravelRestApiSearchBody.fromJson(Map json) { return LaravelRestApiSearchBody( text: json['text'] != null ? TextSearch.fromJson(json['text']) : null, - scopes: (json['scopes'] as List?) - ?.map((e) => Scope.fromJson(e)) - .toList(), - filters: (json['filters'] as List?) - ?.map((e) => Filter.fromJson(e)) - .toList(), - sorts: (json['sorts'] as List?) - ?.map((e) => Sort.fromJson(e)) - .toList(), - selects: (json['selects'] as List?) - ?.map((e) => Select.fromJson(e)) - .toList(), - includes: (json['includes'] as List?) - ?.map((e) => Include.fromJson(e)) - .toList(), - aggregates: (json['aggregates'] as List?) - ?.map((e) => Aggregate.fromJson(e)) - .toList(), - instructions: (json['instructions'] as List?) - ?.map((e) => Instruction.fromJson(e)) - .toList(), + scopes: + (json['scopes'] as List?) + ?.map((e) => Scope.fromJson(e)) + .toList(), + filters: + (json['filters'] as List?) + ?.map((e) => Filter.fromJson(e)) + .toList(), + sorts: + (json['sorts'] as List?) + ?.map((e) => Sort.fromJson(e)) + .toList(), + selects: + (json['selects'] as List?) + ?.map((e) => Select.fromJson(e)) + .toList(), + includes: + (json['includes'] as List?) + ?.map((e) => Include.fromJson(e)) + .toList(), + aggregates: + (json['aggregates'] as List?) + ?.map((e) => Aggregate.fromJson(e)) + .toList(), + instructions: + (json['instructions'] as List?) + ?.map((e) => Instruction.fromJson(e)) + .toList(), gates: (json['gates'] as List?)?.map((e) => e as String).toList(), page: json['page'] as int?, @@ -86,9 +93,7 @@ class TextSearch { } Map toJson() { - return { - if (value != null) 'value': value, - }; + return {if (value != null) 'value': value}; } } @@ -106,10 +111,7 @@ class Scope { } Map toJson() { - return { - 'name': name, - if (parameters != null) 'parameters': parameters, - }; + return {'name': name, if (parameters != null) 'parameters': parameters}; } } @@ -128,9 +130,10 @@ class Filter { operator: json['operator'] as String?, value: json['value'], type: json['type'] as String?, - nested: (json['nested'] as List?) - ?.map((e) => Filter.fromJson(e)) - .toList(), + nested: + (json['nested'] as List?) + ?.map((e) => Filter.fromJson(e)) + .toList(), ); } @@ -159,10 +162,7 @@ class Sort { } Map toJson() { - return { - 'field': field, - 'direction': direction, - }; + return {'field': field, 'direction': direction}; } } @@ -172,15 +172,11 @@ class Select { Select({required this.field}); factory Select.fromJson(Map json) { - return Select( - field: json['field'] as String, - ); + return Select(field: json['field'] as String); } Map toJson() { - return { - 'field': field, - }; + return {'field': field}; } } @@ -194,9 +190,10 @@ class Include { factory Include.fromJson(Map json) { return Include( relation: json['relation'] as String, - filters: (json['filters'] as List?) - ?.map((e) => Filter.fromJson(e)) - .toList(), + filters: + (json['filters'] as List?) + ?.map((e) => Filter.fromJson(e)) + .toList(), limit: json['limit'] as int?, ); } @@ -228,9 +225,10 @@ class Aggregate { relation: json['relation'] as String, type: json['type'] as String, field: json['field'] as String, - filters: (json['filters'] as List?) - ?.map((e) => Filter.fromJson(e)) - .toList(), + filters: + (json['filters'] as List?) + ?.map((e) => Filter.fromJson(e)) + .toList(), ); } @@ -253,17 +251,15 @@ class Instruction { factory Instruction.fromJson(Map json) { return Instruction( name: json['name'] as String, - fields: (json['fields'] as List) - .map((e) => Field.fromJson(e)) - .toList(), + fields: + (json['fields'] as List) + .map((e) => Field.fromJson(e)) + .toList(), ); } Map toJson() { - return { - 'name': name, - 'fields': fields.map((e) => e.toJson()).toList(), - }; + return {'name': name, 'fields': fields.map((e) => e.toJson()).toList()}; } } @@ -274,16 +270,10 @@ class Field { Field({required this.name, required this.value}); factory Field.fromJson(Map json) { - return Field( - name: json['name'] as String, - value: json['value'], - ); + return Field(name: json['name'] as String, value: json['value']); } Map toJson() { - return { - 'name': name, - 'value': value, - }; + return {'name': name, 'value': value}; } } diff --git a/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_mutate_reponse.dart b/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_mutate_reponse.dart index b2cf869..d16abef 100644 --- a/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_mutate_reponse.dart +++ b/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_mutate_reponse.dart @@ -2,10 +2,7 @@ class LaravelRestApiMutateResponse { final List created; final List updated; - LaravelRestApiMutateResponse({ - required this.created, - required this.updated, - }); + LaravelRestApiMutateResponse({required this.created, required this.updated}); factory LaravelRestApiMutateResponse.fromJson(Map json) { return LaravelRestApiMutateResponse( @@ -15,9 +12,6 @@ class LaravelRestApiMutateResponse { } Map toJson() { - return { - 'created': created, - 'updated': updated, - }; + return {'created': created, 'updated': updated}; } } diff --git a/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_search_response.dart b/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_search_response.dart index c995f78..9408dd8 100644 --- a/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_search_response.dart +++ b/lib/data/core/models/laravel_rest_api/response/laravel_rest_api_search_response.dart @@ -22,9 +22,10 @@ class PaginatedResponse { factory PaginatedResponse.fromJson(Map json) { return PaginatedResponse( currentPage: json['current_page'], - data: (json['data'] as List) - .map((item) => UserData.fromJson(item)) - .toList(), + data: + (json['data'] as List) + .map((item) => UserData.fromJson(item)) + .toList(), from: json['from'], lastPage: json['last_page'], perPage: json['per_page'], @@ -64,11 +65,7 @@ class UserData { } Map toJson() { - return { - 'id': id, - 'name': name, - 'gates': gates.toJson(), - }; + return {'id': id, 'name': name, 'gates': gates.toJson()}; } } @@ -114,15 +111,11 @@ class MetaData { MetaData({required this.gates}); factory MetaData.fromJson(Map json) { - return MetaData( - gates: MetaGates.fromJson(json['gates']), - ); + return MetaData(gates: MetaGates.fromJson(json['gates'])); } Map toJson() { - return { - 'gates': gates.toJson(), - }; + return {'gates': gates.toJson()}; } } @@ -132,14 +125,10 @@ class MetaGates { MetaGates({required this.authorizedToCreate}); factory MetaGates.fromJson(Map json) { - return MetaGates( - authorizedToCreate: json['authorized_to_create'], - ); + return MetaGates(authorizedToCreate: json['authorized_to_create']); } Map toJson() { - return { - 'authorized_to_create': authorizedToCreate, - }; + return {'authorized_to_create': authorizedToCreate}; } } diff --git a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_actions_factory.dart b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_actions_factory.dart index d7dadd5..fe52a5a 100644 --- a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_actions_factory.dart +++ b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_actions_factory.dart @@ -32,10 +32,7 @@ mixin ActionsFactory { data: response.body?['data']['impacted'] ?? 0, ); } catch (exception) { - return RestApiResponse( - message: response.message, - statusCode: 500, - ); + return RestApiResponse(message: response.message, statusCode: 500); } } } diff --git a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_delete_factory.dart b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_delete_factory.dart index b0fd2b0..64b87c8 100644 --- a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_delete_factory.dart +++ b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_delete_factory.dart @@ -22,14 +22,13 @@ mixin DeleteFactory { baseRoute, apiMethod: ApiMethod.delete, client: httpClient, - body: { - "resources": resourceIds, - }, + body: {"resources": resourceIds}, ); - final items = (response.body?['data'] as List) - .map((item) => fromJson(item)) - .toList(); + final items = + (response.body?['data'] as List) + .map((item) => fromJson(item)) + .toList(); return RestApiResponse>( statusCode: response.statusCode, body: response.body, diff --git a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_search_factory.dart b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_search_factory.dart index 0ad0399..0b35dd3 100644 --- a/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_search_factory.dart +++ b/lib/data/core/rest_api_factories/laravel_rest_api/laravel_rest_api_search_factory.dart @@ -84,7 +84,7 @@ mixin SearchFactory { 'instructions': instructions.map((e) => e.toJson()).toList(), if (limit != null) 'limit': limit, if (instructions != null) 'page': page, - } + }, }; // Sending the request using a REST API client. @@ -117,9 +117,10 @@ mixin SearchFactory { ); } try { - final items = (response.body?['data'] as List) - .map((item) => fromJson(item)) - .toList(); + final items = + (response.body?['data'] as List) + .map((item) => fromJson(item)) + .toList(); return RestApiResponse>( data: items, body: response.body, diff --git a/lib/data/core/rest_api_repository.dart b/lib/data/core/rest_api_repository.dart index 8d319ea..fbd3b95 100644 --- a/lib/data/core/rest_api_repository.dart +++ b/lib/data/core/rest_api_repository.dart @@ -24,17 +24,9 @@ Future handlingResponse( case ApiMethod.get: response = await client.get(route, headers: headers); case ApiMethod.post: - response = await client.post( - route, - body: body, - headers: headers, - ); + response = await client.post(route, body: body, headers: headers); case ApiMethod.delete: - response = await client.delete( - route, - body: body, - headers: headers, - ); + response = await client.delete(route, body: body, headers: headers); } if (successStatus.contains(response.statusCode)) { diff --git a/lib/data/core/rest_api_testing_interceptor.dart b/lib/data/core/rest_api_testing_interceptor.dart index fbadb59..18b3cd9 100644 --- a/lib/data/core/rest_api_testing_interceptor.dart +++ b/lib/data/core/rest_api_testing_interceptor.dart @@ -5,7 +5,9 @@ class RestApiTestingInterceptor extends Interceptor { @override void onRequest( - RequestOptions options, RequestInterceptorHandler handler) async { + RequestOptions options, + RequestInterceptorHandler handler, + ) async { // await dotenv.load(fileName: ".env/.env.testing"); // options.headers["Authorization"] = dotenv.env['TESTING_JWT']; return super.onRequest(options, handler); diff --git a/lib/data/rest_api_client/api_http_client.dart.dart b/lib/data/rest_api_client/api_http_client.dart.dart index 0e3ddf8..996d6a1 100644 --- a/lib/data/rest_api_client/api_http_client.dart.dart +++ b/lib/data/rest_api_client/api_http_client.dart.dart @@ -7,11 +7,17 @@ class ApiHttpClient implements RestApiClient { ApiHttpClient({required this.dio}); @override - Future get(String url, - {Map? headers, Map? queryParams}) async { + Future get( + String url, { + Map? headers, + Map? queryParams, + }) async { try { - final response = await dio.get("${dio.options.baseUrl}$url", - options: Options(headers: headers), queryParameters: queryParams); + final response = await dio.get( + "${dio.options.baseUrl}$url", + options: Options(headers: headers), + queryParameters: queryParams, + ); return RestApiResponse( statusCode: response.statusCode ?? 500, body: response.data, @@ -29,14 +35,17 @@ class ApiHttpClient implements RestApiClient { Object? body, }) async { try { - final response = await dio.post("${dio.options.baseUrl}$url", - options: Options(headers: headers), - queryParameters: queryParams, - data: body); + final response = await dio.post( + "${dio.options.baseUrl}$url", + options: Options(headers: headers), + queryParameters: queryParams, + data: body, + ); return RestApiResponse( - statusCode: response.statusCode ?? 500, - body: response.data, - message: response.statusMessage); + statusCode: response.statusCode ?? 500, + body: response.data, + message: response.statusMessage, + ); } catch (exception, stackTrace) { return handleError(exception, stackTrace); } @@ -73,9 +82,6 @@ class ApiHttpClient implements RestApiClient { body: exception.response?.data ?? {'error': exception.message}, ); } - return RestApiResponse( - statusCode: 500, - body: {'error': exception}, - ); + return RestApiResponse(statusCode: 500, body: {'error': exception}); } } diff --git a/test/actions_factory_mock_test.dart b/test/actions_factory_mock_test.dart index f6c6cbc..3a299b9 100644 --- a/test/actions_factory_mock_test.dart +++ b/test/actions_factory_mock_test.dart @@ -46,19 +46,14 @@ void main() { requestOptions: RequestOptions(), statusCode: 200, data: { - "data": {"impacted": 10} + "data": {"impacted": 10}, }, ), ); final result = await ItemRepository(mockDio).actions( data: LaravelRestApiActionsBody( - fields: [ - Action( - name: "expires_at", - value: "2023-04-29", - ), - ], + fields: [Action(name: "expires_at", value: "2023-04-29")], ), ); @@ -66,34 +61,33 @@ void main() { }); test('[500] With common laravel error message', () async { - when(mockDio.post( - '/items', - data: { - "fields": [ - {"name": "expires_at", "value": "2023-04-29"}, - ], - }, - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 500, - data: { - "message": "Server error", - "exception": - "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", - "file": - "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", - "line": 23 - }, - )); + when( + mockDio.post( + '/items', + data: { + "fields": [ + {"name": "expires_at", "value": "2023-04-29"}, + ], + }, + ), + ).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 500, + data: { + "message": "Server error", + "exception": + "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", + "file": + "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", + "line": 23, + }, + ), + ); final result = await ItemRepository(mockDio).actions( data: LaravelRestApiActionsBody( - fields: [ - Action( - name: "expires_at", - value: "2023-04-29", - ), - ], + fields: [Action(name: "expires_at", value: "2023-04-29")], ), ); diff --git a/test/delete_factory_mock_test.dart b/test/delete_factory_mock_test.dart index bc6662e..cca87cc 100644 --- a/test/delete_factory_mock_test.dart +++ b/test/delete_factory_mock_test.dart @@ -35,7 +35,7 @@ void main() { mockDio.delete( '/items', data: { - "resources": [5, 6] + "resources": [5, 6], }, ), ).thenAnswer( @@ -45,37 +45,39 @@ void main() { data: { "data": [ {"id": 5, "name": "Axe"}, - {"id": 6, "name": "Hammer"} + {"id": 6, "name": "Hammer"}, ], }, ), ); - final result = await ItemRepository(mockDio).delete( - resourceIds: [5, 6], - ); + final result = await ItemRepository(mockDio).delete(resourceIds: [5, 6]); expect(result.data?.length, 2); }); test('[500] With common laravel error message', () async { - when(mockDio.delete( - '/items', - data: { - "resources": [5, 6] - }, - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 500, - data: { - "message": "Server error", - "exception": - "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", - "file": - "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", - "line": 23 - }, - )); + when( + mockDio.delete( + '/items', + data: { + "resources": [5, 6], + }, + ), + ).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 500, + data: { + "message": "Server error", + "exception": + "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", + "file": + "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", + "line": 23, + }, + ), + ); final result = await ItemRepository(mockDio).delete(resourceIds: [5, 6]); diff --git a/test/mock/item_model.dart b/test/mock/item_model.dart index c66fe40..f7880b3 100644 --- a/test/mock/item_model.dart +++ b/test/mock/item_model.dart @@ -5,16 +5,10 @@ class ItemModel { ItemModel({required this.id, required this.name}); Map toJson() { - return { - 'id': id, - 'name': name, - }; + return {'id': id, 'name': name}; } factory ItemModel.fromJson(Map json) { - return ItemModel( - id: json['id'] as int, - name: json['name'] as String, - ); + return ItemModel(id: json['id'] as int, name: json['name'] as String); } } diff --git a/test/mock/mock_http_client.dart b/test/mock/mock_http_client.dart index 2e4754e..b98d2a5 100644 --- a/test/mock/mock_http_client.dart +++ b/test/mock/mock_http_client.dart @@ -9,8 +9,11 @@ class MockApiHttpClient implements RestApiClient { MockApiHttpClient({required this.dio}); @override - Future get(String url, - {Map? headers, Map? queryParams}) async { + Future get( + String url, { + Map? headers, + Map? queryParams, + }) async { try { final response = await dio.get( url, @@ -52,10 +55,7 @@ class MockApiHttpClient implements RestApiClient { Object? body, }) async { try { - final response = await dio.delete( - url, - data: body, - ); + final response = await dio.delete(url, data: body); return RestApiResponse( statusCode: response.statusCode, body: response.data, @@ -73,9 +73,6 @@ class MockApiHttpClient implements RestApiClient { body: exception.response?.data ?? {'error': exception.message}, ); } - return RestApiResponse( - statusCode: 500, - body: {'error': exception}, - ); + return RestApiResponse(statusCode: 500, body: {'error': exception}); } } diff --git a/test/mutate_factory_mock_test.dart b/test/mutate_factory_mock_test.dart index 06afeac..c067866 100644 --- a/test/mutate_factory_mock_test.dart +++ b/test/mutate_factory_mock_test.dart @@ -41,7 +41,7 @@ void main() { "operation": "create", "attributes": ItemModel(id: 1, name: "name").toJson(), }, - ] + ], }, ), ).thenAnswer( @@ -50,7 +50,7 @@ void main() { statusCode: 200, data: { "created": [1], - "updated": [] + "updated": [], }, ), ); @@ -61,7 +61,7 @@ void main() { Mutation( operation: MutationOperation.create, attributes: ItemModel(id: 1, name: "name").toJson(), - ) + ), ], ), ); @@ -73,28 +73,32 @@ void main() { }); test('[500] With common laravel error message', () async { - when(mockDio.post( - '/items/mutate', - data: { - "mutate": [ - { - "operation": "create", - "attributes": ItemModel(id: 1, name: "name").toJson(), - }, - ] - }, - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 500, - data: { - "message": "Server error", - "exception": - "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", - "file": - "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", - "line": 23 - }, - )); + when( + mockDio.post( + '/items/mutate', + data: { + "mutate": [ + { + "operation": "create", + "attributes": ItemModel(id: 1, name: "name").toJson(), + }, + ], + }, + ), + ).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 500, + data: { + "message": "Server error", + "exception": + "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", + "file": + "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", + "line": 23, + }, + ), + ); final result = await ItemRepository(mockDio).mutate( body: LaravelRestApiMutateBody( @@ -102,7 +106,7 @@ void main() { Mutation( operation: MutationOperation.create, attributes: ItemModel(id: 1, name: "name").toJson(), - ) + ), ], ), ); @@ -112,23 +116,25 @@ void main() { }); }); test('[500] With custom object error message returned', () async { - when(mockDio.post( - '/items/mutate', - data: { - "mutate": [ - { - "operation": "create", - "attributes": ItemModel(id: 1, name: "name").toJson(), - }, - ] - }, - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 500, - data: { - "error": "error", - }, - )); + when( + mockDio.post( + '/items/mutate', + data: { + "mutate": [ + { + "operation": "create", + "attributes": ItemModel(id: 1, name: "name").toJson(), + }, + ], + }, + ), + ).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 500, + data: {"error": "error"}, + ), + ); final result = await ItemRepository(mockDio).mutate( body: LaravelRestApiMutateBody( @@ -136,7 +142,7 @@ void main() { Mutation( operation: MutationOperation.create, attributes: ItemModel(id: 1, name: "name").toJson(), - ) + ), ], ), ); diff --git a/test/search_factory_mock_test.dart b/test/search_factory_mock_test.dart index afce78e..e6da616 100644 --- a/test/search_factory_mock_test.dart +++ b/test/search_factory_mock_test.dart @@ -42,7 +42,8 @@ class ItemRepositoryWithDefaultBody with SearchFactory { @override LaravelRestApiSearchBody? get defaultSearchBody => LaravelRestApiSearchBody( - filters: [Filter(field: "field", operator: "operator", value: "value")]); + filters: [Filter(field: "field", operator: "operator", value: "value")], + ); @override ItemModel fromJson(Map item) => ItemModel.fromJson(item); @@ -64,16 +65,18 @@ void main() { group('Search Factory Tests', () { test('[200] Successful API call with valid JSON', () async { - when(mockDio.post('/items/search')).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 200, - data: { - 'data': [ - {'id': 1, 'name': 'Lou West'}, - {'id': 2, 'name': 'Bridget Wilderman'}, - ], - }, - )); + when(mockDio.post('/items/search')).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 200, + data: { + 'data': [ + {'id': 1, 'name': 'Lou West'}, + {'id': 2, 'name': 'Bridget Wilderman'}, + ], + }, + ), + ); final result = await ItemRepository(mockDio).search(); @@ -82,18 +85,18 @@ void main() { }); test('[200] Successful API call with bad JSON', () async { - when(mockDio.post( - '/items/search', - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 200, - data: { - 'data': [ - {'idd': 1, 'name': 'Lou West'}, - {'idd': 2, 'name': 'Bridget Wilderman'}, - ], - }, - )); + when(mockDio.post('/items/search')).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 200, + data: { + 'data': [ + {'idd': 1, 'name': 'Lou West'}, + {'idd': 2, 'name': 'Bridget Wilderman'}, + ], + }, + ), + ); final result = await ItemRepository(mockDio).search(); @@ -102,20 +105,20 @@ void main() { }); test('[404] With common laravel error message', () async { - when(mockDio.post( - '/items/search', - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 404, - data: { - "message": "Not Found", - "exception": - "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", - "file": - "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", - "line": 23 - }, - )); + when(mockDio.post('/items/search')).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 404, + data: { + "message": "Not Found", + "exception": + "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", + "file": + "/path/to/project/vendor/symfony/http-kernel/Exception/NotFoundHttpException.php", + "line": 23, + }, + ), + ); final result = await ItemRepository(mockDio).search(); @@ -124,15 +127,13 @@ void main() { }); }); test('[500] With custom object error message returned', () async { - when(mockDio.post( - '/items/search', - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 500, - data: { - "error": "error", - }, - )); + when(mockDio.post('/items/search')).thenAnswer( + (_) async => Response( + requestOptions: RequestOptions(), + statusCode: 500, + data: {"error": "error"}, + ), + ); final result = await ItemRepository(mockDio).search(); @@ -141,16 +142,12 @@ void main() { }); test('[500] With custom list error message returned', () async { - when(mockDio.post( - '/items/search', - )).thenAnswer( + when(mockDio.post('/items/search')).thenAnswer( (_) async => Response( requestOptions: RequestOptions(), statusCode: 500, data: [ - { - "error": "error", - } + {"error": "error"}, ], ), ); @@ -162,24 +159,21 @@ void main() { }); test('Check if all attributes filter can be send in body', () async { - when(mockDio.post( - '/items/search', - data: anyNamed('data'), - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 200, - )); + when(mockDio.post('/items/search', data: anyNamed('data'))).thenAnswer( + (_) async => Response(requestOptions: RequestOptions(), statusCode: 200), + ); await ItemRepositoryWithDefaultBody(mockDio).search( - filters: [ - Filter(field: "field", type: "type"), - ], + filters: [Filter(field: "field", type: "type")], aggregates: [ - Aggregate(relation: "relation", type: "type", field: "field") + Aggregate(relation: "relation", type: "type", field: "field"), ], includes: [Include(relation: "relation")], instructions: [ - Instruction(name: "name", fields: [Field(name: "name", value: "value")]) + Instruction( + name: "name", + fields: [Field(name: "name", value: "value")], + ), ], limit: 1, page: 1, @@ -189,10 +183,10 @@ void main() { ); // Check body send to api - final capturedArgs = verify(mockDio.post( - '/items/search', - data: captureAnyNamed('data'), - )).captured; + final capturedArgs = + verify( + mockDio.post('/items/search', data: captureAnyNamed('data')), + ).captured; expect(capturedArgs[0].containsKey('search'), isTrue); expect(capturedArgs[0]["search"].containsKey('filters'), isTrue); @@ -206,23 +200,21 @@ void main() { expect(capturedArgs[0]["search"].containsKey('page'), isTrue); }); test('Check if defaultSearchBody is correctly send to api', () async { - when(mockDio.post( - '/items/search', - data: anyNamed('data'), - )).thenAnswer((_) async => Response( - requestOptions: RequestOptions(), - statusCode: 200, - )); + when(mockDio.post('/items/search', data: anyNamed('data'))).thenAnswer( + (_) async => Response(requestOptions: RequestOptions(), statusCode: 200), + ); - await ItemRepositoryWithDefaultBody(mockDio).search(aggregates: [ - Aggregate(relation: "relation", type: "type", field: "field") - ]); + await ItemRepositoryWithDefaultBody(mockDio).search( + aggregates: [ + Aggregate(relation: "relation", type: "type", field: "field"), + ], + ); // Check body send to api - final capturedArgs = verify(mockDio.post( - '/items/search', - data: captureAnyNamed('data'), - )).captured; + final capturedArgs = + verify( + mockDio.post('/items/search', data: captureAnyNamed('data')), + ).captured; expect(capturedArgs[0].containsKey('search'), isTrue); expect(capturedArgs[0]["search"].containsKey('filters'), isTrue);