Comments (16)
@rolurq The mocks here are here for testing that the correct calls are made to the plugins. You might want a simpler mock just by implementing DatabaseFactory
:
import 'dart:async';
import 'package:sqflite/sqflite.dart';
class DatabaseFactoryMock implements DatabaseFactory {
@override
Future<bool> databaseExists(String path) async {
return false;
}
@override
Future<void> deleteDatabase(String path) async {}
@override
Future<String> getDatabasesPath() async {
return null;
}
@override
Future<Database> openDatabase(String path, {OpenDatabaseOptions options}) {
return null;
}
}
final databaseFactoryMock = DatabaseFactoryMock();
and use databaseFactoryMock
instead of databaseFactory
in your test code.
Then you might say that you need a real Database object so you will need to mock Database then but here it will depends on your need so it is hard to make something that everybody will agree on...
from sqflite.
i also getting this error while testing..
from sqflite.
I think setMockMethodCallHandler is deprecated in the next flutter release. A solution is available here:
https://github.com/tekartik/sqflite/blob/master/sqflite_common_ffi/doc/testing.md
Example:
import 'package:flutter_test/flutter_test.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite/sqflite.dart';
Future main() async {
// Setup sqflite_common_ffi for flutter test
setUpAll(() {
// Initialize FFI
sqfliteFfiInit();
// Change the default factory
databaseFactory = databaseFactoryFfi;
});
test('Simple test', () async {
var db = await openDatabase(inMemoryDatabasePath, version: 1,
onCreate: (db, version) async {
await db
.execute('CREATE TABLE Test (id INTEGER PRIMARY KEY, value TEXT)');
});
// Insert some data
await db.insert('Test', {'value': 'my_value'});
// Check content
expect(await db.query('Test'), [
{'id': 1, 'value': 'my_value'}
]);
await db.close();
});
}
from sqflite.
I don't think we can run test with plugins as the tests don't run on the device. As a TDD fan, I miss this feature too. At least I did not find a way to do it. None of the "official" flutter plugins have any as far as I can tell and according to https://stackoverflow.com/questions/46382789/how-to-run-tests-with-code-that-depends-on-plugins there is no solution. I remember seeing an example of native code and flutter driver somewhere but I could not make it work.
If you can leave with mocking the API, you could take a look at what I did here (it uses some private source so might break): https://github.com/tekartik/sqflite/blob/master/test/src_database_test.dart if that could help (but it might be a pain to mock return values from the plugin)
from sqflite.
hi, can you provide some suggestions to do testing with sqflite? Do I have to run flutter drive
?
Thank you
from sqflite.
I'm not aware of any way of doing dart unit-testing with plugins. flutter drive could be a (currently painful) solution. Even plugins in https://github.com/flutter/plugins/ don't have these kinds of test. I'm still waiting for a solution
from sqflite.
Can we not provide a native backend as well where it handles sqlite calls to https://pub.dartlang.org/packages/sqlite?
I understand this might basically triple the work needed for implementation, but I think it might be worth it...
from sqflite.
I have not tried this package yet, have you? Your are talking about testing right (I'm not aware of native backend without objc/java yet). For testing I guess it should have to work on the workstation used (mac, linux, win) so I guess a dart2 compatible native sqlite package should work (if you know any)
An alternative for testing would be to have sqlite server and mock native calls to it. If you know any flutter package with native calls and complete unit tests, that would be great.
from sqflite.
I did not try the package yet, it seems it just wraps the native C/C++ library so it should work on mac, linux and win so should in general work for unit testing purposes
It seems the package is a bit unmainaintaned though. Not sure what its test coverage though
from sqflite.
I could not manage to make it work...
from sqflite.
现在还是没办法写测试吗
Is it still impossible to write tests now?
from sqflite.
There is still no simple/managed ways to run unit test on the device. In this thread #83, there is a solution though
# run the test
flutter run test/sqlite_test.dart
with some caveats...
from sqflite.
@alextekartik how about if you provide the mocks you used in https://github.com/tekartik/sqflite/blob/master/test/src_database_test.dart in an extra package to ease with testing?
Or maybe the same package but as an extra.
Some plugins provide mocking capabilities for testing, so is not unusual behavior.
from sqflite.
I can run sqflite tests using flutter run "test file path" but I need to include test in coverage report. How should I do that. any code samples appreciated
from sqflite.
I started a doc about testing here:
#49
If anyone has already tried e2e testing or find a good way to test code with sqflite, I'd be happy to complete the documentation.
from sqflite.
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
const MethodChannel('com.tekartik.sqflite')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'openDatabase') {
return await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
}
});
Worked for me.
from sqflite.
Related Issues (20)
- Intermittent Data Incompleteness Issue When Copying SQLite Database File for Backup HOT 1
- Adding Offline Syncing to Online Database HOT 2
- Cursors HOT 1
- Support for .sql files HOT 2
- COLLATE NOCASE is not supporting in web build
- [sqflite_common_ffi_web 0.4.3] - db is not accessible in Android Chrome HOT 7
- Update PRAGMA version in windows
- SQLite Upgrade? HOT 2
- ConflictAlgorithm.replace deletes the row with FOREIGN KEY ON DELETE CASCADE ON UPDATE NO ACTION
- power function not supported HOT 1
- Inserting a row after clearing a table is not working HOT 2
- Can you tell me the general area on how to create a stream/watch a table? HOT 3
- Possibility to create functions? HOT 1
- Not an error as an error HOT 2
- Android Blob Lookup Fixed? HOT 1
- java.lang.IllegalArgumentException: Invalid token limit HOT 3
- sqlflite web db not persistent HOT 2
- Cursor closed despite moveNext() returning true HOT 1
- Macos - SqfliteFfiException(sqlite_error: 14, , open_failed: SqliteException(14): while opening the database, bad parameter or other API misuse, bad parameter or other API misuse (code 21)}) HOT 2
- [typo] empy found HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqflite.