Skip to content

Commit

Permalink
fix: dont use both filters at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricziel committed Jan 9, 2025
1 parent 7a5074f commit 7e68968
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/src/rows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class ListRowsOptions {
}).join(',');
}

// Handle JSON filters
if (filters != null && filters!.isNotEmpty) {
final filterJson = {
'filter_type': filterType,
Expand All @@ -490,14 +491,18 @@ class ListRowsOptions {
params['filters'] = jsonEncode(filterJson);
}

// Add individual field filters
fieldFilters?.forEach((field, conditions) {
conditions.forEach((type, value) {
params['filter__${field}__$type'] = value;
// Handle field filters
if (fieldFilters != null && fieldFilters!.isNotEmpty) {
fieldFilters?.forEach((field, conditions) {
conditions.forEach((type, value) {
params['filter__${field}__$type'] = value;
});
});
});
}

if (filterType != 'AND') {
// Add filter_type if either type of filter is present
if ((filters != null && filters!.isNotEmpty) ||
(fieldFilters != null && fieldFilters!.isNotEmpty)) {
params['filter_type'] = filterType;
}

Expand Down
142 changes: 142 additions & 0 deletions test/rows_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,148 @@ void main() {
expect(rows[3].fields['field_1'], equals('Row 4'));
});

test('correctly converts filter parameters', () async {
// Test with JSON filters
final uriWithJsonFilters =
Uri.parse('http://localhost/api/database/rows/table/1/')
.replace(queryParameters: {
'page': '1',
'size': '100',
'filters': jsonEncode({
'filter_type': 'OR',
'filters': [
{
'field': 'status',
'type': 'equal',
'value': 'active',
}
],
}),
'filter_type': 'OR',
});

when(mockClient.get(
uriWithJsonFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).thenAnswer((_) async => http.Response(
jsonEncode({
'count': 0,
'next': null,
'previous': null,
'results': [],
}),
200));

await client.listAllRows(
1,
options: ListRowsOptions(
filterType: 'OR',
filters: [
RowFilter(
field: 'status',
operator: FilterOperator.equal,
value: 'active',
),
],
),
);

verify(mockClient.get(
uriWithJsonFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).called(1);

// Test with field filters
final uriWithFieldFilters =
Uri.parse('http://localhost/api/database/rows/table/1/')
.replace(queryParameters: {
'page': '1',
'size': '100',
'filter__status__equal': 'active',
'filter_type': 'AND',
});

when(mockClient.get(
uriWithFieldFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).thenAnswer((_) async => http.Response(
jsonEncode({
'count': 0,
'next': null,
'previous': null,
'results': [],
}),
200));

await client.listAllRows(
1,
options: ListRowsOptions(
fieldFilters: {
'status': {'equal': 'active'},
},
),
);

verify(mockClient.get(
uriWithFieldFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).called(1);

// Test with both filter types
final uriWithBothFilters =
Uri.parse('http://localhost/api/database/rows/table/1/')
.replace(queryParameters: {
'page': '1',
'size': '100',
'filters': jsonEncode({
'filter_type': 'OR',
'filters': [
{
'field': 'status',
'type': 'equal',
'value': 'active',
}
],
}),
'filter__priority__equal': 'high',
'filter_type': 'OR',
});

when(mockClient.get(
uriWithBothFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).thenAnswer((_) async => http.Response(
jsonEncode({
'count': 0,
'next': null,
'previous': null,
'results': [],
}),
200));

await client.listAllRows(
1,
options: ListRowsOptions(
filterType: 'OR',
filters: [
RowFilter(
field: 'status',
operator: FilterOperator.equal,
value: 'active',
),
],
fieldFilters: {
'priority': {'equal': 'high'},
},
),
);

verify(mockClient.get(
uriWithBothFilters,
headers: argThat(isA<Map<String, String>>(), named: 'headers'),
)).called(1);
});

test('respects provided options', () async {
final uri = Uri.parse('http://localhost/api/database/rows/table/1/')
.replace(queryParameters: {
Expand Down

0 comments on commit 7e68968

Please sign in to comment.