Skip to content

Commit

Permalink
Tests using APIs on collection when database is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenNguyen14 committed Aug 16, 2022
1 parent cb1567f commit 9b23bb1
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
49 changes: 49 additions & 0 deletions common/test/java/com/couchbase/lite/CollectionListenerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,55 @@ class CollectionListenerTest : BaseCollectionTest() {
testCollection.removeCollectionChangeListener(token)
}

// Test that addChangeListener to a collection in a closed database doesn't throw an exception
@Test
fun testAddChangeListenerToCollectionInClosedDatabase() {
baseTestDb.close()
testCollection.addChangeListener(testSerialExecutor) {}
}

// Test that addDocumentChangeListener to a collection in a closed database doesn't throw an exception
@Test
fun testAddDocumentChangeListenerToCollectionInClosedDatabase() {
val docID = "testDoc"
val doc = MutableDocument(docID)
testCollection.save(doc)

baseTestDb.close()
testCollection.addDocumentChangeListener(docID, testSerialExecutor) {}
}

// Test that removeChangeListener from a collection in a closed database doesn't throw exception
@Test
fun testRemoveChangeListenerFromCollectionInClosedDatabase(){
val doc1Id = "doc_1"
val doc2Id = "doc_2"

var changes: MutableList<String>? = null
var latch: CountDownLatch? = null

val token = testCollection.addChangeListener() { c ->
changes?.addAll(c.documentIDs)
if ((changes?.size ?: 0) >= 2) {
latch?.countDown()
}
}

latch = CountDownLatch(1)
changes = mutableListOf()

testCollection.save(MutableDocument(doc1Id))
testCollection.save(MutableDocument(doc2Id))
assertTrue(latch.await(STD_TIMEOUT_SEC, TimeUnit.SECONDS))
assertEquals(2, changes.size)
assertTrue(changes.contains(doc1Id))
assertTrue(changes.contains(doc2Id))

baseTestDb.close()
testCollection.removeCollectionChangeListener(token)
}


// These tests tests are incredibly finicky.

// Create two collections, A and B.
Expand Down
67 changes: 67 additions & 0 deletions common/test/java/com/couchbase/lite/CollectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class CollectionTest : BaseCollectionTest() {
testCollection.getDocument("doc1")
}

// Test getting doc from collection in a closed db causes CBL Exception
@Test(expected = CouchbaseLiteException::class)
fun testGetDocFromCollectionInClosedDB() {
createSingleDocInCollectionWithId("doc_id")
baseTestDb.close()
testCollection.getDocument("doc_id")
}

// Test getting doc count from deleted collection returns 0
@Test
fun testGetDocCountFromDeletedCollection() {
Expand All @@ -118,6 +126,16 @@ class CollectionTest : BaseCollectionTest() {
assertEquals(0, testCollection.count)
}

// Test getting doc count from a collection in a closed database returns 0
@Test
fun testGetDocCountFromCollectionInClosedDatabase() {
val docID = "doc_id"
val doc = MutableDocument(docID)
saveDocInBaseCollectionTest(doc)
assertEquals(1, testCollection.count)
baseTestDb.close()
assertEquals(0, testCollection.count)
}
//---------------------------------------------
// Save Document
//---------------------------------------------
Expand Down Expand Up @@ -214,6 +232,13 @@ class CollectionTest : BaseCollectionTest() {
testCollection.save(doc)
}

// Test saving document in a collection of a closed database causes CBLException
@Test(expected = CouchbaseLiteException::class)
fun testSaveDocToCollectionInClosedDB() {
baseTestDb.close()
saveDocInBaseCollectionTest(MutableDocument("invalid"))
}

@Test
fun testSaveAndUpdateMutableDoc() {
val doc = MutableDocument("doc1")
Expand Down Expand Up @@ -400,6 +425,14 @@ class CollectionTest : BaseCollectionTest() {
testCollection.delete(doc)
}

// Test deleting doc on a collection in a closed db causes CBLException
@Test(expected = CouchbaseLiteException::class)
fun testDeleteDocOnCollectionInClosedDB() {
val doc = createSingleDocInCollectionWithId("doc_id")
baseTestDb.close()
testCollection.delete(doc)
}

@Test
@Throws(CouchbaseLiteException::class)
fun testDeleteAlreadyDeletedDoc() {
Expand Down Expand Up @@ -572,6 +605,14 @@ class CollectionTest : BaseCollectionTest() {
testCollection.purge(doc)
}

// Test purging doc from a collection in a closed database causes CBL exception
@Test(expected = CouchbaseLiteException::class)
fun testPurgeDocFromCollectionInClosedDB() {
val doc = createSingleDocInCollectionWithId("doc_id")
baseTestDb.close()
testCollection.purge(doc)
}

//---------------------------------------------
// Index functionalities
//---------------------------------------------
Expand Down Expand Up @@ -707,6 +748,32 @@ class CollectionTest : BaseCollectionTest() {
testCollection.deleteIndex("index1")
}

// Test that getIndexes from collection in closed database causes CBLException
@Test(expected = CouchbaseLiteException::class)
fun testGetIndexesFromCollectionInClosedDatabase() {
testCollection.createIndex("test_index", ValueIndexConfiguration("firstName", "lastName"))
assertEquals(1, testCollection.indexes.size)
baseTestDb.close()
testCollection.indexes
}

// Test that createIndex in collection in closed database causes CBLException
@Test(expected = CouchbaseLiteException::class)
fun testCreateIndexInCollectionInClosedDatabase() {
baseTestDb.close()
testCollection.createIndex("test_index", ValueIndexConfiguration("firstName", "lastName"))
}

// Test that deletedIndex in collection in closed database causes CBLException
@Test(expected = CouchbaseLiteException::class)
fun testDeleteIndexInCollectionInClosedDatabase(){
val name = "test_index"
testCollection.createIndex(name, ValueIndexConfiguration("firstName", "lastName"))
assertEquals(1, testCollection.indexes.size)
baseTestDb.close()
testCollection.deleteIndex(name)
}

//---------------------------------------------
// Operations with Conflict
//---------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions common/test/java/com/couchbase/lite/DbCollectionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,13 @@ class DbCollectionsTest : BaseCollectionTest() {
assertNotNull(testCollection.scope)
assertEquals(testColName, testCollection.name)
}

// Test getting scope, and collection name from a collection when database is closed returns the scope and name
@Test
fun testGetScopeAndCollectionNameFromAClosedDatabase() {
baseTestDb.close()
assertNotNull(testCollection.scope)
assertEquals(testColName, testCollection.name)
}
}

37 changes: 37 additions & 0 deletions common/test/java/com/couchbase/lite/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,43 @@ public void testGetExpirationOnDocInCollectionDeletedInDifferentDBInstance() thr
}
}

// Test setting expiration on doc in a collection of closed database throws CBLException
@Test
public void testSetExpirationOnDocInCollectionOfClosedDB() {
Date expiration = new Date(System.currentTimeMillis() + 30000L);
try {
String id = "doc_id";
MutableDocument document = new MutableDocument(id);
baseTestDb.close();
testCollection.setDocumentExpiration(id, expiration);
fail("Expect CouchbaseLiteException");
}
catch (CouchbaseLiteException e) {
assertEquals(CBLError.Code.NOT_OPEN, e.getCode());
}
}

// Test getting expiration on doc in a collection of closed database throws CBLException
@Ignore("CBL-3575")
@Test
public void testGetExpirationOnDocInACollectionOfClosedDatabase() throws CouchbaseLiteException {
Date expiration = new Date(System.currentTimeMillis() + 30000L);
// add doc in collection
String id = "test_doc";
MutableDocument document = new MutableDocument(id);
saveDocInBaseCollectionTest(document);
testCollection.setDocumentExpiration(id, expiration);
baseTestDb.deleteCollection(testColName, testScopeName);

try {
testCollection.getDocumentExpiration(id);
fail("Expect CouchbaseLiteException");
}
catch (CouchbaseLiteException e) {
assertEquals(CBLError.Code.NOT_OPEN, e.getCode());
}
}

@Test
public void testLongExpiration() throws Exception {
Date now = new Date(System.currentTimeMillis());
Expand Down

0 comments on commit 9b23bb1

Please sign in to comment.