This repository was archived by the owner on May 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbasic_test.dart
173 lines (146 loc) · 5.1 KB
/
basic_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
import 'package:postgrest/src/count_option.dart';
import 'package:test/test.dart';
import 'package:postgrest/postgrest.dart';
void main() {
const rootUrl = 'http://localhost:3000';
PostgrestClient postgrest;
setUp(() {
postgrest = PostgrestClient(rootUrl);
});
test('basic select table', () async {
final res = await postgrest.from('users').select().execute();
expect(res.data.length, 4);
});
test('stored procedure', () async {
final res =
await postgrest.rpc('get_status', {'name_param': 'supabot'}).execute();
expect(res.data, 'ONLINE');
});
test('custom headers', () async {
final postgrest = PostgrestClient(rootUrl, headers: {'apikey': 'foo'});
expect(postgrest.from('users').select().headers['apikey'], 'foo');
});
test('auth', () async {
postgrest = PostgrestClient(rootUrl).auth('foo');
expect(postgrest.from('users').select().headers['Authorization'],
'Bearer foo');
});
test('switch schema', () async {
final postgrest = PostgrestClient(rootUrl, schema: 'personal');
final res = await postgrest.from('users').select().execute();
expect(res.data.length, 5);
});
test('on_conflict insert', () async {
final res = await postgrest.from('users').insert(
{'username': 'dragarcia', 'status': 'OFFLINE'},
upsert: true, onConflict: 'username').execute();
expect(res.data[0]['status'], 'OFFLINE');
});
test('upsert', () async {
final res = await postgrest.from('messages').insert(
{'id': 3, 'message': 'foo', 'username': 'supabot', 'channel_id': 2},
upsert: true).execute();
//{id: 3, message: foo, username: supabot, channel_id: 2}
expect(res.data[0]['id'], 3);
final resMsg = await postgrest.from('messages').select().execute();
expect(resMsg.data.length, 3);
});
test('bulk insert', () async {
final res = await postgrest.from('messages').insert([
{'id': 4, 'message': 'foo', 'username': 'supabot', 'channel_id': 2},
{'id': 5, 'message': 'foo', 'username': 'supabot', 'channel_id': 1}
]).execute();
expect(res.data.length, 2);
});
test('basic update', () async {
await postgrest
.from('messages')
.update({'channel_id': 2})
.eq('message', 'foo')
.execute();
final resMsg = await postgrest
.from('messages')
.select()
.filter('message', 'eq', 'foo')
.execute();
resMsg.data.forEach((rec) => expect(rec['channel_id'], 2));
});
test('basic delete', () async {
await postgrest.from('messages').delete().eq('message', 'foo').execute();
final resMsg = await postgrest
.from('messages')
.select()
.filter('message', 'eq', 'foo')
.execute();
expect(resMsg.data.length, 0);
});
test('missing table', () async {
final res = await postgrest.from('missing_table').select().execute();
expect(res.error.code, '404');
});
test('connection error', () async {
final postgrest = PostgrestClient('http://this.url.does.not.exist');
final res = await postgrest.from('user').select().execute();
expect(res.error.code, 'SocketException');
});
test('select with head:true', () async {
final res = await postgrest.from('users').select().execute(head: true);
expect(res.data, null);
});
test('select with head:true, count: exact', () async {
final res = await postgrest
.from('users')
.select()
.execute(head: true, count: CountOption.exact);
expect(res.data, null);
expect(res.count, 4);
});
test('select with count: planned', () async {
final res = await postgrest
.from('users')
.select()
.execute(count: CountOption.exact);
expect(res.count, const TypeMatcher<int>());
});
test('select with head:true, count: estimated', () async {
final res = await postgrest
.from('users')
.select()
.execute(count: CountOption.exact);
expect(res.count, const TypeMatcher<int>());
});
test('stored procedure with head: true', () async {
final res =
await postgrest.from('users').rpc('get_status').execute(head: true);
expect(res.data, null);
});
test('stored procedure with count: exact', () async {
final res = await postgrest
.from('users')
.rpc('get_status')
.execute(count: CountOption.exact);
expect(res.count, const TypeMatcher<int>());
});
test('insert with count: exact', () async {
final res = await postgrest.from('users').insert(
{'username': 'countexact', 'status': 'OFFLINE'},
upsert: true, onConflict: 'username').execute(count: CountOption.exact);
expect(res.count, 1);
});
test('update with count: exact', () async {
final res = await postgrest
.from('users')
.update({'status': 'ONLINE'})
.eq('username', 'countexact')
.execute(count: CountOption.exact);
expect(res.count, 1);
});
test('delete with count: exact', () async {
final res = await postgrest
.from('users')
.delete()
.eq('username', 'countexact')
.execute(count: CountOption.exact);
expect(res.count, 1);
});
}