Added the CSV_MIME constant to test_report.py to simplify the ReportTestCase test case.

This commit is contained in:
依瑪貓 2023-04-09 12:09:52 +08:00
parent 3a8618f7c3
commit 1224d6f83e

View File

@ -29,6 +29,8 @@ from testlib import create_test_app, get_client, Accounts, BaseTestData
PREFIX: str = "/accounting" PREFIX: str = "/accounting"
"""The URL prefix for the reports.""" """The URL prefix for the reports."""
CSV_MIME: str = "text/csv; charset=utf-8"
"""The MIME type of the downloaded CSV files."""
class ReportTestCase(unittest.TestCase): class ReportTestCase(unittest.TestCase):
@ -152,88 +154,77 @@ class ReportTestCase(unittest.TestCase):
response = client.get(f"{PREFIX}?as=csv") response = client.get(f"{PREFIX}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/journal") response = client.get(f"{PREFIX}/journal")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/journal?as=csv") response = client.get(f"{PREFIX}/journal?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/ledger") response = client.get(f"{PREFIX}/ledger")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/ledger?as=csv") response = client.get(f"{PREFIX}/ledger?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/income-expenses") response = client.get(f"{PREFIX}/income-expenses")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/income-expenses?as=csv") response = client.get(f"{PREFIX}/income-expenses?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/trial-balance") response = client.get(f"{PREFIX}/trial-balance")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/trial-balance?as=csv") response = client.get(f"{PREFIX}/trial-balance?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/income-statement") response = client.get(f"{PREFIX}/income-statement")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/income-statement?as=csv") response = client.get(f"{PREFIX}/income-statement?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/balance-sheet") response = client.get(f"{PREFIX}/balance-sheet")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/balance-sheet?as=csv") response = client.get(f"{PREFIX}/balance-sheet?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/unapplied") response = client.get(f"{PREFIX}/unapplied")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/unapplied?as=csv") response = client.get(f"{PREFIX}/unapplied?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}") response = client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv") response = client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/search?q=Salary") response = client.get(f"{PREFIX}/search?q=Salary")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/search?q=Salary&as=csv") response = client.get(f"{PREFIX}/search?q=Salary&as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = client.get(f"{PREFIX}/search?q=薪水") response = client.get(f"{PREFIX}/search?q=薪水")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/search?q=薪水&as=csv") response = client.get(f"{PREFIX}/search?q=薪水&as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
def test_editor(self) -> None: def test_editor(self) -> None:
"""Test the permission as editor. """Test the permission as editor.
@ -248,64 +239,56 @@ class ReportTestCase(unittest.TestCase):
response = self.client.get(f"{PREFIX}?as=csv") response = self.client.get(f"{PREFIX}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/journal") response = self.client.get(f"{PREFIX}/journal")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/journal?as=csv") response = self.client.get(f"{PREFIX}/journal?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/ledger") response = self.client.get(f"{PREFIX}/ledger")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/ledger?as=csv") response = self.client.get(f"{PREFIX}/ledger?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/income-expenses") response = self.client.get(f"{PREFIX}/income-expenses")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/income-expenses?as=csv") response = self.client.get(f"{PREFIX}/income-expenses?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/trial-balance") response = self.client.get(f"{PREFIX}/trial-balance")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/trial-balance?as=csv") response = self.client.get(f"{PREFIX}/trial-balance?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/income-statement") response = self.client.get(f"{PREFIX}/income-statement")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/income-statement?as=csv") response = self.client.get(f"{PREFIX}/income-statement?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/balance-sheet") response = self.client.get(f"{PREFIX}/balance-sheet")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/balance-sheet?as=csv") response = self.client.get(f"{PREFIX}/balance-sheet?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/unapplied") response = self.client.get(f"{PREFIX}/unapplied")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/unapplied?as=csv") response = self.client.get(f"{PREFIX}/unapplied?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}") response = self.client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -313,24 +296,21 @@ class ReportTestCase(unittest.TestCase):
response = self.client.get( response = self.client.get(
f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv") f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/search?q=Salary") response = self.client.get(f"{PREFIX}/search?q=Salary")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/search?q=Salary&as=csv") response = self.client.get(f"{PREFIX}/search?q=Salary&as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/search?q=薪水") response = self.client.get(f"{PREFIX}/search?q=薪水")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/search?q=薪水&as=csv") response = self.client.get(f"{PREFIX}/search?q=薪水&as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
def test_empty_db(self) -> None: def test_empty_db(self) -> None:
"""Tests the empty database. """Tests the empty database.
@ -344,64 +324,56 @@ class ReportTestCase(unittest.TestCase):
response = self.client.get(f"{PREFIX}?as=csv") response = self.client.get(f"{PREFIX}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/journal") response = self.client.get(f"{PREFIX}/journal")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/journal?as=csv") response = self.client.get(f"{PREFIX}/journal?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/ledger") response = self.client.get(f"{PREFIX}/ledger")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/ledger?as=csv") response = self.client.get(f"{PREFIX}/ledger?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/income-expenses") response = self.client.get(f"{PREFIX}/income-expenses")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/income-expenses?as=csv") response = self.client.get(f"{PREFIX}/income-expenses?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/trial-balance") response = self.client.get(f"{PREFIX}/trial-balance")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/trial-balance?as=csv") response = self.client.get(f"{PREFIX}/trial-balance?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/income-statement") response = self.client.get(f"{PREFIX}/income-statement")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/income-statement?as=csv") response = self.client.get(f"{PREFIX}/income-statement?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/balance-sheet") response = self.client.get(f"{PREFIX}/balance-sheet")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/balance-sheet?as=csv") response = self.client.get(f"{PREFIX}/balance-sheet?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/unapplied") response = self.client.get(f"{PREFIX}/unapplied")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/unapplied?as=csv") response = self.client.get(f"{PREFIX}/unapplied?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}") response = self.client.get(f"{PREFIX}/unapplied/{Accounts.PAYABLE}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -409,16 +381,14 @@ class ReportTestCase(unittest.TestCase):
response = self.client.get( response = self.client.get(
f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv") f"{PREFIX}/unapplied/{Accounts.PAYABLE}?as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
response = self.client.get(f"{PREFIX}/search?q=Salary") response = self.client.get(f"{PREFIX}/search?q=Salary")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/search?q=Salary&as=csv") response = self.client.get(f"{PREFIX}/search?q=Salary&as=csv")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["Content-Type"], self.assertEqual(response.headers["Content-Type"], CSV_MIME)
"text/csv; charset=utf-8")
class ReportTestData(BaseTestData): class ReportTestData(BaseTestData):