Skip to main content

No project description provided

Project description

This package contains test fixtures and resources that bring up an isolated database and SQLAlchemy test fixtures, so that your Python unit tests will run without interfering with each other when you are using the SQLAlchemy ORM.

The database is initialised once at the start of a test process run, as is the session fixture. The session fixture ensures that any commits do not permanently commit, and rolls back the database to a clean state after each test completes.

Requirements

Python 3.7 and beyond should work.

Quickstart

Install with pip:

pip install db-testtools

Example base test class:

class DBTestCase(testresources.ResourcedTestCase, testtools.TestCase):
    """Base class for all DB tests.

   Brings up a temporary database and gives each test its own session.
   """

   # These are resources that stay active for the entire
   # duration of all the tests being run.
   db_fixture = DatabaseResource(
       ModelBase,
       'myproject.models',
       future=True,
   )
   resources = [('database', db_fixture)]

   def setUp(self):
       super().setUp()

       self.session_fixture = SessionFixture(self.database, future=True)
       self.useFixture(self.session_fixture)
       # The session itself.
       self.session = self.session_fixture.session
       # The session factory.
       self.Session = self.session_fixture.Session

This base test class will start a SQLite-based database by default and inject self.session as the SQLAlchemy session and self.Session as the SQLAlchemy session factory.

If you need to use a different database, then you can either:
  • pass the engine_fixture_name parameter to DatabaseResource

  • set an environment variable TEST_ENGINE_FIXTURE

with the name of the engine fixture to use. Currently two are available:

  • SqliteMemoryFixture

  • PostgresContainerFixture

Engine drivers

Currently the two drivers mentioned above are implemented. The SQLite fixture implements a simple in-memory database which is completely dropped and re-instated on every test.

The PostgresContainerFixture starts its own Postgres instance in a local Docker container. Therefore you must have Docker installed before using this fixture.

Help needed!

This fixture suite is currently not tested itself and would benefit from anyone willing to contribute some unit tests. However, it has been in use daily on a large project at Cisco for the last 2 years, and is very stable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

db-testtools-2023.1.27.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

db_testtools-2023.1.27-py2.py3-none-any.whl (15.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file db-testtools-2023.1.27.tar.gz.

File metadata

  • Download URL: db-testtools-2023.1.27.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for db-testtools-2023.1.27.tar.gz
Algorithm Hash digest
SHA256 07864ad4ce6375a86a3cd23bbd66cc730ec02f6b03e2a10ed1118803e43050fa
MD5 3d85c56e48ae5c7a871bd92bc1e82c8f
BLAKE2b-256 9726e053150cb955cabd67960c50f40cbcefce19ee57f3e84800e93dc0cb8b91

See more details on using hashes here.

File details

Details for the file db_testtools-2023.1.27-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for db_testtools-2023.1.27-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6f7528c53346e1f3223ae6ac4460ed5626f610c8d913a4013cf8e9527c058cb7
MD5 a2c20f31193638c4f5623a29d5b312d2
BLAKE2b-256 adc75332bc19272a251bd2c49a52c4973f3d3abf4a881d4c1dc9701372bfe489

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page