Skip to content

Commit

Permalink
CBL-3341: Review and Complete Collection Tests
Browse files Browse the repository at this point in the history
CBL-3357: Validate 32+ select items in the query fails or not
Fix the error code for getting documents from a deleted collection
  • Loading branch information
LaurenNguyen14 committed Jul 12, 2022
1 parent cd0fa3e commit 1da811c
Show file tree
Hide file tree
Showing 10 changed files with 978 additions and 288 deletions.
2 changes: 1 addition & 1 deletion android/main/res/raw/errors.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"CreateDBDirectoryFailed": "Unable to create database directory.", "CloseDBFailedReplications": "Cannot close the database. Please stop all of the replicators before closing the database.", "CloseDBFailedQueryListeners": "Cannot close the database. Please remove all of the query listeners before closing the database.", "DeleteDBFailedReplications": "Cannot delete the database. Please stop all of the replicators before closing the database.", "DeleteDBFailedQueryListeners": "Cannot delete the database. Please remove all of the query listeners before closing the database.", "DeleteDocFailedNotSaved": "Cannot delete a document that has not yet been saved.", "DocumentNotFound": "The document doesn't exist in the database.", "DocumentAnotherDatabase": "Cannot operate on a document from another database.", "BlobDifferentDatabase": "A document contains a blob that was saved to a different database. The save operation cannot complete.", "BlobContentNull": "No data available to write for install. Please ensure that all blobs in the document have non-null content.", "ResolvedDocContainsNull": "Resolved document has a null body.", "ResolvedDocFailedLiteCore": "LiteCore failed resolving conflict.", "ResolvedDocWrongDb": "Resolved document's database %1$s is different from expected database %2$s.", "DBClosed": "Attempt to perform an operation on a closed database.", "NoDocumentRevision": "No revision data on the document!", "FragmentPathNotExist": "Specified fragment path does not exist in object; cannot set value.", "InvalidCouchbaseObjType": "%1$s is not a valid type. You may only pass %2$s, Blob, a one-dimensional array or a dictionary whose members are one of the preceding types.", "InvalidValueToBeDeserialized": "Non-string or null key in data to be deserialized.", "BlobContainsNoData": "Blob has no data available.", "NotFileBasedURL": "%1$s must be a file-based URL.", "BlobReadStreamNotOpen": "Stream is not open.", "CannotSetLogLevel": "Cannot set logging level without a configuration.", "InvalidSchemeURLEndpoint": "Invalid scheme for URLEndpoint url (%1$s). It must be either 'ws:' or 'wss:'.", "InvalidEmbeddedCredentialsInURL": "Embedded credentials in a URL (username:password@url) are not allowed. Use the BasicAuthenticator class instead.", "ReplicatorNotStopped": "Replicator is not stopped. Resetting checkpoint is only allowed when the replicator is in the stopped state.", "QueryParamNotAllowedContainCollections": "Query parameters are not allowed to contain collections.", "MissASforJoin": "Missing AS clause for JOIN.", "MissONforJoin": "Missing ON statement for JOIN.", "ExpressionsMustBeIExpressionOrString": "Expressions must either be %1$s or String.", "InvalidExpressionValueBetween": "Invalid expression value for expression of Between(%1$s).", "ResultSetAlreadyEnumerated": "This result set has already been enumerated. Please re-execute the original query.", "ExpressionsMustContainOnePlusElement": "%1$s expressions must contain at least one element.", "DuplicateSelectResultName": "Duplicate select result named %1$s.", "NoAliasInJoin": "The default database must have an alias in order to use a JOIN statement (Make sure your data source uses the As() function).", "InvalidQueryDBNull": "Invalid query: The database is null.", "InvalidQueryMissingSelectOrFrom": "Invalid query: missing Select or From.", "PullOnlyPendingDocIDs": "Pending Document IDs are not supported on pull-only replicators."}
{"CreateDBDirectoryFailed": "Unable to create database directory.", "CloseDBFailedReplications": "Cannot close the database. Please stop all of the replicators before closing the database.", "CloseDBFailedQueryListeners": "Cannot close the database. Please remove all of the query listeners before closing the database.", "DeleteDBFailedReplications": "Cannot delete the database. Please stop all of the replicators before closing the database.", "DeleteDBFailedQueryListeners": "Cannot delete the database. Please remove all of the query listeners before closing the database.", "DeleteDocFailedNotSaved": "Cannot delete a document that has not yet been saved.", "DocumentNotFound": "The document doesn't exist in the database.", "DocumentAnotherDatabase": "Cannot operate on a document from another database.", "BlobDifferentDatabase": "A document contains a blob that was saved to a different database. The save operation cannot complete.", "BlobContentNull": "No data available to write for install. Please ensure that all blobs in the document have non-null content.", "ResolvedDocContainsNull": "Resolved document has a null body.", "ResolvedDocFailedLiteCore": "LiteCore failed resolving conflict.", "ResolvedDocWrongDb": "Resolved document's database %1$s is different from expected database %2$s.", "DBClosedOrCollectionDeleted": "Attempt to perform an operation on a closed database or a deleted collection.", "NoDocumentRevision": "No revision data on the document!", "FragmentPathNotExist": "Specified fragment path does not exist in object; cannot set value.", "InvalidCouchbaseObjType": "%1$s is not a valid type. You may only pass %2$s, Blob, a one-dimensional array or a dictionary whose members are one of the preceding types.", "InvalidValueToBeDeserialized": "Non-string or null key in data to be deserialized.", "BlobContainsNoData": "Blob has no data available.", "NotFileBasedURL": "%1$s must be a file-based URL.", "BlobReadStreamNotOpen": "Stream is not open.", "CannotSetLogLevel": "Cannot set logging level without a configuration.", "InvalidSchemeURLEndpoint": "Invalid scheme for URLEndpoint url (%1$s). It must be either 'ws:' or 'wss:'.", "InvalidEmbeddedCredentialsInURL": "Embedded credentials in a URL (username:password@url) are not allowed. Use the BasicAuthenticator class instead.", "ReplicatorNotStopped": "Replicator is not stopped. Resetting checkpoint is only allowed when the replicator is in the stopped state.", "QueryParamNotAllowedContainCollections": "Query parameters are not allowed to contain collections.", "MissASforJoin": "Missing AS clause for JOIN.", "MissONforJoin": "Missing ON statement for JOIN.", "ExpressionsMustBeIExpressionOrString": "Expressions must either be %1$s or String.", "InvalidExpressionValueBetween": "Invalid expression value for expression of Between(%1$s).", "ResultSetAlreadyEnumerated": "This result set has already been enumerated. Please re-execute the original query.", "ExpressionsMustContainOnePlusElement": "%1$s expressions must contain at least one element.", "DuplicateSelectResultName": "Duplicate select result named %1$s.", "NoAliasInJoin": "The default database must have an alias in order to use a JOIN statement (Make sure your data source uses the As() function).", "InvalidQueryDBNull": "Invalid query: The database is null.", "InvalidQueryMissingSelectOrFrom": "Invalid query: missing Select or From.", "PullOnlyPendingDocIDs": "Pending Document IDs are not supported on pull-only replicators.", "NoDocEditInReplicationFilter": "Documents from a replication filter cannot be edited.", "IdentityNotFound": "The identity is not present in the %1$s", "FailToConvertC4Cert": "Couldn't convert from C4Cert to %1$s Array: %2$s", "DuplicateCertificate": "Certificate already exists with the label", "MissingCommonName": "The Common Name attribute is required", "FailToRemoveKeyPair": "Couldn't remove a keypair with error: %1$s"}
12 changes: 11 additions & 1 deletion common/main/java/com/couchbase/lite/AbstractDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,22 @@ public long getCount() {
* @param id the document ID
* @return the Document object
* @deprecated Use getDefaultCollection().getCount()
*
*
*/
@SuppressWarnings("PMD.PreserveStackTrace")
@Deprecated
@Nullable
public Document getDocument(@NonNull String id) {
try { return getDefaultCollectionOrThrow().getDocument(id); }
catch (CouchbaseLiteException e) { throw new IllegalStateException(Log.lookupStandardMessage("DBClosed"), e); }
catch (CouchbaseLiteException e) {
if (e.getDomain().equals(CBLError.Domain.CBLITE) && e.getCode() == CBLError.Code.NOT_OPEN) {
throw new IllegalStateException(
Log.lookupStandardMessage("DBClosedOrCollectionDeleted"),
e);
}
return null;
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions common/main/java/com/couchbase/lite/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,20 @@ long getCount() throws CouchbaseLiteException {
/**
* Gets an existing Document object with the given ID. If the document with the given ID doesn't
* exist in the collection, the value returned will be null.
* <p>
*
*/
@Nullable
public Document getDocument(@NonNull String id) throws CouchbaseLiteException {
Preconditions.assertNotEmpty(id, "id");
return withLockAndOpenDb(() -> {
try { return Document.getDocument(this, id, false); }
catch (CouchbaseLiteException e) { Log.i(LogDomain.DATABASE, "Failed retrieving document: %s", id); }
return null;
catch (CouchbaseLiteException e) {
if (e.getDomain().equals(CBLError.Domain.CBLITE) && e.getCode() == CBLError.Code.NOT_FOUND) {
return null;
}
else { throw e; }
}
});
}

Expand Down
20 changes: 16 additions & 4 deletions common/test/java/com/couchbase/lite/BaseCollectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ import org.junit.Before
import java.util.*

open class BaseCollectionTest : BaseDbTest() {
private var testScope: Scope? = null
private var testCollection: Collection? = null
protected lateinit var testCollection: Collection
protected lateinit var testColName : String
protected lateinit var testScopeName : String

protected val Scope.collectionCount
get() = this.collections.size

@Before
fun setUpBaseCollectionTest() {
testScope = baseTestDb.defaultScope
testCollection = testScope!!.getCollection(Collection.DEFAULT_NAME)
testColName = getUniqueName("test_collection")
testScopeName = getUniqueName("test_scope")
testCollection = baseTestDb.createCollection(testColName, testScopeName)
Report.log(LogLevel.INFO, "Created base test Collection: $testCollection")
}

Expand Down Expand Up @@ -64,5 +66,15 @@ open class BaseCollectionTest : BaseDbTest() {
Assert.assertEquals(doc.id, savedDoc!!.id)
return savedDoc
}

@Throws(CouchbaseLiteException::class)
protected fun createDocsInCollectionTest(n: Int) {
for (i in 0 until n) {
val doc = MutableDocument(String.format(Locale.US, "doc_%03d", i))
doc.setValue("key", i)
saveDocInBaseCollectionTest(doc)
}
Assert.assertEquals(n.toLong(), testCollection!!.count)
}
}

32 changes: 32 additions & 0 deletions common/test/java/com/couchbase/lite/BaseDbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
Expand Down Expand Up @@ -55,6 +59,10 @@ public interface DocValidator extends Fn.ConsumerThrows<Document, CouchbaseLiteE

public static C4Database getC4Db(@NonNull Database db) { return db.getOpenC4Database(); }

protected static <T extends Comparable<T>> void assertContents(List<T> l1, T... contents) {
List<T> l2 = Arrays.asList(contents);
assertTrue(l1.containsAll(l2) && l2.containsAll(l1));
}

protected Database baseTestDb;

Expand Down Expand Up @@ -1655,14 +1663,38 @@ protected final void verifyDocument(DictionaryInterface doc) {
verifyBlob(doc.getBlob("doc-29"));
}

protected Database openDatabase() throws CouchbaseLiteException { return verifyOrDeleteDb(createDb(getUniqueName("test_db"))); }

protected final void reopenBaseTestDb() throws CouchbaseLiteException { baseTestDb = reopenDb(baseTestDb); }

protected final void recreateBastTestDb() throws CouchbaseLiteException { baseTestDb = recreateDb(baseTestDb); }

protected Database duplicateBaseTestDb() throws CouchbaseLiteException { return verifyOrDeleteDb(duplicateDb(baseTestDb)); }


// Some JSON encoding will promote a Float to a Double.
protected final Float demoteToFloat(Object val) {
if (val instanceof Float) { return (Float) val; }
if (val instanceof Double) { return ((Double) val).floatValue(); }
throw new IllegalArgumentException("expected a floating point value");
}
private Database verifyOrDeleteDb(Database db) {
try {
assertNotNull(db);
assertTrue(new File(db.getPath()).getCanonicalPath().endsWith(C4Database.DB_EXTENSION));

return db;
}
catch (IOException e) {
deleteDb(db);
throw new AssertionError("Unable to get db path", e);
}
catch (AssertionError e) {
deleteDb(db);
throw e;
}
}



}
Loading

0 comments on commit 1da811c

Please sign in to comment.