Skip to content

Commit

Permalink
move database table creation to setUpClass and have a rollback of the…
Browse files Browse the repository at this point in the history
… database transaction for tearDown of each test.
  • Loading branch information
scott authored and EvanBldy committed Oct 7, 2024
1 parent 86044c5 commit 0d619f3
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from mixer.backend.flask import mixer

from zou.app import app
from zou.app import app, db
from zou.app.models.status_automation import StatusAutomation
from zou.app.utils import fields, auth, fs
from zou.app.services import (
Expand Down Expand Up @@ -46,6 +46,11 @@
from zou.app.models.working_file import WorkingFile
from zou.app.stores import auth_tokens_store

from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from flask import current_app


TEST_FOLDER = os.path.join("tests", "tmp")

auth_tokens_store.revoked_tokens_store = fakeredis.FakeStrictRedis(
Expand All @@ -59,36 +64,27 @@ class ApiTestCase(unittest.TestCase):
"""
@classmethod
def setUpClass(cls):
"""
Configure resources shared by all tests in the class.
"""
app.test_request_context(headers={"mimetype": "application/json"})
cls.flask_app = app
cls.app = app.test_client()
cls.base_headers = {}
cls.post_headers = {"Content-type": "application/json"}
app.app_context().push()
from zou.app.utils import cache

cache.clear()
pass

@classmethod
def tearDownClass(self):
# Clean up resources after all test methods have run
"""
Clean up resources after all test methods have run.
"""
def tearDownClass(cls):
pass

def setUp(self):
"""
Configure application before each test.
"""
pass
app.test_request_context(headers={"mimetype": "application/json"})
self.flask_app = app
self.app = app.test_client()
self.base_headers = {}
self.post_headers = {"Content-type": "application/json"}
app.app_context().push()

from zou.app.utils import cache
cache.clear()

def tearDown(self):
"""
Configure application after each test.
"""
pass

def log_in(self, email):
Expand Down Expand Up @@ -239,26 +235,57 @@ def download_file(self, path, target_file_path, code=200):


class ApiDBTestCase(ApiTestCase):
"""
Set of helpers for Api tests.
"""
@classmethod
def setUpClass(cls):
"""
Configure application before all test methods.
Create all database tables only once for all tests in this class.
"""
super(ApiDBTestCase, cls).setUpClass()
from zou.app.utils import dbhelpers
with app.app_context():
dbhelpers.drop_all()
dbhelpers.create_all()

@classmethod
def tearDownClass(cls):
"""
Clean up resources after all test methods have run.
Delete database.
"""
super(ApiDBTestCase, cls).tearDownClass()
from zou.app.utils import dbhelpers
with app.app_context():
dbhelpers.drop_all()

def setUp(self):
"""
Reset database before each test.
Configure application before each test.
set up database transaction.
"""
super(ApiDBTestCase, self).setUp()

from zou.app.utils import dbhelpers
self._db_connection = db.engine.connect()
self._db_transaction = self._db_connection.begin()
factory = sessionmaker(bind=self._db_connection, binds={})
self._db_session = scoped_session(factory, current_app._get_current_object)
db.session = self._db_session

dbhelpers.drop_all()
dbhelpers.create_all()
self.generate_fixture_user()
self.log_in_admin()

def tearDown(self):
"""
Delete database after each test.
Configure application after each test.
Rollback transaction to return database to its original state.
"""
from zou.app.utils import dbhelpers

dbhelpers.drop_all()
super(ApiDBTestCase, self).tearDown()
self._db_transaction.rollback()
self._db_connection.close()
self._db_session.remove()

def generate_data(self, cls, number, **kwargs):
"""
Expand Down

0 comments on commit 0d619f3

Please sign in to comment.