From ac1f7636b9a38d3e153eb833019342c4d88634c2 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Mon, 3 Apr 2017 14:40:56 +0200 Subject: [PATCH] Tested thrown exceptions values --- djmoney_rates/backends.py | 4 ++-- tests/test_backends.py | 18 +++++++++++++++--- tests/test_commands.py | 4 +++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/djmoney_rates/backends.py b/djmoney_rates/backends.py index b317216..7e4407b 100644 --- a/djmoney_rates/backends.py +++ b/djmoney_rates/backends.py @@ -28,7 +28,7 @@ def get_source_name(self): Return the name that identifies the ratings source """ if not self.source_name: - raise RateBackendError("'source_name' can't be empty or" + raise RateBackendError("'source_name' can't be empty or " "you should override 'get_source_name'") return self.source_name @@ -38,7 +38,7 @@ def get_base_currency(self): Return the base currency to which the rates are referred """ if not self.base_currency: - raise RateBackendError("'base_currency' can't be empty or" + raise RateBackendError("'base_currency' can't be empty or " "you should override 'get_base_currency'") return self.base_currency diff --git a/tests/test_backends.py b/tests/test_backends.py index 96bcc2f..93c64a9 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -31,9 +31,17 @@ def set_up(): money_rates_settings.OPENEXCHANGE_APP_ID = "fake-app-id" def test_source_name_empty_is_invalid(): - with pytest.raises(RateBackendError): + with pytest.raises(RateBackendError) as exc: BaseRateBackend().get_source_name() + assert "'source_name' can't be empty or you should override 'get_source_name'" in str(exc.value) + +def test_base_currency_empty_is_invalid(): + with pytest.raises(RateBackendError) as exc: + BaseRateBackend().get_base_currency() + + assert "'base_currency' can't be empty or you should override 'get_base_currency'" in str(exc.value) + @pytest.mark.django_db(transaction=True) def test_update_rates(): class RateBackend(BaseRateBackend): @@ -51,15 +59,19 @@ def get_rates(self): def test_missing_url_error(set_up): money_rates_settings.OPENEXCHANGE_URL = "" - with pytest.raises(ImproperlyConfigured): + with pytest.raises(ImproperlyConfigured) as exc: OpenExchangeBackend() + assert "OPENEXCHANGE_URL setting should not be empty when using OpenExchangeBackend" in str(exc.value) + def test_missing_app_id(set_up): money_rates_settings.OPENEXCHANGE_APP_ID = "" - with pytest.raises(ImproperlyConfigured): + with pytest.raises(ImproperlyConfigured) as exc: OpenExchangeBackend() + assert "OPENEXCHANGE_APP_ID setting should not be empty when using OpenExchangeBackend" in str(exc.value) + def test_url_is_correct(set_up): backend = OpenExchangeBackend() assert backend.url == "http://openexchangerates.org/api/latest.json?app_id=fake-app-id&base=USD" diff --git a/tests/test_commands.py b/tests/test_commands.py index f3b1513..ee9966a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -18,9 +18,11 @@ def get_rates(self): def test_fail_when_custom_backend_do_not_exists(): - with pytest.raises(CommandError): + with pytest.raises(CommandError) as exc: call_command("update_rates", "fake.custom.Backend") + assert "Cannot find custom backend fake.custom.Backend. Is it correct" in str(exc.value) + @pytest.mark.django_db(transaction=True) def test_custom_backend_used_when_specified(): call_command("update_rates", "tests.test_commands.CustomBackend")