예제 #1
0
    def test_it_returns_in_the_same_order_as_the_commands(self, db_session, commands):
        # Insert the values to set db order, then update them in reverse
        action = UserUpsertAction(db_session)
        reports = action.execute(commands)
        reversed_reports = action.execute(list(reversed(deepcopy(commands))))

        ids = [report.id for report in reports]
        reversed_ids = [report.id for report in reversed_reports]

        assert reversed_ids == list(reversed(ids))
예제 #2
0
    def test_other_db_errors_are_raised_directly(self, db_session, programming_error):
        action = UserUpsertAction(db_session)

        fake_execute_result = Mock(spec_set=["fetchall"])
        fake_execute_result.fetchall.return_value = [["id", "authority", "username"]]

        with patch.object(action, "_execute_statement") as _execute_statement:
            # We need the second call to fail during _upsert_identities
            _execute_statement.side_effect = [fake_execute_result, programming_error]

            with pytest.raises(ProgrammingError):
                action.execute([upsert_user_command(0)])
예제 #3
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)
예제 #4
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)
예제 #5
0
    def test_if_tails_with_duplicate_identities(self, db_session):
        command_1 = upsert_user_command(1)
        command_2 = upsert_user_command(2)
        commands = [command_1, command_2]

        command_2.body.attributes["identities"] = command_1.body.attributes[
            "identities"]

        with pytest.raises(ConflictingDataError):
            UserUpsertAction(db_session).execute(commands)
예제 #6
0
    def __init__(self, db, authority="wwnorton.com"):
        """
        :param db: DB session object
        :param authority: Restrict all request to this authority
        """
        self.db = db
        self.authority = authority

        self.handlers = {
            (CommandType.UPSERT, DataType.USER): UserUpsertAction(self.db),
            (CommandType.UPSERT, DataType.GROUP): GroupUpsertAction(self.db),
            (
                CommandType.CREATE,
                DataType.GROUP_MEMBERSHIP,
            ): GroupMembershipCreateAction(self.db),
        }

        self.effective_user_id = None
예제 #7
0
    def test_it_fails_with_mismatched_queries(self, db_session, field):
        command = upsert_user_command(**{field: "value"})
        command.body.query[field] = "DIFFERENT"

        with pytest.raises(UnsupportedOperationError):
            UserUpsertAction(db_session).execute([command])
예제 #8
0
    def test_if_fails_with_unsupported_queries(self, db_session):
        command = upsert_user_command()
        command.body.query["something_new"] = "foo"

        with pytest.raises(UnsupportedOperationError):
            UserUpsertAction(db_session).execute([command])