Created At: 2026-11-18T12:50:26Z Completed At: 2026-11-18T12:50:32Z Command exited with code 1. Command output: ============================= test session starts ============================== platform linux -- Python 3.12.3, pytest-9.2.0, pluggy-1.7.0 rootdir: /root/telegram-saas configfile: pyproject.toml plugins: anyio-4.16.0, asyncio-1.5.0 asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function collecting ... collected 5 items tests/unit/test_bot_modular_engine.py .FFFF [100%] =================================== FAILURES =================================== __________________________ test_bot_module_lifecycle ___________________________ self = operation = 'INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)' parameters = (UUID('2c5ead4e-ee4c-4138-81a2-0478caf15178'), 'Modular Test Bot', 'modular_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 30, 516133, tzinfo=datetime.timezone.utc), ...) async def _prepare_and_execute(self, operation, parameters): adapt_connection = self._adapt_connection async with adapt_connection._execute_mutex: if adapt_connection._transaction is None: await adapt_connection._start_transaction() if parameters is None: parameters = () try: prepared_stmt, attributes = await adapt_connection._prepare( operation, self._invalidate_schema_cache_asof ) if attributes: self._description = [ ( attr.name, attr.type.oid, None, None, None, None, None, ) for attr in attributes ] else: self._description = None if self.server_side: self._cursor = await prepared_stmt.cursor(*parameters) self._rowcount = -1 else: > self._rows = deque(await prepared_stmt.fetch(*parameters)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:620: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:177: in fetch data = await self.__bind_execute(args, 0, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:268: in __bind_execute data, status, _ = await self.__do_execute( .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:257: in __do_execute return await executor(protocol) ^^^^^^^^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > ??? E asyncpg.exceptions.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E DETAIL: Key (tenant_id)=(269c1da3-d707-4932-b307-fe95c3a9ba97) is not present in table "tenants". asyncpg/protocol/protocol.pyx:197: ForeignKeyViolationError The above exception was the direct cause of the following exception: self = dialect = context = statement = parameters = [(UUID('2c5ead4e-ee4c-4138-81a2-0478caf15178'), 'Modular Test Bot', 'modular_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 30, 516133, tzinfo=datetime.timezone.utc), ...)] def _exec_single_context( self, dialect: Dialect, context: ExecutionContext, statement: Union[str, Compiled], parameters: Optional[_AnyMultiExecuteParams], ) -> CursorResult[Unpack[TupleAny]]: """continue the _execute_context() method for a single DBAPI cursor.execute() or cursor.executemany() call. """ if dialect.bind_typing is BindTyping.SETINPUTSIZES: generic_setinputsizes = context._prepare_set_input_sizes() if generic_setinputsizes: try: dialect.do_set_input_sizes( context.cursor, generic_setinputsizes, context ) except BaseException as e: self._handle_dbapi_exception( e, str(statement), parameters, None, context ) cursor, str_statement, parameters = ( context.cursor, context.statement, context.parameters, ) effective_parameters: Optional[_AnyExecuteParams] if not context.executemany: effective_parameters = parameters[0] else: effective_parameters = parameters evt_handled: bool = False try: if self._has_events or self.engine._has_events: for fn in self.dispatch.before_cursor_execute: str_statement, effective_parameters = fn( self, cursor, str_statement, effective_parameters, context, context.executemany, ) if self._echo: self._log_info(str_statement) stats = context._get_cache_stats() self._log_parameters( effective_parameters, stats, ismulti=context.executemany ) if context.execute_style is ExecuteStyle.EXECUTEMANY: effective_parameters = cast( "_CoreMultiExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_executemany: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: self.dialect.do_executemany( cursor, str_statement, effective_parameters, context, ) elif not effective_parameters and context.no_parameters: if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute_no_params: if fn(cursor, str_statement, context): evt_handled = True break if not evt_handled: self.dialect.do_execute_no_params( cursor, str_statement, context ) else: effective_parameters = cast( "_CoreSingleExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: > self.dialect.do_execute( cursor, str_statement, effective_parameters, context ) .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: ForeignKeyViolationError The above exception was the direct cause of the following exception: db_session = @pytest.mark.asyncio async def test_bot_module_lifecycle(db_session: AsyncSession): """Tests install, list, toggle on/off, update config, and uninstall of bot modules.""" tenant_id = uuid.uuid4() bot_id = uuid.uuid4() # Create dummy bot bot = Bot( id=bot_id, tenant_id=tenant_id, name="Modular Test Bot", username="modular_test_bot", status="active", default_locale="en", ) db_session.add(bot) > await db_session.commit() tests/unit/test_bot_modular_engine.py:49: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/session.py:1309: in commit await greenlet_spawn(self.sync_session.commit) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:216: in greenlet_spawn result = context.switch(value) ^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2177: in commit trans.commit(_to_root=True) :2: in commit return target(fn, self, _to_root=_to_root) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1412: in commit self._prepare_impl() :2: in _prepare_impl return target(fn, self) ^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1386: in _prepare_impl self.session.flush() .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4626: in flush self._flush(objects) :2: in _flush return target(fn, self, objects=objects) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py:281: in warned return fn(*args, **kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4769: in _flush with util.safe_reraise(): .venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py:166: in __exit__ raise exc_value.with_traceback(exc_tb) .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4730: in _flush flush_context.execute() .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:467: in execute rec.execute(self) .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:643: in execute util.preloaded.orm_persistence._save_obj( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:92: in _save_obj _emit_insert_statements( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:1065: in _emit_insert_statements result = connection.execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1494: in execute return meth( .venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py:534: in _execute_on_connection return connection._execute_clauseelement( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1714: in _execute_clauseelement ret = self._execute_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1868: in _execute_context return self._exec_single_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1996: in _exec_single_context self._handle_dbapi_exception( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:2351: in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: in _exec_single_context self.dialect.do_execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.exc.IntegrityError: (sqlalchemy.dialects.postgresql.asyncpg.ForeignKeyViolationError) insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E [SQL: INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)] E [parameters: (UUID('2c5ead4e-ee4c-4138-81a2-0478caf15178'), 'Modular Test Bot', 'modular_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 30, 516133, tzinfo=datetime.timezone.utc), datetime.datetime(2026, 11, 18, 12, 50, 30, 516136, tzinfo=datetime.timezone.utc), UUID('269c1da3-d707-4932-b307-fe95c3a9ba97'))] E (Background on this error at: https://sqlalche.me/e/21/gkpj) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: IntegrityError _______________________ test_force_join_gating_behavior ________________________ self = operation = 'INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)' parameters = (UUID('ed45aba0-beb1-465e-bb91-e1fd9e64a5b7'), 'Gate Bot', 'gate_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 36188, tzinfo=datetime.timezone.utc), ...) async def _prepare_and_execute(self, operation, parameters): adapt_connection = self._adapt_connection async with adapt_connection._execute_mutex: if adapt_connection._transaction is None: await adapt_connection._start_transaction() if parameters is None: parameters = () try: prepared_stmt, attributes = await adapt_connection._prepare( operation, self._invalidate_schema_cache_asof ) if attributes: self._description = [ ( attr.name, attr.type.oid, None, None, None, None, None, ) for attr in attributes ] else: self._description = None if self.server_side: self._cursor = await prepared_stmt.cursor(*parameters) self._rowcount = -1 else: > self._rows = deque(await prepared_stmt.fetch(*parameters)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:620: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:177: in fetch data = await self.__bind_execute(args, 0, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:268: in __bind_execute data, status, _ = await self.__do_execute( .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:257: in __do_execute return await executor(protocol) ^^^^^^^^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > ??? E asyncpg.exceptions.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E DETAIL: Key (tenant_id)=(91528bd0-ec3e-4bcc-b87f-52613e977582) is not present in table "tenants". asyncpg/protocol/protocol.pyx:197: ForeignKeyViolationError The above exception was the direct cause of the following exception: self = dialect = context = statement = parameters = [(UUID('ed45aba0-beb1-465e-bb91-e1fd9e64a5b7'), 'Gate Bot', 'gate_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 36188, tzinfo=datetime.timezone.utc), ...)] def _exec_single_context( self, dialect: Dialect, context: ExecutionContext, statement: Union[str, Compiled], parameters: Optional[_AnyMultiExecuteParams], ) -> CursorResult[Unpack[TupleAny]]: """continue the _execute_context() method for a single DBAPI cursor.execute() or cursor.executemany() call. """ if dialect.bind_typing is BindTyping.SETINPUTSIZES: generic_setinputsizes = context._prepare_set_input_sizes() if generic_setinputsizes: try: dialect.do_set_input_sizes( context.cursor, generic_setinputsizes, context ) except BaseException as e: self._handle_dbapi_exception( e, str(statement), parameters, None, context ) cursor, str_statement, parameters = ( context.cursor, context.statement, context.parameters, ) effective_parameters: Optional[_AnyExecuteParams] if not context.executemany: effective_parameters = parameters[0] else: effective_parameters = parameters evt_handled: bool = False try: if self._has_events or self.engine._has_events: for fn in self.dispatch.before_cursor_execute: str_statement, effective_parameters = fn( self, cursor, str_statement, effective_parameters, context, context.executemany, ) if self._echo: self._log_info(str_statement) stats = context._get_cache_stats() self._log_parameters( effective_parameters, stats, ismulti=context.executemany ) if context.execute_style is ExecuteStyle.EXECUTEMANY: effective_parameters = cast( "_CoreMultiExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_executemany: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: self.dialect.do_executemany( cursor, str_statement, effective_parameters, context, ) elif not effective_parameters and context.no_parameters: if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute_no_params: if fn(cursor, str_statement, context): evt_handled = True break if not evt_handled: self.dialect.do_execute_no_params( cursor, str_statement, context ) else: effective_parameters = cast( "_CoreSingleExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: > self.dialect.do_execute( cursor, str_statement, effective_parameters, context ) .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: ForeignKeyViolationError The above exception was the direct cause of the following exception: db_session = @pytest.mark.asyncio async def test_force_join_gating_behavior(db_session: AsyncSession): """Verifies that force_join gates non-members and passes active members.""" tenant_id = uuid.uuid4() bot_id = uuid.uuid4() bot = Bot( id=bot_id, tenant_id=tenant_id, name="Gate Bot", username="gate_bot", status="active", default_locale="en", ) db_session.add(bot) > await db_session.commit() tests/unit/test_bot_modular_engine.py:110: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/session.py:1309: in commit await greenlet_spawn(self.sync_session.commit) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:216: in greenlet_spawn result = context.switch(value) ^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2177: in commit trans.commit(_to_root=True) :2: in commit return target(fn, self, _to_root=_to_root) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1412: in commit self._prepare_impl() :2: in _prepare_impl return target(fn, self) ^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1386: in _prepare_impl self.session.flush() .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4626: in flush self._flush(objects) :2: in _flush return target(fn, self, objects=objects) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py:281: in warned return fn(*args, **kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4769: in _flush with util.safe_reraise(): .venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py:166: in __exit__ raise exc_value.with_traceback(exc_tb) .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4730: in _flush flush_context.execute() .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:467: in execute rec.execute(self) .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:643: in execute util.preloaded.orm_persistence._save_obj( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:92: in _save_obj _emit_insert_statements( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:1065: in _emit_insert_statements result = connection.execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1494: in execute return meth( .venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py:534: in _execute_on_connection return connection._execute_clauseelement( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1714: in _execute_clauseelement ret = self._execute_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1868: in _execute_context return self._exec_single_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1996: in _exec_single_context self._handle_dbapi_exception( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:2351: in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: in _exec_single_context self.dialect.do_execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.exc.IntegrityError: (sqlalchemy.dialects.postgresql.asyncpg.ForeignKeyViolationError) insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E [SQL: INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)] E [parameters: (UUID('ed45aba0-beb1-465e-bb91-e1fd9e64a5b7'), 'Gate Bot', 'gate_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 36188, tzinfo=datetime.timezone.utc), datetime.datetime(2026, 11, 18, 12, 50, 31, 36192, tzinfo=datetime.timezone.utc), UUID('91528bd0-ec3e-4bcc-b87f-52613e977582'))] E (Background on this error at: https://sqlalche.me/e/21/gkpj) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: IntegrityError ____________________ test_referrals_and_growth_attribution _____________________ self = operation = 'INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)' parameters = (UUID('8a8e6ea3-f262-4f57-9a7b-732c3a5074c3'), 'Referral Bot', 'referral_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 293379, tzinfo=datetime.timezone.utc), ...) async def _prepare_and_execute(self, operation, parameters): adapt_connection = self._adapt_connection async with adapt_connection._execute_mutex: if adapt_connection._transaction is None: await adapt_connection._start_transaction() if parameters is None: parameters = () try: prepared_stmt, attributes = await adapt_connection._prepare( operation, self._invalidate_schema_cache_asof ) if attributes: self._description = [ ( attr.name, attr.type.oid, None, None, None, None, None, ) for attr in attributes ] else: self._description = None if self.server_side: self._cursor = await prepared_stmt.cursor(*parameters) self._rowcount = -1 else: > self._rows = deque(await prepared_stmt.fetch(*parameters)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:620: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:177: in fetch data = await self.__bind_execute(args, 0, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:268: in __bind_execute data, status, _ = await self.__do_execute( .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:257: in __do_execute return await executor(protocol) ^^^^^^^^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > ??? E asyncpg.exceptions.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E DETAIL: Key (tenant_id)=(940fa053-1b1c-44e5-9909-6725d295c991) is not present in table "tenants". asyncpg/protocol/protocol.pyx:197: ForeignKeyViolationError The above exception was the direct cause of the following exception: self = dialect = context = statement = parameters = [(UUID('8a8e6ea3-f262-4f57-9a7b-732c3a5074c3'), 'Referral Bot', 'referral_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 293379, tzinfo=datetime.timezone.utc), ...)] def _exec_single_context( self, dialect: Dialect, context: ExecutionContext, statement: Union[str, Compiled], parameters: Optional[_AnyMultiExecuteParams], ) -> CursorResult[Unpack[TupleAny]]: """continue the _execute_context() method for a single DBAPI cursor.execute() or cursor.executemany() call. """ if dialect.bind_typing is BindTyping.SETINPUTSIZES: generic_setinputsizes = context._prepare_set_input_sizes() if generic_setinputsizes: try: dialect.do_set_input_sizes( context.cursor, generic_setinputsizes, context ) except BaseException as e: self._handle_dbapi_exception( e, str(statement), parameters, None, context ) cursor, str_statement, parameters = ( context.cursor, context.statement, context.parameters, ) effective_parameters: Optional[_AnyExecuteParams] if not context.executemany: effective_parameters = parameters[0] else: effective_parameters = parameters evt_handled: bool = False try: if self._has_events or self.engine._has_events: for fn in self.dispatch.before_cursor_execute: str_statement, effective_parameters = fn( self, cursor, str_statement, effective_parameters, context, context.executemany, ) if self._echo: self._log_info(str_statement) stats = context._get_cache_stats() self._log_parameters( effective_parameters, stats, ismulti=context.executemany ) if context.execute_style is ExecuteStyle.EXECUTEMANY: effective_parameters = cast( "_CoreMultiExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_executemany: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: self.dialect.do_executemany( cursor, str_statement, effective_parameters, context, ) elif not effective_parameters and context.no_parameters: if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute_no_params: if fn(cursor, str_statement, context): evt_handled = True break if not evt_handled: self.dialect.do_execute_no_params( cursor, str_statement, context ) else: effective_parameters = cast( "_CoreSingleExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: > self.dialect.do_execute( cursor, str_statement, effective_parameters, context ) .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: ForeignKeyViolationError The above exception was the direct cause of the following exception: db_session = @pytest.mark.asyncio async def test_referrals_and_growth_attribution(db_session: AsyncSession): """Verifies deep-link referral tracking, points calculation, and toggle enforcement.""" tenant_id = uuid.uuid4() bot_id = uuid.uuid4() bot = Bot( id=bot_id, tenant_id=tenant_id, name="Referral Bot", username="referral_test_bot", status="active", default_locale="en", ) db_session.add(bot) > await db_session.commit() tests/unit/test_bot_modular_engine.py:164: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/session.py:1309: in commit await greenlet_spawn(self.sync_session.commit) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:216: in greenlet_spawn result = context.switch(value) ^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2177: in commit trans.commit(_to_root=True) :2: in commit return target(fn, self, _to_root=_to_root) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1412: in commit self._prepare_impl() :2: in _prepare_impl return target(fn, self) ^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1386: in _prepare_impl self.session.flush() .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4626: in flush self._flush(objects) :2: in _flush return target(fn, self, objects=objects) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py:281: in warned return fn(*args, **kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4769: in _flush with util.safe_reraise(): .venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py:166: in __exit__ raise exc_value.with_traceback(exc_tb) .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4730: in _flush flush_context.execute() .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:467: in execute rec.execute(self) .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:643: in execute util.preloaded.orm_persistence._save_obj( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:92: in _save_obj _emit_insert_statements( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:1065: in _emit_insert_statements result = connection.execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1494: in execute return meth( .venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py:534: in _execute_on_connection return connection._execute_clauseelement( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1714: in _execute_clauseelement ret = self._execute_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1868: in _execute_context return self._exec_single_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1996: in _exec_single_context self._handle_dbapi_exception( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:2351: in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: in _exec_single_context self.dialect.do_execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.exc.IntegrityError: (sqlalchemy.dialects.postgresql.asyncpg.ForeignKeyViolationError) insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E [SQL: INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)] E [parameters: (UUID('8a8e6ea3-f262-4f57-9a7b-732c3a5074c3'), 'Referral Bot', 'referral_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 293379, tzinfo=datetime.timezone.utc), datetime.datetime(2026, 11, 18, 12, 50, 31, 293383, tzinfo=datetime.timezone.utc), UUID('940fa053-1b1c-44e5-9909-6725d295c991'))] E (Background on this error at: https://sqlalche.me/e/21/gkpj) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: IntegrityError __________________ test_auto_filter_keyword_and_blocked_words __________________ self = operation = 'INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)' parameters = (UUID('8d9365e5-76cc-4e55-9559-e5d8f7c96054'), 'Filter Bot', 'filter_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 528214, tzinfo=datetime.timezone.utc), ...) async def _prepare_and_execute(self, operation, parameters): adapt_connection = self._adapt_connection async with adapt_connection._execute_mutex: if adapt_connection._transaction is None: await adapt_connection._start_transaction() if parameters is None: parameters = () try: prepared_stmt, attributes = await adapt_connection._prepare( operation, self._invalidate_schema_cache_asof ) if attributes: self._description = [ ( attr.name, attr.type.oid, None, None, None, None, None, ) for attr in attributes ] else: self._description = None if self.server_side: self._cursor = await prepared_stmt.cursor(*parameters) self._rowcount = -1 else: > self._rows = deque(await prepared_stmt.fetch(*parameters)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:620: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:177: in fetch data = await self.__bind_execute(args, 0, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:268: in __bind_execute data, status, _ = await self.__do_execute( .venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.py:257: in __do_execute return await executor(protocol) ^^^^^^^^^^^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > ??? E asyncpg.exceptions.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E DETAIL: Key (tenant_id)=(a54bc6e8-13f9-49d0-b726-bbf8b8559e46) is not present in table "tenants". asyncpg/protocol/protocol.pyx:197: ForeignKeyViolationError The above exception was the direct cause of the following exception: self = dialect = context = statement = parameters = [(UUID('8d9365e5-76cc-4e55-9559-e5d8f7c96054'), 'Filter Bot', 'filter_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 528214, tzinfo=datetime.timezone.utc), ...)] def _exec_single_context( self, dialect: Dialect, context: ExecutionContext, statement: Union[str, Compiled], parameters: Optional[_AnyMultiExecuteParams], ) -> CursorResult[Unpack[TupleAny]]: """continue the _execute_context() method for a single DBAPI cursor.execute() or cursor.executemany() call. """ if dialect.bind_typing is BindTyping.SETINPUTSIZES: generic_setinputsizes = context._prepare_set_input_sizes() if generic_setinputsizes: try: dialect.do_set_input_sizes( context.cursor, generic_setinputsizes, context ) except BaseException as e: self._handle_dbapi_exception( e, str(statement), parameters, None, context ) cursor, str_statement, parameters = ( context.cursor, context.statement, context.parameters, ) effective_parameters: Optional[_AnyExecuteParams] if not context.executemany: effective_parameters = parameters[0] else: effective_parameters = parameters evt_handled: bool = False try: if self._has_events or self.engine._has_events: for fn in self.dispatch.before_cursor_execute: str_statement, effective_parameters = fn( self, cursor, str_statement, effective_parameters, context, context.executemany, ) if self._echo: self._log_info(str_statement) stats = context._get_cache_stats() self._log_parameters( effective_parameters, stats, ismulti=context.executemany ) if context.execute_style is ExecuteStyle.EXECUTEMANY: effective_parameters = cast( "_CoreMultiExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_executemany: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: self.dialect.do_executemany( cursor, str_statement, effective_parameters, context, ) elif not effective_parameters and context.no_parameters: if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute_no_params: if fn(cursor, str_statement, context): evt_handled = True break if not evt_handled: self.dialect.do_execute_no_params( cursor, str_statement, context ) else: effective_parameters = cast( "_CoreSingleExecuteParams", effective_parameters ) if self.dialect._has_events: for fn in self.dialect.dispatch.do_execute: if fn( cursor, str_statement, effective_parameters, context, ): evt_handled = True break if not evt_handled: > self.dialect.do_execute( cursor, str_statement, effective_parameters, context ) .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.ForeignKeyViolationError: insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: ForeignKeyViolationError The above exception was the direct cause of the following exception: db_session = @pytest.mark.asyncio async def test_auto_filter_keyword_and_blocked_words(db_session: AsyncSession): """Verifies that auto-filter detects blocked words and matches keywords when active.""" tenant_id = uuid.uuid4() bot_id = uuid.uuid4() bot = Bot( id=bot_id, tenant_id=tenant_id, name="Filter Bot", username="filter_test_bot", status="active", default_locale="en", ) db_session.add(bot) > await db_session.commit() tests/unit/test_bot_modular_engine.py:242: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/session.py:1309: in commit await greenlet_spawn(self.sync_session.commit) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:216: in greenlet_spawn result = context.switch(value) ^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:2177: in commit trans.commit(_to_root=True) :2: in commit return target(fn, self, _to_root=_to_root) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1412: in commit self._prepare_impl() :2: in _prepare_impl return target(fn, self) ^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py:137: in _go ret_value = fn(self, *arg, **kw) ^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:1386: in _prepare_impl self.session.flush() .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4626: in flush self._flush(objects) :2: in _flush return target(fn, self, objects=objects) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py:281: in warned return fn(*args, **kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4769: in _flush with util.safe_reraise(): .venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py:166: in __exit__ raise exc_value.with_traceback(exc_tb) .venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py:4730: in _flush flush_context.execute() .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:467: in execute rec.execute(self) .venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py:643: in execute util.preloaded.orm_persistence._save_obj( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:92: in _save_obj _emit_insert_statements( .venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py:1065: in _emit_insert_statements result = connection.execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1494: in execute return meth( .venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py:534: in _execute_on_connection return connection._execute_clauseelement( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1714: in _execute_clauseelement ret = self._execute_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1868: in _execute_context return self._exec_single_context( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1996: in _exec_single_context self._handle_dbapi_exception( .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:2351: in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e .venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py:1977: in _exec_single_context self.dialect.do_execute( .venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py:1192: in do_execute cursor.execute(statement, parameters) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:671: in execute await_(self._prepare_and_execute(operation, parameters)) .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:171: in await_ return current.parent.switch(awaitable) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/util/concurrency.py:209: in greenlet_spawn value = await result ^^^^^^^^^^^^ .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:633: in _prepare_and_execute self._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:583: in _handle_exception self._adapt_connection._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:862: in _handle_exception super()._handle_exception(error) .venv/lib/python3.12/site-packages/sqlalchemy/connectors/asyncio.py:412: in _handle_exception self._handle_exception_no_connection(self.dbapi, error) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ cls = dbapi = error = ForeignKeyViolationError('insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey"') @classmethod def _handle_exception_no_connection( cls, dbapi: Any, error: Exception ) -> NoReturn: if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error): exception_mapping = dbapi._asyncpg_error_translate for super_ in type(error).__mro__: if super_ in exception_mapping: message = error.args[0] translated_error = exception_mapping[super_]( message, error ) > raise translated_error from error E sqlalchemy.exc.IntegrityError: (sqlalchemy.dialects.postgresql.asyncpg.ForeignKeyViolationError) insert or update on table "bots" violates foreign key constraint "bots_tenant_id_fkey" E [SQL: INSERT INTO bots (id, name, username, status, default_locale, created_at, updated_at, tenant_id) VALUES ($1::UUID, $2::VARCHAR, $3::VARCHAR, $4::VARCHAR, $5::VARCHAR, $6::TIMESTAMP WITH TIME ZONE, $7::TIMESTAMP WITH TIME ZONE, $8::UUID)] E [parameters: (UUID('8d9365e5-76cc-4e55-9559-e5d8f7c96054'), 'Filter Bot', 'filter_test_bot', 'active', 'en', datetime.datetime(2026, 11, 18, 12, 50, 31, 528214, tzinfo=datetime.timezone.utc), datetime.datetime(2026, 11, 18, 12, 50, 31, 528219, tzinfo=datetime.timezone.utc), UUID('a54bc6e8-13f9-49d0-b726-bbf8b8559e46'))] E (Background on this error at: https://sqlalche.me/e/21/gkpj) .venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:855: IntegrityError =========================== short test summary info ============================ FAILED tests/unit/test_bot_modular_engine.py::test_bot_module_lifecycle - sql... FAILED tests/unit/test_bot_modular_engine.py::test_force_join_gating_behavior FAILED tests/unit/test_bot_modular_engine.py::test_referrals_and_growth_attribution FAILED tests/unit/test_bot_modular_engine.py::test_auto_filter_keyword_and_blocked_words ========================= 4 failed, 1 passed in 1.38s ==========================