Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

notification_tests.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from django.core import mail
  2. from django.test import TestCase
  3. from django.contrib.auth.models import User
  4. from django_dynamic_fixture import get
  5. from oscar.apps.catalogue.models import Product, ProductClass
  6. from oscar.apps.partner.models import StockRecord
  7. from oscar.apps.catalogue.notification.models import ProductNotification
  8. class TestANotificationForARegisteredUser(TestCase):
  9. def setUp(self):
  10. self.product = get(Product)
  11. self.user = get(User)
  12. self.notification = ProductNotification.objects.create(
  13. user=self.user,
  14. product=self.product)
  15. def test_uses_the_users_email_for_notifications(self):
  16. self.assertEquals(self.notification.get_notification_email(),
  17. self.user.email)
  18. def test_defaults_to_inactive_status(self):
  19. self.assertFalse(self.notification.is_active())
  20. def test_defaults_to_unconfirmed(self):
  21. self.assertFalse(self.notification.is_confirmed())
  22. class TestANotificationForAnAnonymousUser(TestCase):
  23. def setUp(self):
  24. self.product = get(Product)
  25. self.email = 'test@oscarcommerce.com'
  26. self.notification = ProductNotification.objects.create(
  27. email=self.email,
  28. product=self.product)
  29. def test_uses_the_specified_email_for_notifications(self):
  30. self.assertEquals(self.notification.get_notification_email(),
  31. self.email)
  32. class TestNotificationEmails(TestCase):
  33. def setUp(self):
  34. self.product_class = ProductClass.objects.create(name='books')
  35. self.product = get(Product, product_class=self.product_class,
  36. title='product', upc='000000000001')
  37. self.reference_product = get(Product, product_class=self.product_class,
  38. title='reference product', upc='000000000002')
  39. self.first_user = get(User, email='firstuser@one.com')
  40. self.second_user = get(User, email='seconduser@two.com')
  41. get(StockRecord, product=self.product, num_in_stock=0)
  42. def test_are_not_sent_when_stock_level_remains_0(self):
  43. stock_record = StockRecord.objects.get(id=1)
  44. self.assertEquals(stock_record.num_in_stock, 0)
  45. stock_record.save()
  46. self.assertEquals(stock_record.num_in_stock, 0)
  47. self.assertEquals(len(mail.outbox), 0)
  48. def test_are_not_sent_when_there_are_no_notifications(self):
  49. stock_record = StockRecord.objects.get(id=1)
  50. self.assertEquals(stock_record.num_in_stock, 0)
  51. stock_record.num_in_stock = 20
  52. stock_record.save()
  53. self.assertEquals(len(mail.outbox), 0)
  54. def test_are_sent_correctly_when_there_are_notifications(self):
  55. ProductNotification.objects.create(user=self.first_user,
  56. product=self.product,
  57. status=ProductNotification.ACTIVE)
  58. ProductNotification.objects.create(user=self.second_user,
  59. product=self.product,
  60. status=ProductNotification.ACTIVE)
  61. ProductNotification.objects.create(user=None,
  62. email='anonymous@test.com',
  63. product=self.product,
  64. status=ProductNotification.ACTIVE)
  65. ProductNotification.objects.create(user=self.second_user,
  66. product=self.reference_product,
  67. status=ProductNotification.ACTIVE)
  68. ProductNotification.objects.create(product=self.product,
  69. status=ProductNotification.INACTIVE)
  70. stock_record = StockRecord.objects.get(id=1)
  71. self.assertEquals(stock_record.num_in_stock, 0)
  72. stock_record.num_in_stock = 20
  73. stock_record.save()
  74. self.assertEquals(len(mail.outbox), 3)
  75. self.assertItemsEqual(
  76. [e.to[0] for e in mail.outbox],
  77. [self.first_user.email, self.second_user.email, 'anonymous@test.com'],
  78. )
  79. notification = ProductNotification.objects.get(id=1)
  80. self.assertEquals(notification.status, ProductNotification.INACTIVE)
  81. self.assertNotEquals(notification.date_notified, None)
  82. notification = ProductNotification.objects.get(id=2)
  83. self.assertEquals(notification.status, ProductNotification.INACTIVE)
  84. self.assertNotEquals(notification.date_notified, None)
  85. notification = ProductNotification.objects.get(id=3)
  86. self.assertEquals(notification.status, ProductNotification.INACTIVE)
  87. self.assertNotEquals(notification.date_notified, None)
  88. notification = ProductNotification.objects.get(id=4)
  89. self.assertEquals(notification.status, ProductNotification.ACTIVE)
  90. self.assertEquals(notification.date_notified, None)