Added the limitation so that the default currency and the currencies in use cannot be deleted.

This commit is contained in:
依瑪貓 2023-03-22 00:37:39 +08:00
parent b60cc7902d
commit bbf3ee3320
4 changed files with 185 additions and 120 deletions

View File

@ -160,6 +160,9 @@ def delete_currency(currency: Currency) -> redirect:
:return: The redirection to the currency list on success, or the currency :return: The redirection to the currency list on success, or the currency
detail on error. detail on error.
""" """
if not currency.can_delete:
flash(s(lazy_gettext("The currency cannot be deleted.")), "error")
return redirect(inherit_next(__get_detail_uri(currency)))
currency.delete() currency.delete()
db.session.commit() db.session.commit()
flash(s(lazy_gettext("The currency is deleted successfully.")), "success") flash(s(lazy_gettext("The currency is deleted successfully.")), "success")

View File

@ -432,6 +432,17 @@ class Currency(db.Model):
return True return True
return False return False
@property
def can_delete(self) -> bool:
"""Returns whether the currency can be deleted.
:return: True if the currency can be deleted, or False otherwise.
"""
from accounting.template_globals import default_currency_code
if self.code == default_currency_code():
return False
return len(self.line_items) == 0
def delete(self) -> None: def delete(self) -> None:
"""Deletes the currency. """Deletes the currency.

View File

@ -37,10 +37,17 @@ First written: 2023/2/6
</a> </a>
{% endif %} {% endif %}
{% if accounting_can_edit() %} {% if accounting_can_edit() %}
{% if obj.can_delete %}
<button class="btn btn-danger" type="button" data-bs-toggle="modal" data-bs-target="#accounting-delete-modal"> <button class="btn btn-danger" type="button" data-bs-toggle="modal" data-bs-target="#accounting-delete-modal">
<i class="fa-solid fa-trash"></i> <i class="fa-solid fa-trash"></i>
<span class="d-none d-md-inline">{{ A_("Delete") }}</span> <span class="d-none d-md-inline">{{ A_("Delete") }}</span>
</button> </button>
{% else %}
<button class="btn btn-secondary" type="button" disabled="disabled">
<i class="fa-solid fa-trash"></i>
<span class="d-none d-md-inline">{{ A_("Delete") }}</span>
</button>
{% endif %}
{% endif %} {% endif %}
</div> </div>
@ -52,7 +59,7 @@ First written: 2023/2/6
</div> </div>
{% endif %} {% endif %}
{% if accounting_can_edit() %} {% if accounting_can_edit() and obj.can_delete %}
<form action="{{ url_for("accounting.currency.delete", currency=obj) }}" method="post"> <form action="{{ url_for("accounting.currency.delete", currency=obj) }}" method="post">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
{% if request.args.next %} {% if request.args.next %}

View File

@ -20,7 +20,7 @@
import csv import csv
import typing as t import typing as t
import unittest import unittest
from datetime import timedelta from datetime import timedelta, date
import httpx import httpx
from click.testing import Result from click.testing import Result
@ -29,6 +29,7 @@ from flask.testing import FlaskCliRunner
from test_site import db from test_site import db
from testlib import create_test_app, get_client, set_locale from testlib import create_test_app, get_client, set_locale
from testlib_journal_entry import add_journal_entry, NEXT_URI
class CurrencyData: class CurrencyData:
@ -46,14 +47,14 @@ class CurrencyData:
"""The name.""" """The name."""
zza: CurrencyData = CurrencyData("ZZA", "Testing Dollar #A") USD: CurrencyData = CurrencyData("USD", "US Dollar")
"""The first test currency.""" """The US dollars."""
zzb: CurrencyData = CurrencyData("ZZB", "Testing Dollar #B") EUR: CurrencyData = CurrencyData("EUR", "Euro")
"""The second test currency.""" """The European dollars."""
zzc: CurrencyData = CurrencyData("ZZC", "Testing Dollar #C") TWD: CurrencyData = CurrencyData("TWD", "Taiwan dollars")
"""The third test currency.""" """The Taiwan dollars."""
zzd: CurrencyData = CurrencyData("ZZD", "Testing Dollar #D") JPY: CurrencyData = CurrencyData("JPY", "Japanese yen")
"""The fourth test currency.""" """The Japanese yen."""
PREFIX: str = "/accounting/currencies" PREFIX: str = "/accounting/currencies"
"""The URL prefix for the currency management.""" """The URL prefix for the currency management."""
@ -140,17 +141,17 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(f"{PREFIX}/store", response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zza.code, "code": USD.code,
"name": zza.name}) "name": USD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{zza.code}") self.assertEqual(response.headers["Location"], f"{PREFIX}/{USD.code}")
response = self.client.post(f"{PREFIX}/store", response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzb.code, "code": EUR.code,
"name": zzb.name}) "name": EUR.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{zzb.code}") self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
def test_nobody(self) -> None: def test_nobody(self) -> None:
"""Test the permission as nobody. """Test the permission as nobody.
@ -163,7 +164,7 @@ class CurrencyTestCase(unittest.TestCase):
response = client.get(PREFIX) response = client.get(PREFIX)
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.get(f"{PREFIX}/{zza.code}") response = client.get(f"{PREFIX}/{USD.code}")
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.get(f"{PREFIX}/create") response = client.get(f"{PREFIX}/create")
@ -171,20 +172,20 @@ class CurrencyTestCase(unittest.TestCase):
response = client.post(f"{PREFIX}/store", response = client.post(f"{PREFIX}/store",
data={"csrf_token": csrf_token, data={"csrf_token": csrf_token,
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.get(f"{PREFIX}/{zza.code}/edit") response = client.get(f"{PREFIX}/{USD.code}/edit")
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.post(f"{PREFIX}/{zza.code}/update", response = client.post(f"{PREFIX}/{USD.code}/update",
data={"csrf_token": csrf_token, data={"csrf_token": csrf_token,
"code": zzd.code, "code": JPY.code,
"name": zzd.name}) "name": JPY.name})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.post(f"{PREFIX}/{zzb.code}/delete", response = client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": csrf_token}) data={"csrf_token": csrf_token})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
@ -199,7 +200,7 @@ class CurrencyTestCase(unittest.TestCase):
response = client.get(PREFIX) response = client.get(PREFIX)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/{zza.code}") response = client.get(f"{PREFIX}/{USD.code}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = client.get(f"{PREFIX}/create") response = client.get(f"{PREFIX}/create")
@ -207,20 +208,20 @@ class CurrencyTestCase(unittest.TestCase):
response = client.post(f"{PREFIX}/store", response = client.post(f"{PREFIX}/store",
data={"csrf_token": csrf_token, data={"csrf_token": csrf_token,
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.get(f"{PREFIX}/{zza.code}/edit") response = client.get(f"{PREFIX}/{USD.code}/edit")
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.post(f"{PREFIX}/{zza.code}/update", response = client.post(f"{PREFIX}/{USD.code}/update",
data={"csrf_token": csrf_token, data={"csrf_token": csrf_token,
"code": zzd.code, "code": JPY.code,
"name": zzd.name}) "name": JPY.name})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
response = client.post(f"{PREFIX}/{zzb.code}/delete", response = client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": csrf_token}) data={"csrf_token": csrf_token})
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
@ -234,7 +235,7 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.get(PREFIX) response = self.client.get(PREFIX)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/{zza.code}") response = self.client.get(f"{PREFIX}/{USD.code}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/create") response = self.client.get(f"{PREFIX}/create")
@ -242,22 +243,22 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(f"{PREFIX}/store", response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{zzc.code}") self.assertEqual(response.headers["Location"], f"{PREFIX}/{TWD.code}")
response = self.client.get(f"{PREFIX}/{zza.code}/edit") response = self.client.get(f"{PREFIX}/{USD.code}/edit")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post(f"{PREFIX}/{zza.code}/update", response = self.client.post(f"{PREFIX}/{USD.code}/update",
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzd.code, "code": JPY.code,
"name": zzd.name}) "name": JPY.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{zzd.code}") self.assertEqual(response.headers["Location"], f"{PREFIX}/{JPY.code}")
response = self.client.post(f"{PREFIX}/{zzb.code}/delete", response = self.client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.csrf_token}) data={"csrf_token": self.csrf_token})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], PREFIX) self.assertEqual(response.headers["Location"], PREFIX)
@ -270,31 +271,31 @@ class CurrencyTestCase(unittest.TestCase):
from accounting.models import Currency from accounting.models import Currency
create_uri: str = f"{PREFIX}/create" create_uri: str = f"{PREFIX}/create"
store_uri: str = f"{PREFIX}/store" store_uri: str = f"{PREFIX}/store"
detail_uri: str = f"{PREFIX}/{zzc.code}" detail_uri: str = f"{PREFIX}/{TWD.code}"
response: httpx.Response response: httpx.Response
with self.app.app_context(): with self.app.app_context():
self.assertEqual({x.code for x in Currency.query.all()}, self.assertEqual({x.code for x in Currency.query.all()},
{zza.code, zzb.code}) {USD.code, EUR.code})
# Missing CSRF token # Missing CSRF token
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"code": zzc.code, data={"code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
# CSRF token mismatch # CSRF token mismatch
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": f"{self.csrf_token}-2", data={"csrf_token": f"{self.csrf_token}-2",
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 400) self.assertEqual(response.status_code, 400)
# Empty code # Empty code
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": " ", "code": " ",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri) self.assertEqual(response.headers["Location"], create_uri)
@ -302,7 +303,7 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": " create ", "code": " create ",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri) self.assertEqual(response.headers["Location"], create_uri)
@ -310,14 +311,14 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": " zzc ", "code": " zzc ",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri) self.assertEqual(response.headers["Location"], create_uri)
# Empty name # Empty name
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzc.code, "code": TWD.code,
"name": " "}) "name": " "})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri) self.assertEqual(response.headers["Location"], create_uri)
@ -325,26 +326,26 @@ class CurrencyTestCase(unittest.TestCase):
# Success, with spaces to be stripped # Success, with spaces to be stripped
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": f" {zzc.code} ", "code": f" {TWD.code} ",
"name": f" {zzc.name} "}) "name": f" {TWD.name} "})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
# Duplicated code # Duplicated code
response = self.client.post(store_uri, response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri) self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context(): with self.app.app_context():
self.assertEqual({x.code for x in Currency.query.all()}, self.assertEqual({x.code for x in Currency.query.all()},
{zza.code, zzb.code, zzc.code}) {USD.code, EUR.code, TWD.code})
currency: Currency = db.session.get(Currency, zzc.code) currency: Currency = db.session.get(Currency, TWD.code)
self.assertEqual(currency.code, zzc.code) self.assertEqual(currency.code, TWD.code)
self.assertEqual(currency.name_l10n, zzc.name) self.assertEqual(currency.name_l10n, TWD.name)
def test_basic_update(self) -> None: def test_basic_update(self) -> None:
"""Tests the basic rules to update a user. """Tests the basic rules to update a user.
@ -352,30 +353,30 @@ class CurrencyTestCase(unittest.TestCase):
:return: None. :return: None.
""" """
from accounting.models import Currency from accounting.models import Currency
detail_uri: str = f"{PREFIX}/{zza.code}" detail_uri: str = f"{PREFIX}/{USD.code}"
edit_uri: str = f"{PREFIX}/{zza.code}/edit" edit_uri: str = f"{PREFIX}/{USD.code}/edit"
update_uri: str = f"{PREFIX}/{zza.code}/update" update_uri: str = f"{PREFIX}/{USD.code}/update"
detail_c_uri: str = f"{PREFIX}/{zzc.code}" detail_c_uri: str = f"{PREFIX}/{TWD.code}"
response: httpx.Response response: httpx.Response
# Success, with spaces to be stripped # Success, with spaces to be stripped
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": f" {zza.code} ", "code": f" {USD.code} ",
"name": f" {zza.name}-1 "}) "name": f" {USD.name}-1 "})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency: Currency = db.session.get(Currency, zza.code) currency: Currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.code, zza.code) self.assertEqual(currency.code, USD.code)
self.assertEqual(currency.name_l10n, f"{zza.name}-1") self.assertEqual(currency.name_l10n, f"{USD.name}-1")
# Empty code # Empty code
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": " ", "code": " ",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri) self.assertEqual(response.headers["Location"], edit_uri)
@ -383,7 +384,7 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": " create ", "code": " create ",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri) self.assertEqual(response.headers["Location"], edit_uri)
@ -391,14 +392,14 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": "abc/def", "code": "abc/def",
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri) self.assertEqual(response.headers["Location"], edit_uri)
# Empty name # Empty name
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzc.code, "code": TWD.code,
"name": " "}) "name": " "})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri) self.assertEqual(response.headers["Location"], edit_uri)
@ -406,16 +407,16 @@ class CurrencyTestCase(unittest.TestCase):
# Duplicated code # Duplicated code
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzb.code, "code": EUR.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri) self.assertEqual(response.headers["Location"], edit_uri)
# Change code # Change code
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zzc.code, "code": TWD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_c_uri) self.assertEqual(response.headers["Location"], detail_c_uri)
@ -431,20 +432,20 @@ class CurrencyTestCase(unittest.TestCase):
:return: None. :return: None.
""" """
from accounting.models import Currency from accounting.models import Currency
detail_uri: str = f"{PREFIX}/{zza.code}" detail_uri: str = f"{PREFIX}/{USD.code}"
update_uri: str = f"{PREFIX}/{zza.code}/update" update_uri: str = f"{PREFIX}/{USD.code}/update"
currency: Currency | None currency: Currency | None
response: httpx.Response response: httpx.Response
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": f" {zza.code} ", "code": f" {USD.code} ",
"name": f" {zza.name} "}) "name": f" {USD.name} "})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertIsNotNone(currency) self.assertIsNotNone(currency)
currency.created_at \ currency.created_at \
= currency.created_at - timedelta(seconds=5) = currency.created_at - timedelta(seconds=5)
@ -453,13 +454,13 @@ class CurrencyTestCase(unittest.TestCase):
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zza.code, "code": USD.code,
"name": zzc.name}) "name": TWD.name})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertIsNotNone(currency) self.assertIsNotNone(currency)
self.assertLess(currency.created_at, self.assertLess(currency.created_at,
currency.updated_at) currency.updated_at)
@ -472,25 +473,25 @@ class CurrencyTestCase(unittest.TestCase):
from accounting.models import Currency from accounting.models import Currency
editor_username, editor2_username = "editor", "editor2" editor_username, editor2_username = "editor", "editor2"
client, csrf_token = get_client(self.app, editor2_username) client, csrf_token = get_client(self.app, editor2_username)
detail_uri: str = f"{PREFIX}/{zza.code}" detail_uri: str = f"{PREFIX}/{USD.code}"
update_uri: str = f"{PREFIX}/{zza.code}/update" update_uri: str = f"{PREFIX}/{USD.code}/update"
currency: Currency currency: Currency
response: httpx.Response response: httpx.Response
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.created_by.username, editor_username) self.assertEqual(currency.created_by.username, editor_username)
self.assertEqual(currency.updated_by.username, editor_username) self.assertEqual(currency.updated_by.username, editor_username)
response = client.post(update_uri, response = client.post(update_uri,
data={"csrf_token": csrf_token, data={"csrf_token": csrf_token,
"code": zza.code, "code": USD.code,
"name": f"{zza.name}-2"}) "name": f"{USD.name}-2"})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.created_by.username, editor_username) self.assertEqual(currency.created_by.username, editor_username)
self.assertEqual(currency.updated_by.username, editor2_username) self.assertEqual(currency.updated_by.username, editor2_username)
@ -502,14 +503,14 @@ class CurrencyTestCase(unittest.TestCase):
response: httpx.Response response: httpx.Response
response = self.client.get( response = self.client.get(
f"/accounting/api/currencies/exists-code?q={zza.code}") f"/accounting/api/currencies/exists-code?q={USD.code}")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = response.json() data = response.json()
self.assertEqual(set(data.keys()), {"exists"}) self.assertEqual(set(data.keys()), {"exists"})
self.assertTrue(data["exists"]) self.assertTrue(data["exists"])
response = self.client.get( response = self.client.get(
f"/accounting/api/currencies/exists-code?q={zza.code}-1") f"/accounting/api/currencies/exists-code?q={USD.code}-1")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
data = response.json() data = response.json()
self.assertEqual(set(data.keys()), {"exists"}) self.assertEqual(set(data.keys()), {"exists"})
@ -521,60 +522,60 @@ class CurrencyTestCase(unittest.TestCase):
:return: None :return: None
""" """
from accounting.models import Currency from accounting.models import Currency
detail_uri: str = f"{PREFIX}/{zza.code}" detail_uri: str = f"{PREFIX}/{USD.code}"
update_uri: str = f"{PREFIX}/{zza.code}/update" update_uri: str = f"{PREFIX}/{USD.code}/update"
currency: Currency currency: Currency
response: httpx.Response response: httpx.Response
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, zza.name) self.assertEqual(currency.name_l10n, USD.name)
self.assertEqual(currency.l10n, []) self.assertEqual(currency.l10n, [])
set_locale(self.client, self.csrf_token, "zh_Hant") set_locale(self.client, self.csrf_token, "zh_Hant")
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zza.code, "code": USD.code,
"name": f"{zza.name}-zh_Hant"}) "name": f"{USD.name}-zh_Hant"})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, zza.name) self.assertEqual(currency.name_l10n, USD.name)
self.assertEqual({(x.locale, x.name) for x in currency.l10n}, self.assertEqual({(x.locale, x.name) for x in currency.l10n},
{("zh_Hant", f"{zza.name}-zh_Hant")}) {("zh_Hant", f"{USD.name}-zh_Hant")})
set_locale(self.client, self.csrf_token, "en") set_locale(self.client, self.csrf_token, "en")
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zza.code, "code": USD.code,
"name": f"{zza.name}-2"}) "name": f"{USD.name}-2"})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, f"{zza.name}-2") self.assertEqual(currency.name_l10n, f"{USD.name}-2")
self.assertEqual({(x.locale, x.name) for x in currency.l10n}, self.assertEqual({(x.locale, x.name) for x in currency.l10n},
{("zh_Hant", f"{zza.name}-zh_Hant")}) {("zh_Hant", f"{USD.name}-zh_Hant")})
set_locale(self.client, self.csrf_token, "zh_Hant") set_locale(self.client, self.csrf_token, "zh_Hant")
response = self.client.post(update_uri, response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token, data={"csrf_token": self.csrf_token,
"code": zza.code, "code": USD.code,
"name": f"{zza.name}-zh_Hant-2"}) "name": f"{USD.name}-zh_Hant-2"})
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri) self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context(): with self.app.app_context():
currency = db.session.get(Currency, zza.code) currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, f"{zza.name}-2") self.assertEqual(currency.name_l10n, f"{USD.name}-2")
self.assertEqual({(x.locale, x.name) for x in currency.l10n}, self.assertEqual({(x.locale, x.name) for x in currency.l10n},
{("zh_Hant", f"{zza.name}-zh_Hant-2")}) {("zh_Hant", f"{USD.name}-zh_Hant-2")})
def test_delete(self) -> None: def test_delete(self) -> None:
"""Tests to delete a currency. """Tests to delete a currency.
@ -582,15 +583,58 @@ class CurrencyTestCase(unittest.TestCase):
:return: None. :return: None.
""" """
from accounting.models import Currency from accounting.models import Currency
detail_uri: str = f"{PREFIX}/{zza.code}" detail_uri: str = f"{PREFIX}/{JPY.code}"
delete_uri: str = f"{PREFIX}/{zza.code}/delete" delete_uri: str = f"{PREFIX}/{JPY.code}/delete"
list_uri: str = PREFIX list_uri: str = PREFIX
response: httpx.Response response: httpx.Response
runner: FlaskCliRunner = self.app.test_cli_runner()
with self.app.app_context():
from accounting.models import BaseAccount
if BaseAccount.query.first() is None:
result = runner.invoke(args="accounting-init-base")
self.assertEqual(result.exit_code, 0)
response = self.client.post("/accounting/accounts/store",
data={"csrf_token": self.csrf_token,
"base_code": "1111",
"title": "Cash"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"],
"/accounting/accounts/1111-001")
response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token,
"code": JPY.code,
"name": JPY.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
add_journal_entry(self.client,
form={"csrf_token": self.csrf_token,
"next": NEXT_URI,
"date": date.today().isoformat(),
"currency-1-code": EUR.code,
"currency-1-credit-1-account_code": "1111-001",
"currency-1-credit-1-amount": "20"})
with self.app.app_context(): with self.app.app_context():
self.assertEqual({x.code for x in Currency.query.all()}, self.assertEqual({x.code for x in Currency.query.all()},
{zza.code, zzb.code}) {USD.code, EUR.code, JPY.code})
# Cannot delete the default currency
response = self.client.post(f"{PREFIX}/{USD.code}/delete",
data={"csrf_token": self.csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{USD.code}")
# Cannot delete the account that is in use
response = self.client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
# Success
response = self.client.get(detail_uri) response = self.client.get(detail_uri)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
response = self.client.post(delete_uri, response = self.client.post(delete_uri,
@ -600,7 +644,7 @@ class CurrencyTestCase(unittest.TestCase):
with self.app.app_context(): with self.app.app_context():
self.assertEqual({x.code for x in Currency.query.all()}, self.assertEqual({x.code for x in Currency.query.all()},
{zzb.code}) {USD.code, EUR.code})
response = self.client.get(detail_uri) response = self.client.get(detail_uri)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)