|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.contrib.admin.models import CHANGE, LogEntry |
| 5 | +from django.contrib.contenttypes.models import ContentType |
| 6 | + |
| 7 | +from apps.b2b.services import DealCurrencyChanger |
| 8 | + |
| 9 | +pytestmark = [ |
| 10 | + pytest.mark.django_db, |
| 11 | + pytest.mark.usefixtures("_set_current_user", "usd"), |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize("new_currency_code", ["usd", "UsD"]) |
| 16 | +def test(deal, new_currency_code): |
| 17 | + assert deal.currency == "RUB" |
| 18 | + |
| 19 | + DealCurrencyChanger(deal=deal, new_currency_code=new_currency_code)() |
| 20 | + deal.refresh_from_db() |
| 21 | + |
| 22 | + assert deal.currency == "USD" |
| 23 | + assert deal.currency_rate_on_creation == Decimal(100) |
| 24 | + |
| 25 | + |
| 26 | +def test_wrong_currency_code(deal): |
| 27 | + with pytest.raises(TypeError): |
| 28 | + DealCurrencyChanger(deal=deal, new_currency_code="NONEXISTANT-STUPID-CODE")() |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.auditlog |
| 32 | +def test_auditlog(deal, user): |
| 33 | + DealCurrencyChanger(deal=deal, new_currency_code="usd")() |
| 34 | + |
| 35 | + log = LogEntry.objects.filter( |
| 36 | + content_type=ContentType.objects.get_for_model(deal).id, |
| 37 | + ).last() |
| 38 | + |
| 39 | + assert log.action_flag == CHANGE |
| 40 | + assert "currency" in log.change_message |
| 41 | + assert "USD" in log.change_message |
| 42 | + |
| 43 | + assert log.user == user |
| 44 | + assert log.object_id == str(deal.id) |
| 45 | + assert log.object_repr == str(deal) |
0 commit comments