Skip to content

Commit

Permalink
Create PostgreSQL database for SQLAlchemy tests
Browse files Browse the repository at this point in the history
Previously, the test suite expected the existence of an
`alch_factory_boy_test` database. In order to facilitate local
development, create it when the test suite runs, and destroy it
afterwards.
  • Loading branch information
kingbuzzman authored and francoisfreitag committed Jun 23, 2022
1 parent 9aef4e5 commit 05e7d96
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ jobs:
- 5432:5432
env:
POSTGRES_PASSWORD: password
POSTGRES_DB: alch_factory_boy_test

env:
TOXENV: ${{ matrix.tox-environment }}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
except ImportError:
raise unittest.SkipTest("sqlalchemy tests disabled.")

from sqlalchemy_utils import create_database, database_exists, drop_database

import factory
from factory.alchemy import SQLAlchemyModelFactory

Expand Down Expand Up @@ -75,6 +77,17 @@ class Meta:
text = factory.Sequence(lambda n: "text%s" % n)


if models.USING_POSTGRES:
# sqlite test database gets created/destroyed automatically, postgres does not.

def setUpModule():
if not database_exists(models.engine.url):
create_database(models.engine.url)

def tearDownModule():
drop_database(models.engine.url)


class TransactionTestCase(unittest.TestCase):
def setUp(self):
models.Base.metadata.create_all(models.engine)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ passenv =
POSTGRES_DATABASE
deps =
alchemy: SQLAlchemy
alchemy: sqlalchemy_utils
mongo: mongoengine
django{22,32,40,main}: Pillow
django22: Django>=2.2,<2.3
Expand Down

0 comments on commit 05e7d96

Please sign in to comment.