from pytest_mock_resources import ( create_mysql_fixture, create_postgres_fixture, create_redshift_fixture, create_sqlite_fixture, Statements, ) statements = Statements("CREATE VIEW cool_view as select 3", "CREATE VIEW cool_view_2 as select 1") postgres = create_postgres_fixture(statements) sqlite = create_sqlite_fixture(statements) mysql = create_mysql_fixture(statements) def test_statements_postgres(postgres): execute = postgres.execute(""" SELECT table_name FROM INFORMATION_SCHEMA.views WHERE table_name in ('cool_view', 'cool_view_2') ORDER BY table_name """) result = [row[0] for row in execute] assert ["cool_view", "cool_view_2"] == result def test_statements_mysql(mysql): execute = mysql.execute(""" SELECT table_name FROM INFORMATION_SCHEMA.views
import pytest from sqlalchemy import create_engine, text from pytest_mock_resources import ( compat, create_mysql_fixture, create_postgres_fixture, create_redshift_fixture, create_sqlite_fixture, ) from pytest_mock_resources.fixture.database.relational.generic import EngineManager sqlite = create_sqlite_fixture() postgres = create_postgres_fixture() redshift = create_redshift_fixture() mysql = create_mysql_fixture() def test_basic_sqlite_fixture(sqlite): sqlite.execute("select 1") def test_basic_postgres_fixture(postgres): postgres.execute("select 1") def test_basic_redshift_fixture(redshift): redshift.execute("select 1") def test_basic_postgres_and_redshift_fixture(postgres, redshift):
import datetime import json from pytest_mock_resources import create_sqlite_fixture from pytest import fixture import app from models import Accounts, Sessions, Visits from test_db_setup import set_up_test_db, add_salon_id_for_hairdressers sq = create_sqlite_fixture(Accounts, session=True) def show_user(session): return session.query(Accounts).first() def show_session_data(session): # Return the session ID that our test user receives when test logging in session_result = session.query(Sessions).first() return session_result.session_id, session_result.account_id def show_visits_data(session): return session.query(Visits).first() @fixture def client(): app.app.config['TESTING'] = True
class Other(Base): __tablename__ = "other" __table_args__ = {"schema": "public"} id = Column(Integer, autoincrement=True, primary_key=True) name = Column(Unicode(5), nullable=False) class Other2(Base): __tablename__ = "other" __table_args__ = {"schema": "other"} id = Column(Integer, autoincrement=True, primary_key=True) sqlite_model = create_sqlite_fixture(Base, tables=[Thing]) sqlite_table = create_sqlite_fixture(Base, tables=[Thing.__table__]) sqlite_name_implicit_schema = create_sqlite_fixture(Base, tables=["thing"]) sqlite_name_explicit_schema = create_sqlite_fixture(Base, tables=["other.other"]) sqlite_name_bad_table = create_sqlite_fixture(Base, tables=["foo"]) sqlite_duplicate = create_sqlite_fixture( Base, tables=[Thing, Other2.__table__, "thing", "other.other"]) sqlite_glob = create_sqlite_fixture(Base, tables=["*.other"]) class TestTablesArg: def test_model_object(self, sqlite_model): sqlite_model.execute("select * from thing") with pytest.raises(sqlalchemy.exc.OperationalError): sqlite_model.execute("select * from other.other")
id = Column(Integer, autoincrement=True, primary_key=True) thing_id = Column(Integer, ForeignKey("other.thing.id"), nullable=False) class ColumnTypesTable(Base): __tablename__ = "column_types_table" id = Column(Integer, autoincrement=True, primary_key=True) json = Column(JSON(none_as_null=True)) jsonb = Column(JSONB(none_as_null=True)) column_types_table = ColumnTypesTable.__table__ sqlite = create_sqlite_fixture(Base, Rows(Thing(id=1, name="foo"))) def test_create_schema(sqlite): result = list(sqlite.execute("select * from other.thing")) assert result == [(1, "foo")] def test_create_all(sqlite): Base.metadata.create_all(bind=sqlite) result = list(sqlite.execute("select * from other.thing")) assert result == [(1, "foo")] def test_foreign_keys(sqlite):
from pytest_mock_resources import create_sqlite_fixture db = create_sqlite_fixture(session=True)