use of com.google.firebase.firestore.conformance.TestCollection in project firebase-android-sdk by firebase.
the class ConformanceTest method executeTestCases.
private void executeTestCases(TestCase testCase, ConformanceRuntime runtime) throws Exception {
// Note: This method is copied from Google3 and modified to match the Android API.
TestCollection testCollection;
TestDocument testDocument;
for (Collection collection : testCase.getCollections()) {
testCollection = runtime.addInitialCollectionWithPath(collection.getName());
for (Document document : collection.getDocuments()) {
testDocument = testCollection.addDocumentWithId(getId(document));
for (Map.Entry<String, Value> field : document.getFieldsMap().entrySet()) {
testDocument.putField(field.getKey(), decodeValue(firestore, field.getValue()));
}
}
if (testCase.getException()) {
runtime.expectException();
} else {
Result result = testCase.getResult();
for (Document document : result.getDocuments()) {
testDocument = runtime.addExpectedDocumentWithId(getId(document));
for (Map.Entry<String, Value> field : document.getFieldsMap().entrySet()) {
testDocument.putField(field.getKey(), decodeValue(firestore, field.getValue()));
}
}
}
try {
runtime.setup();
try {
Query query = runtime.createQueryAtPath(testCase.getQuery().getCollection());
for (QueryFilter filter : testCase.getQuery().getFilters()) {
Where where = filter.getWhere();
if (where != null) {
switch(where.getOp()) {
case LESS_THAN:
query = query.whereLessThan(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case LESS_THAN_OR_EQUAL:
query = query.whereLessThanOrEqualTo(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case GREATER_THAN:
query = query.whereGreaterThan(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case GREATER_THAN_OR_EQUAL:
query = query.whereGreaterThanOrEqualTo(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case EQUAL:
query = query.whereEqualTo(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case NOT_EQUAL:
query = query.whereNotEqualTo(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case ARRAY_CONTAINS:
query = query.whereArrayContains(formatFieldPath(where.getField()), decodeValue(firestore, where.getValue()));
break;
case IN:
query = query.whereIn(formatFieldPath(where.getField()), Collections.singletonList(decodeValue(firestore, where.getValue())));
break;
case ARRAY_CONTAINS_ANY:
query = query.whereArrayContainsAny(formatFieldPath(where.getField()), Collections.singletonList(decodeValue(firestore, where.getValue())));
break;
case NOT_IN:
query = query.whereNotIn(formatFieldPath(where.getField()), Collections.singletonList(decodeValue(firestore, where.getValue())));
break;
default:
throw new Exception("Unexpected operation: " + where.getOp());
}
}
Order order = filter.getOrder();
if (order != null) {
query = query.orderBy(formatFieldPath(order.getField()), order.getDirection().equals(StructuredQuery.Direction.ASCENDING) ? Query.Direction.ASCENDING : Query.Direction.DESCENDING);
}
Value startAt = filter.getStartAt();
if (startAt != null) {
query = query.startAt(decodeValue(firestore, startAt));
}
Value startAfter = filter.getStartAfter();
if (startAfter != null) {
query = query.startAfter(decodeValue(firestore, startAfter));
}
Value endBefore = filter.getEndBefore();
if (endBefore != null) {
query = query.endBefore(decodeValue(firestore, endBefore));
}
Value endAt = filter.getEndAt();
if (endAt != null) {
query = query.endAt(decodeValue(firestore, endAt));
}
Long limit = filter.getLimit();
if (limit != null && limit > 0) {
query = query.limit(limit);
}
}
runtime.runQuery(query);
} catch (RuntimeException | AssertionError x) {
runtime.checkQueryError(x);
}
} finally {
runtime.teardown();
}
}
}
Aggregations