Пример #1
0
class SingleProductGroupTest(unittest.TestCase):
    item_template = 'killer_tent{}'

    def setUp(self):
        self.client, self.app, self.db = get_app()
        self.app.testing = True

    def create_fixtures(self):
        self.item_name = self.item_template.format(random_string(8))
        self.item = ProductGroup(type='tent',
                                 name=self.item_name,
                                 capacity_max=1,
                                 expires=datetime(2012, 8, 31))
        self.db.session.add(self.item)

        self.db.session.commit()

    def create_purchases(self, tier, count):
        basket = Basket(self.user, 'GBP')
        basket[tier] = count
        basket.create_purchases()
        basket.ensure_purchase_capacity()
        payment = basket.create_payment(BankPayment)
        assert len(payment.purchases) == count
        self.db.session.commit()

        return payment.purchases

    def test_has_capacity(self):
        with self.app.app_context():
            self.create_fixtures()

            # With no parent this is a trivial test
            self.assertTrue(self.item.has_capacity())

            with self.assertRaises(ValueError):
                self.item.has_capacity(-1)

    @unittest.skip('not how it works any more')
    def test_has_expired(self):
        with self.app.app_context():
            self.create_fixtures()

            with patch('models.mixins.datetime') as mock_good_datetime:
                mock_good_datetime.utcnow = Mock(
                    return_value=datetime(2012, 8, 2))
                self.assertFalse(self.item.has_expired())

            with patch('models.mixins.datetime') as mock_expired_datetime:
                mock_expired_datetime.utcnow = Mock(
                    return_value=datetime(2012, 9, 2))
                self.assertTrue(self.item.has_expired())

    @unittest.skip('not how it works any more')
    def test_reserve_tickets(self):
        with self.app.app_context():
            self.create_fixtures()

            # Will raise an error if we try to issue once expired
            with patch('models.mixins.datetime') as mock_expired_datetime:
                mock_expired_datetime.utcnow = Mock(
                    return_value=datetime(2012, 9, 2))

                with self.assertRaises(CapacityException):
                    self.create_purchases(item, 1)

            # Now test with a good value for now()
            with patch('models.mixins.datetime') as mock_good_datetime:
                mock_good_datetime.utcnow = Mock(
                    return_value=datetime(2012, 8, 2))

                self.create_purchases(item, 1)
                self.db.session.commit()

                self.assertFalse(self.item.has_capacity())
                with self.assertRaises(CapacityException):
                    self.create_purchases(item, 1)

    def test_capacity_remaining(self):
        with self.app.app_context():
            self.create_fixtures()

            self.assertEqual(self.item.capacity_max,
                             self.item.get_total_remaining_capacity())

            self.item.capacity_used = self.item.capacity_max

            self.db.session.commit()
            self.assertEqual(0, self.item.get_total_remaining_capacity())
Пример #2
0
class MultipleProductGroupTest(unittest.TestCase):
    user_email_template = '{}@test.invalid'
    group_template = 'group{}'
    product1_name = 'product'
    product2_name = 'product2'
    tier1_1_name = 'tier1'
    tier1_2_name = 'tier2'
    tier3_name = 'tier3'

    def setUp(self):
        self.client, self.app, self.db = get_app()
        self.app.testing = True

    def create_fixtures(self):
        self.user_email = self.user_email_template.format(random_string(8))
        self.user = User(self.user_email, 'test_user')
        self.db.session.add(self.user)

        self.group_name = self.group_template.format(random_string(8))
        self.group = ProductGroup(type='test',
                                  name=self.group_name,
                                  capacity_max=10)
        self.product1 = Product(name=self.product1_name,
                                parent=self.group,
                                capacity_max=3)
        self.tier1_1 = PriceTier(name=self.tier1_1_name, parent=self.product1)
        self.price1_1 = Price(price_tier=self.tier1_1,
                              currency='GBP',
                              price_int=10)
        self.db.session.add(self.tier1_1)

        self.tier1_2 = PriceTier(name=self.tier1_2_name, parent=self.product1)
        self.price1_2 = Price(price_tier=self.tier1_2,
                              currency='GBP',
                              price_int=20)
        self.db.session.add(self.tier1_2)

        self.product2 = Product(name=self.product2_name, parent=self.group)
        self.tier3 = PriceTier(name=self.tier3_name, parent=self.product2)
        self.price3 = Price(price_tier=self.tier3,
                            currency='GBP',
                            price_int=30)
        self.db.session.add(self.tier3)

        self.db.session.commit()

    def create_purchases(self, tier, count):
        basket = Basket(self.user, 'GBP')
        basket[tier] = count
        basket.create_purchases()
        basket.ensure_purchase_capacity()
        payment = basket.create_payment(BankPayment)
        assert len(payment.purchases) == count
        self.db.session.commit()

        return payment.purchases

    def test_capacity_propagation(self):
        with self.app.app_context():
            self.create_fixtures()

            # Check all our items have the correct initial capacity
            assert self.group.get_total_remaining_capacity() == 10

            assert self.product1.get_total_remaining_capacity() == 3
            assert self.tier1_1.get_total_remaining_capacity() == 3
            assert self.tier1_2.get_total_remaining_capacity() == 3

            assert self.product2.get_total_remaining_capacity() == 10
            assert self.tier3.get_total_remaining_capacity() == 10

            # Issue three instances to exhaust product1
            self.create_purchases(self.tier1_1, 3)
            self.db.session.commit()

            # Now we shouldn't be able to issue any more tickets from this product
            with pytest.raises(CapacityException):
                self.create_purchases(self.tier1_1, 1)

            with pytest.raises(CapacityException):
                self.create_purchases(self.tier1_2, 1)

            # All the capacity went from product1
            assert self.tier1_1.get_total_remaining_capacity() == 0
            assert self.tier1_2.get_total_remaining_capacity() == 0
            assert self.product1.get_total_remaining_capacity() == 0

            # product2 still has capacity but is limited by the parent
            assert self.group.get_total_remaining_capacity() == 7
            assert self.product2.get_total_remaining_capacity() == 7
            assert self.tier3.get_total_remaining_capacity() == 7

    def test_get_cheapest(self):
        with self.app.app_context():
            self.create_fixtures()

            price1 = Price(price_tier=self.tier1_1,
                           currency='GBP',
                           price_int=5)
            price2 = Price(price_tier=self.tier1_2,
                           currency='GBP',
                           price_int=500)

            self.db.session.add(price1)
            self.db.session.add(price2)
            self.db.session.commit()

            assert price1 == self.product1.get_cheapest_price('GBP')