I would like to have a nice and simple layer over a mongodb instance using dart, and I cannot manage to do it :
Given a mongodb instance "test-mongo-layer" :
mongo shell :
use test-mongo-layer switched to db test-mongo-layer db.users.find() { "_id" : ObjectId("524b1bf8fbfe21c9c4cdb772"), "name" : "Toto", "login" : "toto" } { "_id" : ObjectId("524b1bf8fbfe21c9c4cdb772"), "name" : "Titi", "login" : "titi" }
And a model.dart source file :
class User {
String name;
String login;
User(this.name, this.login)
User.fromJson(Map map) {
name = map['name'];
login = map['login'];
}
}
And a persistence.dart source file :
import 'package:mongo_dart/mongo_dart.dart';
import '../lib/model.dart';
class PersistenceLayer{
Db _mongodb;
PersistenceLayer(this._mongodb)
List<User> getUsers() {
List<User> users = new List();
_mongodb.open().then((o){
return __mongodb.collection('users').find().forEach((jsonUser){
User user = new User.fromJson(jsonUser);
users.add(user);
});
}).then((jsonUsers){
_mongodb.close();
});
return users;
}
}
When I launch the following test :
import 'package:mongo_dart/mongo_dart.dart';
import 'package:unittest/unittest.dart';
import '../lib/model.dart' ;
import '../lib/persistence.dart' ;
main() {
Db db = new Db("mongodb://127.0.0.1/test-mongo-layer");
test('Test get users', () {
PersistenceLayer persistence = new PersistenceLayer(db);
var users = persistence.getUsers();
expect(users.length, 2);
}
}
Then a got an exception :
Uncaught Error: Expected: <2> Actual: <0><
This is quite simple but it's not working, i must mess with the Future because the code is not even executed when I execute it on debug...
SOLUTION : thanks to Alexandre
persistence.dart source file :
Future<List<User>> getUsers() {
List<User> users = new List();
return _mongodb.open()
.then((_){
return _mongodb.collection('users').find().forEach((jsonUser){
User user = new User.fromJson(jsonUser);
users.add(user);
});
})
.then((_) {
return users;
})
.whenComplete((){
_mongodb.close();
});
}
test :
test('Test get users', () {
PersistenceLayer persitence = new MongoPersistence(db);
Future<List<User>> usersFuture = persistence.getUsers();
usersFuture.then((users) => expect(users.length, 2) );
});