Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_database_url_repr():
u = DatabaseURL("postgresql://localhost/name")
assert repr(u) == "DatabaseURL('postgresql://localhost/name')"
u = DatabaseURL("postgresql://username@localhost/name")
assert repr(u) == "DatabaseURL('postgresql://username@localhost/name')"
u = DatabaseURL("postgresql://username:password@localhost/name")
assert repr(u) == "DatabaseURL('postgresql://username:********@localhost/name')"
def test_replace_database_url_components():
u = DatabaseURL("postgresql://localhost/mydatabase")
assert u.database == "mydatabase"
new = u.replace(database="test_" + u.database)
assert new.database == "test_mydatabase"
assert str(new) == "postgresql://localhost/test_mydatabase"
assert u.driver == ""
new = u.replace(driver="asyncpg")
assert new.driver == "asyncpg"
assert str(new) == "postgresql+asyncpg://localhost/mydatabase"
assert u.port is None
new = u.replace(port=123)
assert new.port == 123
assert str(new) == "postgresql://localhost:123/mydatabase"
def test_database_url_properties():
u = DatabaseURL("postgresql+asyncpg://username:password@localhost:123/mydatabase")
assert u.dialect == "postgresql"
assert u.driver == "asyncpg"
assert u.username == "username"
assert u.password == "password"
assert u.hostname == "localhost"
assert u.port == 123
assert u.database == "mydatabase"
def test_database_url_repr():
u = DatabaseURL("postgresql://localhost/name")
assert repr(u) == "DatabaseURL('postgresql://localhost/name')"
u = DatabaseURL("postgresql://username@localhost/name")
assert repr(u) == "DatabaseURL('postgresql://username@localhost/name')"
u = DatabaseURL("postgresql://username:password@localhost/name")
assert repr(u) == "DatabaseURL('postgresql://username:********@localhost/name')"
async def test_database_url_interface(database_url):
"""
Test that Database instances expose a `.url` attribute.
"""
async with Database(database_url) as database:
assert isinstance(database.url, DatabaseURL)
assert database.url == database_url
def create_test_database():
# Create test databases
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.dialect == "mysql":
url = str(database_url.replace(driver="pymysql"))
engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
# Run the test suite
yield
# Drop test databases
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.dialect == "mysql":
url = str(database_url.replace(driver="pymysql"))
engine = sqlalchemy.create_engine(url)
metadata.drop_all(engine)
async def test_iterate_outside_transaction_with_temp_table(database_url):
"""
Same as test_iterate_outside_transaction_with_values but uses a
temporary table instead of a list of values.
"""
database_url = DatabaseURL(database_url)
if database_url.dialect == "sqlite":
pytest.skip("SQLite interface does not work with temporary tables.")
async with Database(database_url) as database:
query = "CREATE TEMPORARY TABLE no_transac(num INTEGER)"
await database.execute(query)
query = "INSERT INTO no_transac(num) VALUES (1), (2), (3), (4), (5)"
await database.execute(query)
query = "SELECT * FROM no_transac"
iterate_results = []
async for result in database.iterate(query=query):
iterate_results.append(result)
from loguru import logger
from starlette.config import Config
from starlette.datastructures import CommaSeparatedStrings, Secret
from app.core.logging import InterceptHandler
API_PREFIX = "/api"
JWT_TOKEN_PREFIX = "Token" # noqa: S105
VERSION = "0.0.0"
config = Config(".env")
DEBUG: bool = config("DEBUG", cast=bool, default=False)
DATABASE_URL: DatabaseURL = config("DB_CONNECTION", cast=DatabaseURL)
MAX_CONNECTIONS_COUNT: int = config("MAX_CONNECTIONS_COUNT", cast=int, default=10)
MIN_CONNECTIONS_COUNT: int = config("MIN_CONNECTIONS_COUNT", cast=int, default=10)
SECRET_KEY: Secret = config("SECRET_KEY", cast=Secret)
PROJECT_NAME: str = config("PROJECT_NAME", default="FastAPI example application")
ALLOWED_HOSTS: List[str] = config(
"ALLOWED_HOSTS", cast=CommaSeparatedStrings, default=""
)
# logging configuration
LOGGING_LEVEL = logging.DEBUG if DEBUG else logging.INFO
logging.basicConfig(
handlers=[InterceptHandler(level=LOGGING_LEVEL)], level=LOGGING_LEVEL
)
from databases import DatabaseURL
from ..conf import settings
from .core import Database
database_url = DatabaseURL(settings.database_url)
if settings.test and settings.test_database_name:
database_url = database_url.replace(database=settings.test_database_name)
database = Database(database_url, force_rollback=settings.test)