Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ mixin SearchFactory<T> {

/// Performs a search request and returns a list of items of type `T`.
Future<RestApiResponse<List<T>>> search({
/// **Text Search**: Allows for full-text search on enabled resources.
/// Use the `text` parameter with a `value` containing your search terms.
TextSearch? text,

/// **Scopes**: Allows for scoped queries defined in your Laravel resource.
/// Specify the scope name and its parameters.
List<Scope>? scopes,
Expand Down Expand Up @@ -70,6 +74,7 @@ mixin SearchFactory<T> {
final requestBody = {
"search": {
...?defaultSearchBody?.toJson(),
if (text != null) 'text': text.toJson(),
if (scopes != null) 'scopes': scopes.map((e) => e.toJson()).toList(),
if (filters != null)
'filters': filters.map((e) => e.toJson()).toList(),
Expand All @@ -84,7 +89,7 @@ mixin SearchFactory<T> {
'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.
Expand Down Expand Up @@ -117,9 +122,10 @@ mixin SearchFactory<T> {
);
}
try {
final items = (response.body?['data'] as List<dynamic>)
.map<T>((item) => fromJson(item))
.toList();
final items =
(response.body?['data'] as List<dynamic>)
.map<T>((item) => fromJson(item))
.toList();
return RestApiResponse<List<T>>(
data: items,
body: response.body,
Expand Down
158 changes: 76 additions & 82 deletions test/search_factory_mock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ItemRepositoryWithDefaultBody with SearchFactory<ItemModel> {

@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<String, dynamic> item) => ItemModel.fromJson(item);
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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"},
],
),
);
Expand All @@ -162,24 +159,22 @@ 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"),
],
text: TextSearch(value: "my text search"),
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,
Expand All @@ -189,12 +184,13 @@ 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('text'), isTrue);
expect(capturedArgs[0]["search"].containsKey('filters'), isTrue);
expect(capturedArgs[0]["search"].containsKey('aggregates'), isTrue);
expect(capturedArgs[0]["search"].containsKey('includes'), isTrue);
Expand All @@ -206,23 +202,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);
Expand Down