diff --git a/longclaw/longclawcheckout/tests.py b/longclaw/longclawcheckout/tests.py index cf288f7..71e7f0f 100644 --- a/longclaw/longclawcheckout/tests.py +++ b/longclaw/longclawcheckout/tests.py @@ -189,4 +189,4 @@ class GatewayTests(TestCase): def test_js_tag(self): js = tags.gateway_client_js() - self.assertIsInstance(js, (tuple, list)) \ No newline at end of file + self.assertIsInstance(js, (tuple, list)) diff --git a/longclaw/longclawcore/views.py b/longclaw/longclawcore/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/longclaw/longclawcore/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here. diff --git a/longclaw/longclawshipping/tests.py b/longclaw/longclawshipping/tests.py index 8001202..2a0cf6c 100644 --- a/longclaw/longclawshipping/tests.py +++ b/longclaw/longclawshipping/tests.py @@ -1,9 +1,11 @@ from django.test import TestCase from django.forms.models import model_to_dict -from longclaw.tests.utils import LongclawTestCase, AddressFactory, CountryFactory +from longclaw.tests.utils import LongclawTestCase, AddressFactory, CountryFactory, ShippingRateFactory from longclaw.longclawshipping.forms import AddressForm +from longclaw.longclawshipping.utils import get_shipping_cost +from longclaw.longclawsettings.models import LongclawSettings -class AddressTest(LongclawTestCase): +class ShippingTests(LongclawTestCase): def setUp(self): self.country = CountryFactory() def test_create_address(self): @@ -19,6 +21,21 @@ class AddressTest(LongclawTestCase): } self.post_test(data, 'longclaw_address_list') + def test_shipping_cost(self): + sr = ShippingRateFactory(countries=[self.country]) + result = get_shipping_cost(self.country.pk, sr.name, LongclawSettings()) + self.assertEqual(result["rate"], sr.rate) + + def test_multiple_shipping_cost(self): + sr = ShippingRateFactory(countries=[self.country]) + sr2 = ShippingRateFactory(countries=[self.country]) + result = get_shipping_cost(self.country.pk, sr.name, LongclawSettings()) + self.assertEqual(result["rate"], sr.rate) + + def test_default_shipping_cost(self): + ls = LongclawSettings(default_shipping_enabled=True) + result = get_shipping_cost("GB", "Something", ls) + self.assertEqual(ls.default_shipping_rate, result["rate"]) class AddressFormTest(TestCase): @@ -28,3 +45,4 @@ class AddressFormTest(TestCase): def test_address_form(self): form = AddressForm(data=model_to_dict(self.address)) self.assertTrue(form.is_valid(), form.errors.as_json()) + diff --git a/longclaw/longclawshipping/utils.py b/longclaw/longclawshipping/utils.py index 13418c8..dfb9d24 100644 --- a/longclaw/longclawshipping/utils.py +++ b/longclaw/longclawshipping/utils.py @@ -4,32 +4,40 @@ from longclaw.longclawshipping import models class InvalidShippingRate(Exception): pass + class InvalidShippingCountry(Exception): pass + def get_shipping_cost(country_code, name, settings): """ Return the shipping cost for a given country code and shipping option (shipping rate name) """ - try: - qrs = models.ShippingRate.objects.filter(countries__in=[country_code]) - try: - if qrs.count() > 1: - shipping_rate = qrs.filter(name=name)[0] - else: - shipping_rate = qrs[0] - return { - "rate": shipping_rate.rate, - "description": shipping_rate.description, - "carrier": shipping_rate.carrier - } - except models.ShippingRate.DoesNotExist: - raise InvalidShippingRate - - except models.ShippingRate.DoesNotExist: + qrs = models.ShippingRate.objects.filter(countries__in=[country_code]) + count = qrs.count() + if count == 1: + shipping_rate_qrs = qrs[0] + shipping_rate = { + "rate": shipping_rate_qrs.rate, + "description": shipping_rate_qrs.description, + "carrier": shipping_rate_qrs.carrier} + elif count > 1: + qrs = qrs.filter(name=name) + if qrs.count() == 1: + shipping_rate_qrs = qrs[0] + shipping_rate = { + "rate": shipping_rate_qrs.rate, + "description": shipping_rate_qrs.description, + "carrier": shipping_rate_qrs.carrier} + else: + raise InvalidShippingRate() + else: if settings.default_shipping_enabled: - return {"rate": settings.default_shipping_rate, - "description": "Standard shipping to rest of world", - "carrier": settings.default_shipping_rate} + shipping_rate = { + "rate": settings.default_shipping_rate, + "description": "Standard shipping to rest of world", + "carrier": settings.default_shipping_carrier} + else: raise InvalidShippingCountry + return shipping_rate