コード例 #1
0
    def test_if_fails_with_unsupported_queries(self, db_session, user):
        command = group_upsert_command()
        command.body.query["something_new"] = "foo"

        with pytest.raises(UnsupportedOperationError):
            GroupUpsertAction(db_session).execute([command],
                                                  effective_user_id=user.id)
コード例 #2
0
ファイル: _actions_test.py プロジェクト: bibliotechie/h
    def test_it_fails_with_duplicate_groups(self, db_session, user):
        command = group_upsert_command(0)

        with pytest.raises(ConflictingDataError):
            GroupUpsertAction(db_session).execute(
                [command, command], effective_user_id=user.id
            )
コード例 #3
0
    def test_it_fails_with_mismatched_queries(self, db_session, field, user):
        command = group_upsert_command(**{field: "value"})
        command.body.query[field] = "DIFFERENT"

        with pytest.raises(UnsupportedOperationError):
            GroupUpsertAction(db_session).execute([command],
                                                  effective_user_id=user.id)
コード例 #4
0
ファイル: _actions_test.py プロジェクト: bibliotechie/h
    def test_other_db_errors_are_raised_directly(
        self, db_session, user, programming_error
    ):
        action = GroupUpsertAction(db_session)
        with patch.object(action, "_execute_statement") as _execute_statement:
            _execute_statement.side_effect = programming_error

            with pytest.raises(ProgrammingError):
                action.execute([group_upsert_command(0)], effective_user_id=user.id)
コード例 #5
0
ファイル: _actions_test.py プロジェクト: bibliotechie/h
    def test_it_can_update_records(self, db_session, commands, user):
        update_commands = [
            group_upsert_command(i, name=f"changed_{i}") for i in range(3)
        ]

        GroupUpsertAction(db_session).execute(commands, effective_user_id=user.id)
        reports = GroupUpsertAction(db_session).execute(
            update_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, update_commands)
        self.assert_groups_are_private_and_owned_by_user(db_session, user)
コード例 #6
0
ファイル: _actions_test.py プロジェクト: bibliotechie/h
 def commands(self):
     return [group_upsert_command(i) for i in range(3)]
コード例 #7
0
ファイル: _executor_test.py プロジェクト: zhujinlong/h
class TestDBExecutor:
    @pytest.mark.parametrize(
        "command_type,data_type,handler",
        (
            (CommandType.UPSERT, DataType.USER, "UserUpsertAction"),
            (CommandType.UPSERT, DataType.GROUP, "GroupUpsertAction"),
            (
                CommandType.CREATE,
                DataType.GROUP_MEMBERSHIP,
                "GroupMembershipCreateAction",
            ),
        ),
        indirect=["handler"],
    )
    def test_it_calls_correct_db_handler(self, db_session, command_type,
                                         data_type, handler, commands):
        executor = BulkExecutor(db_session)
        handler.assert_called_once_with(db_session)

        executor.effective_user_id = 1

        # These commands aren't actually the right type, but it doesn't
        # matter for this test
        executor.execute_batch(
            command_type,
            data_type,
            {"config_option": 2},
            commands,
        )

        bulk_upsert = handler.return_value
        bulk_upsert.execute.assert_called_once_with(commands,
                                                    effective_user_id=1,
                                                    config_option=2)

    @pytest.mark.parametrize(
        "command_type,data_type",
        (
            (CommandType.CREATE, DataType.USER),
            (CommandType.CREATE, DataType.GROUP),
            (CommandType.UPSERT, DataType.GROUP_MEMBERSHIP),
        ),
    )
    def test_it_raises_UnsupportedOperationError_for_invalid_actions(
            self, db_session, command_type, data_type, commands):
        executor = BulkExecutor(db_session)

        with pytest.raises(UnsupportedOperationError):
            executor.execute_batch(command_type, data_type, commands, {})

    def test_it_raises_InvalidDeclarationError_with_non_lms_authority(self):
        config = Configuration.create(
            effective_user="******", total_instructions=2)

        with pytest.raises(InvalidDeclarationError):
            BulkExecutor(sentinel.db).configure(config)

    @pytest.mark.parametrize(
        "command",
        (
            param(
                upsert_user_command(authority="bad"),
                id="User: bad authority in attributes",
            ),
            param(
                upsert_user_command(query_authority="bad"),
                id="User: bad authority in query",
            ),
            param(
                group_upsert_command(authority="bad"),
                id="Group: bad authority in attributes",
            ),
            param(
                group_upsert_command(query_authority="bad"),
                id="User: bad authority in query",
            ),
        ),
    )
    def test_it_raises_InvalidDeclarationError_with_called_with_non_lms_authority(
            self, command):
        with pytest.raises(InvalidDeclarationError):
            BulkExecutor(sentinel.db).execute_batch(command.type,
                                                    command.body.type, {},
                                                    [command])

    def test_configure_looks_up_the_effective_user(self, db_session, user):
        executor = BulkExecutor(db_session)

        assert executor.effective_user_id is None

        executor.configure(
            Configuration.create(effective_user=user.userid,
                                 total_instructions=2))

        assert executor.effective_user_id == user.id

    def test_configure_raises_if_the_effective_user_does_not_exist(
            self, db_session):
        with pytest.raises(InvalidDeclarationError):
            BulkExecutor(db_session).configure(
                Configuration.create(
                    effective_user="******",
                    total_instructions=2))

    @pytest.fixture
    def handler(self, patch, request):
        return patch(f"h.services.bulk_executor._executor.{request.param}")

    @pytest.fixture
    def commands(self):
        return [group_upsert_command()]
コード例 #8
0
ファイル: _executor_test.py プロジェクト: zhujinlong/h
 def commands(self):
     return [group_upsert_command()]
コード例 #9
0
 def commands(self):
     return [group_upsert_command(authority=AUTHORITY)]