You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from decimal import Decimal
  2. from django.test import TestCase
  3. from oscar.apps.address.models import Country
  4. from oscar.apps.basket.models import Basket
  5. from oscar.apps.order.models import ShippingAddress, Order, Line, ShippingEvent, ShippingEventType, ShippingEventQuantity
  6. ORDER_PLACED = 'order_placed'
  7. class ShippingAddressTest(TestCase):
  8. def test_titleless_salutation_is_stripped(self):
  9. country = Country.objects.get(iso_3166_1_a2='GB')
  10. a = ShippingAddress.objects.create(last_name='Barrington', line1="75 Smith Road", postcode="N4 8TY", country=country)
  11. self.assertEquals("Barrington", a.salutation())
  12. class OrderTest(TestCase):
  13. fixtures = ['sample-order.json']
  14. def setUp(self):
  15. self.order = Order.objects.get(number='100002')
  16. def event(self, type):
  17. """
  18. Creates an order-level shipping event
  19. """
  20. type = ShippingEventType.objects.get(code=type)
  21. event = ShippingEvent.objects.create(order=self.order, event_type=type)
  22. for line in self.order.lines.all():
  23. ShippingEventQuantity.objects.create(event=event, line=line)
  24. def test_shipping_status_is_empty_with_no_events(self):
  25. self.assertEquals("", self.order.shipping_status)
  26. def test_shipping_status_after_one_order_level_events(self):
  27. self.event(ORDER_PLACED)
  28. self.assertEquals("Order placed", self.order.shipping_status)
  29. class LineTest(TestCase):
  30. fixtures = ['sample-order.json']
  31. def setUp(self):
  32. self.order = Order.objects.get(number='100002')
  33. self.line = self.order.lines.get(id=1)
  34. def event(self, type, quantity=None):
  35. """
  36. Creates a shipping event for the test line
  37. """
  38. event = ShippingEvent.objects.create(order=self.order, event_type=type)
  39. if quantity == None:
  40. quantity = self.line.quantity
  41. event_quantity = ShippingEventQuantity.objects.create(event=event, line=self.line, quantity=quantity)
  42. def test_shipping_status_is_empty_to_start_with(self):
  43. self.assertEquals('', self.line.shipping_status)
  44. def test_shipping_status_after_full_line_event(self):
  45. type = ShippingEventType.objects.get(code='order_placed')
  46. self.event(type)
  47. self.assertEquals(type.name, self.line.shipping_status)
  48. def test_shipping_status_after_two_full_line_events(self):
  49. type1 = ShippingEventType.objects.get(code='order_placed')
  50. self.event(type1)
  51. type2 = ShippingEventType.objects.get(code='dispatched')
  52. self.event(type2)
  53. self.assertEquals(type2.name, self.line.shipping_status)
  54. def test_shipping_status_after_partial_line_event(self):
  55. type = ShippingEventType.objects.get(code='order_placed')
  56. self.event(type, 3)
  57. expected = "%s (%d/%d items)" % (type.name, 3, self.line.quantity)
  58. self.assertEquals(expected, self.line.shipping_status)
  59. def test_has_passed_shipping_status_after_full_line_event(self):
  60. type = ShippingEventType.objects.get(code='order_placed')
  61. self.event(type)
  62. self.assertTrue(self.line.has_shipping_event_occurred(type))
  63. def test_has_passed_shipping_status_after_partial_line_event(self):
  64. type = ShippingEventType.objects.get(code='order_placed')
  65. self.event(type, self.line.quantity - 1)
  66. self.assertFalse(self.line.has_shipping_event_occurred(type))
  67. def test_has_passed_shipping_status_after_multiple_line_event(self):
  68. event_types = [ShippingEventType.objects.get(code='order_placed'),
  69. ShippingEventType.objects.get(code='dispatched')]
  70. for type in event_types:
  71. self.event(type)
  72. for type in event_types:
  73. self.assertTrue(self.line.has_shipping_event_occurred(type))
  74. def test_inconsistent_shipping_status_setting(self):
  75. type = ShippingEventType.objects.get(code='order_placed')
  76. self.event(type, self.line.quantity - 1)
  77. with self.assertRaises(ValueError):
  78. # Quantity is higher for second event than first
  79. type = ShippingEventType.objects.get(code='dispatched')
  80. self.event(type, self.line.quantity)
  81. def test_inconsistent_shipping_quantities(self):
  82. type = ShippingEventType.objects.get(code='order_placed')
  83. self.event(type, self.line.quantity - 1)
  84. with self.assertRaises(ValueError):
  85. # Total quantity is too high
  86. self.event(type, 2)
  87. class ShippingEventQuantityTest(TestCase):
  88. fixtures = ['sample-order.json']
  89. def setUp(self):
  90. self.order = Order.objects.get(number='100002')
  91. self.line = self.order.lines.get(id=1)
  92. def test_quantity_defaults_to_all(self):
  93. type = ShippingEventType.objects.get(code='order_placed')
  94. event = ShippingEvent.objects.create(order=self.order, event_type=type)
  95. event_quantity = ShippingEventQuantity.objects.create(event=event, line=self.line)
  96. self.assertEquals(self.line.quantity, event_quantity.quantity)