-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrud_test.dart
275 lines (232 loc) · 8.98 KB
/
crud_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
import 'dart:convert';
import 'package:sqlite3/common.dart';
import 'package:test/test.dart';
import 'utils/native_test_utils.dart';
void main() {
group('crud tests', () {
late CommonDatabase db;
setUp(() async {
db = openTestDatabase();
});
tearDown(() {
db.dispose();
});
test('powersync_diff - single value', () {
var r1 =
db.select('select powersync_diff(?, ?) as diff', ['{}', '{}']).first;
expect(r1['diff'], equals('{}'));
var r2 = db.select(
'select powersync_diff(?, ?) as diff', ['{}', '{"test":1}']).first;
expect(r2['diff'], equals('{"test":1}'));
var r3 = db.select('select powersync_diff(?, ?) as diff',
['{"test":1}', '{"test":1}']).first;
expect(r3['diff'], equals('{}'));
var r4 = db.select(
'select powersync_diff(?, ?) as diff', ['{"test":1}', '{}']).first;
expect(r4['diff'], equals('{"test":null}'));
var r5 = db.select('select powersync_diff(?, ?) as diff',
['{"test":1}', '{"test":null}']).first;
expect(r5['diff'], equals('{"test":null}'));
var r6 = db.select('select powersync_diff(?, ?) as diff',
['{"test":1}', '{"test":2}']).first;
expect(r6['diff'], equals('{"test":2}'));
});
test('powersync_diff - multiple values', () {
var r1 = db.select('select powersync_diff(?, ?) as diff',
['{"a":1,"b":"test"}', '{}']).first;
expect(r1['diff'], equals('{"a":null,"b":null}'));
var r2 = db.select('select powersync_diff(?, ?) as diff',
['{}', '{"a":1,"b":"test"}']).first;
expect(r2['diff'], equals('{"a":1,"b":"test"}'));
var r3 = db.select('select powersync_diff(?, ?) as diff',
['{"a":1,"b":"test"}', '{"a":1,"b":"test"}']).first;
expect(r3['diff'], equals('{}'));
var r4 = db.select('select powersync_diff(?, ?) as diff',
['{"a":1}', '{"b":"test"}']).first;
expect(r4['diff'], equals('{"a":null,"b":"test"}'));
var r5 = db.select('select powersync_diff(?, ?) as diff',
['{"a":1}', '{"a":1,"b":"test"}']).first;
expect(r5['diff'], equals('{"b":"test"}'));
});
test('includes empty updates by default', () {
db
..execute('select powersync_replace_schema(?)', [
json.encode({
'tables': [
{
'name': 'items',
'columns': [
{'name': 'col', 'type': 'text'}
],
}
]
})
])
..execute(
'INSERT INTO items (id, col) VALUES (uuid(), ?)', ['new item'])
..execute('UPDATE items SET col = LOWER(col)');
// Should record insert and update operation.
expect(db.select('SELECT * FROM ps_crud'), hasLength(2));
});
test('can ignore empty updates', () {
db
..execute('select powersync_replace_schema(?)', [
json.encode({
'tables': [
{
'name': 'items',
'columns': [
{'name': 'col', 'type': 'text'}
],
'ignore_empty_update': true,
}
]
})
])
..execute(
'INSERT INTO items (id, col) VALUES (uuid(), ?)', ['new item'])
..execute('UPDATE items SET col = LOWER(col)');
// The update which didn't change any rows should not be recorded.
expect(db.select('SELECT * FROM ps_crud'), hasLength(1));
});
var runCrudTest = (int numberOfColumns) {
var columns = [];
for (var i = 0; i < numberOfColumns; i++) {
columns.add({'name': 'column$i', 'type': 'TEXT'});
}
var tableSchema = {
'tables': [
{'name': 'items', 'columns': columns}
]
};
db.select('select powersync_init()');
// 1. Test schema initialization
db.select(
'select powersync_replace_schema(?)', [jsonEncode(tableSchema)]);
var columnNames = columns.map((c) => c['name']).join(', ');
var columnValues = columns.map((c) => "'${c['name']}'").join(', ');
// 2. Test insert
db.select(
"insert into items(id, ${columnNames}) values('test_id', ${columnValues})");
var item = db.select('select * from items').first;
var expectedData =
Map.fromEntries(columns.map((c) => MapEntry(c['name'], c['name'])));
expect(item, equals({'id': 'test_id', ...expectedData}));
var crud = db.select('select * from ps_crud').first;
var crudData = jsonDecode(crud['data']);
expect(crud['tx_id'], equals(1));
expect(
crudData,
equals({
'op': 'PUT',
'type': 'items',
'id': 'test_id',
'data': expectedData
}));
// 3. Test update
db.select('update items set column0 = ?', ['new_value']);
var itemUpdated = db.select('select * from items').first;
expect(itemUpdated,
equals({'id': 'test_id', ...expectedData, 'column0': 'new_value'}));
var crudUpdated = db.select('select * from ps_crud where id = 2').first;
var crudDataUpdated = jsonDecode(crudUpdated['data']);
expect(crudUpdated['tx_id'], equals(2));
expect(
crudDataUpdated,
equals({
'op': 'PATCH',
'type': 'items',
'id': 'test_id',
'data': {'column0': 'new_value'}
}));
// 4. Test delete
db.select('delete from items');
var itemDeleted = db.select('select * from items').firstOrNull;
expect(itemDeleted, equals(null));
var crudDeleted = db.select('select * from ps_crud where id = 3').first;
var crudDataDeleted = jsonDecode(crudDeleted['data']);
expect(crudDeleted['tx_id'], equals(3));
expect(crudDataDeleted,
equals({'op': 'DELETE', 'type': 'items', 'id': 'test_id'}));
};
var runCrudTestLocalOnly = (int numberOfColumns) {
var columns = [];
for (var i = 0; i < numberOfColumns; i++) {
columns.add({'name': 'column$i', 'type': 'TEXT'});
}
var tableSchema = {
'tables': [
{'name': 'items', 'columns': columns, 'local_only': true}
]
};
db.select('select powersync_init()');
// 1. Test schema initialization
db.select(
'select powersync_replace_schema(?)', [jsonEncode(tableSchema)]);
var columnNames = columns.map((c) => c['name']).join(', ');
var columnValues = columns.map((c) => "'${c['name']}'").join(', ');
// 2. Test insert
db.select(
"insert into items(id, ${columnNames}) values('test_id', ${columnValues})");
var item = db.select('select * from items').first;
var expectedData =
Map.fromEntries(columns.map((c) => MapEntry(c['name'], c['name'])));
expect(item, equals({'id': 'test_id', ...expectedData}));
// 3. Test update
db.select('update items set column0 = ?', ['new_value']);
var itemUpdated = db.select('select * from items').first;
expect(itemUpdated,
equals({'id': 'test_id', ...expectedData, 'column0': 'new_value'}));
// 4. Test delete
db.select('delete from items');
var itemDeleted = db.select('select * from items').firstOrNull;
expect(itemDeleted, equals(null));
};
var runCrudTestInsertOnly = (int numberOfColumns) {
var columns = [];
for (var i = 0; i < numberOfColumns; i++) {
columns.add({'name': 'column$i', 'type': 'TEXT'});
}
var tableSchema = {
'tables': [
{'name': 'items', 'columns': columns, 'insert_only': true}
]
};
db.select('select powersync_init()');
// 1. Test schema initialization
db.select(
'select powersync_replace_schema(?)', [jsonEncode(tableSchema)]);
var columnNames = columns.map((c) => c['name']).join(', ');
var columnValues = columns.map((c) => "'${c['name']}'").join(', ');
// 2. Test insert
db.select(
"insert into items(id, ${columnNames}) values('test_id', ${columnValues})");
var item = db.select('select * from items').firstOrNull;
expect(item, equals(null));
var expectedData =
Map.fromEntries(columns.map((c) => MapEntry(c['name'], c['name'])));
var crud = db.select('select * from ps_crud').first;
var crudData = jsonDecode(crud['data']);
expect(crud['tx_id'], equals(1));
expect(
crudData,
equals({
'op': 'PUT',
'type': 'items',
'id': 'test_id',
'data': expectedData
}));
};
for (var numberOfColumns in [1, 49, 50, 51, 63, 64, 100, 1999]) {
test('crud test with $numberOfColumns columns', () async {
runCrudTest(numberOfColumns);
});
test('crud test with $numberOfColumns columns - local_only', () async {
runCrudTestLocalOnly(numberOfColumns);
});
test('crud test with $numberOfColumns columns - insert_only', () async {
runCrudTestInsertOnly(numberOfColumns);
});
}
});
}