Skip to content

Add support to datetime on gt. gte. lt. lte. filter #8

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 3 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
60 changes: 56 additions & 4 deletions lib/src/mock_supabase_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,68 @@ class MockSupabaseHttpClient extends BaseClient {
return (row) => row[columnName].toString() != value;
} else if (postrestFilter.startsWith('gt.')) {
final value = postrestFilter.substring(3);
return (row) => row[columnName] > num.tryParse(value);

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');
}
} else if (postrestFilter.startsWith('lt.')) {
final value = postrestFilter.substring(3);
return (row) => row[columnName] < num.tryParse(value);

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');
}
} else if (postrestFilter.startsWith('gte.')) {
final value = postrestFilter.substring(4);
return (row) => row[columnName] >= num.tryParse(value);

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');
}
} else if (postrestFilter.startsWith('lte.')) {
final value = postrestFilter.substring(4);
return (row) => row[columnName] <= num.tryParse(value);

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');
}
} else if (postrestFilter.startsWith('like.')) {
final value = postrestFilter.substring(5);
final regex = RegExp(value.replaceAll('%', '.*'));
Expand Down
106 changes: 106 additions & 0 deletions test/mock_supabase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,112 @@ void main() {
expect(count, 2);
});

test('count with gt filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
'author_id': 1,
'createdAt': '2021-08-01 11:26:15.307+00'
},
{
'title': 'Second post',
'author_id': 2,
'createdAt': '2021-08-02 11:26:15.307+00'
},
{
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
}
]);
final count = await mockSupabase
.from('data')
.count()
.gt('createdAt', '2021-08-02 10:26:15.307+00');

expect(count, 2);
});

test('count with gte. filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
'author_id': 1,
'createdAt': '2021-08-01 11:26:15.307+00'
},
{
'title': 'Second post',
'author_id': 2,
'createdAt': '2021-08-02 11:26:15.307+00'
},
{
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
}
]);

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

expect(count, 2);
});

test('count with lt filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
'author_id': 1,
'createdAt': '2021-08-01 11:26:15.307+00'
},
{
'title': 'Second post',
'author_id': 2,
'createdAt': '2021-08-02 11:26:15.307+00'
},
{
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
}
]);
final count = await mockSupabase
.from('data')
.count()
.lt('createdAt', '2021-08-02 12:26:15.307+00');

expect(count, 2);
});

test('count with lte. filter with datetime format', () async {
await mockSupabase.from('data').insert([
{
'title': 'First post',
'author_id': 1,
'createdAt': '2021-08-01 11:26:15.307+00'
},
{
'title': 'Second post',
'author_id': 2,
'createdAt': '2021-08-02 11:26:15.307+00'
},
{
'title': 'Third post',
'author_id': 1,
'createdAt': '2021-08-03 11:26:15.307+00'
}
]);

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

expect(count, 2);
});

test('count with data and filter', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post', 'author_id': 1},
Expand Down