def test_capacity_propagation(db, parent_group, user): product1 = Product(name="product", parent=parent_group, capacity_max=3) tier1_1 = PriceTier(name="tier1", parent=product1) Price(price_tier=tier1_1, currency="GBP", price_int=10) db.session.add(tier1_1) tier1_2 = PriceTier(name="tier2", parent=product1) Price(price_tier=tier1_2, currency="GBP", price_int=20) db.session.add(tier1_2) product2 = Product(name="product2", parent=parent_group) tier3 = PriceTier(name="tier3", parent=product2) Price(price_tier=tier3, currency="GBP", price_int=30) db.session.commit() # Check all our items have the correct initial capacity assert parent_group.get_total_remaining_capacity() == 10 assert product1.get_total_remaining_capacity() == 3 assert tier1_1.get_total_remaining_capacity() == 3 assert tier1_2.get_total_remaining_capacity() == 3 assert product2.get_total_remaining_capacity() == 10 assert tier3.get_total_remaining_capacity() == 10 # Issue three instances to exhaust product1 create_purchases(tier1_1, 3, user) db.session.commit() # Now we shouldn't be able to issue any more tickets from this product with pytest.raises(CapacityException): create_purchases(tier1_1, 1, user) with pytest.raises(CapacityException): create_purchases(tier1_2, 1, user) db.session.commit() # All the capacity went from product1 assert tier1_1.get_total_remaining_capacity() == 0 assert tier1_2.get_total_remaining_capacity() == 0 assert product1.get_total_remaining_capacity() == 0 # produtill has capacity but is limited by the parent assert parent_group.get_total_remaining_capacity() == 7 assert product2.get_total_remaining_capacity() == 7 assert tier3.get_total_remaining_capacity() == 7 price1 = Price(price_tier=tier1_1, currency="GBP", price_int=5) price2 = Price(price_tier=tier1_2, currency="GBP", price_int=500) db.session.add(price1) db.session.add(price2) db.session.commit() assert price1 == product1.get_cheapest_price("GBP")
def test_capacity_propagation(db, parent_group, user): product1 = Product(name='product', parent=parent_group, capacity_max=3) tier1_1 = PriceTier(name='tier1', parent=product1) Price(price_tier=tier1_1, currency='GBP', price_int=10) db.session.add(tier1_1) tier1_2 = PriceTier(name='tier2', parent=product1) Price(price_tier=tier1_2, currency='GBP', price_int=20) db.session.add(tier1_2) product2 = Product(name='product2', parent=parent_group) tier3 = PriceTier(name='tier3', parent=product2) Price(price_tier=tier3, currency='GBP', price_int=30) db.session.commit() # Check all our items have the correct initial capacity assert parent_group.get_total_remaining_capacity() == 10 assert product1.get_total_remaining_capacity() == 3 assert tier1_1.get_total_remaining_capacity() == 3 assert tier1_2.get_total_remaining_capacity() == 3 assert product2.get_total_remaining_capacity() == 10 assert tier3.get_total_remaining_capacity() == 10 # Issue three instances to exhaust product1 create_purchases(tier1_1, 3, user) db.session.commit() # Now we shouldn't be able to issue any more tickets from this product with pytest.raises(CapacityException): create_purchases(tier1_1, 1, user) with pytest.raises(CapacityException): create_purchases(tier1_2, 1, user) db.session.commit() # All the capacity went from product1 assert tier1_1.get_total_remaining_capacity() == 0 assert tier1_2.get_total_remaining_capacity() == 0 assert product1.get_total_remaining_capacity() == 0 # produtill has capacity but is limited by the parent assert parent_group.get_total_remaining_capacity() == 7 assert product2.get_total_remaining_capacity() == 7 assert tier3.get_total_remaining_capacity() == 7 price1 = Price(price_tier=tier1_1, currency='GBP', price_int=5) price2 = Price(price_tier=tier1_2, currency='GBP', price_int=500) db.session.add(price1) db.session.add(price2) db.session.commit() assert price1 == product1.get_cheapest_price('GBP')
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')