示例#1
0
 def setUp(self):
     self.s = Subscription(
         customer=self.customer,
         product=self.product,
         buy_price=150
     )
     self.s.save()
示例#2
0
 def setUp(self):
     self.customer = create_customer()
     self.subscription = Subscription(
         customer=self.customer,
         product=Product1.objects.get(pk=1),
         buy_price=150,
     )
     self.deactivator = create_customer().user
    def test_subscription_name(self):
        product = products.Product1.objects.get(pk=self.TEST_PRODUCT_ID)
        s = Subscription(
            customer=self.customer,
            product=product,
            buy_price=150,
        )
        s.save()

        self.assertEqual(s.name_for_user, product.name)
示例#4
0
    def test_subbscription_stores_duration(self):
        """
        Every new subscription should take its duration from the purchaed product
        """
        product = products.Product1.objects.get(pk=self.TEST_PRODUCT_ID)
        product.duration = timedelta(days=221)
        product.save()

        s = Subscription(customer=self.customer,
                         product=product,
                         buy_price=150)
        s.save()

        self.assertEqual(s.duration, timedelta(days=221))
    def setUpTestData(cls):
        cls.customer = create_customer()
        cls.teacher = create_teacher(works_24x7=True)
        cls.subscription = Subscription(
            customer=cls.customer,
            product=Product1.objects.get(pk=1),
            buy_price=150,
        )

        cls.subscription.save()
 def test_no_deletion(self):
     """
     No buyable product can ever be deleted.
     """
     product = products.Product1.objects.get(pk=self.TEST_PRODUCT_ID)
     s = Subscription(customer=self.customer,
                      product=product,
                      buy_price=150)
     s.save()
     self.assertFalse(s.is_fully_used)
     s.delete()
     s.refresh_from_db()
     self.assertTrue(s.is_fully_used)
示例#7
0
class TestSubscriptionSignals(TestCase):
    fixtures = ('lessons', 'products')

    def setUp(self):
        self.customer = create_customer()
        self.subscription = Subscription(
            customer=self.customer,
            product=Product1.objects.get(pk=1),
            buy_price=150,
        )
        self.deactivator = create_customer().user

    def test_deactivation_signal_is_beeing_sent(self):
        handler = MagicMock()
        signals.subscription_deactivated.connect(handler)
        self.subscription.deactivate()
        self.assertEqual(handler.call_count, 1)

    def test_log_entry_creation(self):
        self.subscription.deactivate(user=self.deactivator)

        log_entry = LogEntry.objects.first()
        self.assertEqual(log_entry.user, self.deactivator)
        self.assertIn('deactivated', log_entry.change_message)
示例#8
0
class BuySubscriptionTestCase(TestCase):
    fixtures = ('lessons', 'products')
    TEST_PRODUCT_ID = 1

    @classmethod
    def setUpTestData(cls):
        cls.customer = create_customer()
        cls.product = products.Product1.objects.get(pk=cls.TEST_PRODUCT_ID)

    def setUp(self):
        self.subscription = Subscription(customer=self.customer,
                                         product=self.product,
                                         buy_price=150)
        self.subscription.save()

    def test_buy_a_single_subscription(self):
        """
        When buing a subscription, all lessons in it should become beeing
        available to the customer
        """

        # TODO: Clean this up
        def _get_lessons_count(product):
            cnt = 0
            for lesson_type in product.LESSONS:
                cnt += getattr(product, lesson_type).all().count()
            return cnt

        active_lessons_count = Class.objects.filter(
            subscription=self.subscription).count()
        active_lessons_in_product_count = _get_lessons_count(self.product)

        # two lessons with natives and four with curators
        self.assertEqual(
            active_lessons_count, active_lessons_in_product_count,
            'When buying a subscription should add all of its available lessons'
        )

    test_second_time = test_buy_a_single_subscription  # let's test for the second time :-)

    def test_store_class_source(self):
        """
        When buying a subscription, every purchased class should have a sign
        about that it's purchased buy subscription.
        """
        for c in self.subscription.classes.all():
            self.assertEqual(c.buy_source, 'subscription')

    def test_subbscription_stores_duration(self):
        """
        Every new subscription should take its duration from the purchaed product
        """
        product = products.Product1.objects.get(pk=self.TEST_PRODUCT_ID)
        product.duration = timedelta(days=221)
        product.save()

        s = Subscription(customer=self.customer,
                         product=product,
                         buy_price=150)
        s.save()

        self.assertEqual(s.duration, timedelta(days=221))

    def test_disabling_subscription(self):
        for c in self.subscription.classes.all():
            self.assertFalse(c.is_fully_used)

        # now, disable the subscription for any reason
        self.subscription.deactivate()
        self.subscription.save()
        for c in self.subscription.classes.all():
            self.assertTrue(
                c.is_fully_used,
                'Every class in subscription should become inactive now')

    def test_mark_as_fully_used(self):
        """
        Buy a subscription, than mark all classes from it as used, one by one
        """
        self.assertFalse(self.subscription.is_fully_used)

        classes = [c for c in self.subscription.classes.all()]

        for c in classes[:-1]:
            c.mark_as_fully_used()
            self.assertFalse(self.subscription.is_fully_used)

        classes[-1].mark_as_fully_used()
        self.assertTrue(
            self.subscription.is_fully_used
        )  # the last lesson should have marked it's parent subscription as fully used

    def test_finished_class_sets_the_first_lesson_date_of_the_parent_subscription(
            self, *args):
        first_class = self.subscription.classes.first()
        first_class.save()

        self.subscription.update_first_lesson_date = MagicMock(spec=True)
        first_class.mark_as_fully_used()
        self.assertEqual(self.subscription.update_first_lesson_date.call_count,
                         1)
示例#9
0
class TestSubscriptionUnit(TestCase):
    fixtures = ('products', 'lessons')

    @classmethod
    def setUpTestData(cls):
        cls.product = Product1.objects.get(pk=1)
        cls.product.duration = timedelta(days=5)
        cls.product.save()

        cls.customer = create_customer()

    def setUp(self):
        self.s = Subscription(
            customer=self.customer,
            product=self.product,
            buy_price=150
        )
        self.s.save()

    @patch('market.models.signals.class_scheduled.send')
    def _schedule(self, c, date, *args):
        c.timeline = mixer.blend(
            'timeline.Entry',
            lesson_type=c.lesson_type,
            teacher=create_teacher(),
            start=date,
        )
        c.save()

    def test_is_due(self):
        self.s.first_lesson_date = self.tzdatetime(2032, 12, 2, 12, 0)
        self.s.save()
        self.assertFalse(self.s.is_due())
        with freeze_time('2032-12-10 12:00'):  # move 9 days forward
            self.assertTrue(self.s.is_due())

    def test_is_due_for_subscription_without_any_completed_class(self):
        """
        For subscription without classes is_due should be based on their buy_date
        """
        self.assertFalse(self.s.is_due())
        with freeze_time('2032-12-10 12:00'):  # move 9 days forward
            self.assertTrue(self.s.is_due())

    def test_update_first_lesson_date(self):
        first_class = self.s.classes.first()

        self._schedule(first_class, self.tzdatetime(2032, 12, 5, 13, 33))

        self.s.first_lesson_date = None  # set to None in case of first_class has set it up manualy — we check the subscription, not the class logic

        self.s.update_first_lesson_date()
        self.s.refresh_from_db()
        self.assertEqual(self.s.first_lesson_date, self.tzdatetime(2032, 12, 5, 13, 33))

    def test_update_first_lesson_uses_only_first_lesson(self):
        classes = self.s.classes.all()
        self._schedule(classes[0], self.tzdatetime(2032, 12, 5, 13, 33))
        self._schedule(classes[1], self.tzdatetime(2033, 12, 5, 13, 33))

        self.s.update_first_lesson_date()
        self.s.refresh_from_db()
        self.assertEqual(self.s.first_lesson_date, self.tzdatetime(2032, 12, 5, 13, 33))  # should be taken from the first class, not from the second