You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You need to provide a couple of fixtures to inject your sqlalchemy Base and Session classes ( the ones your regular app uses ). eg::
54
+
The plugin will create a completely new test database that lasts as long as the test session and is destroyed at the end.
55
+
You can execute each test case in an isolated transaction by including the ``db_session`` fixture, eg:
56
+
57
+
.. code-block:: python
58
+
59
+
deftest_create_foo(db_session):
60
+
foo = Foo(name="bar")
61
+
db_session.add(foo)
62
+
db_session.commit()
63
+
assert db_session.query(Foo).count()
64
+
65
+
The transaction is automatically rolled back at the end of the test function giving you a clean slate for the next test.
66
+
67
+
You need to define a couple of fixtures to be able to use the plugin, this is mostly to 'patch' your existing Session and Base classes to use the testing database. This is probably the most tricky bit of the plugin as sqlalchemy usage in projects can vary somewhat
68
+
69
+
Required fixtures
70
+
^^^^^^^^^^^^^^^^^
71
+
``sqlalchemy_base_class``
72
+
73
+
You must set this to your base class that you use for defining models in your project, eg:
74
+
75
+
.. code-block:: python
53
76
54
77
# my_project/tests/conftest.py
55
-
56
-
from my_app.db import Base, Session
78
+
79
+
frommy_project.databaseimport Base
57
80
58
81
@pytest.fixture(scope='session')
59
82
defsqlalchemy_base():
60
83
return Base
61
84
85
+
``sqlalchemy_session_class``
86
+
87
+
Use this fixture to supply your project's sqlalchemy Session factory, eg:
88
+
89
+
.. code-block:: python
90
+
91
+
# my_project/database.py
92
+
93
+
from sqlalchemy.ext.declarative import declarative_base
94
+
from sqlalchemy.orm import scoped_session, sessionmaker
95
+
96
+
Base = declarative_base()
97
+
Session = scoped_session(sessionmaker())
98
+
99
+
...
100
+
101
+
# my_project/tests/conftest.py
102
+
103
+
from my_project.database import Session
104
+
62
105
@pytest.fixture(scope='session')
63
-
def sqlalchemy_session():
106
+
defsqlalchemy_session_class():
64
107
return Session
65
108
109
+
If your project uses a different way to obtain a sqlalchemy session, then you'll need to figure out some other way to configure that session to use the test database, possibly by mocking it in individual test cases.
110
+
111
+
Optional Fixtures
112
+
^^^^^^^^^^^^^^^^^
113
+
114
+
``database_url``
115
+
116
+
This defaults to ``os.environ['DATABASE_URL']`` but is designed to be overridden to supply an alternative. The plugin will attempt to connect to whatever database is specified and create another database alongside the original, prefixed with ``test_``
117
+
118
+
``test_db_prefix``
119
+
120
+
If you don't like ``test_`` as a prefix for your testing database, return something else here.
0 commit comments