def test_get_basket_with_existing_baskets(self): """ If the user has existing baskets in editable states, the method should return a single merged basket. """ user = factories.UserFactory() # Create baskets in a state that qualifies them for merging editable_baskets = [] for status in Basket.editable_statuses: editable_baskets.append(self.create_basket(user, self.site, status)) # Create baskets that should NOT be merged non_editable_baskets = [] for status in (Basket.MERGED, Basket.FROZEN, Basket.SUBMITTED): basket = self.create_basket(user, self.site) basket.status = status basket.save() non_editable_baskets.append(basket) # Create a basket for the other site/tenant site2 = SiteConfigurationFactory().site Basket.get_basket(user, site2) self.assertEqual(user.baskets.count(), 6) basket = Basket.get_basket(user, self.site) # No new basket should be created self.assertEqual(user.baskets.count(), 6) # Check the basic details of the new basket self.assertEqual(basket.status, Basket.OPEN) self.assertEqual(basket.owner, user) # One of the previously editable baskets should be in the merged state. actual_states = [ Basket.objects.get(id=eb.id).status for eb in editable_baskets ] self.assertEqual(actual_states, [Basket.OPEN, Basket.MERGED]) # The merged basket should include the products from the original baskets expected_lines = list( itertools.chain.from_iterable( [list(eb.lines.all()) for eb in editable_baskets])) self.assertEqual(list(basket.lines.all()), expected_lines) # Verify the basket for the second site/tenant is not modified self.assert_basket_state(user.baskets.get(site=site2), Basket.OPEN, user, site2)
def test_get_basket_without_existing_baskets(self): """ If the user has no existing baskets, the method should return a new one. """ user = factories.UserFactory() self.assertEqual(user.baskets.count(), 0, 'A new user should not have any associated Baskets.') basket = Basket.get_basket(user, self.site1) # Check the basic details of the new basket self.assert_basket_state(basket, Basket.OPEN, user, self.site1) self.assertEqual(len(basket.all_lines()), 0, 'The new basket should be empty') self.assertEqual(user.baskets.count(), 1, 'No basket was created for the user.') # Verify we create new baskets for other sites/tenants basket = Basket.get_basket(user, self.site2) self.assert_basket_state(basket, Basket.OPEN, user, self.site2) self.assertEqual(len(basket.all_lines()), 0, 'The new basket should be empty') self.assertEqual(user.baskets.count(), 2, 'A new basket was not created for the second site.')
def test_create_basket(self): """ Verify the method creates a new basket. """ user = factories.UserFactory() basket = Basket.create_basket(self.site, user) self.assertEqual(basket.site, self.site) self.assertEqual(basket.owner, user)