Added the "populate" method to the BaseTestData class, and changed it so that the tests need to call the "populate" method to populate the data, so that it may return the data with populating the database in the future.

This commit is contained in:
依瑪貓 2023-04-12 12:28:34 +08:00
parent 3bada28b8f
commit 85d1b13ccd
4 changed files with 23 additions and 12 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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

View File

@ -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]: