Exemple #1
0
    def test_add_where(self, queue, factories, db_session, now):
        matching = [
            factories.Annotation(shared=True),
            factories.Annotation(shared=True),
        ]
        # Add some noise
        factories.Annotation(shared=False)

        queue.add_where(
            where=[Annotation.shared.is_(True)],
            tag="test_tag",
            priority=1234,
            schedule_in=ONE_WEEK_IN_SECONDS,
        )

        assert db_session.query(Job).all() == [
            Any.instance_of(Job).with_attrs(
                dict(
                    enqueued_at=Any.instance_of(datetime_.datetime),
                    scheduled_at=now + ONE_WEEK,
                    tag="test_tag",
                    priority=1234,
                    kwargs={
                        "annotation_id": self.database_id(annotation),
                        "force": False,
                    },
                )) for annotation in matching
        ]
Exemple #2
0
    def test_command_parsing_ok(self, executor, ndjson_bytes):
        """Sanity test that hits most elements of parsing."""

        BulkAPI.from_byte_stream(ndjson_bytes,
                                 executor=executor,
                                 observer=Observer())

        executor.configure.assert_called_with(
            config=Any.instance_of(Configuration))

        executor.execute_batch.assert_has_calls([
            call(
                command_type=CommandType.UPSERT,
                data_type=DataType.USER,
                batch=[Any.instance_of(DataCommand)],
                default_config={},
            ),
            call(
                command_type=CommandType.UPSERT,
                data_type=DataType.GROUP,
                batch=[Any.instance_of(DataCommand)],
                default_config={},
            ),
            call(
                command_type=CommandType.CREATE,
                data_type=DataType.GROUP_MEMBERSHIP,
                batch=[Any.instance_of(DataCommand)],
                default_config={"on_duplicate": "continue"},
            ),
        ])
Exemple #3
0
    def test_it(self, userid, users_annotations, db_session, queue, force,
                schedule_in):
        queue.add_users_annotations(userid,
                                    tag="test_tag",
                                    force=force,
                                    schedule_in=schedule_in)

        assert (db_session.query(Job).all() == Any.list.containing([
            Any.instance_of(Job).with_attrs({
                "tag":
                "test_tag",
                "name":
                "sync_annotation",
                "scheduled_at":
                Any.instance_of(datetime_.datetime),
                "priority":
                100,
                "kwargs": {
                    "annotation_id":
                    str(uuid.UUID(URLSafeUUID.url_safe_to_hex(annotation.id))),
                    "force":
                    force,
                },
            }) for annotation in users_annotations
        ]).only())
Exemple #4
0
    def test_it(self, annotation_ids, db_session, queue, force):
        queue.add_annotations_between_times(
            datetime_.datetime(2020, 9, 9),
            datetime_.datetime(2020, 9, 11),
            "test_tag",
            force,
        )

        assert (db_session.query(Job).all() == Any.list.containing([
            Any.instance_of(Job).with_attrs({
                "tag":
                "test_tag",
                "name":
                "sync_annotation",
                "scheduled_at":
                Any.instance_of(datetime_.datetime),
                "priority":
                1000,
                "kwargs": {
                    "annotation_id":
                    str(uuid.UUID(URLSafeUUID.url_safe_to_hex(annotation_id))),
                    "force":
                    force,
                },
            }) for annotation_id in annotation_ids
        ]).only())
Exemple #5
0
    def test_it(self, db_session, queue, now):
        queue.add("test_annotation_id", "test_tag", schedule_in=ONE_WEEK)

        assert db_session.query(Job).all() == [
            Any.instance_of(Job).with_attrs(
                dict(
                    enqueued_at=Any.instance_of(datetime_.datetime),
                    scheduled_at=now + ONE_WEEK,
                    tag="test_tag",
                    priority=1,
                    kwargs={"annotation_id": "test_annotation_id"},
                )),
        ]
Exemple #6
0
    def test_it_can_insert_new_records(self, db_session, commands):
        reports = GroupMembershipCreateAction(db_session).execute(commands)

        assert reports == Any.iterable.comprised_of(
            Any.instance_of(Report)).of_size(3)

        self.assert_membership_matches_commands(db_session, commands)
    def test_we_call_third_parties_correctly(self, call_route_by_content,
                                             get_url_details):
        url = "http://example.com/path%2C?a=b"
        call_route_by_content(url, params={"other": "value"})

        get_url_details.assert_called_once_with(
            url, Any.instance_of(EnvironHeaders))
Exemple #8
0
    def _assert_process_called_with_generator_of_commands(
            self, CommandProcessor):
        (generator, ), _ = CommandProcessor.return_value.process.call_args

        assert isinstance(generator, GeneratorType)
        assert generator == Any.iterable.comprised_of(
            Any.instance_of(Command)).of_size(4)
Exemple #9
0
    def test_it_calls_bulk_api_correctly(self, pyramid_request, BulkAPI):
        bulk(pyramid_request)

        BulkAPI.from_byte_stream.assert_called_once_with(
            pyramid_request.body_file,
            executor=Any.instance_of(AuthorityCheckingExecutor),
        )
Exemple #10
0
    def test_it_can_insert_new_records(self, db_session, commands):
        reports = UserUpsertAction(db_session).execute(commands)

        assert reports == Any.iterable.comprised_of(
            Any.instance_of(Report)).of_size(3)

        self.assert_users_match_commands(db_session, commands)
    def test_it_can_match_objects_with_equals(self):
        class NeverMatches:
            def __eq__(self, other):  # pragma: no cover
                return False

        matcher = HostClass.comprised_of(Any.instance_of(NeverMatches))

        assert matcher == [NeverMatches()]
Exemple #12
0
    def test_it_can_insert_new_records(self, db_session, commands, user):
        reports = GroupUpsertAction(db_session).execute(
            commands, effective_user_id=user.id)

        assert reports == Any.iterable.comprised_of(
            Any.instance_of(Report)).of_size(3)

        self.assert_groups_match_commands(db_session, commands)
    def test_combo(self):
        matcher = (Any().list().containing([
            Any(),
            Any.instance_of(ValueError),
            Any().dict().containing({"a": Any().iterable().containing([2])}),
        ]).of_size(at_least=2))

        assert matcher == [{"a": range(4), "b": None}, None, ValueError()]
    def test_it_initializes_sentry_sdk(self, pyramid_config, sentry_sdk):
        includeme(pyramid_config)

        sentry_sdk.init.assert_called_once_with(
            integrations=[Any.instance_of(PyramidIntegration)],
            send_default_pii=True,
            before_send=Any.function(),
        )
Exemple #15
0
    def test_from_string(self, nd_json, executor, CommandProcessor):
        BulkAPI.from_string(nd_json, executor)

        CommandProcessor.assert_called_once_with(
            executor=executor, observer=Any.instance_of(Observer))

        self._assert_process_called_with_generator_of_commands(
            CommandProcessor)
Exemple #16
0
    def test_execute_batch_returns_an_appropriate_type(self, command_type):
        results = AutomaticReportExecutor().execute_batch(
            command_type,
            sentinel.data_type,
            sentinel.config,
            batch=[sentinel.command])

        assert results == [Any.instance_of(Report)]
Exemple #17
0
    def test_from_models(self, factories):
        auth_client = factories.AuthClient.build()

        model = LongLivedAuthClient.from_model(auth_client)

        assert model == Any.instance_of(LongLivedAuthClient).with_attrs(
            {"id": auth_client.id, "authority": auth_client.authority}
        )
Exemple #18
0
    def test_from_models(self, factories):
        group = factories.Group.build()

        model = LongLivedGroup.from_model(group)

        assert model == Any.instance_of(LongLivedGroup).with_attrs(
            {"id": group.id, "pubid": group.pubid}
        )
Exemple #19
0
    def test_reports_are_stored_if_view_is_not_None(self, command_processor,
                                                    commands, config_command):
        config_command.body.raw["view"] = "to_be_decided"

        command_processor.process(commands)

        assert command_processor.reports == Any.dict.containing(
            {DataType.USER: [Any.instance_of(Report)]})
Exemple #20
0
    def test_it_can_continue_with_existing_records(self, db_session, commands):
        GroupMembershipCreateAction(db_session).execute(commands)
        reports = GroupMembershipCreateAction(db_session).execute(
            commands, on_duplicate="continue"
        )

        assert reports == Any.iterable.comprised_of(Any.instance_of(Report)).of_size(3)

        self.assert_membership_matches_commands(db_session, commands)
Exemple #21
0
    def test_from_input(self, input_data, executor, bulk_method,
                        CommandProcessor):
        result = bulk_method(input_data, executor)

        CommandProcessor.assert_called_once_with(
            executor=executor, observer=Any.instance_of(Observer))
        assert result == sentinel.reports
        self._assert_process_called_with_generator_of_commands(
            CommandProcessor)
Exemple #22
0
    def test_from_models_with_None(self, LongLivedUser, LongLivedAuthClient):
        identity = Identity.from_models()

        LongLivedUser.from_model.assert_not_called()
        LongLivedAuthClient.from_model.assert_not_called()

        assert identity == Any.instance_of(Identity).with_attrs(
            {"user": None, "auth_client": None}
        )
Exemple #23
0
    def test_passes_args_including_renderer_to_form_ctor(
            self, Form, pyramid_request):
        form.create_form(pyramid_request, mock.sentinel.schema, foo="bar")

        Form.assert_called_once_with(
            mock.sentinel.schema,
            foo="bar",
            renderer=Any.instance_of(form.Jinja2Renderer),
        )
Exemple #24
0
    def test_it_raises_if_write_permission_is_missing(self, pyramid_request,
                                                      annotation_data,
                                                      has_permission):
        has_permission.return_value = False

        with pytest.raises(ValidationError):
            storage.create_annotation(pyramid_request, annotation_data)

        has_permission.assert_called_once_with(
            Permission.Group.WRITE, context=Any.instance_of(GroupContext))
Exemple #25
0
    def test_it_raises_if_the_response_is_an_error(self, svc, url, status):
        httpretty.register_uri("GET", url, status=status)

        with pytest.raises(UnhandledUpstreamException) as exc_info:
            svc.request("GET", url)

        assert isinstance(exc_info.value.__cause__, requests.HTTPError)
        assert exc_info.value.response == Any.instance_of(requests.Response).with_attrs(
            {"status_code": status}
        )
Exemple #26
0
    def test_it_doesnt_raise_if_the_response_is_an_error_when_disabled(
        self, svc, url, status
    ):
        httpretty.register_uri("GET", url, status=status)

        response = svc.request("GET", url, raise_for_status=False)

        assert response == Any.instance_of(requests.Response).with_attrs(
            {"status_code": status}
        )
Exemple #27
0
    def test_it(self, db_session, factories, queue, now):
        annotation = factories.Annotation.build()

        queue.add(annotation.id, "test_tag", schedule_in=ONE_WEEK_IN_SECONDS)

        assert db_session.query(Job).all() == [
            Any.instance_of(Job).with_attrs(
                dict(
                    enqueued_at=Any.instance_of(datetime_.datetime),
                    scheduled_at=now + ONE_WEEK,
                    tag="test_tag",
                    priority=1,
                    kwargs={
                        "annotation_id":
                        URLSafeUUID.url_safe_to_hex(annotation.id),
                        "force": False,
                    },
                )),
        ]
Exemple #28
0
    def test_execute_batch_generates_fake_ids(self):
        results = AutomaticReportExecutor().execute_batch(
            sentinel.command_type,
            sentinel.data_type,
            sentinel.config,
            batch=[sentinel.command, sentinel.command, sentinel.command],
        )

        assert results == Any.list.comprised_of(
            Any.instance_of(Report)).of_size(3)
        assert len({report.id for report in results}) == 3
Exemple #29
0
    def test_it_can_update_records(self, db_session, commands):
        update_commands = [
            upsert_user_command(i, display_name=f"changed_{i}") for i in range(3)
        ]

        action = UserUpsertAction(db_session)
        action.execute(commands)
        reports = action.execute(update_commands)

        assert reports == Any.iterable.comprised_of(Any.instance_of(Report)).of_size(3)

        self.assert_users_match_commands(db_session, update_commands)
Exemple #30
0
    def test_from_models(self, LongLivedUser, LongLivedAuthClient):
        identity = Identity.from_models(
            user=sentinel.user, auth_client=sentinel.auth_client
        )

        LongLivedUser.from_model.assert_called_once_with(sentinel.user)
        LongLivedAuthClient.from_model.assert_called_once_with(sentinel.auth_client)
        assert identity == Any.instance_of(Identity).with_attrs(
            {
                "user": LongLivedUser.from_model.return_value,
                "auth_client": LongLivedAuthClient.from_model.return_value,
            }
        )