class When_adding_an_item_with_quantity: def given_an_empty_basket(self): self.basket = Basket() def because_we_add_an_apple(self): self.basket.add_item("apple", qty=3) def it_should_add_one_apple(self): assert self.basket['apple'] == 3
class When_adding_a_new_item: def given_an_empty_basket(self): self.basket = Basket() def because_we_add_an_apple(self): self.basket.add_item("apple") def it_should_add_one_apple(self): assert self.basket['apple'] == 1
class When_removing_a_product: def given_a_basket(self): self.basket = Basket() self.basket.add_item("apple", 2) def because_we_remove_an_item(self): self.basket.remove("apple") def it_should_empty_the_items(self): assert self.basket.is_empty()
class When_adding_more_to_an_existing_product: def given_a_basket(self): self.basket = Basket() self.basket.add_item("apple", 2) def because_we_add_an_apple(self): self.basket.add_item("apple", qty=3) def it_should_add_to_the_quantity(self): assert self.basket['apple'] == 5
class When_removing_a_product_that_doesnt_exist: def given_a_basket(self): self.basket = Basket() self.basket.add_item("apple") def because_we_remove_an_item(self): try: self.basket.remove("sausages") except Exception as e: self.exn = e def it_should_not_empty_the_items(self): assert not self.basket.is_empty() def it_should_raise_a_key_error(self): assert isinstance(self.exn, KeyError)
class Test_WhenAddingANewItem(BasketTest): def given_an_empty_basket(self) -> None: self.basket = Basket([events.BasketCreated(BasketTest.BASKET_ID)]) def add_coffe_to_basket(self) -> None: self.basket.add_item("coffee", qty=3) def test_it_should_contain_the_product(self) -> None: self.given_an_empty_basket() self.add_coffe_to_basket() assert self.basket.get_item("coffee") == 3, "Should contain 3 coffees" def test_it_should_raise_item_added(self) -> None: self.given_an_empty_basket() self.add_coffe_to_basket() assert isinstance(self.event, events.ItemAdded) assert self.event.basket_id == self.BASKET_ID assert self.event.product == "coffee" assert self.event.qty == 3 def test_it_should_not_be_empty(self) -> None: self.given_an_empty_basket() self.add_coffe_to_basket() assert not self.basket.is_empty()