Guest User

Untitled

a guest
Nov 15th, 2026
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. ```python3
  2. from uuid import uuid4
  3.  
  4. import asyncpg
  5. import httpx
  6. from asgi_lifespan import LifespanManager
  7. from redis.asyncio import Redis
  8.  
  9. from app.core.config import Config
  10. from app.core.database import Database
  11. from app.setup import build_app
  12.  
  13.  
  14. class TestApp:
  15. def __init__(self):
  16. config = Config()
  17.  
  18. self.redis_key_prefix = uuid4().hex
  19. self.config = config.model_copy(
  20. update={
  21. "database": config.database.model_copy(
  22. update={"name": f"test_{uuid4().hex}"},
  23. ),
  24. "redis": config.redis.model_copy(
  25. update={"key_prefix": self.redis_key_prefix},
  26. ),
  27. }
  28. )
  29.  
  30. self.admin_connection: asyncpg.Connection | None = None
  31.  
  32. self.database: Database | None = None
  33. self.redis: Redis | None = None
  34.  
  35. self.client: httpx.AsyncClient | None = None
  36. self._lifespan: LifespanManager | None = None
  37.  
  38. async def __aenter__(self):
  39. try:
  40. self.admin_connection = await asyncpg.connect(
  41. host=self.config.database.host,
  42. port=self.config.database.port,
  43. user=self.config.database.user,
  44. password=self.config.database.password.get_secret_value(),
  45. database="postgres",
  46. ssl="require" if self.config.database.ssl else None,
  47. )
  48.  
  49. await self.admin_connection.execute(
  50. f'CREATE DATABASE "{self.config.database.name}"'
  51. )
  52.  
  53. self.database = Database(self.config.database)
  54.  
  55. self.redis = Redis(
  56. host=self.config.redis.host,
  57. port=self.config.redis.port,
  58. decode_responses=True,
  59. )
  60.  
  61. app = build_app(self.config)
  62.  
  63. self._lifespan = LifespanManager(app)
  64. await self._lifespan.__aenter__()
  65.  
  66. self.client = httpx.AsyncClient(
  67. transport=httpx.ASGITransport(app=app),
  68. base_url="http://test",
  69. )
  70.  
  71. return self
  72.  
  73. except BaseException:
  74. await self._cleanup()
  75. raise
  76.  
  77. async def __aexit__(self, exc_type, exc, tb):
  78. await self._cleanup(exc_type, exc, tb)
  79.  
  80. async def _cleanup(self, exc_type=None, exc=None, tb=None):
  81. if self.client is not None:
  82. await self.client.aclose()
  83. self.client = None
  84.  
  85. if self._lifespan is not None:
  86. await self._lifespan.__aexit__(exc_type, exc, tb)
  87. self._lifespan = None
  88.  
  89. if self.redis is not None:
  90. async for key in self.redis.scan_iter(
  91. match=f"{self.redis_key_prefix}:*",
  92. ):
  93. await self.redis.unlink(key)
  94.  
  95. await self.redis.aclose()
  96. self.redis = None
  97.  
  98. if self.database is not None:
  99. await self.database.dispose()
  100. self.database = None
  101.  
  102. if self.admin_connection is not None:
  103. await self.admin_connection.execute(
  104. """
  105. SELECT pg_terminate_backend(pid)
  106. FROM pg_stat_activity
  107. WHERE datname = $1
  108. AND pid <> pg_backend_pid()
  109. """,
  110. self.config.database.name,
  111. )
  112.  
  113. await self.admin_connection.execute(
  114. f'DROP DATABASE IF EXISTS "{self.config.database.name}"'
  115. )
  116.  
  117. await self.admin_connection.close()
  118. self.admin_connection = None
  119.  
  120.  
  121.  
  122. import pytest
  123.  
  124. from tests.integration import TestApp
  125.  
  126.  
  127. @pytest.fixture
  128. async def test_app(monkeypatch):
  129. monkeypatch.setenv("ENV", "test")
  130.  
  131. async with TestApp() as app:
  132. yield app
  133.  
  134.  
  135.  
  136. import pytest
  137. from fastapi.testclient import TestClient
  138.  
  139.  
  140. @pytest.mark.anyio
  141. async def test_signup(test_app):
  142. response = await test_app.client.post(
  143. "/api/v1/auth/sign-up",
  144. json={
  145. "email": "[email protected]",
  146. "password": "Warcacexleb",
  147. "first_name": "test",
  148. "last_name": "test",
  149. },
  150. )
  151.  
  152. assert response.status_code == 201
  153.  
  154.  
  155. ```
Advertisement
Add Comment
Please, Sign In to add comment