Skip to content

Commit a80881d

Browse files
authored
Merge pull request #10 from supabase-community/chore/publish
chore: publish v0.0.3+1
2 parents b3bd6f6 + e8c2ecb commit a80881d

File tree

4 files changed

+118
-68
lines changed

4 files changed

+118
-68
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.3+1
2+
3+
- fix: Add support to datetime on gt. gte. lt. lte. filter [#8](https://github.com/supabase-community/mock_supabase_http_client/pull/8)
4+
15
## 0.0.3
26

37
- feat: Add support for count and select with count. [#6](https://github.com/supabase-community/mock_supabase_http_client/pull/6)

lib/src/mock_supabase_http_client.dart

Lines changed: 86 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -542,69 +542,29 @@ class MockSupabaseHttpClient extends BaseClient {
542542
final value = postrestFilter.substring(4);
543543
return (row) => row[columnName].toString() != value;
544544
} else if (postrestFilter.startsWith('gt.')) {
545-
final value = postrestFilter.substring(3);
546-
547-
if (DateTime.tryParse(value) != null) {
548-
final dateTime = DateTime.parse(value);
549-
550-
return (row) {
551-
final rowDate = DateTime.tryParse(row[columnName].toString());
552-
return rowDate != null && rowDate.isAfter(dateTime);
553-
};
554-
} else if (num.tryParse(value) != null) {
555-
return (row) => row[columnName] > num.tryParse(value);
556-
} else {
557-
throw UnimplementedError('Unsupported value type');
558-
}
545+
return _handleComparison(
546+
operator: 'gt',
547+
value: postrestFilter.substring(3),
548+
columnName: columnName,
549+
);
559550
} else if (postrestFilter.startsWith('lt.')) {
560-
final value = postrestFilter.substring(3);
561-
562-
if (DateTime.tryParse(value) != null) {
563-
final dateTime = DateTime.parse(value);
564-
565-
return (row) {
566-
final rowDate = DateTime.tryParse(row[columnName].toString());
567-
return rowDate != null && rowDate.isBefore(dateTime);
568-
};
569-
} else if (num.tryParse(value) != null) {
570-
return (row) => row[columnName] < num.tryParse(value);
571-
} else {
572-
throw UnimplementedError('Unsupported value type');
573-
}
551+
return _handleComparison(
552+
operator: 'lt',
553+
value: postrestFilter.substring(3),
554+
columnName: columnName,
555+
);
574556
} else if (postrestFilter.startsWith('gte.')) {
575-
final value = postrestFilter.substring(4);
576-
577-
if (DateTime.tryParse(value) != null) {
578-
final dateTime = DateTime.parse(value);
579-
580-
return (row) {
581-
final rowDate = DateTime.tryParse(row[columnName].toString());
582-
if (rowDate == null) return false;
583-
return rowDate.isAtSameMomentAs(dateTime) ||
584-
rowDate.isAfter(dateTime);
585-
};
586-
} else if (num.tryParse(value) != null) {
587-
return (row) => row[columnName] >= num.tryParse(value);
588-
} else {
589-
throw UnimplementedError('Unsupported value type');
590-
}
557+
return _handleComparison(
558+
operator: 'gte',
559+
value: postrestFilter.substring(4),
560+
columnName: columnName,
561+
);
591562
} else if (postrestFilter.startsWith('lte.')) {
592-
final value = postrestFilter.substring(4);
593-
594-
if (DateTime.tryParse(value) != null) {
595-
final dateTime = DateTime.parse(value);
596-
597-
return (row) {
598-
final rowDate = DateTime.tryParse(row[columnName].toString());
599-
if (rowDate == null) return false;
600-
return rowDate.isAtSameMomentAs(dateTime) ||
601-
rowDate.isBefore(dateTime);
602-
};
603-
} else if (num.tryParse(value) != null) {
604-
return (row) => row[columnName] <= num.tryParse(value);
605-
} else {
606-
throw UnimplementedError('Unsupported value type');
607-
}
563+
return _handleComparison(
564+
operator: 'lte',
565+
value: postrestFilter.substring(4),
566+
columnName: columnName,
567+
);
608568
} else if (postrestFilter.startsWith('like.')) {
609569
final value = postrestFilter.substring(5);
610570
final regex = RegExp(value.replaceAll('%', '.*'));
@@ -664,6 +624,72 @@ class MockSupabaseHttpClient extends BaseClient {
664624
return (row) => true;
665625
}
666626

627+
/// Handles comparison operations for date and numeric values.
628+
///
629+
/// This function creates a filter based on the given comparison [operator],
630+
/// [value], and [columnName]. It supports both date and numeric comparisons.
631+
///
632+
/// [operator] can be 'gt', 'lt', 'gte', or 'lte'.
633+
/// [value] is the string representation of the value to compare against.
634+
/// [columnName] is the name of the column to compare in each row.
635+
///
636+
/// Returns a function that takes a row and returns a boolean indicating
637+
/// whether the row matches the comparison criteria.
638+
bool Function(Map<String, dynamic> row) _handleComparison({
639+
required String operator,
640+
required String value,
641+
required String columnName,
642+
}) {
643+
// Check if the value is a valid date
644+
if (DateTime.tryParse(value) != null) {
645+
final dateTime = DateTime.parse(value);
646+
return (row) {
647+
final rowDate = DateTime.tryParse(row[columnName].toString());
648+
if (rowDate == null) return false;
649+
// Perform date comparison based on the operator
650+
switch (operator) {
651+
case 'gt':
652+
return rowDate.isAfter(dateTime);
653+
case 'lt':
654+
return rowDate.isBefore(dateTime);
655+
case 'gte':
656+
return rowDate.isAtSameMomentAs(dateTime) ||
657+
rowDate.isAfter(dateTime);
658+
case 'lte':
659+
return rowDate.isAtSameMomentAs(dateTime) ||
660+
rowDate.isBefore(dateTime);
661+
default:
662+
throw UnimplementedError('Unsupported operator: $operator');
663+
}
664+
};
665+
}
666+
// Check if the value is a valid number
667+
else if (num.tryParse(value) != null) {
668+
final numValue = num.parse(value);
669+
return (row) {
670+
final rowValue = num.tryParse(row[columnName].toString());
671+
if (rowValue == null) return false;
672+
// Perform numeric comparison based on the operator
673+
switch (operator) {
674+
case 'gt':
675+
return rowValue > numValue;
676+
case 'lt':
677+
return rowValue < numValue;
678+
case 'gte':
679+
return rowValue >= numValue;
680+
case 'lte':
681+
return rowValue <= numValue;
682+
default:
683+
throw UnimplementedError('Unsupported operator: $operator');
684+
}
685+
};
686+
}
687+
// Throw an error if the value is neither a date nor a number
688+
else {
689+
throw UnimplementedError('Unsupported value type');
690+
}
691+
}
692+
667693
StreamedResponse _createResponse(dynamic data,
668694
{int statusCode = 200,
669695
required BaseRequest request,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: mock_supabase_http_client
22
description: A mock Supabase client for testing API calls without making actual http requests for Dart/Flutter apps that use Supabase.
3-
version: 0.0.3
3+
version: 0.0.3+1
44
homepage: 'https://supabase.com'
55
repository: https://github.com/supabase-community/mock_supabase_http_client
66

test/mock_supabase_test.dart

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,17 +707,22 @@ void main() {
707707
'title': 'Third post',
708708
'author_id': 1,
709709
'createdAt': '2021-08-03 11:26:15.307+00'
710+
},
711+
{
712+
'title': 'Fourth post',
713+
'author_id': 2,
714+
'createdAt': '2021-08-04 11:26:15.307+00'
710715
}
711716
]);
712717
final count = await mockSupabase
713718
.from('data')
714719
.count()
715-
.gt('createdAt', '2021-08-02 10:26:15.307+00');
720+
.gt('createdAt', '2021-08-02 11:26:15.307+00');
716721

717722
expect(count, 2);
718723
});
719724

720-
test('count with gte. filter with datetime format', () async {
725+
test('count with gte filter with datetime format', () async {
721726
await mockSupabase.from('data').insert([
722727
{
723728
'title': 'First post',
@@ -733,6 +738,11 @@ void main() {
733738
'title': 'Third post',
734739
'author_id': 1,
735740
'createdAt': '2021-08-03 11:26:15.307+00'
741+
},
742+
{
743+
'title': 'Fourth post',
744+
'author_id': 2,
745+
'createdAt': '2021-08-04 11:26:15.307+00'
736746
}
737747
]);
738748

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

744-
expect(count, 2);
754+
expect(count, 3);
745755
});
746756

747757
test('count with lt filter with datetime format', () async {
@@ -760,17 +770,22 @@ void main() {
760770
'title': 'Third post',
761771
'author_id': 1,
762772
'createdAt': '2021-08-03 11:26:15.307+00'
773+
},
774+
{
775+
'title': 'Fourth post',
776+
'author_id': 2,
777+
'createdAt': '2021-08-04 11:26:15.307+00'
763778
}
764779
]);
765780
final count = await mockSupabase
766781
.from('data')
767782
.count()
768-
.lt('createdAt', '2021-08-02 12:26:15.307+00');
783+
.lt('createdAt', '2021-08-03 11:26:15.307+00');
769784

770785
expect(count, 2);
771786
});
772787

773-
test('count with lte. filter with datetime format', () async {
788+
test('count with lte filter with datetime format', () async {
774789
await mockSupabase.from('data').insert([
775790
{
776791
'title': 'First post',
@@ -786,15 +801,20 @@ void main() {
786801
'title': 'Third post',
787802
'author_id': 1,
788803
'createdAt': '2021-08-03 11:26:15.307+00'
804+
},
805+
{
806+
'title': 'Fourth post',
807+
'author_id': 2,
808+
'createdAt': '2021-08-04 11:26:15.307+00'
789809
}
790810
]);
791811

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

797-
expect(count, 2);
817+
expect(count, 3);
798818
});
799819

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

0 commit comments

Comments
 (0)