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" assert u.username is None assert u.userinfo is None u = DatabaseURL("sqlite:///mydatabase") assert u.database == "mydatabase" new = u.replace(database="test_" + u.database) assert new.database == "test_mydatabase" assert str(new) == "sqlite:///test_mydatabase" u = DatabaseURL("sqlite:////absolute/path") assert u.database == "/absolute/path" new = u.replace(database=u.database + "_test") assert new.database == "/absolute/path_test" assert str(new) == "sqlite:////absolute/path_test"
def __init__(self, url: DatabaseURL): url = DatabaseURL(url) if url.scheme == "postgres": # The default postgres backend for databases does not return # RowProxy objects, unlike all the other backends. # Therefore, we use aiopg so that we have dialect-agnostic results. url = url.replace(scheme="postgres+aiopg") self.url = url self.database = DatabaseBackend(url) self.metadata = MetaData() class DatabaseModelMetaclass(orm.models.ModelMetaclass): def __new__( cls: type, name: str, bases: typing.Sequence[type], attrs: dict, ) -> type: attrs["__database__"] = self.database attrs["__metadata__"] = self.metadata return super(DatabaseModelMetaclass, cls).__new__(cls, name, bases, attrs) class DatabaseModel(orm.Model, metaclass=DatabaseModelMetaclass): __abstract__ = True self.Model = DatabaseModel
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 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)
def create_test_database(): # Create test databases with tables creation for url in DATABASE_URLS: database_url = DatabaseURL(url) if database_url.scheme == "mysql": url = str(database_url.replace(driver="pymysql")) elif database_url.scheme == "postgresql+aiopg": url = str(database_url.replace(driver=None)) 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.scheme == "mysql": url = str(database_url.replace(driver="pymysql")) elif database_url.scheme == "postgresql+aiopg": url = str(database_url.replace(driver=None)) engine = sqlalchemy.create_engine(url) metadata.drop_all(engine)