Пример #1
0
def test_listen_testprotos(test_proto):  # pragma: NO COVER
    # test_proto.listen has 'reponses' messages,
    # 'google.firestore_v1.ListenResponse'
    # and then an expected list of 'snapshots' (local 'Snapshot'), containing
    # 'docs' (list of 'google.firestore_v1.Document'),
    # 'changes' (list lof local 'DocChange', and 'read_time' timestamp.
    from google.cloud.firestore_v1 import Client
    from google.cloud.firestore_v1 import DocumentReference
    from google.cloud.firestore_v1 import DocumentSnapshot
    from google.cloud.firestore_v1 import Watch
    import google.auth.credentials

    testcase = test_proto.listen
    testname = test_proto.description

    credentials = mock.Mock(spec=google.auth.credentials.Credentials)
    client = Client(project="project", credentials=credentials)
    modulename = "google.cloud.firestore_v1.watch"
    with mock.patch("%s.Watch.ResumableBidiRpc" % modulename, DummyRpc):
        with mock.patch("%s.Watch.BackgroundConsumer" % modulename,
                        DummyBackgroundConsumer):
            with mock.patch(  # conformance data sets WATCH_TARGET_ID to 1
                    "%s.WATCH_TARGET_ID" % modulename, 1):
                snapshots = []

                def callback(keys, applied_changes, read_time):
                    snapshots.append((keys, applied_changes, read_time))

                collection = DummyCollection(client=client)
                query = DummyQuery(parent=collection)
                watch = Watch.for_query(query, callback, DocumentSnapshot,
                                        DocumentReference)
                # conformance data has db string as this
                db_str = "projects/projectID/databases/(default)"
                watch._firestore._database_string_internal = db_str

                wrapped_responses = [
                    firestore.ListenResponse.wrap(proto)
                    for proto in testcase.responses
                ]
                if testcase.is_error:
                    try:
                        for proto in wrapped_responses:
                            watch.on_snapshot(proto)
                    except RuntimeError:
                        # listen-target-add-wrong-id.textpro
                        # listen-target-remove.textpro
                        pass

                else:
                    for proto in wrapped_responses:
                        watch.on_snapshot(proto)

                    assert len(snapshots) == len(testcase.snapshots)
                    for i, (expected_snapshot, actual_snapshot) in enumerate(
                            zip(testcase.snapshots, snapshots)):
                        expected_changes = expected_snapshot.changes
                        actual_changes = actual_snapshot[1]
                        if len(expected_changes) != len(actual_changes):
                            raise AssertionError(
                                "change length mismatch in %s (snapshot #%s)" %
                                (testname, i))
                        for y, (expected_change, actual_change) in enumerate(
                                zip(expected_changes, actual_changes)):
                            expected_change_kind = expected_change.kind
                            actual_change_kind = actual_change.type.value
                            if expected_change_kind != actual_change_kind:
                                raise AssertionError(
                                    "change type mismatch in %s (snapshot #%s, change #%s')"
                                    % (testname, i, y))
Пример #2
0
def test_listen_testprotos(test_proto):  # pragma: NO COVER
    # test_proto.listen has 'reponses' messages,
    # 'google.firestore_v1.ListenResponse'
    # and then an expected list of 'snapshots' (local 'Snapshot'), containing
    # 'docs' (list of 'google.firestore_v1.Document'),
    # 'changes' (list lof local 'DocChange', and 'read_time' timestamp.
    from google.cloud.firestore_v1 import Client
    from google.cloud.firestore_v1 import DocumentReference
    from google.cloud.firestore_v1 import DocumentSnapshot
    from google.cloud.firestore_v1 import Watch
    import google.auth.credentials

    testcase = test_proto.listen
    testname = test_proto.description

    credentials = mock.Mock(spec=google.auth.credentials.Credentials)
    client = Client(project="project", credentials=credentials)
    modulename = "google.cloud.firestore_v1.watch"
    with mock.patch("%s.Watch.ResumableBidiRpc" % modulename, DummyRpc):
        with mock.patch(
            "%s.Watch.BackgroundConsumer" % modulename, DummyBackgroundConsumer
        ):
            with mock.patch(  # conformance data sets WATCH_TARGET_ID to 1
                "%s.WATCH_TARGET_ID" % modulename, 1
            ):
                snapshots = []

                def callback(keys, applied_changes, read_time):
                    snapshots.append((keys, applied_changes, read_time))

                query = DummyQuery(client=client)
                watch = Watch.for_query(
                    query, callback, DocumentSnapshot, DocumentReference
                )
                # conformance data has db string as this
                db_str = "projects/projectID/databases/(default)"
                watch._firestore._database_string_internal = db_str

                if testcase.is_error:
                    try:
                        for proto in testcase.responses:
                            watch.on_snapshot(proto)
                    except RuntimeError:
                        # listen-target-add-wrong-id.textpro
                        # listen-target-remove.textpro
                        pass

                else:
                    for proto in testcase.responses:
                        watch.on_snapshot(proto)

                    assert len(snapshots) == len(testcase.snapshots)
                    for i, (expected_snapshot, actual_snapshot) in enumerate(
                        zip(testcase.snapshots, snapshots)
                    ):
                        expected_changes = expected_snapshot.changes
                        actual_changes = actual_snapshot[1]
                        if len(expected_changes) != len(actual_changes):
                            raise AssertionError(
                                "change length mismatch in %s (snapshot #%s)"
                                % (testname, i)
                            )
                        for y, (expected_change, actual_change) in enumerate(
                            zip(expected_changes, actual_changes)
                        ):
                            expected_change_kind = expected_change.kind
                            actual_change_kind = actual_change.type.value
                            if expected_change_kind != actual_change_kind:
                                raise AssertionError(
                                    "change type mismatch in %s (snapshot #%s, change #%s')"
                                    % (testname, i, y)
                                )