Integration test for RETURNING

This commit is contained in:
Simon Binder 2021-05-06 17:39:59 +02:00
parent bbef0d6048
commit 43fea9bc2d
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 44 additions and 1 deletions

View File

@ -4,4 +4,6 @@ targets:
moor_generator:
options:
generate_connect_constructor: true
override_hash_and_equals_in_result_sets: true
override_hash_and_equals_in_result_sets: true
sqlite:
version: "3.35"

View File

@ -87,6 +87,7 @@ class PreferenceConverter extends TypeConverter<Preferences, String> {
'userCount': 'SELECT COUNT(id) FROM users',
'settingsFor': 'SELECT preferences FROM users WHERE id = :user',
'usersById': 'SELECT * FROM users WHERE id IN ?',
'returning': 'INSERT INTO friendships VALUES (?, ?, ?) RETURNING *;',
},
)
class Database extends _$Database {

View File

@ -647,6 +647,19 @@ abstract class _$Database extends GeneratedDatabase {
readsFrom: {users}).map(users.mapFromRow);
}
Future<List<Friendship>> returning(int var1, int var2, bool var3) {
return customWriteReturning(
'INSERT INTO friendships VALUES (?, ?, ?) RETURNING *;',
variables: [
Variable<int>(var1),
Variable<int>(var2),
Variable<bool>(var3)
],
updates: {
friendships
}).then((rows) => rows.map(friendships.mapFromRow).toList());
}
@override
Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
@override

View File

@ -21,6 +21,18 @@ void crudTests(TestExecutor executor) {
await db.close();
});
test('supports RETURNING', () async {
final db = Database(executor.createConnection());
final result = await db.returning(1, 2, true);
expect(result,
[Friendship(firstUser: 1, secondUser: 2, reallyGoodFriends: true)]);
await db.close();
},
skip: executor.supportsReturning
? null
: 'Runner does not support RETURNING');
test('IN ? expressions can be expanded', () async {
// regression test for https://github.com/simolus3/moor/issues/156
final db = Database(executor.createConnection());

View File

@ -9,6 +9,8 @@ import 'migrations.dart';
abstract class TestExecutor {
DatabaseConnection createConnection();
bool get supportsReturning => false;
/// Delete the data that would be written by the executor.
Future deleteData();
}

View File

@ -1,6 +1,7 @@
import 'dart:io';
import 'package:moor/ffi.dart';
import 'package:sqlite3/sqlite3.dart';
import 'package:tests/tests.dart';
import 'package:path/path.dart' show join;
@ -9,6 +10,12 @@ class VmExecutor extends TestExecutor {
static String fileName = 'moor-vm-tests-${DateTime.now().toIso8601String()}';
final File file = File(join(Directory.systemTemp.path, fileName));
@override
bool get supportsReturning {
final version = sqlite3.version;
return version.versionNumber > 3035000;
}
@override
DatabaseConnection createConnection() {
return DatabaseConnection.fromExecutor(VmDatabase(file));

View File

@ -8,6 +8,9 @@ import 'package:moor/moor_web.dart';
class WebExecutor extends TestExecutor {
final String name = 'db';
@override
bool get supportsReturning => true;
@override
DatabaseConnection createConnection() {
return DatabaseConnection.fromExecutor(WebDatabase(name));
@ -21,6 +24,9 @@ class WebExecutor extends TestExecutor {
}
class WebExecutorIndexedDb extends TestExecutor {
@override
bool get supportsReturning => true;
@override
DatabaseConnection createConnection() {
return DatabaseConnection.fromExecutor(