More tests for moor_ffi

This commit is contained in:
Simon Binder 2020-02-19 14:17:23 +01:00
parent b266e5e53f
commit aaa0967b6f
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 84 additions and 20 deletions

View File

@ -11,6 +11,16 @@ void main() {
expect(() => db.execute('SELECT 1'), throwsA(anything)); expect(() => db.execute('SELECT 1'), throwsA(anything));
}); });
test('getUpdatedRows', () {
final db = Database.memory();
db
..execute('CREATE TABLE foo (bar INT);')
..execute('INSERT INTO foo VALUES (3);');
expect(db.getUpdatedRows(), 1);
});
test('closing multiple times works', () { test('closing multiple times works', () {
final db = Database.memory(); final db = Database.memory();
db.execute('SELECT 1'); db.execute('SELECT 1');
@ -18,4 +28,29 @@ void main() {
db.close(); db.close();
db.close(); // shouldn't throw or crash db.close(); // shouldn't throw or crash
}); });
test('throws exception on an invalid statement', () {
final db = Database.memory();
db.execute('CREATE TABLE foo (bar INTEGER CHECK (bar > 10));');
expect(
() => db.execute('INSERT INTO foo VALUES (3);'),
throwsA(const TypeMatcher<SqliteException>().having(
(e) => e.message, 'message', contains('CHECK constraint failed'))),
);
db.close();
});
test('throws when preparing an invalid statement', () {
final db = Database.memory();
expect(
() => db.prepare('INSERT INTO foo VALUES (3);'),
throwsA(const TypeMatcher<SqliteException>()
.having((e) => e.message, 'message', contains('no such table'))),
);
db.close();
});
} }

View File

@ -43,35 +43,46 @@ void main() {
expect(stmt.select, throwsA(anything)); expect(stmt.select, throwsA(anything));
}); });
test('can bind empty blob in prepared statements', () { Uint8List _insertBlob(Uint8List value) {
final opened = Database.memory();
opened.execute('CREATE TABLE tbl (x BLOB NOT NULL);');
final insert = opened.prepare('INSERT INTO tbl VALUES (?)');
insert.execute([Uint8List(0)]);
insert.close();
final select = opened.prepare('SELECT * FROM tbl');
final result = select.select().single;
expect(result['x'], <int>[]);
opened.close();
});
test('can bind null blob in prepared statements', () {
final opened = Database.memory(); final opened = Database.memory();
opened.execute('CREATE TABLE tbl (x BLOB);'); opened.execute('CREATE TABLE tbl (x BLOB);');
final insert = opened.prepare('INSERT INTO tbl VALUES (?)'); final insert = opened.prepare('INSERT INTO tbl VALUES (?)');
insert.execute([null]); insert.execute([value]);
insert.close(); insert.close();
final select = opened.prepare('SELECT * FROM tbl'); final select = opened.prepare('SELECT * FROM tbl');
final result = select.select().single; final result = select.select().single;
expect(result['x'], isNull);
opened.close(); opened.close();
return result['x'] as Uint8List;
}
test('can bind empty blob in prepared statements', () {
expect(_insertBlob(Uint8List(0)), isEmpty);
});
test('can bind null blob in prepared statements', () {
expect(_insertBlob(null), isNull);
});
test('can bind and read non-empty blob', () {
const bytes = [1, 2, 3];
expect(_insertBlob(Uint8List.fromList(bytes)), bytes);
});
test('throws when sql statement has an error', () {
final db = Database.memory();
db.execute('CREATE TABLE foo (id INTEGER CHECK (id > 10));');
final stmt = db.prepare('INSERT INTO foo VALUES (9)');
expect(
stmt.execute,
throwsA(const TypeMatcher<SqliteException>()
.having((e) => e.message, 'message', contains('foo'))),
);
db.close();
}); });
} }

View File

@ -84,4 +84,22 @@ void main() {
]); ]);
}); });
}); });
test('throws when using a long function name', () {
final db = Database.memory();
expect(
() => db.createFunction('foo' * 100, 10, nullptr), throwsArgumentError);
db.close();
});
test('throws when using an invalid argument count', () {
final db = Database.memory();
expect(() => db.createFunction('foo', -2, nullptr), throwsArgumentError);
expect(() => db.createFunction('foo', 128, nullptr), throwsArgumentError);
db.close();
});
} }