Exemplo n.º 1
0
class Test_WhenRemovingAProductThatDoesNotExists(BasketTest):
    def given_a_basket(self) -> None:
        self.exn: Union[Exception, None] = None
        self.basket = Basket([
            events.BasketCreated(self.BASKET_ID),
            events.ItemAdded(BasketTest.BASKET_ID, "apple", 1),
        ])

    def because_we_remove_an_unknown_item(self) -> None:
        try:
            self.basket.remove("sausages")
        except Exception as e:
            self.exn = e

    def test_it_should_not_empty_the_items(self) -> None:
        self.given_a_basket()
        self.because_we_remove_an_unknown_item()
        assert not self.basket.is_empty()

    def test_it_should_raise_a_key_error(self) -> None:
        self.given_a_basket()
        self.because_we_remove_an_unknown_item()
        assert isinstance(self.exn, KeyError)

    def test_it_should_not_raise_any_events(self) -> None:
        self.given_a_basket()
        self.because_we_remove_an_unknown_item()
        assert self.raised_no_events()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
class Test_WhenRemovingAProduct(BasketTest):
    def given_a_basket(self) -> None:
        self.basket = Basket([
            events.BasketCreated(self.BASKET_ID),
            events.ItemAdded(BasketTest.BASKET_ID, "apple", 2),
        ])

    def because_we_remove_an_item(self) -> None:
        self.basket.remove("apple")

    def test_it_should_empty_the_items(self) -> None:
        self.given_a_basket()
        self.because_we_remove_an_item()
        assert self.basket.is_empty()

    def test_it_should_raise_item_removed(self) -> None:
        self.given_a_basket()
        self.because_we_remove_an_item()
        assert self.has_event(events.ItemRemoved)
        assert self.event.basket_id == self.BASKET_ID
        assert self.event.product == "apple"  # type: ignore
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
class When_creating_a_new_basket:
    def because_we_create_a_basket(self):
        self.basket = Basket()

    def it_should_not_contain_any_products(self):
        assert self.basket.is_empty()