Search in sources :

Example 1 with Collection

use of com.google.firebase.firestore.conformance.model.Collection 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();
        }
    }
}
Also used : Order(com.google.firebase.firestore.conformance.model.Order) StructuredQuery(com.google.firestore.v1.StructuredQuery) TestDocument(com.google.firebase.firestore.conformance.TestDocument) Document(com.google.firestore.v1.Document) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Result(com.google.firebase.firestore.conformance.model.Result) TestDocument(com.google.firebase.firestore.conformance.TestDocument) QueryFilter(com.google.firebase.firestore.conformance.model.QueryFilter) TestUtil.decodeValue(com.google.firebase.firestore.testutil.TestUtil.decodeValue) Value(com.google.firestore.v1.Value) TestCollection(com.google.firebase.firestore.conformance.TestCollection) Collection(com.google.firebase.firestore.conformance.model.Collection) TestCollection(com.google.firebase.firestore.conformance.TestCollection) Map(java.util.Map) Where(com.google.firebase.firestore.conformance.model.Where)

Example 2 with Collection

use of com.google.firebase.firestore.conformance.model.Collection in project firebase-android-sdk by firebase.

the class TestCaseConverter method convertTestCases.

public List<TestCase> convertTestCases(DatastoreTestTrace.TestTrace trace) {
    List<TestCase> testCases = new ArrayList<>();
    for (DatastoreTestTrace.DatastoreAction action : trace.getActionList()) {
        if (actionFilter.test(action)) {
            TestCase.Builder builder = TestCase.Builder.builder();
            DatastoreTestTrace.FirestoreV1Action firestoreAction = action.getFirestoreV1Action();
            List<Collection> collections = convertDatabaseContents(firestoreAction);
            StructuredQuery queryProto = firestoreAction.getRunQuery().getRequest().getStructuredQuery();
            String parentPath = ResourcePath.fromString(firestoreAction.getRunQuery().getRequest().getParent()).popFirst(5).toString();
            Query query = convertQuery(queryProto, parentPath);
            // Add query collection to contents if there are no pre-existing documents.
            if (collections.isEmpty()) {
                collections.add(Collection.Builder.builder().setName(query.getCollection()).build());
            }
            builder.setCollections(collections);
            builder.setQuery(query);
            // Convert results.
            builder.setException(firestoreAction.hasStatus());
            if (!firestoreAction.hasStatus()) {
                Result.Builder result = Result.Builder.builder();
                List<RunQueryResponse> responses = firestoreAction.getRunQuery().getResponseList();
                result.setDocuments(responses.stream().filter(r -> r.hasDocument()).map(r -> r.getDocument()).collect(Collectors.toList()));
                builder.setResult(result.build());
            }
            builder.setName(String.format("%s_%d", trace.getTraceId(), action.getActionId()));
            testCases.add(builder.build());
        }
    }
    return testCases;
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) StructuredQuery(com.google.firestore.v1.StructuredQuery) DatastoreTestTrace(com.google.apphosting.datastore.testing.DatastoreTestTrace) Document(com.google.firestore.v1.Document) Where(com.google.firebase.firestore.conformance.model.Where) ResourcePath(com.google.firebase.firestore.model.ResourcePath) QueryFilter(com.google.firebase.firestore.conformance.model.QueryFilter) Collectors(java.util.stream.Collectors) Order(com.google.firebase.firestore.conformance.model.Order) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Value(com.google.firestore.v1.Value) Result(com.google.firebase.firestore.conformance.model.Result) Cursor(com.google.firestore.v1.Cursor) Query(com.google.firebase.firestore.conformance.model.Query) Collection(com.google.firebase.firestore.conformance.model.Collection) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) DocumentKey(com.google.firebase.firestore.model.DocumentKey) TestCase(com.google.firebase.firestore.conformance.model.TestCase) StructuredQuery(com.google.firestore.v1.StructuredQuery) Query(com.google.firebase.firestore.conformance.model.Query) ArrayList(java.util.ArrayList) DatastoreTestTrace(com.google.apphosting.datastore.testing.DatastoreTestTrace) Result(com.google.firebase.firestore.conformance.model.Result) TestCase(com.google.firebase.firestore.conformance.model.TestCase) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) Collection(com.google.firebase.firestore.conformance.model.Collection)

Example 3 with Collection

use of com.google.firebase.firestore.conformance.model.Collection in project firebase-android-sdk by firebase.

the class TestCaseConverter method convertDatabaseContents.

private List<Collection> convertDatabaseContents(DatastoreTestTrace.FirestoreV1Action action) {
    LinkedHashMap<String, List<Document>> collections = new LinkedHashMap<>();
    List<RunQueryResponse> responses = action.getDatabaseContentsBeforeAction().getResponseList();
    for (RunQueryResponse response : responses) {
        if (response.hasDocument()) {
            Document document = response.getDocument();
            DocumentKey documentKey = DocumentKey.fromName(document.getName());
            String collectionId = documentKey.getCollectionGroup();
            List<Document> documents = collections.computeIfAbsent(collectionId, name -> new ArrayList<>());
            documents.add(document);
        }
    }
    return collections.entrySet().stream().map(entry -> Collection.Builder.builder().setName(entry.getKey()).setDocuments(entry.getValue()).build()).collect(Collectors.toList());
}
Also used : StructuredQuery(com.google.firestore.v1.StructuredQuery) DatastoreTestTrace(com.google.apphosting.datastore.testing.DatastoreTestTrace) Document(com.google.firestore.v1.Document) Where(com.google.firebase.firestore.conformance.model.Where) ResourcePath(com.google.firebase.firestore.model.ResourcePath) QueryFilter(com.google.firebase.firestore.conformance.model.QueryFilter) Collectors(java.util.stream.Collectors) Order(com.google.firebase.firestore.conformance.model.Order) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Value(com.google.firestore.v1.Value) Result(com.google.firebase.firestore.conformance.model.Result) Cursor(com.google.firestore.v1.Cursor) Query(com.google.firebase.firestore.conformance.model.Query) Collection(com.google.firebase.firestore.conformance.model.Collection) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) DocumentKey(com.google.firebase.firestore.model.DocumentKey) TestCase(com.google.firebase.firestore.conformance.model.TestCase) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) List(java.util.List) Document(com.google.firestore.v1.Document) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Collection (com.google.firebase.firestore.conformance.model.Collection)3 Order (com.google.firebase.firestore.conformance.model.Order)3 QueryFilter (com.google.firebase.firestore.conformance.model.QueryFilter)3 Result (com.google.firebase.firestore.conformance.model.Result)3 Where (com.google.firebase.firestore.conformance.model.Where)3 Document (com.google.firestore.v1.Document)3 StructuredQuery (com.google.firestore.v1.StructuredQuery)3 Value (com.google.firestore.v1.Value)3 DatastoreTestTrace (com.google.apphosting.datastore.testing.DatastoreTestTrace)2 Query (com.google.firebase.firestore.conformance.model.Query)2 TestCase (com.google.firebase.firestore.conformance.model.TestCase)2 DocumentKey (com.google.firebase.firestore.model.DocumentKey)2 ResourcePath (com.google.firebase.firestore.model.ResourcePath)2 Cursor (com.google.firestore.v1.Cursor)2 RunQueryResponse (com.google.firestore.v1.RunQueryResponse)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 TestCollection (com.google.firebase.firestore.conformance.TestCollection)1