Hi!
It would be useful to add the support to write unit/integration tests.
Maybe an in-memory database is a pretty good solution to achieve this.
Have you planned to add this feature?
Moor could definitely provide some apis to make testing easier. To support testing we'd have to use a version of sqlite that works on the Dart VM, so this is blocked on #76 for now.
You could make this work by changing your database constructor from
Database(): super(FlutterQueryExecutor(...));
to
Database(QueryExecutor e): super(e);
Then, add a dev_dependency on moor_ffi: ^0.0.1. In your app, you would construct your database with the FlutterQueryExecutor. In your test code, use a VmDatabase.memory() instead. This let's you run tests on the Dart VM, provided that you have sqlite3 installed where you run the tests.
I'll write some proper documentation for this soon.
I've published some basic docs on testing here. It basically expands the explanation from my previous comment, but if you run into any issues or would like to see more detailed descriptions, just let me know.
@simolus3 I've tried the documentation that you have provided (as well as copied your test to see if I was doing something wrong) but I get the same error both times.
`dart:ffi new DynamicLibrary.open
package:moor_ffi/src/load_library.dart 24:27 _defaultOpen
package:moor_ffi/src/load_library.dart 78:12 OpenDynamicLibrary.openSqlite
package:moor_ffi/src/bindings/bindings.dart 97:19 new _SQLiteBindings
package:moor_ffi/src/bindings/bindings.dart 197:53 bindings
package:moor_ffi/src/impl/database.dart 38:9 new Database.open
package:moor_ffi/src/impl/database.dart 30:41 new Database.memory
package:moor_ffi/src/vm_database.dart 41:22 _VmDelegate.open
package:moor/src/runtime/executor/helpers/engines.dart 251:22 DelegatedDatabase.ensureOpen.Invalid argument(s): Failed to load dynamic library (libsqlite3.so: cannot open shared object file: No such file or directory)`
Ah yeah - sqlite needs to be available as a shared library on your system (we can't bundle it with our pub package, unfortunately). This should be pointed out from the documentation, I'll add that.
How to install it depends on your distro, on Ubuntu (and I guess other Debians) it would be apt install libsqlite3-dev, on Arch that's included in the sqlite package directly.
@simolus3 that was it! Thanks so much!
@simolus3 there is no example on how to install sqlite on windows? and one is really needed
there is no example on how to install sqlite on windows? and one is really needed
@easazade The way I handle it is by just bundling sqlite3.dll with my app.
On the other hand I am not quite sure about how one would go about solving this for any given linux distro... @simolus3 I reckon this will have to be handled by any given installation package based on the particular specifics... So for instance in Ubuntu, one would have to ensure that libsqlite3-dev is installed as a dependency. Is that correct?
So for instance in Ubuntu, one would have to ensure that
libsqlite3-devis installed as a dependency
Yes, this is correct. You could also ship libsqlite3.so with your app of course.
I am trying to use moor_flutter library on my application So in order to I added moor_flutter: ^3.1.0 and moor_generatoron pubspec.yaml file.
After creating Tables and dao and database :
database:
LazyDatabase openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return VmDatabase(file);
});
}
@UseMoor(include: {
'index.moor'
}, tables: [
LocationCategories,
], daos: [
LocationCategoriesDao,
])
class Database extends _$Database {
Database(QueryExecutor e)
: super(openConnection);
Dao class:
@UseDao(tables: [LocationCategories])
class LocationCategoriesDao extends DatabaseAccessor<Database>
with _$LocationCategoriesDaoMixin {
final Database db;
LocationCategoriesDao(this.db) : super(db);
Future insertCategory(Insertable<LocationCatTable> locCat) =>
into(locationCategories).insert(locCat);
Now I am trying to write test according official site.
I am using Mxlinux base on debian so I installed libsqlite3-dev package. After installing I wrote this test:
import 'package:easy_life/database/database.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moor/ffi.dart';
import 'package:moor_flutter/moor_flutter.dart';
import 'package:uuid/uuid.dart';
void main() {
Database db;
setUp(() {
db = Database(VmDatabase.memory());
});
tearDown(() async {
await db.close();
});
test("test moor test", () async {
await db.locationCategoriesDao.insertCategory(
LocationCategoriesCompanion(uid: Value(Uuid().v4().toString())));
});
}
But After running test, My Linux completely freeze and I have to restart that??
What is my mistake and I am new on testing stuff.Thanks
But After running test, My Linux completely freeze and I have to restart that??
That definitely doesn't sound right. If you can share a full repro I'd love to take a look at what's going wrong here. Does the test pass though? Or does it already start to hang when the test is running?
Database(QueryExecutor e) : super(openConnection);
Shouldn't this be Database(QueryExecutor e): super(e)? That should probably be unrelated though.
Most helpful comment
You could make this work by changing your database constructor from
to
Then, add a
dev_dependencyonmoor_ffi: ^0.0.1. In your app, you would construct your database with theFlutterQueryExecutor. In your test code, use aVmDatabase.memory()instead. This let's you run tests on the Dart VM, provided that you have sqlite3 installed where you run the tests.I'll write some proper documentation for this soon.