def test_adding(self): self.assertEqual(BaseBasket.objects.count(), 0) aggregator = get_basket_aggregator(self.request) item1, item2, item3 = create_items(aggregator.basket, [ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) aggregator.add(item1) self.assertEqual(aggregator.basket.price, Decimal(1)) self.assertEqual(get_basket_items_amount(aggregator.basket), 1) aggregator.add(item2) self.assertEqual(aggregator.basket.price, Decimal(7)) self.assertEqual(get_basket_items_amount(aggregator.basket), 4) aggregator.add(item3) self.assertEqual(aggregator.basket.price, Decimal(22)) self.assertEqual(get_basket_items_amount(aggregator.basket), 9)
def test_remove(self): self.test_create() basket = BaseBasket.objects.first() basket_item = basket.basket_items.first() expected_amount_after_removing = get_basket_items_amount( basket) - basket_item.amount response = self.client.post("/api/basket/remove/", data={"basket_items": basket_item.id}) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(get_basket_items_amount(basket), expected_amount_after_removing)
def test_create(self): response = self.client.post("/api/basket/create/items/", data={ "basket_items": [ { "product": self.product1.id, "amount": 1 }, { "product": self.product2.id, "amount": 3 }, { "product": self.product3.id, "amount": 5 }, ] }, format="json") basket = BaseBasket.objects.first() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, BasketSerializer(basket).data) self.assertEqual( basket.price, self.product1.price + self.product2.price * 3 + self.product3.price * 5) self.assertEqual(get_basket_items_amount(basket), 9)
def test_clear(self): self.test_create() response = self.client.post("/api/basket/clean/") basket = BaseBasket.objects.first() self.assertEqual(get_basket_items_amount(basket), 0) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(basket.price, 0)
def test_removing(self): self.assertEqual(BaseBasket.objects.count(), 0) aggregator = get_basket_aggregator(self.request) item1, item2, item3 = aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) self.assertIn(item1, aggregator.basket.basket_items.all()) aggregator.remove([item1]) self.assertNotIn(item1, aggregator.basket.basket_items.all()) self.assertIn(item2, aggregator.basket.basket_items.all()) self.assertIn(item3, aggregator.basket.basket_items.all()) aggregator.remove([item2, item3]) self.assertEqual(get_basket_items_amount(aggregator.basket), 0) self.assertFalse(bool(item1.id)) self.assertFalse(bool(item2.id)) self.assertFalse(bool(item3.id))
def test_session_merging(self): anonymous_aggregator = get_basket_aggregator(self.request) anonymous_aggregator.create_items([ { "product": self.product1, "amount": 1 }, ]) self.request.user = self.user self.request.session.cycle_key() user_aggregator = get_basket_aggregator(self.request) user_aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 2 }, ]) self.assertEqual(BaseBasket.objects.count(), 1) self.assertEqual(get_basket_items_amount(user_aggregator.basket), 4) self.assertEqual(user_aggregator.basket.price, self.product1.price * 2 + self.product2.price * 2)
def test_empty(self): self.assertEqual(BaseBasket.objects.count(), 0) aggregator = get_basket_aggregator(self.request) item1, item2, item3 = aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) self.assertIn(item1, aggregator.basket.basket_items.all()) self.assertIn(item2, aggregator.basket.basket_items.all()) self.assertIn(item3, aggregator.basket.basket_items.all()) aggregator.empty_basket() self.assertNotIn(item1, aggregator.basket.basket_items.all()) self.assertNotIn(item2, aggregator.basket.basket_items.all()) self.assertNotIn(item3, aggregator.basket.basket_items.all()) self.assertEqual(get_basket_items_amount(aggregator.basket), 0) self.assertFalse(DynamicBasketItem.objects.exists())
def test_removing_with_basket_delete(self): basket_settings._settings["is_delete_removing"] = False self.assertEqual(BaseBasket.objects.count(), 0) aggregator = get_basket_aggregator(self.request) items = aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) self.assertEqual(get_basket_items_amount(aggregator.basket), 9) aggregator.empty_basket() aggregator = get_basket_aggregator(self.request) items2 = aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) self.assertEqual(get_basket_items_amount(aggregator.basket), 9) aggregator.remove(items) for item in [*items, *items2]: self.assertTrue(bool(item))
def test_many_adding_and_empty(self): self.assertEqual(BaseBasket.objects.count(), 0) aggregator = get_basket_aggregator(self.request) aggregator.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 3 }, { "product": self.product3, "amount": 5 }, ]) self.assertEqual(get_basket_items_amount(aggregator.basket), 9) self.assertEqual(aggregator.basket.price, Decimal(22)) # Test basket empty aggregator.empty_basket() self.assertEqual(get_basket_items_amount(aggregator.basket), 0) self.assertEqual(aggregator.basket.price, Decimal(0))
def test_adding(self): basket_item1 = BasketItem.objects.create(product=self.product1, amount=1, price=self.product1.price) basket_item2 = BasketItem.objects.create(product=self.product2, amount=2, price=self.product2.price * 2) response = self.client.post( "/api/basket/add/", data={"basket_items": [basket_item1.id, basket_item2.id]}, format="json") basket = BaseBasket.objects.first() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, BasketSerializer(basket).data) self.assertEqual(basket.price, self.product1.price + self.product2.price * 2) self.assertEqual(get_basket_items_amount(basket), 3)
def test_merge_basket_queryset(self): self.assertFalse(BaseBasket.objects.exists()) basket1 = BasketAggregator(get_empty_basket()) basket2 = BasketAggregator(get_empty_basket()) basket3 = BasketAggregator(get_empty_basket()) basket4 = BasketAggregator(get_empty_basket()) self.assertEqual(BaseBasket.objects.count(), 4) basket1.create_items([ { "product": self.product1, "amount": 1 }, ]) basket2.create_items([ { "product": self.product2, "amount": 2 }, ]) basket3.create_items([ { "product": self.product3, "amount": 2 }, ]) basket4.create_items([ { "product": self.product3, "amount": 2 }, ]) merge_baskets_queryset([basket1.basket, basket2.basket], basket3.basket) general_basket = merge_baskets_queryset(BaseBasket.objects.all()) self.assertEqual(BaseBasket.objects.count(), 1) self.assertEqual(get_basket_items_amount(general_basket), 7) self.assertEqual(general_basket.price, (self.product1.price + self.product2.price * 2 + self.product3.price * 4))
def test_get_basket(self): basket1 = BasketAggregator(get_empty_basket(self.user)) basket2 = BasketAggregator(get_empty_basket(self.user)) basket1.create_items([ { "product": self.product1, "amount": 1 }, { "product": self.product2, "amount": 2 }, ]) basket2.create_items([ { "product": self.product1, "amount": 1 }, ]) basket = get_basket(user=self.user) self.assertEqual(get_basket_items_amount(basket), 4) self.assertEqual(basket.price, self.product1.price * 2 + self.product2.price * 2)
def test_adding(self): aggregator = get_basket_aggregator(self.request) _ = aggregator.add_many([self.product1, self.product2, self.product3]) self.assertEqual(get_basket_items_amount(aggregator.basket), 3)