diff --git a/tests/test_offset.py b/tests/test_offset.py index a230dec..37d2ed7 100644 --- a/tests/test_offset.py +++ b/tests/test_offset.py @@ -52,6 +52,7 @@ class OffsetTestCase(unittest.TestCase): self.client, self.csrf_token = get_client(self.app, "editor") self.data: OffsetTestData = OffsetTestData(self.app, "editor") + self.data.populate() def test_add_receivable_offset(self) -> None: """Tests to add the receivable offset. diff --git a/tests/test_report.py b/tests/test_report.py index d277a1b..fa20b51 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -55,7 +55,7 @@ class ReportTestCase(unittest.TestCase): :return: None. """ client, csrf_token = get_client(self.app, "nobody") - ReportTestData(self.app, "editor") + ReportTestData(self.app, "editor").populate() response: httpx.Response response = client.get(PREFIX) @@ -130,7 +130,7 @@ class ReportTestCase(unittest.TestCase): :return: None. """ client, csrf_token = get_client(self.app, "viewer") - ReportTestData(self.app, "editor") + ReportTestData(self.app, "editor").populate() response: httpx.Response response = client.get(PREFIX) @@ -215,7 +215,7 @@ class ReportTestCase(unittest.TestCase): :return: None. """ - ReportTestData(self.app, "editor") + ReportTestData(self.app, "editor").populate() response: httpx.Response response = self.client.get(PREFIX) diff --git a/tests/test_unmatched_offset.py b/tests/test_unmatched_offset.py index 9d12926..423300d 100644 --- a/tests/test_unmatched_offset.py +++ b/tests/test_unmatched_offset.py @@ -54,7 +54,7 @@ class UnmatchedOffsetTestCase(unittest.TestCase): :return: None. """ client, csrf_token = get_client(self.app, "nobody") - DifferentTestData(self.app, "nobody") + DifferentTestData(self.app, "nobody").populate() response: httpx.Response response = client.get(PREFIX) @@ -73,7 +73,7 @@ class UnmatchedOffsetTestCase(unittest.TestCase): :return: None. """ client, csrf_token = get_client(self.app, "viewer") - DifferentTestData(self.app, "viewer") + DifferentTestData(self.app, "viewer").populate() response: httpx.Response response = client.get(PREFIX) @@ -91,7 +91,7 @@ class UnmatchedOffsetTestCase(unittest.TestCase): :return: None. """ - DifferentTestData(self.app, "editor") + DifferentTestData(self.app, "editor").populate() response: httpx.Response response = self.client.get(PREFIX) @@ -133,6 +133,7 @@ class UnmatchedOffsetTestCase(unittest.TestCase): from accounting.models import Account, JournalEntryLineItem from accounting.utils.offset_matcher import OffsetMatcher data: DifferentTestData = DifferentTestData(self.app, "editor") + data.populate() account: Account | None line_item: JournalEntryLineItem | None matcher: OffsetMatcher @@ -248,6 +249,7 @@ class UnmatchedOffsetTestCase(unittest.TestCase): from accounting.models import Account, JournalEntryLineItem from accounting.utils.offset_matcher import OffsetMatcher data: SameTestData = SameTestData(self.app, "editor") + data.populate() account: Account | None line_item: JournalEntryLineItem | None matcher: OffsetMatcher diff --git a/tests/testlib.py b/tests/testlib.py index daf6b8b..5d45c97 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -303,8 +303,8 @@ class BaseTestData(ABC): :param app: The Flask application. :param username: The username. """ - from accounting.models import JournalEntry, JournalEntryLineItem - with app.app_context(): + self.__app: Flask = app + with self.__app.app_context(): current_user: User | None = User.query\ .filter(User.username == username).first() assert current_user is not None @@ -312,10 +312,6 @@ class BaseTestData(ABC): self.__journal_entries: list[dict[str, t.Any]] = [] self.__line_items: list[dict[str, t.Any]] = [] self._init_data() - db.session.execute(sa.insert(JournalEntry), self.__journal_entries) - db.session.execute(sa.insert(JournalEntryLineItem), - self.__line_items) - db.session.commit() @abstractmethod def _init_data(self) -> None: @@ -324,6 +320,18 @@ class BaseTestData(ABC): :return: None """ + def populate(self) -> None: + """Populates the data into the database. + + :return: None + """ + from accounting.models import JournalEntry, JournalEntryLineItem + with self.__app.app_context(): + db.session.execute(sa.insert(JournalEntry), self.__journal_entries) + db.session.execute(sa.insert(JournalEntryLineItem), + self.__line_items) + db.session.commit() + @staticmethod def _couple(description: str, amount: str, debit: str, credit: str) \ -> tuple[JournalEntryLineItemData, JournalEntryLineItemData]: