Skip to content

Commit

Permalink
Unit tests: make certain databases are closed at end of each test
Browse files Browse the repository at this point in the history
Leaving a db open between tests can cause problems if it's reopened by
another test and already has things like docs or validations in it.

Conflicts:
	Source/CBLReplicator_Tests.m
  • Loading branch information
snej committed Oct 23, 2014
1 parent 0bc852f commit decb569
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 142 deletions.
28 changes: 22 additions & 6 deletions Source/API/APITestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

#import "CouchbaseLitePrivate.h"
#import "CBLInternal.h"
#import "CBL_Shared.h"
#import "Test.h"


#if DEBUG

static CBLDatabase* createEmptyDB(void) {
CBLManager* dbmgr = [CBLManager sharedInstance];

__unused
static CBLDatabase* createManagerAndEmptyDBAtPath(NSString* path) {
CBLManager* dbmgr = [CBLManager createEmptyAtTemporaryPath: path];
CAssert(dbmgr);
NSError* error;
CBLDatabase* db = [dbmgr createEmptyDatabaseNamed: @"test_db" error: &error];
Expand All @@ -23,20 +26,33 @@ static CBLDatabase* createEmptyDB(void) {
}


static void closeTestDB(CBLDatabase* db) {
CAssert(db != nil);
[db _close];
static CBLDatabase* createEmptyDB(void) {
CBLManager* dbmgr = [CBLManager sharedInstance];
CAssert(dbmgr);
Assert(![dbmgr.shared isDatabaseOpened: @"test_db"], @"Last test forgot to close test_db!");
NSError* error;
CBLDatabase* db = [dbmgr createEmptyDatabaseNamed: @"test_db" error: &error];
CAssert(db, @"Couldn't create test_db: %@", error);
AfterThisTest(^{
[db _close];
Assert(![[CBLManager sharedInstance].shared isDatabaseOpened: @"test_db"],
@"Someone still has test_db open!");
});
return db;
}


__unused
static CBLDatabase* reopenTestDB(CBLDatabase* db) {
closeTestDB(db);
[db _close];
[[CBLManager sharedInstance] _forgetDatabase: db];
NSError* error;
CBLDatabase* db2 = [[CBLManager sharedInstance] databaseNamed: @"test_db" error: &error];
CAssert(db2, @"Couldn't reopen db: %@", error);
CAssert(db2 != db, @"reopenTestDB couldn't make a new instance");
AfterThisTest(^{
[db2 _close];
});
return db2;
}

Expand Down
24 changes: 0 additions & 24 deletions Source/API/APITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@
CAssertEqual(doc2.currentRevision.revisionID, currentRevisionID);

CAssertNil([db existingDocumentWithID: @"b0gus"]);

closeTestDB(db);
}


Expand All @@ -165,8 +163,6 @@
CBLDocument* doc = [db documentWithID: @"missing"];
Assert(doc != nil);
AssertNil([db existingDocumentWithID: @"missing"]);

closeTestDB(db);
}


Expand Down Expand Up @@ -213,7 +209,6 @@
CBLSavedRevision* rev3 = [newRev save: &error];
CAssert(rev3);
CAssertEqual(rev3.userProperties, newRev.userProperties);
closeTestDB(db);
}

TestCase(API_RevisionIdEquivalentRevisions) {
Expand Down Expand Up @@ -248,7 +243,6 @@
// since both rev2a and rev2b have same content, they should have
// the same rev ids.
CAssertEqual(rev2a.revisionID, rev2b.revisionID);

}

TestCase(API_CreateNewRevisions) {
Expand Down Expand Up @@ -323,8 +317,6 @@
CAssertEqual([doc getLeafRevisions: &error], @[rev3]);
CBLDocument* doc2 = db[doc.documentID];
CAssertEq(doc2, doc);

closeTestDB(db);
}

#if 0
Expand Down Expand Up @@ -361,7 +353,6 @@
CAssertEqual([doc.currentRevision.properties objectForKey: @"misc"],
@"updated!");
}
closeTestDB(db);
}


Expand All @@ -386,7 +377,6 @@
CAssertEqual([doc.currentRevision.properties objectForKey: @"order"],
[NSNumber numberWithInt: i]);
}
closeTestDB(db);
}


Expand All @@ -410,7 +400,6 @@
}

CAssertEq([db getDocumentCount], (NSInteger)0);
closeTestDB(db);
}
#endif

Expand Down Expand Up @@ -439,7 +428,6 @@
// currentRevision is inconsistent with a freshly-loaded CBLDocument's behavior, where if the
// document was previously deleted its currentRevision will initially be nil. (#265)
CAssertNil(doc.currentRevision);
closeTestDB(db);
}


Expand All @@ -454,7 +442,6 @@

CBLDocument* redoc = [db _cachedDocumentWithID:doc.documentID];
CAssert(!redoc);
closeTestDB(db);
}

TestCase(API_Validation) {
Expand All @@ -476,7 +463,6 @@
CAssert(![doc putProperties: properties error: &error]);
CAssertEq(error.code, 403);
//CAssertEqual(error.localizedDescription, @"forbidden: uncool"); //TODO: Not hooked up yet
closeTestDB(db);
}

TestCase(API_AllDocuments) {
Expand Down Expand Up @@ -506,7 +492,6 @@
n++;
}
CAssertEq(n, kNDocs);
closeTestDB(db);
}


Expand Down Expand Up @@ -534,7 +519,6 @@
CAssert(![db deleteLocalDocumentWithID: @"dock" error: &error],
@"Second delete should have failed");
CAssertEq(error.code, kCBLStatusNotFound);
closeTestDB(db);
}

#pragma mark - HISTORY
Expand Down Expand Up @@ -576,7 +560,6 @@

CAssertEqual([doc getConflictingRevisions: &error], @[rev2]);
CAssertEqual([doc getLeafRevisions: &error], @[rev2]);
closeTestDB(db);
}


Expand Down Expand Up @@ -618,8 +601,6 @@
AssertEq(revs.count, 2u);
AssertEqual(revs[0], defaultRev);
AssertEqual(revs[1], otherRev);

closeTestDB(db);
}

TestCase(API_Resolve_Conflict) {
Expand Down Expand Up @@ -673,9 +654,6 @@
AssertEq([[doc getLeafRevisions: &error] count], 2u);
AssertEq([[doc getConflictingRevisions: &error] count], 1u);
AssertEqual(doc.currentRevision, newRevSaved);

closeTestDB(db);

}


Expand Down Expand Up @@ -735,7 +713,6 @@
CAssert(!error);
CAssert(rev4);
CAssertEq([rev4.attachmentNames count], (NSUInteger)0);
closeTestDB(db);
}

#pragma mark - CHANGE TRACKING
Expand All @@ -759,7 +736,6 @@
CAssertEq(changeCount, 1);

CAssertEq(db.lastSequenceNumber, 5);
closeTestDB(db);
}


Expand Down
20 changes: 3 additions & 17 deletions Source/API/APIViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
CAssertEq(row.sequenceNumber, (UInt64)expectedKey+1);
++expectedKey;
}
closeTestDB(db);
}


Expand Down Expand Up @@ -86,7 +85,6 @@
CAssertEq(row.document, prevDoc);
++rowNumber;
}
closeTestDB(db);
}


Expand Down Expand Up @@ -209,8 +207,6 @@
AssertEqual(rows.nextRow.value, @[@"none"]);
AssertEqual(rows.nextRow.value, @[@"furry"]);
AssertNil(rows.nextRow);

closeTestDB(db);
}


Expand Down Expand Up @@ -238,8 +234,6 @@
AssertEqual(rows.nextRow.value, @"furry");
AssertEqual(rows.nextRow.value, @"scaly");
AssertNil(rows.nextRow);

closeTestDB(db);
}


Expand All @@ -262,8 +256,6 @@
AssertEqual(rows.nextRow.key, @"2");
AssertEqual(rows.nextRow.key, @"3");
AssertNil(rows.nextRow);

closeTestDB(db);
}


Expand All @@ -272,7 +264,7 @@

TestCase(API_LiveQuery) {
RequireTestCase(API_CreateView);
CBLDatabase* db = createEmptyDB();
CBLDatabase* db = createManagerAndEmptyDBAtPath(@"API_LiveQuery");
CBLView* view = [db viewNamed: @"vu"];
[view setMapBlock: MAPBLOCK({
emit(doc[@"sequence"], nil);
Expand Down Expand Up @@ -309,7 +301,6 @@
}
[query stop];
CAssert(finished, @"Live query timed out!");
closeTestDB(db);
}


Expand Down Expand Up @@ -405,15 +396,14 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
dispatch_sync(queue, ^{
[query removeObserver: observer forKeyPath: @"rows"];
[query stop];
closeTestDB(db);
[dbmgr close];
});
}


TestCase(API_AsyncViewQuery) {
RequireTestCase(API_CreateView);
CBLDatabase* db = createEmptyDB();
CBLDatabase* db = createManagerAndEmptyDBAtPath(@"API_AsyncViewQuery");
CBLView* view = [db viewNamed: @"vu"];
[view setMapBlock: MAPBLOCK({
emit(doc[@"sequence"], nil);
Expand Down Expand Up @@ -451,13 +441,12 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
break;
}
CAssert(finished, @"Async query timed out!");
closeTestDB(db);
}

// Ensure that when the view mapblock changes, a related live query
// will be notified and automatically updated
TestCase(API_LiveQuery_UpdatesWhenViewChanges) {
CBLDatabase* db = createEmptyDB();
CBLDatabase* db = createManagerAndEmptyDBAtPath(@"UpdatesWhenViewChanges");

CBLView* view = [db viewNamed: @"vu"];

Expand Down Expand Up @@ -542,8 +531,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

[liveQuery stop];
[liveQuery removeObserver:observer forKeyPath:@"rows"];

closeTestDB(db);
}


Expand Down Expand Up @@ -580,7 +567,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
return @"ok";
}];
CAssertEqual(result, @"ok");
closeTestDB(db);
[mgr close];
}

Expand Down
4 changes: 4 additions & 0 deletions Source/API/CBLManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ + (instancetype) createEmptyAtPath: (NSString*)path {
error: &error];
Assert(dbm, @"Failed to create db manager at %@: %@", path, error);
AssertEqual(dbm.directory, path);
AfterThisTest(^{
[dbm close];
[[NSFileManager defaultManager] removeItemAtPath: path error: NULL];
});
return dbm;
}

Expand Down
10 changes: 3 additions & 7 deletions Source/CBLBlobStore_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
NSError* error;
CBL_BlobStore* store = [[CBL_BlobStore alloc] initWithPath: path error: &error];
CAssert(store, @"Couldn't create CBL_BlobStore: %@", error);
AfterThisTest(^{
[[NSFileManager defaultManager] removeItemAtPath: path error: NULL];
});
return store;
}

static void deleteStore(CBL_BlobStore* store) {
[[NSFileManager defaultManager] removeItemAtPath: store.path error: NULL];
}


TestCase(CBL_BlobStoreBasic) {
CBL_BlobStore* store = createStore();
Expand All @@ -34,7 +33,6 @@ static void deleteStore(CBL_BlobStore* store) {

NSData* readItem = [store blobForKey: key];
CAssertEqual(readItem, item);
deleteStore(store);
}


Expand All @@ -51,8 +49,6 @@ static void deleteStore(CBL_BlobStore* store) {

NSData* readItem = [store blobForKey: writer.blobKey];
CAssertEqual(readItem, [@"part 1, part 2, part 3" dataUsingEncoding: NSUTF8StringEncoding]);

deleteStore(store);
}


Expand Down
Loading

0 comments on commit decb569

Please sign in to comment.