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)
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 )
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)
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)
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_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)
def test_it_returns_in_the_same_order_as_the_commands( self, db_session, commands, user): # Insert the values to set db order, then update them in reverse action = GroupUpsertAction(db_session) reports = action.execute(commands, effective_user_id=user.id) reversed_reports = action.execute(list(reversed(deepcopy(commands))), effective_user_id=user.id) ids = [report.id for report in reports] reversed_ids = [report.id for report in reversed_reports] assert reversed_ids == list(reversed(ids))
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
def test_it_fails_with_no_effective_user(self, db_session): with pytest.raises(CommandSequenceError): GroupUpsertAction(db_session).execute( sentinel.batch, effective_user_id=None )