Skip to content

chore: publish v0.0.3+1 #10

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

Merged
merged 2 commits into from
Oct 17, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.3+1

- fix: Add support to datetime on gt. gte. lt. lte. filter [#8](https://github.com/supabase-community/mock_supabase_http_client/pull/8)

## 0.0.3

- feat: Add support for count and select with count. [#6](https://github.com/supabase-community/mock_supabase_http_client/pull/6)
Expand Down
146 changes: 86 additions & 60 deletions lib/src/mock_supabase_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -542,69 +542,29 @@ class MockSupabaseHttpClient extends BaseClient {
final value = postrestFilter.substring(4);
return (row) => row[columnName].toString() != value;
} else if (postrestFilter.startsWith('gt.')) {
final value = postrestFilter.substring(3);

if (DateTime.tryParse(value) != null) {
final dateTime = DateTime.parse(value);

return (row) {
final rowDate = DateTime.tryParse(row[columnName].toString());
return rowDate != null && rowDate.isAfter(dateTime);
};
} else if (num.tryParse(value) != null) {
return (row) => row[columnName] > num.tryParse(value);
} else {
throw UnimplementedError('Unsupported value type');
}
return _handleComparison(
operator: 'gt',
value: postrestFilter.substring(3),
columnName: columnName,
);
} else if (postrestFilter.startsWith('lt.')) {
final value = postrestFilter.substring(3);

if (DateTime.tryParse(value) != null) {
final dateTime = DateTime.parse(value);

return (row) {
final rowDate = DateTime.tryParse(row[columnName].toString());
return rowDate != null && rowDate.isBefore(dateTime);
};
} else if (num.tryParse(value) != null) {
return (row) => row[columnName] < num.tryParse(value);
} else {
throw UnimplementedError('Unsupported value type');
}
return _handleComparison(
operator: 'lt',
value: postrestFilter.substring(3),
columnName: columnName,
);
} else if (postrestFilter.startsWith('gte.')) {
final value = postrestFilter.substring(4);

if (DateTime.tryParse(value) != null) {
final dateTime = DateTime.parse(value);

return (row) {
final rowDate = DateTime.tryParse(row[columnName].toString());
if (rowDate == null) return false;
return rowDate.isAtSameMomentAs(dateTime) ||
rowDate.isAfter(dateTime);
};
} else if (num.tryParse(value) != null) {
return (row) => row[columnName] >= num.tryParse(value);
} else {
throw UnimplementedError('Unsupported value type');
}
return _handleComparison(
operator: 'gte',
value: postrestFilter.substring(4),
columnName: columnName,
);
} else if (postrestFilter.startsWith('lte.')) {
final value = postrestFilter.substring(4);

if (DateTime.tryParse(value) != null) {
final dateTime = DateTime.parse(value);

return (row) {
final rowDate = DateTime.tryParse(row[columnName].toString());
if (rowDate == null) return false;
return rowDate.isAtSameMomentAs(dateTime) ||
rowDate.isBefore(dateTime);
};
} else if (num.tryParse(value) != null) {
return (row) => row[columnName] <= num.tryParse(value);
} else {
throw UnimplementedError('Unsupported value type');
}
return _handleComparison(
operator: 'lte',
value: postrestFilter.substring(4),
columnName: columnName,
);
} else if (postrestFilter.startsWith('like.')) {
final value = postrestFilter.substring(5);
final regex = RegExp(value.replaceAll('%', '.*'));
Expand Down Expand Up @@ -664,6 +624,72 @@ class MockSupabaseHttpClient extends BaseClient {
return (row) => true;
}

/// Handles comparison operations for date and numeric values.
///
/// This function creates a filter based on the given comparison [operator],
/// [value], and [columnName]. It supports both date and numeric comparisons.
///
/// [operator] can be 'gt', 'lt', 'gte', or 'lte'.
/// [value] is the string representation of the value to compare against.
/// [columnName] is the name of the column to compare in each row.
///
/// Returns a function that takes a row and returns a boolean indicating
/// whether the row matches the comparison criteria.
bool Function(Map<String, dynamic> row) _handleComparison({
required String operator,
required String value,
required String columnName,
}) {
// Check if the value is a valid date
if (DateTime.tryParse(value) != null) {
final dateTime = DateTime.parse(value);
return (row) {
final rowDate = DateTime.tryParse(row[columnName].toString());
if (rowDate == null) return false;
// Perform date comparison based on the operator
switch (operator) {
case 'gt':
return rowDate.isAfter(dateTime);
case 'lt':
return rowDate.isBefore(dateTime);
case 'gte':
return rowDate.isAtSameMomentAs(dateTime) ||
rowDate.isAfter(dateTime);
case 'lte':
return rowDate.isAtSameMomentAs(dateTime) ||
rowDate.isBefore(dateTime);
default:
throw UnimplementedError('Unsupported operator: $operator');
}
};
}
// Check if the value is a valid number
else if (num.tryParse(value) != null) {
final numValue = num.parse(value);
return (row) {
final rowValue = num.tryParse(row[columnName].toString());
if (rowValue == null) return false;
// Perform numeric comparison based on the operator
switch (operator) {
case 'gt':
return rowValue > numValue;
case 'lt':
return rowValue < numValue;
case 'gte':
return rowValue >= numValue;
case 'lte':
return rowValue <= numValue;
default:
throw UnimplementedError('Unsupported operator: $operator');
}
};
}
// Throw an error if the value is neither a date nor a number
else {
throw UnimplementedError('Unsupported value type');
}
}

StreamedResponse _createResponse(dynamic data,
{int statusCode = 200,
required BaseRequest request,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mock_supabase_http_client
description: A mock Supabase client for testing API calls without making actual http requests for Dart/Flutter apps that use Supabase.
version: 0.0.3
version: 0.0.3+1
homepage: 'https://supabase.com'
repository: https://github.com/supabase-community/mock_supabase_http_client

Expand Down
34 changes: 27 additions & 7 deletions test/mock_supabase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -707,17 +707,22 @@ void main() {
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
},
{
'title': 'Fourth post',
'author_id': 2,
'createdAt': '2021-08-04 11:26:15.307+00'
}
]);
final count = await mockSupabase
.from('data')
.count()
.gt('createdAt', '2021-08-02 10:26:15.307+00');
.gt('createdAt', '2021-08-02 11:26:15.307+00');

expect(count, 2);
});

test('count with gte. filter with datetime format', () async {
test('count with gte filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
Expand All @@ -733,6 +738,11 @@ void main() {
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
},
{
'title': 'Fourth post',
'author_id': 2,
'createdAt': '2021-08-04 11:26:15.307+00'
}
]);

Expand All @@ -741,7 +751,7 @@ void main() {
.count()
.gte('createdAt', '2021-08-02 11:26:15.307+00');

expect(count, 2);
expect(count, 3);
});

test('count with lt filter with datetime format', () async {
Expand All @@ -760,17 +770,22 @@ void main() {
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
},
{
'title': 'Fourth post',
'author_id': 2,
'createdAt': '2021-08-04 11:26:15.307+00'
}
]);
final count = await mockSupabase
.from('data')
.count()
.lt('createdAt', '2021-08-02 12:26:15.307+00');
.lt('createdAt', '2021-08-03 11:26:15.307+00');

expect(count, 2);
});

test('count with lte. filter with datetime format', () async {
test('count with lte filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
Expand All @@ -786,15 +801,20 @@ void main() {
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
},
{
'title': 'Fourth post',
'author_id': 2,
'createdAt': '2021-08-04 11:26:15.307+00'
}
]);

final count = await mockSupabase
.from('data')
.count()
.lte('createdAt', '2021-08-02 11:26:15.307+00');
.lte('createdAt', '2021-08-03 11:26:15.307+00');

expect(count, 2);
expect(count, 3);
});

test('count with data and filter', () async {
Expand Down