Skip to content

Commit

Permalink
Fix on db._masterConectionVerified
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio committed Sep 4, 2020
1 parent b3a89b4 commit 65d65a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 0.4.3

* New `isConnected` `Db` getter. It returns `true` if the state of the database is open and at least the primary connection is on.
* db._masterConnectionVerified() now throws an explicit error if the db state is not OPEN (it was throwing a `NoSuchMethodError`).

### 0.4.2

Expand Down
10 changes: 8 additions & 2 deletions lib/src/database/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ class Db {

_Connection get _masterConnection => _connectionManager.masterConnection;

_Connection get _masterConnectionVerified =>
_connectionManager.masterConnectionVerified;
_Connection get _masterConnectionVerified {
if (state != State.OPEN) {
throw MongoDartError(
'The required operation can be executed only when the db is open');
}
return _connectionManager.masterConnectionVerified;
}

WriteConcern _writeConcern;
AuthenticationScheme _authenticationScheme;
ReadPreference readPreference = ReadPreference.primary;
Expand Down
32 changes: 32 additions & 0 deletions test/database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const dbAddress = '127.0.0.1';

const DefaultUri = 'mongodb://$dbAddress:27017/$dbName';

var throwsMongoDartError = throwsA((e) => e is MongoDartError);

Db db;
Uuid uuid = Uuid();
List<String> usedCollectionNames = [];
Expand All @@ -21,6 +23,34 @@ String getRandomCollectionName() {
return name;
}

Future testDbCreate() async {
var dbCreate = Db(DefaultUri);
await dbCreate.open();
await dbCreate.close();

dbCreate = await Db.create(DefaultUri);
await dbCreate.open();
await dbCreate.close();
}

Future testOperationNotInOpenState() async {
var dbCreate = await Db.create(DefaultUri);
var coll = dbCreate.collection('test-error');
expect(
() async =>
await coll.findAndModify(query: {'value': 1}, update: {'value': 1}),
throwsMongoDartError);

dbCreate = Db(DefaultUri);
await dbCreate.open();
coll = dbCreate.collection('test-error');
await dbCreate.close();
expect(
() async =>
await coll.findAndModify(query: {'value': 1}, update: {'value': 1}),
throwsMongoDartError);
}

Future testDbConnectionString() async {
var db = Db('mongodb://www.example.com');
expect(db.uriList.first, 'mongodb://www.example.com');
Expand Down Expand Up @@ -1349,6 +1379,8 @@ void main() async {

group('Db creation tests:', () {
test('test connection string', testDbConnectionString);
test('test db.create()', testDbCreate);
test('Error - operation not in Open state', testOperationNotInOpenState);
});
group('DbCollection tests:', () {
test('testAuthComponents', testAuthComponents);
Expand Down

0 comments on commit 65d65a6

Please sign in to comment.