-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_supabase_http_client_test.dart
1157 lines (1043 loc) · 38.1 KB
/
mock_supabase_http_client_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import 'package:mock_supabase_http_client/mock_supabase_http_client.dart';
import 'package:supabase/supabase.dart';
import 'package:test/test.dart';
void main() {
late final SupabaseClient mockSupabase;
late final MockSupabaseHttpClient mockHttpClient;
setUpAll(() {
// Initialize the mock HTTP client and Supabase client
mockHttpClient = MockSupabaseHttpClient();
mockSupabase = SupabaseClient(
'https://mock.supabase.co',
'supabaseKey',
httpClient: mockHttpClient,
);
});
tearDown(() async {
// Reset the mock client after each test
mockHttpClient.reset();
});
tearDownAll(() {
// Close the mock client after all tests
mockHttpClient.close();
});
group('basic CRUD tests', () {
test('Insert', () async {
// Test inserting a record
await mockSupabase.from('posts').insert({'title': 'Hello, world!'});
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'title': 'Hello, world!'});
});
test('Upsert', () async {
// Test upserting a record
await mockSupabase
.from('posts')
.upsert({'id': 1, 'title': 'Initial post'});
await mockSupabase
.from('posts')
.upsert({'id': 1, 'title': 'Updated post'});
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'id': 1, 'title': 'Updated post'});
});
test('Update', () async {
// Test updating a record
await mockSupabase
.from('posts')
.insert({'id': 1, 'title': 'Original title'});
await mockSupabase
.from('posts')
.update({'title': 'Updated title'}).eq('id', 1);
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'id': 1, 'title': 'Updated title'});
});
test('Delete', () async {
// Test deleting a record
await mockSupabase
.from('posts')
.insert({'id': 1, 'title': 'To be deleted'});
await mockSupabase.from('posts').delete().eq('id', 1);
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 0);
});
test('Select all columns', () async {
// Test selecting all records
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post', 'content': 'Content of first post'},
{'id': 2, 'title': 'Second post', 'content': 'Content of second post'}
]);
final posts = await mockSupabase.from('posts').select().order('id');
expect(posts.length, 2);
// Order by id descending by default
expect(posts[0], {
'id': 2,
'title': 'Second post',
'content': 'Content of second post'
});
expect(posts[1],
{'id': 1, 'title': 'First post', 'content': 'Content of first post'});
});
test('Select specific columns', () async {
// Test selecting specific columns
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post', 'content': 'Content of first post'},
{'id': 2, 'title': 'Second post', 'content': 'Content of second post'}
]);
final titlesOnly =
await mockSupabase.from('posts').select('id, title').order('id');
expect(titlesOnly.length, 2);
expect(titlesOnly[0], {'id': 2, 'title': 'Second post'});
expect(titlesOnly[1], {'id': 1, 'title': 'First post'});
});
});
group('custom schema basic CRUD tests', () {
test('Insert', () async {
// Test inserting a record
await mockSupabase
.schema('custom_schema')
.from('posts')
.insert({'title': 'Hello, world!'});
final posts =
await mockSupabase.schema('custom_schema').from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'title': 'Hello, world!'});
});
test('Upsert', () async {
// Test upserting a record
await mockSupabase
.schema('custom_schema')
.from('posts')
.upsert({'id': 1, 'title': 'Initial post'});
await mockSupabase
.schema('custom_schema')
.from('posts')
.upsert({'id': 1, 'title': 'Updated post'});
final posts =
await mockSupabase.schema('custom_schema').from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'id': 1, 'title': 'Updated post'});
});
test('Update', () async {
// Test updating a record
await mockSupabase
.schema('custom_schema')
.from('posts')
.insert({'id': 1, 'title': 'Original title'});
await mockSupabase
.schema('custom_schema')
.from('posts')
.update({'title': 'Updated title'}).eq('id', 1);
final posts =
await mockSupabase.schema('custom_schema').from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'id': 1, 'title': 'Updated title'});
});
test('Delete', () async {
// Test deleting a record
await mockSupabase
.schema('custom_schema')
.from('posts')
.insert({'id': 1, 'title': 'To be deleted'});
await mockSupabase
.schema('custom_schema')
.from('posts')
.delete()
.eq('id', 1);
final posts =
await mockSupabase.schema('custom_schema').from('posts').select();
expect(posts.length, 0);
});
test('Select all columns', () async {
// Test selecting all records
await mockSupabase.schema('custom_schema').from('posts').insert([
{'id': 1, 'title': 'First post', 'content': 'Content of first post'},
{'id': 2, 'title': 'Second post', 'content': 'Content of second post'}
]);
final posts = await mockSupabase
.schema('custom_schema')
.from('posts')
.select()
.order('id');
expect(posts.length, 2);
// Order by id descending by default
expect(posts[0], {
'id': 2,
'title': 'Second post',
'content': 'Content of second post'
});
expect(posts[1],
{'id': 1, 'title': 'First post', 'content': 'Content of first post'});
});
test('Select specific columns', () async {
// Test selecting specific columns
await mockSupabase.schema('custom_schema').from('posts').insert([
{'id': 1, 'title': 'First post', 'content': 'Content of first post'},
{'id': 2, 'title': 'Second post', 'content': 'Content of second post'}
]);
final titlesOnly = await mockSupabase
.schema('custom_schema')
.from('posts')
.select('id, title')
.order('id');
expect(titlesOnly.length, 2);
expect(titlesOnly[0], {'id': 2, 'title': 'Second post'});
expect(titlesOnly[1], {'id': 1, 'title': 'First post'});
});
test('No mixing with default schema', () async {
// Insert into custom schema
await mockSupabase
.schema('custom_schema')
.from('posts')
.insert({'id': 1, 'title': 'Custom schema post'});
// Insert into default schema
await mockSupabase
.from('posts')
.insert({'id': 2, 'title': 'Default schema post'});
// Check custom schema
final customPosts =
await mockSupabase.schema('custom_schema').from('posts').select();
expect(customPosts.length, 1);
expect(customPosts.first, {'id': 1, 'title': 'Custom schema post'});
// Check default schema
final defaultPosts = await mockSupabase.from('posts').select();
expect(defaultPosts.length, 1);
expect(defaultPosts.first, {'id': 2, 'title': 'Default schema post'});
});
});
group('Filter tests', () {
test('Filter by equality', () async {
// Test filtering by equality
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().eq('id', 2);
expect(posts.length, 1);
expect(posts.first, {'id': 2, 'title': 'Second post'});
});
test('Filter by inequality', () async {
// Test filtering by inequality
await mockSupabase.from('posts').insert({'title': 'Hello, world!'});
final posts = await mockSupabase
.from('posts')
.select()
.neq('title', 'Goodbye, world!');
expect(posts.length, 1);
expect(posts.first, {'title': 'Hello, world!'});
});
test('Filter by range', () async {
// Test filtering by range
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts =
await mockSupabase.from('posts').select().gte('id', 1).lte('id', 2);
expect(posts.length, 2);
});
test('Filter by like', () async {
// Test filtering by like
await mockSupabase.from('posts').insert({'title': 'Hello, world!'});
final posts =
await mockSupabase.from('posts').select().like('title', '%world%');
expect(posts.length, 1);
expect(posts.first, {'title': 'Hello, world!'});
});
test('Filter by is null', () async {
// Test filtering by is null
await mockSupabase.from('posts').insert({'title': null});
final posts =
await mockSupabase.from('posts').select().isFilter('title', null);
expect(posts.length, 1);
expect(posts.first, {'title': null});
});
test('Filter by greater than', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().gt('id', 1);
expect(posts.length, 1);
expect(posts.first, {'id': 2, 'title': 'Second post'});
});
test('Filter by less than', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().lt('id', 2);
expect(posts.length, 1);
expect(posts.first, {'id': 1, 'title': 'First post'});
});
test('Filter by greater than or equal', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().gte('id', 1);
expect(posts.length, 2);
});
test('Filter by less than or equal', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().lte('id', 2);
expect(posts.length, 2);
});
test('Filter by in', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts =
await mockSupabase.from('posts').select().inFilter('id', [1, 2]);
expect(posts.length, 2);
});
group('Not filters', () {
setUp(() async {
await mockSupabase.from('posts').insert([
{
'id': 1,
'title': 'First post',
'views': 100,
'tags': ['tag1', 'tag2']
},
{
'id': 2,
'title': 'Second post',
'views': 200,
'tags': ['tag2', 'tag3']
},
{
'id': 3,
'title': 'Third post',
'views': 300,
'tags': ['tag3', 'tag4']
}
]);
});
test('Filter by not equal', () async {
final posts =
await mockSupabase.from('posts').select().not('id', 'eq', 1);
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([2, 3]));
});
test('Filter by not greater than', () async {
final posts =
await mockSupabase.from('posts').select().not('views', 'gt', 200);
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([1, 2]));
});
test('Filter by not less than', () async {
final posts =
await mockSupabase.from('posts').select().not('views', 'lt', 200);
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([2, 3]));
});
test('Filter by not like', () async {
final posts = await mockSupabase
.from('posts')
.select()
.not('title', 'like', '%Second%');
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([1, 3]));
});
test('Filter by not in', () async {
final posts =
await mockSupabase.from('posts').select().not('id', 'in', '(1,2)');
expect(posts.length, 1);
expect(posts.first['id'], 3);
});
test('Filter by not contains', () async {
final posts = await mockSupabase
.from('posts')
.select()
.not('tags', 'cs', '{"tag1"}');
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([2, 3]));
});
test('Combine not with and', () async {
final posts = await mockSupabase
.from('posts')
.select()
.not('id', 'eq', 1)
.not('views', 'gt', 250);
expect(posts.length, 1);
expect(posts.first['id'], 2);
});
test('Combine not with or', () async {
final posts = await mockSupabase
.from('posts')
.select()
.or('id.not.eq.1,views.not.lte.200');
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([2, 3]));
});
});
test('Filter by contains', () async {
await mockSupabase.from('posts').insert([
{
'id': 1,
'tags': ['tag1', 'tag2']
},
{
'id': 2,
'tags': ['tag1', 'tag3']
},
{
'id': 3,
'tags': ['tag2', 'tag3']
},
]);
final posts =
await mockSupabase.from('posts').select().contains('tags', ['tag1']);
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([1, 2]));
});
test('Filter by contained by', () async {
await mockSupabase.from('posts').insert({
'tags': ['tag1']
});
final posts = await mockSupabase
.from('posts')
.select()
.containedBy('tags', ['tag1', 'tag2']);
expect(posts.length, 1);
expect(posts.first, {
'tags': ['tag1']
});
});
test('Filter by overlap', () async {
await mockSupabase.from('posts').insert({
'tags': ['tag1', 'tag2']
});
final posts =
await mockSupabase.from('posts').select().overlaps('tags', ['tag2']);
expect(posts.length, 1);
expect(posts.first, {
'tags': ['tag1', 'tag2']
});
});
test('Filter by full text search', () async {
await mockSupabase.from('posts').insert({'content': 'Hello world'});
final posts = await mockSupabase
.from('posts')
.select()
.textSearch('content', 'Hello');
expect(posts.length, 1);
expect(posts.first, {'content': 'Hello world'});
});
test('Filter by match', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post', 'content': 'Hello world'},
{'id': 2, 'title': 'More Posts', 'content': 'Hello world'},
{'id': 3, 'title': 'More Posts', 'content': 'Hello world'}
]);
final posts = await mockSupabase
.from('posts')
.select()
.match({'title': 'More Posts', 'content': 'Hello world'});
expect(posts.length, 2);
expect(posts.map((post) => post['id']), containsAll([2, 3]));
});
});
group('Modifier tests', () {
test('Limit', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase.from('posts').select().limit(1);
expect(posts.length, 1);
});
test('limit with a filter', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post', 'author_id': 1},
{'id': 2, 'title': 'Second post', 'author_id': 2},
{'id': 3, 'title': 'Third post', 'author_id': 1}
]);
final posts =
await mockSupabase.from('posts').select().eq('author_id', 1).limit(1);
expect(posts.length, 1);
});
test('Order', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
final posts = await mockSupabase
.from('posts')
.select()
.order('id', ascending: false);
expect(posts.length, 2);
expect(posts.first, {'id': 2, 'title': 'Second post'});
});
test('Range', () async {
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'},
{'id': 3, 'title': 'Third post'},
{'id': 4, 'title': 'Fourth post'},
]);
final posts = await mockSupabase.from('posts').select().range(1, 2);
expect(posts.length, 2);
expect(posts.first, {'id': 2, 'title': 'Second post'});
expect(posts.last, {'id': 3, 'title': 'Third post'});
});
test('Single', () async {
await mockSupabase.from('posts').insert({'id': 1, 'title': 'First post'});
final post = await mockSupabase.from('posts').select().single();
expect(post, {'id': 1, 'title': 'First post'});
});
test('maybeSingle', () async {
// Test with one record
await mockSupabase.from('posts').insert({'id': 1, 'title': 'First post'});
var post = await mockSupabase.from('posts').select().maybeSingle();
expect(post, {'id': 1, 'title': 'First post'});
// Test with no records
await mockSupabase.from('posts').delete().eq('id', 1);
post = await mockSupabase.from('posts').select().maybeSingle();
expect(post, null);
// Test with multiple records
await mockSupabase.from('posts').insert([
{'id': 1, 'title': 'First post'},
{'id': 2, 'title': 'Second post'}
]);
expect(() => mockSupabase.from('posts').select().maybeSingle(),
throwsException);
});
});
group('Referenced table queries', () {
setUp(() async {
// posts table has a many-to-one relationship with authors table
// posts table has a one-to-many relationship with comments table
await mockSupabase.from('posts').insert([
{
'id': 1,
'title': 'First post',
'authors': {'id': 1, 'name': 'Author One'},
'comments': [
{'id': 1, 'content': 'First comment'},
{'id': 2, 'content': 'Second comment'}
]
},
{
'id': 2,
'title': 'Second post',
'authors': {'id': 2, 'name': 'Author Two'},
'comments': [
{'id': 3, 'content': 'Third comment'},
{'id': 4, 'content': 'Fourth comment'},
{'id': 5, 'content': 'Fifth comment'}
]
}
]);
});
test('Join with referenced table', () async {
// Test joining with the referenced table
final posts = await mockSupabase
.from('posts')
.select('*, authors(*)')
.order('id', ascending: true);
expect(posts.length, 2);
expect(posts[0]['authors']['name'], 'Author One');
expect(posts[1]['authors']['name'], 'Author Two');
});
test('Filter by referenced table column', () async {
// Test filtering by a column in the referenced table
final posts = await mockSupabase
.from('posts')
.select('*, authors(*)')
.eq('authors.name', 'Author One')
.order('id', ascending: true);
print(posts);
expect(posts.length, 2);
expect(posts.first['title'], 'First post');
expect(posts.first['authors'], {'id': 1, 'name': 'Author One'});
expect(posts.last['authors'], null);
});
test('Order by referenced table column', () async {
// Test ordering by a column in the referenced table
final posts = await mockSupabase
.from('posts')
.select('*, comments(*)')
.order('id', ascending: false, referencedTable: 'comments');
expect(posts.length, 2);
expect(posts.first['comments'].first['id'], 2);
});
test('Limit with referenced table', () async {
// Test limiting results with referenced table
final posts =
await mockSupabase.from('posts').select('*, authors(*)').limit(1);
expect(posts.length, 1);
});
test('Limit on the referenced table', () async {
// Test limiting results with referenced table
final posts = await mockSupabase
.from('posts')
.select('*, comments(*)')
.limit(1, referencedTable: 'comments');
expect(posts.first['comments'].length, 1);
expect(posts[1]['comments'].length, 1);
});
test('Range with referenced table', () async {
// Test range with referenced table
final posts = await mockSupabase
.from('posts')
.select('*, authors(*)')
.order('id', ascending: true, referencedTable: 'comments')
.range(1, 2, referencedTable: 'comments');
expect(posts.length, 2);
expect(posts[0]['comments'].length, 1);
expect(posts[1]['comments'].length, 2);
expect(posts[1]['comments'].first['content'], 'Fourth comment');
});
});
group('count', () {
test('count', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post'},
{'title': 'Second post'}
]);
final count = await mockSupabase.from('posts').count();
expect(count, 2);
});
test('count with data', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post'},
{'title': 'Second post'}
]);
final response = await mockSupabase.from('posts').select().count();
expect(response.data.length, 2);
expect(response.data.first['title'], 'First post');
expect(response.count, 2);
});
test('count with filter', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post', 'author_id': 1},
{'title': 'Second post', 'author_id': 2},
{'title': 'Third post', 'author_id': 1}
]);
final count = await mockSupabase.from('posts').count().eq('author_id', 1);
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'
},
{
'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 11: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'
},
{
'title': 'Fourth post',
'author_id': 2,
'createdAt': '2021-08-04 11:26:15.307+00'
}
]);
final count = await mockSupabase
.from('data')
.count()
.gte('createdAt', '2021-08-02 11:26:15.307+00');
expect(count, 3);
});
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'
},
{
'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-03 11: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'
},
{
'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-03 11:26:15.307+00');
expect(count, 3);
});
test('count with data and filter', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post', 'author_id': 1},
{'title': 'Second post', 'author_id': 2},
{'title': 'Third post', 'author_id': 1}
]);
final response =
await mockSupabase.from('posts').select().eq('author_id', 1).count();
expect(response.data.length, 2);
expect(response.data.first['title'], 'First post');
expect(response.count, 2);
});
test('count with filter and modifier', () async {
await mockSupabase.from('posts').insert([
{'title': 'First post', 'author_id': 1},
{'title': 'Second post', 'author_id': 2},
{'title': 'Third post', 'author_id': 1}
]);
final response = await mockSupabase
.from('posts')
.select()
.eq('author_id', 1)
.limit(1)
.count();
expect(response.data.length, 1);
expect(response.data.first['title'], 'First post');
expect(response.count, 2);
});
});
group('non-ASCII characters tests', () {
test('Insert Japanese text', () async {
await mockSupabase.from('posts').insert({'title': 'こんにちは'});
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'title': 'こんにちは'});
});
test('Insert emoji', () async {
await mockSupabase.from('posts').insert({'title': '😀'});
final posts = await mockSupabase.from('posts').select();
expect(posts.length, 1);
expect(posts.first, {'title': '😀'});
});
});
group('RPC function tests', () {
setUp(() {
// Register RPC functions
mockHttpClient.registerRpcFunction(
'get_server_time',
(params, tables) => {'current_time': DateTime.now().toIso8601String()},
);
mockHttpClient.registerRpcFunction(
'calculate_sum',
(params, tables) =>
{'sum': (params!['a'] as int) + (params['b'] as int)},
);
mockHttpClient.registerRpcFunction(
'get_recent_posts',
(params, tables) => [
{'id': 1, 'title': 'Recent Post 1'},
{'id': 2, 'title': 'Recent Post 2'},
],
);
mockHttpClient.registerRpcFunction(
'process_order',
(params, tables) {
final order = params!['order'] as Map<String, dynamic>;
final items = List<Map<String, dynamic>>.from(order['items']);
final total = items.fold<int>(
0,
(sum, item) =>
sum + (item['price'] as int) * (item['quantity'] as int),
);
return {
'processed_order': {
'status': 'processed',
'total': total,
}
};
},
);
mockHttpClient.registerRpcFunction(
'update_user_points',
(params, tables) {
final userId = params!['user_id'] as int;
final pointsToAdd = params['points_to_add'] as int;
final users = tables['public.users']!;
final userIndex = users.indexWhere((user) => user['id'] == userId);
if (userIndex != -1) {
users[userIndex]['points'] =
(users[userIndex]['points'] as int) + pointsToAdd;
return {'success': true, 'new_points': users[userIndex]['points']};
}
return {'success': false};
},
);
// Add new RPC function for custom schema
mockHttpClient.registerRpcFunction(
'update_custom_schema_user_points',
(params, tables) {
final userId = params!['user_id'] as int;
final pointsToAdd = params['points_to_add'] as int;
final users = tables['custom_schema.users']!;
final userIndex = users.indexWhere((user) => user['id'] == userId);
if (userIndex != -1) {
users[userIndex]['points'] =
(users[userIndex]['points'] as int) + pointsToAdd;
return {'success': true, 'new_points': users[userIndex]['points']};
}
return {'success': false};
},
);
mockHttpClient.registerRpcFunction(
'transfer_points_between_schemas',
(params, tables) {
final fromUserId = params!['from_user_id'] as int;
final toUserId = params['to_user_id'] as int;
final points = params['points'] as int;
// Get users from both schemas
final publicUsers = tables['public.users']!;
final customUsers = tables['custom_schema.users']!;
final fromUserIndex =
publicUsers.indexWhere((user) => user['id'] == fromUserId);
final toUserIndex =
customUsers.indexWhere((user) => user['id'] == toUserId);
if (fromUserIndex != -1 && toUserIndex != -1) {
// Check if source user has enough points
if (publicUsers[fromUserIndex]['points'] as int >= points) {
// Deduct points from source user
publicUsers[fromUserIndex]['points'] =
(publicUsers[fromUserIndex]['points'] as int) - points;
// Add points to destination user
customUsers[toUserIndex]['points'] =
(customUsers[toUserIndex]['points'] as int) + points;
return {
'success': true,
'from_user_points': publicUsers[fromUserIndex]['points'],
'to_user_points': customUsers[toUserIndex]['points']
};
}
}
return {'success': false};
},
);
});
test('Call RPC function without parameters', () async {
final result = await mockSupabase.rpc('get_server_time');
expect(result, isA<Map<String, dynamic>>());
expect(result, containsPair('current_time', isA<String>()));
});
test('Call RPC function with parameters', () async {
final result =
await mockSupabase.rpc('calculate_sum', params: {'a': 5, 'b': 3});
expect(result, isA<Map<String, dynamic>>());
expect(result, containsPair('sum', 8));
});
test('Call RPC function that returns a list', () async {
final result = await mockSupabase.rpc('get_recent_posts');
expect(result, isA<List>());
expect(result, hasLength(greaterThan(0)));
expect(result.first, isA<Map<String, dynamic>>());
expect(result.first, containsPair('id', isA<int>()));
expect(result.first, containsPair('title', isA<String>()));
});
test('Call RPC function with complex parameters and return value',
() async {
final result = await mockSupabase.rpc('process_order', params: {
'order': {