Advertisement
pacho_the_python

Untitled

Sep 14th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. from unittest import TestCase
  2.  
  3. from project.shopping_cart import ShoppingCart
  4.  
  5.  
  6. class TestShoppingCart(TestCase):
  7.     def setUp(self) -> None:
  8.         self.shop = ShoppingCart("Shop", 10)
  9.  
  10.     def test_shopping_catd_init(self):
  11.         self.assertEqual(self.shop.shop_name, "Shop")
  12.         self.assertEqual(self.shop.budget, 10)
  13.  
  14.     def test_shopping_card_setter_upper(self):
  15.         with self.assertRaises(ValueError) as error:
  16.             shop = ShoppingCart("shop", 10)
  17.         self.assertEqual(str(error.exception), "Shop must contain only letters and must start with capital letter!")
  18.  
  19.     def test_shop_card_setter_value_is_alpha(self):
  20.         with self.assertRaises(ValueError) as error:
  21.             shop = ShoppingCart("Shop2", 10)
  22.         self.assertEqual(str(error.exception), "Shop must contain only letters and must start with capital letter!")
  23.  
  24.     def test_add_to_card_cost_to_mush_100(self):
  25.         product_name = "Name"
  26.         product_price = 100
  27.         with self.assertRaises(ValueError) as error:
  28.             self.shop.add_to_cart(product_name, product_price)
  29.         self.assertEqual(str(error.exception), f"Product {product_name} cost too much!")
  30.  
  31.     def test_add_to_card_more_than_100(self):
  32.         product_name = "Name"
  33.         product_price = 101
  34.         with self.assertRaises(ValueError) as error:
  35.             self.shop.add_to_cart(product_name, product_price)
  36.         self.assertEqual(str(error.exception), f"Product {product_name} cost too much!")
  37.  
  38.     def test_add_to_card_item_with_price(self):
  39.         product_name = "Name"
  40.         product_price = 50
  41.         result = self.shop.add_to_cart(product_name, product_price)
  42.         expect = f"{product_name} product was successfully added to the cart!"
  43.         self.assertEqual(result, expect)
  44.  
  45.     def test_remove_from_card_if_product_name_not_in_the_dict(self):
  46.         self.shop.products = {"item1": 20, "item2": 30}
  47.         product_name = "item3"
  48.         with self.assertRaises(ValueError) as error:
  49.             self.shop.remove_from_cart(product_name)
  50.         self.assertEqual(str(error.exception), f"No product with name {product_name} in the cart!")
  51.  
  52.     def test_remove_from_card_product_name_in_in_dict(self):
  53.         self.shop.products = {"item1": 20, "item2": 30}
  54.         product_name = "item2"
  55.         act = self.shop.remove_from_cart(product_name)
  56.         expect = f"Product {product_name} was successfully removed from the cart!"
  57.         self.assertEqual(act, expect)
  58.         self.assertEqual(self.shop.products, {"item1": 20})
  59.  
  60.     def test_add_new_shop_name(self):
  61.         shop2 = ShoppingCart("Hell", 20)
  62.         act = self.shop + shop2
  63.  
  64.         self.assertEqual(act.shop_name, "ShopHell")
  65.  
  66.     def test_add_new_shop_budged(self):
  67.         shop2 = ShoppingCart("Hell", 20)
  68.         act = self.shop + shop2
  69.  
  70.         self.assertEqual(act.budget, 30)
  71.  
  72.     def test_add_new_shop_shopping_cart(self):
  73.         shop2 = ShoppingCart("Hell", 20)
  74.         shop2.add_to_cart("bla", 2)
  75.         shop2.add_to_cart("blabla", 3)
  76.         act = self.shop + shop2
  77.  
  78.         self.assertEqual(act.products, {"bla": 2, "blabla": 3})
  79.  
  80.     def test_buy_product_value_error(self):
  81.         self.shop.products = {"item1": 7, "item2": 5}
  82.         total_sum = 12
  83.         with self.assertRaises(ValueError) as error:
  84.             self.shop.buy_products()
  85.         self.assertEqual(str(error.exception), f"Not enough money to buy the products! Over budget with "
  86.                                                f"{total_sum - self.shop.budget:.2f}lv!")
  87.  
  88.     def test_buy_product_possible(self):
  89.         self.shop.products = {"item1": 3, "item2": 5}
  90.         total_sum = 8
  91.         act = self.shop.buy_products()
  92.         result = f'Products were successfully bought! Total cost: {total_sum:.2f}lv.'
  93.         self.assertEqual(act, result)
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement