Advertisement
GeorgiLukanov87

OOP Exams - Unit Tests

Nov 21st, 2022 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 37.73 KB | None | 0 0
  1. # Python OOP Exam - 14 August 2022
  2. # https://judge.softuni.org/Contests/Practice/Index/3558#2
  3.  
  4. from project.bookstore import Bookstore
  5.  
  6. from unittest import TestCase, main
  7.  
  8.  
  9. class BookstoreTests(TestCase):
  10.     # def setUp(self) -> None:
  11.     #     books_limit = 10
  12.     #     self.shop = Bookstore(books_limit)
  13.     #     self.shop.availability_in_store_by_book_titles = {'title1': 5}
  14.     #     self.shop._Bookstore__total_sold_books = 5
  15.  
  16.     def test_init(self):
  17.         store = Bookstore(10)
  18.         self.assertEqual(10, store.books_limit)
  19.         self.assertEqual({}, store.availability_in_store_by_book_titles)
  20.         self.assertEqual(0, store._Bookstore__total_sold_books)
  21.  
  22.     def test_total_sold_books_property_returns_proper_result(self):
  23.         store = Bookstore(10)
  24.         store.availability_in_store_by_book_titles = {'title1': 5}
  25.         store.sell_book('title1', 3)
  26.         self.assertEqual(3, store.total_sold_books)
  27.  
  28.     def test_books_limit_returns_proper_result(self):
  29.         store = Bookstore(10)
  30.         self.assertEqual(10, store.books_limit)
  31.         store.books_limit = 5
  32.         self.assertEqual(5, store.books_limit)
  33.  
  34.     def test_books_limit_raises_if_value_is_zero_or_below_zero(self):
  35.         with self.assertRaises(ValueError) as error:
  36.             store = Bookstore(0)
  37.         self.assertEqual(f"Books limit of 0 is not valid", str(error.exception))
  38.  
  39.         with self.assertRaises(ValueError) as error:
  40.             store = Bookstore(-5)
  41.         self.assertEqual(f"Books limit of -5 is not valid", str(error.exception))
  42.  
  43.     def test_len_returns_proper_result(self):
  44.         store = Bookstore(5)
  45.         self.assertEqual(0, store.__len__())
  46.         store.availability_in_store_by_book_titles = {'book1': 3}
  47.         self.assertEqual(3, store.__len__())
  48.         store.availability_in_store_by_book_titles = {'book1': 5}
  49.         self.assertEqual(5, store.__len__())
  50.  
  51.     def test_receive_book_raises_if_books_limit_reached(self):
  52.         store = Bookstore(5)
  53.  
  54.         store.availability_in_store_by_book_titles = {'title1': 3}
  55.         with self.assertRaises(Exception) as ex:
  56.             store.receive_book('title1', 4)
  57.         self.assertEqual("Books limit is reached. Cannot receive more books!", str(ex.exception))
  58.  
  59.     def test_receive_book_if_space_is_enough(self):
  60.         store = Bookstore(10)
  61.         store.availability_in_store_by_book_titles = {'title1': 2}
  62.         store.receive_book('title1', 3)
  63.         self.assertEqual(5, store.availability_in_store_by_book_titles['title1'])
  64.         store.availability_in_store_by_book_titles = {'title1': 2}
  65.         store.receive_book('title2', 3)
  66.         self.assertEqual(3, store.availability_in_store_by_book_titles['title2'])
  67.         store.availability_in_store_by_book_titles = {'title1': 2}
  68.         self.assertEqual(f"8 copies of title1 are available in the bookstore."
  69.                          , store.receive_book('title1', 6))
  70.  
  71.     def test_sell_book_raises_if_book_doesnt_exist(self):
  72.         store = Bookstore(10)
  73.         with self.assertRaises(Exception) as ex:
  74.             store.sell_book('title1', 1)
  75.         self.assertEqual(f"Book title1 doesn't exist!", str(ex.exception))
  76.  
  77.     def test_sell_book_raises_if_not_enough_copies_to_sell(self):
  78.         store = Bookstore(10)
  79.         store.receive_book('title1', 2)
  80.         with self.assertRaises(Exception) as ex:
  81.             store.sell_book('title1', 4)
  82.         self.assertEqual(f"title1 has not enough copies to sell. Left: 2", str(ex.exception))
  83.  
  84.     def test_sell_book_if_can_sell_successfully(self):
  85.         store = Bookstore(10)
  86.         store.receive_book('title1', 5)
  87.         self.assertEqual(f"Sold 3 copies of title1", store.sell_book('title1', 3))
  88.         self.assertEqual(2, store.availability_in_store_by_book_titles['title1'])
  89.         self.assertEqual(3, store._Bookstore__total_sold_books)
  90.  
  91.     def test_str_method(self):
  92.         store = Bookstore(10)
  93.         store.availability_in_store_by_book_titles = {'book1': 5, 'book2': 3}
  94.         result1 = f'Total sold books: 0\n'
  95.         result1 += f"Current availability: 8\n"
  96.         result1 += f" - book1: 5 copies\n"
  97.         result1 += f" - book2: 3 copies\n"
  98.  
  99.         self.assertEqual(result1.rstrip(), str(store))
  100.  
  101.  
  102. if __name__ == '__main__':
  103.     main()
  104.  
  105. ===================================================================================
  106. #Python OOP Retake Exam - 22 August 2022
  107. # https://judge.softuni.org/Contests/Practice/Index/3591#2
  108.  
  109. from project.shopping_cart import ShoppingCart
  110. from unittest import TestCase, main
  111.  
  112.  
  113. class ShoppingCartTests(TestCase):
  114.     def test_init(self):
  115.         shopping_cart = ShoppingCart('Name', 200)
  116.  
  117.         self.assertEqual('Name', shopping_cart.shop_name)
  118.         self.assertEqual(200, shopping_cart.budget)
  119.         self.assertEqual({}, shopping_cart.products)
  120.  
  121.     def test_setter_name_raises_wrong_name_starts_with_lower_letter(self):
  122.         shopping_cart = ShoppingCart('Name', 200)
  123.  
  124.         with self.assertRaises(ValueError) as error:
  125.             shopping_cart.shop_name = 'asd'
  126.         self.assertEqual('Shop must contain only letters and must start with capital letter!', str(error.exception))
  127.  
  128.     def test_setter_name_raises_wrong_name_contains_not_isaplha(self):
  129.         shopping_cart = ShoppingCart('Name', 200)
  130.  
  131.         with self.assertRaises(ValueError) as error:
  132.             shopping_cart.shop_name = 'N123'
  133.         self.assertEqual('Shop must contain only letters and must start with capital letter!', str(error.exception))
  134.  
  135.     def test_add_raises_if_product_cost_too_much(self):
  136.         shopping_cart = ShoppingCart('Name', 200)
  137.  
  138.         with self.assertRaises(ValueError) as error:
  139.             shopping_cart.add_to_cart('product1', 100)
  140.         self.assertEqual(f"Product product1 cost too much!", str(error.exception))
  141.  
  142.     def test_add_if_added_successfully(self):
  143.         shopping_cart = ShoppingCart('Name', 200)
  144.  
  145.         result = shopping_cart.add_to_cart('product1', 20)
  146.         expected = f"product1 product was successfully added to the cart!"
  147.         self.assertEqual(expected, result)
  148.         self.assertEqual(20, shopping_cart.products['product1'])
  149.         self.assertEqual({'product1': 20}, shopping_cart.products)
  150.  
  151.     def test_remove_return_proper_string(self):
  152.         shopping_cart = ShoppingCart('Name', 200)
  153.         shopping_cart.products = {'product1': 20}
  154.  
  155.         result = shopping_cart.remove_from_cart("product1")
  156.         expected = f"Product product1 was successfully removed from the cart!"
  157.         self.assertEqual(expected, result)
  158.         self.assertEqual({}, shopping_cart.products)
  159.  
  160.     def test_remove_raises_if_product_not_in_cart(self):
  161.         shopping_cart = ShoppingCart('Name', 200)
  162.         shopping_cart.products = {'test1': 10}
  163.         with self.assertRaises(ValueError) as error:
  164.             shopping_cart.remove_from_cart('test2')
  165.  
  166.         expected = f"No product with name test2 in the cart!"
  167.         self.assertEqual(expected, str(error.exception))
  168.         self.assertEqual({'test1': 10}, shopping_cart.products)
  169.  
  170.         shopping_cart.products = {}
  171.  
  172.         self.assertEqual({}, shopping_cart.products)
  173.  
  174.     def test_buy_returns_proper_string(self):
  175.         shopping_cart = ShoppingCart('Name', 200)
  176.         shopping_cart.products = {'product1': 20, 'product2': 30}
  177.  
  178.         expected = shopping_cart.buy_products()
  179.         result = f'Products were successfully bought! Total cost: 50.00lv.'
  180.  
  181.         self.assertEqual(expected, result)
  182.  
  183.     def test_buy_raises_if_no_enough_budget(self):
  184.         shopping_cart = ShoppingCart('Name', 200)
  185.         shopping_cart.products = {'product1': 150, 'product2': 60}
  186.         with self.assertRaises(ValueError) as error:
  187.             shopping_cart.buy_products()
  188.         expected = f"Not enough money to buy the products! Over budget with 10.00lv!"
  189.         self.assertEqual(expected, str(error.exception))
  190.  
  191.     def test_add_method(self):
  192.         first = ShoppingCart('Test', 200)
  193.         first.add_to_cart('from_first', 1)
  194.  
  195.         second = ShoppingCart('SecondTest', 100)
  196.         second.add_to_cart('from_second', 2)
  197.  
  198.         merged = first.__add__(second)
  199.  
  200.         self.assertEqual('TestSecondTest', merged.shop_name)
  201.         self.assertEqual(300, merged.budget)
  202.         self.assertEqual({'from_first': 1, 'from_second': 2}, merged.products)
  203.  
  204.  
  205. if __name__ == '__main__':
  206.     main()
  207.  
  208. ===================================================================================
  209. # Python OOP Retake Exam - 18 April 2022
  210. # https://judge.softuni.org/Contests/Practice/Index/3431#2
  211.  
  212. from project.plantation import Plantation
  213.  
  214. from unittest import TestCase, main
  215.  
  216.  
  217. class PlantationTests(TestCase):
  218.     def test_init(self):
  219.         self.garden = Plantation(10)
  220.         self.assertEqual(10, self.garden.size)
  221.         self.assertEqual({}, self.garden.plants)
  222.         self.assertEqual([], self.garden.workers)
  223.  
  224.     def test_raises_if_size_negative(self):
  225.         self.garden = Plantation(5)
  226.         with self.assertRaises(ValueError) as error:
  227.             self.garden.size = -1
  228.         self.assertEqual("Size must be positive number!", str(error.exception))
  229.  
  230.     def test_if_worker_is_already_hired(self):
  231.         self.garden = Plantation(10)
  232.         self.garden.hire_worker('Gosho')
  233.         with self.assertRaises(ValueError) as error:
  234.             self.garden.hire_worker('Gosho')
  235.         self.assertEqual("Worker already hired!", str(error.exception))
  236.  
  237.     def test_hire_worker_returns_proper_string_when_successfully_added(self):
  238.         self.garden = Plantation(10)
  239.         result = self.garden.hire_worker('Gosho')
  240.         self.assertEqual(len(self.garden.workers), 1)
  241.         self.assertEqual(f"Gosho successfully hired.", result)
  242.  
  243.     def test_len_method(self):
  244.         self.garden = Plantation(10)
  245.         self.garden.hire_worker('Gosho')
  246.         self.assertEqual(0, self.garden.__len__())
  247.         self.garden.planting('Gosho', 'plant1')
  248.         self.garden.planting('Gosho', 'plant2')
  249.         self.assertEqual(2, self.garden.__len__())
  250.  
  251.     def test_len_method1(self):
  252.         self.garden = Plantation(5)
  253.         self.garden.hire_worker('Gosho')
  254.         self.garden.hire_worker('Pesho')
  255.         self.garden.plants['Gosho'] = ['plant1']
  256.         self.garden.plants['Pesho'] = ['plant2']
  257.         self.assertEqual(2, self.garden.__len__())
  258.  
  259.     def test_planting_raises_when_worker_not_in_workers_list_names(self):
  260.         self.garden = Plantation(10)
  261.         with self.assertRaises(ValueError) as error:
  262.             self.garden.planting('Pesho', 'plant1')
  263.         self.assertEqual(f"Worker with name Pesho is not hired!", str(error.exception))
  264.  
  265.     def test_planting_raises_when_plantation_is_full(self):
  266.         self.garden = Plantation(1)
  267.         self.garden.hire_worker('Gosho')
  268.         self.garden.planting('Gosho', 'plant1')
  269.         with self.assertRaises(ValueError) as error:
  270.             self.garden.planting('Gosho', 'plant2')
  271.         self.assertEqual("The plantation is full!", str(error.exception))
  272.  
  273.     def test_planting_worker_is_correct_and_make_first_planting_returns_proper_string(self):
  274.         self.garden = Plantation(5)
  275.         self.garden.hire_worker('Gosho')
  276.         self.assertEqual(f"Gosho planted it's first plant1.", self.garden.planting('Gosho', 'plant1'))
  277.  
  278.     def test_planting_worker_is_correct_and_make_second_or_more_plantings_returns_proper_string(self):
  279.         self.garden = Plantation(5)
  280.         self.garden.hire_worker('Gosho')
  281.         self.garden.planting('Gosho', 'plant1')
  282.         self.assertEqual(len(self.garden.plants['Gosho']), 1)
  283.         self.assertEqual(f"Gosho planted plant2.", self.garden.planting('Gosho', 'plant2'))
  284.  
  285.     def test_str_wrong_output(self):
  286.         self.assertEqual(Plantation(2).__str__().strip(), 'Plantation size: 2')
  287.         self.pl = Plantation(2)
  288.         self.pl.hire_worker('Martin')
  289.         self.pl.planting('Martin', 'Radishes')
  290.         self.assertEqual(self.pl.__str__().strip(), 'Plantation size: 2\nMartin\nMartin planted: Radishes')
  291.  
  292.     def test_repr_wrong_output(self):
  293.         self.assertEqual(Plantation(2).__repr__().strip(), 'Size: 2\nWorkers:')
  294.         self.pl = Plantation(2)
  295.         self.pl.hire_worker('Martin')
  296.         self.pl.planting('Martin', 'Radishes')
  297.         self.assertEqual(self.pl.__repr__().strip(), 'Size: 2\nWorkers: Martin')
  298.  
  299.  
  300. if __name__ == '__main__':
  301.     main()
  302.  
  303. ===================================================================================
  304. # Python OOP Exam - 10 April 2022
  305. # https://judge.softuni.org/Contests/Practice/Index/3426#2
  306.  
  307.  
  308. from project.movie import Movie
  309. from unittest import TestCase, main
  310.  
  311.  
  312. class MovieTests(TestCase):
  313.     NAME = 'GOT'
  314.     YEAR = 2000
  315.     RATING = 10
  316.  
  317.     def setUp(self) -> None:
  318.         self.movie = Movie(self.NAME, self.YEAR, self.RATING)
  319.  
  320.     def test_init(self):
  321.         self.assertEqual('GOT', self.movie.name)
  322.         self.assertEqual(2000, self.movie.year)
  323.         self.assertEqual(10, self.movie.rating)
  324.         self.assertEqual([], self.movie.actors)
  325.         self.assertEqual(0, len(self.movie.actors))
  326.  
  327.     def test_wrong_name_validation_raises(self):
  328.         with self.assertRaises(ValueError) as error:
  329.             self.movie.name = ""
  330.         self.assertEqual(str(error.exception), "Name cannot be an empty string!")
  331.  
  332.     def test_wrong_year_validation_raises(self):
  333.         with self.assertRaises(ValueError) as error:
  334.             self.movie.year = 1886
  335.         self.assertEqual(str(error.exception), "Year is not valid!")
  336.  
  337.     def test_add_actor_when_actor_already_in_return_string(self):
  338.         self.movie.actors = ['Neo']
  339.         self.assertEqual(self.movie.add_actor('Neo'), f"Neo is already added in the list of actors!")
  340.  
  341.     def test_add_actor_when_actor_not_exist_(self):
  342.         self.assertEqual(len(self.movie.actors), 0)
  343.         self.movie.add_actor('Neo')
  344.         self.assertEqual(len(self.movie.actors), 1)
  345.         self.assertEqual(self.movie.actors, ['Neo'])
  346.  
  347.         self.movie.add_actor('Trinity')
  348.         self.assertEqual(len(self.movie.actors), 2)
  349.         self.assertEqual(self.movie.actors, ['Neo', 'Trinity'])
  350.  
  351.     def test_gt_method(self):
  352.         self.another_movie = Movie('HOTD', 2020, 8)
  353.         result1 = self.movie > self.another_movie
  354.         result2 = self.another_movie > self.movie
  355.  
  356.         self.assertEqual(result1, f'"{self.movie.name}" is better than "{self.another_movie.name}"')
  357.         self.assertEqual(result2, f'"{self.movie.name}" is better than "{self.another_movie.name}"')
  358.  
  359.     def test_repr_method(self):
  360.         self.movie.add_actor('Neo')
  361.         result = []
  362.         result.append(f"Name: GOT")
  363.         result.append(f"Year of Release: 2000")
  364.         result.append(f"Rating: 10.00")
  365.         result.append(f"Cast: Neo")
  366.  
  367.         self.assertEqual(self.movie.__repr__(), '\n'.join(result))
  368.  
  369.  
  370. if __name__ == "__main__":
  371.     main()
  372.  
  373. ===================================================================================
  374. # Python OOP Retake Exam - 23 August 2021
  375. # https://judge.softuni.org/Contests/Practice/Index/3093#2
  376.  
  377. from project.library import Library
  378. from unittest import TestCase, main
  379.  
  380.  
  381. class LibraryTests(TestCase):
  382.     NAME = 'name'
  383.  
  384.     def setUp(self) -> None:
  385.         self.library = Library(self.NAME)
  386.  
  387.     def test_init(self):
  388.         self.assertEqual('name', self.library.name)
  389.         self.assertEqual({}, self.library.books_by_authors)
  390.         self.assertEqual({}, self.library.readers)
  391.  
  392.     def test_wrong_name(self):
  393.         with self.assertRaises(ValueError) as error:
  394.             self.library.name = ''
  395.         self.assertEqual('Name cannot be empty string!', str(error.exception))
  396.  
  397.     def test_add_book(self):
  398.         author = 'author1'
  399.         title1 = 'title1'
  400.         title2 = 'title2'
  401.         self.library.add_book(author, title1)
  402.         self.assertEqual(True, author in self.library.books_by_authors)
  403.         self.assertEqual(True, title1 in self.library.books_by_authors[author])
  404.         self.assertEqual(1, len(self.library.books_by_authors[author]))
  405.  
  406.         self.library.add_book(author, title2)
  407.         self.assertEqual(True, title2 in self.library.books_by_authors[author])
  408.         self.assertEqual(2, len(self.library.books_by_authors[author]))
  409.  
  410.     def test_add_reader_when_readers_is_empty(self):
  411.         reader1 = 'reader1'
  412.         self.library.add_reader(reader1)
  413.         self.assertEqual(True, reader1 in self.library.readers)
  414.  
  415.     def test_add_reader_when_reader_already_exist(self):
  416.         reader1 = 'reader1'
  417.         self.library.add_reader(reader1)
  418.         result = self.library.add_reader(reader1)
  419.         self.assertEqual(f"reader1 is already registered in the name library.", result)
  420.         self.assertEqual(1, len(self.library.readers))
  421.         self.assertTrue(True, reader1 in self.library.readers)
  422.  
  423.     def test_rent_book_if_reader_not_registered(self):
  424.         reader1 = 'reader1'
  425.         reader2 = 'reader2'
  426.         author1 = 'author1'
  427.         title1 = 'title1'
  428.         self.library.readers = {reader2: []}
  429.         result = self.library.rent_book(reader1, author1, title1)
  430.         self.assertEqual(f"reader1 is not registered in the name Library.", result)
  431.         self.assertFalse(False, reader1 in self.library.readers)
  432.         self.assertEqual([], self.library.readers[reader2])
  433.         self.assertEqual(1, self.library.readers.__len__())
  434.  
  435.     def test_rent_book_if_author_not_exist(self):
  436.         reader1 = 'reader1'
  437.         author1 = 'author1'
  438.         author2 = 'author2'
  439.         title1 = 'title1'
  440.         title2 = 'title2'
  441.         self.library.add_reader(reader1)
  442.         self.library.books_by_authors = {author2: [title2]}
  443.         result = self.library.rent_book(reader1, author1, title1)
  444.         self.assertEqual(f"name Library does not have any author1's books.", result)
  445.         self.assertFalse(False, author1 in self.library.books_by_authors)
  446.         self.assertEqual([], self.library.readers[reader1])
  447.         self.assertEqual([title2], self.library.books_by_authors[author2])
  448.  
  449.     def test_rent_book_if_books_title_doesnt_exist(self):
  450.         reader1 = 'reader1'
  451.         author1 = 'author1'
  452.         title1 = 'title1'
  453.         title2 = 'title2'
  454.         self.library.add_reader(reader1)
  455.         self.library.add_book(author1, title1)
  456.         result = self.library.rent_book(reader1, author1, title2)
  457.         self.assertEqual(f"""name Library does not have author1's "title2".""", result)
  458.         self.assertEqual([title1], self.library.books_by_authors[author1])
  459.  
  460.     def test_rent_book_remove_book_successfully(self):
  461.         reader1 = 'reader1'
  462.         author1 = 'author1'
  463.         title1 = 'title1'
  464.         title2 = 'title2'
  465.         self.library.add_reader(reader1)
  466.         self.library.add_book(author1, title1)
  467.         self.library.add_book(author1, title2)
  468.  
  469.         self.assertEqual([title1, title2], self.library.books_by_authors[author1])
  470.         self.library.rent_book(reader1, author1, title1)
  471.  
  472.         self.assertEqual([title2], self.library.books_by_authors[author1])
  473.         self.assertTrue(True, title2 not in self.library.books_by_authors[author1])
  474.         self.assertTrue(True, title1 in self.library.readers[reader1])
  475.  
  476.  
  477. if __name__ == '__main__':
  478.     main()
  479.  
  480. ===================================================================================
  481. # Python OOP Exam - 15 August 2021
  482. # https://judge.softuni.org/Contests/Practice/Index/3066#2
  483.  
  484.  
  485. from project.pet_shop import PetShop
  486. from unittest import TestCase, main
  487.  
  488.  
  489. class PetShopTests(TestCase):
  490.     NAME = 'shop'
  491.  
  492.     def setUp(self) -> None:
  493.         self.shop = PetShop(self.NAME)
  494.  
  495.     def test_init(self):
  496.         self.assertEqual('shop', self.shop.name)
  497.         self.assertEqual({}, self.shop.food)
  498.         self.assertEqual([], self.shop.pets)
  499.  
  500.     def test_add_food_raises_qty_0(self):
  501.         with self.assertRaises(ValueError) as error:
  502.             self.shop.add_food('dog_food', 0)
  503.         self.assertEqual("Quantity cannot be equal to or less than 0", str(error.exception))
  504.  
  505.     def test_add_food_raises_if_qty_negative_number(self):
  506.         with self.assertRaises(ValueError) as error:
  507.             self.shop.add_food('dog_food', -10)
  508.         self.assertEqual("Quantity cannot be equal to or less than 0", str(error.exception))
  509.  
  510.     def test_add_food_if_food_name_not_in_food_return_proper_string_when_added_successfully(self):
  511.         food1 = 'dog food'
  512.         self.assertFalse(food1 in self.shop.food)
  513.         self.assertEqual(f"Successfully added 10.00 grams of dog food.",
  514.                          self.shop.add_food(food1, 10))
  515.         self.assertEqual(10, self.shop.food[food1])
  516.  
  517.     def test_add_food_if_food_name_already_exist_return_proper_string_when_added_successfully(self):
  518.         food1 = 'cat food'
  519.         qty = 20
  520.         self.shop.food = {food1: qty}
  521.         self.assertTrue(food1 in self.shop.food)
  522.         self.assertEqual(f"Successfully added 20.00 grams of cat food.",
  523.                          self.shop.add_food(food1, qty))
  524.  
  525.     def test_add_pet_raises_when_pet_name_already_exist(self):
  526.         pet1 = 'dog'
  527.         self.shop.pets = [pet1]
  528.         with self.assertRaises(Exception) as ex:
  529.             self.shop.add_pet(pet1)
  530.         self.assertEqual("Cannot add a pet with the same name", str(ex.exception))
  531.         self.assertEqual([pet1], self.shop.pets)
  532.  
  533.     def test_add_pet_returns_proper_string_if_add_is_successfully(self):
  534.         pet1 = 'dog'
  535.         pet2 = 'cat'
  536.         self.assertEqual(f"Successfully added dog.", self.shop.add_pet(pet1))
  537.         self.assertEqual([pet1], self.shop.pets)
  538.  
  539.         self.assertEqual(f"Successfully added cat.", self.shop.add_pet(pet2))
  540.         self.assertEqual([pet1, pet2], self.shop.pets)
  541.  
  542.     def test_feed_pet_raises_if_pet_name_not_exist(self):
  543.         food1 = 'dog food'
  544.         name1 = 'gosho'
  545.         name2 = 'pesho'
  546.         self.shop.pets = [name2]
  547.  
  548.         with self.assertRaises(Exception) as ex:
  549.             self.shop.feed_pet(food1, name1)
  550.         self.assertEqual(f"Please insert a valid pet name", str(ex.exception))
  551.         self.assertFalse(name1 in self.shop.pets)
  552.         self.assertEqual([name2], self.shop.pets)
  553.  
  554.     def test_feed_pet_if_food_name_not_exist_return_proper_string(self):
  555.         food1 = 'dog food'
  556.         food2 = 'cat food'
  557.         qty1 = 10
  558.         pet_name1 = 'gosho'
  559.         self.shop.pets = [pet_name1]
  560.         self.shop.food = {food1: qty1}
  561.         self.assertEqual(f'You do not have cat food', self.shop.feed_pet(food2, pet_name1))
  562.         self.assertFalse(food2 in self.shop.food)
  563.         self.assertEqual({food1: qty1}, self.shop.food)
  564.  
  565.     def test_feed_pet_if_qty_is_lower_than_100_return_proper_string(self):
  566.         food1 = 'dog food'
  567.         qty1 = 99
  568.         pet_name1 = 'gosho'
  569.         self.shop.pets = [pet_name1]
  570.         self.shop.food = {food1: qty1}
  571.         self.assertEqual("Adding food...", self.shop.feed_pet(food1, pet_name1))
  572.         self.assertEqual(1099.00, self.shop.food[food1])
  573.  
  574.     def test_feed_pet_successfully_return_proper_string(self):
  575.         food1 = 'dog food'
  576.         qty1 = 150
  577.         pet_name1 = 'gosho'
  578.         self.shop.pets = [pet_name1]
  579.         self.shop.food = {food1: qty1}
  580.         self.assertEqual(f"gosho was successfully fed", self.shop.feed_pet(food1, pet_name1))
  581.         self.assertEqual(50, self.shop.food[food1])
  582.  
  583.     def test_repr(self):
  584.         pet_name1 = 'gosho'
  585.         pet_name2 = 'pesho'
  586.         self.shop.pets = [pet_name1, pet_name2]
  587.  
  588.         result = f'Shop shop:\nPets: gosho, pesho'
  589.         self.assertEqual(result, self.shop.__repr__())
  590.  
  591.  
  592. if __name__ == '__main__':
  593.     main()
  594.  
  595. ===================================================================================
  596. # Python OOP Exam - 10 April 2021
  597. # https://judge.softuni.org/Contests/Practice/Index/2934#2
  598.  
  599.  
  600. from unittest import TestCase, main
  601.  
  602. from project.train.train import Train
  603.  
  604.  
  605. class TrainTests(TestCase):
  606.     TRAIN_FULL = "Train is full"
  607.     PASSENGER_EXISTS = "Passenger {} Exists"
  608.     PASSENGER_NOT_FOUND = "Passenger Not Found"
  609.     PASSENGER_ADD = "Added passenger {}"
  610.     PASSENGER_REMOVED = "Removed {}"
  611.     ZERO_CAPACITY = 0
  612.  
  613.     def setUp(self) -> None:
  614.         self.train = Train('train', 10)
  615.  
  616.     def test_init(self):
  617.         self.assertEqual('train', self.train.name)
  618.         self.assertEqual(10, self.train.capacity)
  619.         self.assertEqual([], self.train.passengers)
  620.  
  621.     def test_add_raises_if_train_full(self):
  622.         self.train.capacity = 1
  623.         self.train.passengers = ['Gogo']
  624.         with self.assertRaises(ValueError) as error:
  625.             self.train.add('John')
  626.         self.assertEqual(self.TRAIN_FULL, str(error.exception))
  627.         self.assertEqual(1, self.train.passengers.__len__())
  628.         self.assertEqual(['Gogo'], self.train.passengers)
  629.  
  630.     def test_add_raises_if_passenger_exist(self):
  631.         self.train.capacity = 5
  632.         self.train.passengers = ['Gogo', 'Pesho']
  633.         with self.assertRaises(ValueError) as error:
  634.             self.train.add('Gogo')
  635.         self.assertEqual("Passenger Gogo Exists", str(error.exception))
  636.         self.assertTrue('Gogo' in self.train.passengers)
  637.         self.assertEqual(['Gogo', 'Pesho'], self.train.passengers)
  638.         self.assertEqual(2, self.train.passengers.__len__())
  639.  
  640.     def test_add_when_added_successfully_return_proper_string(self):
  641.         self.train.capacity = 5
  642.         self.train.passengers = ['Gogo', 'Pesho']
  643.         self.assertEqual("Added passenger Jivko", self.train.add('Jivko'))
  644.         self.assertTrue('Jivko' in self.train.passengers)
  645.         self.assertEqual(['Gogo', 'Pesho', 'Jivko'], self.train.passengers)
  646.         self.assertEqual(3, self.train.passengers.__len__())
  647.  
  648.     def test_remove_passenger_raises_if_passanger_name_not_exist(self):
  649.         self.train.passengers = ['Go', 'Sho']
  650.         name1 = 'Pesho'
  651.         with self.assertRaises(ValueError) as error:
  652.             self.train.remove(name1)
  653.         self.assertEqual("Passenger Not Found", str(error.exception))
  654.         self.assertFalse(name1 in self.train.passengers)
  655.         self.assertEqual(['Go', 'Sho'], self.train.passengers)
  656.         self.assertEqual(2, self.train.passengers.__len__())
  657.  
  658.     def test_remove_successfully_removed_return_proper_string(self):
  659.         self.train.passengers = ['Go', 'Sho']
  660.         name1 = 'Go'
  661.         self.assertEqual("Removed Go", self.train.remove(name1))
  662.         self.assertFalse(name1 in self.train.passengers)
  663.         self.assertEqual(['Sho'], self.train.passengers)
  664.         self.assertEqual(1, self.train.passengers.__len__())
  665.  
  666.  
  667. if __name__ == '__main__':
  668.     main()
  669.    
  670. ===================================================================================
  671. # Python OOP Exam - 11 December 2021
  672. # https://judge.softuni.org/Contests/Practice/Index/3305#2
  673.  
  674. from project.team import Team
  675. from unittest import TestCase, main
  676.  
  677.  
  678. class TeamTests(TestCase):
  679.     NAME = "RED"
  680.  
  681.     def setUp(self) -> None:
  682.         self.team = Team(self.NAME)
  683.  
  684.     def test_init(self):
  685.         self.assertEqual('RED', self.team.name)
  686.         self.assertEqual({}, self.team.members)
  687.  
  688.     def test_wrong_name_raises(self):
  689.         with self.assertRaises(ValueError) as error:
  690.             new_team = Team('team123')
  691.         self.assertEqual("Team Name can contain only letters!", str(error.exception))
  692.  
  693.     def test_add_member(self):
  694.         member_to_add = {'gogo': 25}
  695.         result = self.team.add_member(**member_to_add)
  696.         self.assertEqual(f"Successfully added: gogo", result)
  697.         self.assertTrue('gogo' in self.team.members.keys())
  698.         self.assertEqual(25, self.team.members['gogo'])
  699.  
  700.     def test_remove_member_if_member_not_exist(self):
  701.         member_to_add = {'gogo': 25}
  702.         self.team.add_member(**member_to_add)
  703.         result = self.team.remove_member('pesho')
  704.         self.assertEqual(f"Member with name pesho does not exist", result)
  705.         self.assertFalse('pesho' in self.team.members)
  706.  
  707.     def test_remove_successfully(self):
  708.         member_to_add = {'gogo': 25, 'pesho': 20}
  709.         self.team.add_member(**member_to_add)
  710.         result = self.team.remove_member('gogo')
  711.         self.assertEqual(f"Member gogo removed", result)
  712.         self.assertFalse('gogo' in self.team.members)
  713.  
  714.     def test_gt_(self):
  715.         other_team = Team('BLUE')
  716.         member_to_add1 = {'stoqn': 23}
  717.         other_team.add_member(**member_to_add1)
  718.  
  719.         member_to_add2 = {'gogo': 25, 'pesho': 20}
  720.         self.team.add_member(**member_to_add2)
  721.  
  722.         result1 = self.team > other_team  # True
  723.         result2 = other_team > self.team  # False
  724.  
  725.         # self.assertTrue(result1)  # WRONG !
  726.         # self.assertFalse(result2)  # WRONG !
  727.  
  728.         self.assertEqual(True, result1)
  729.         self.assertEqual(False, result2)
  730.  
  731.     def test_len_(self):
  732.         self.assertEqual(0, self.team.__len__())
  733.         member_to_add = {'gogo': 25, 'pesho': 20}
  734.         self.team.add_member(**member_to_add)
  735.         self.assertEqual(2, self.team.__len__())
  736.         self.assertTrue('gogo' in self.team.members)
  737.         self.assertTrue('pesho' in self.team.members)
  738.         self.assertEqual({'gogo': 25, 'pesho': 20}, self.team.members)
  739.  
  740.     def test__add__(self):
  741.         member_to_add2 = {'gogo': 25}
  742.         self.team.add_member(**member_to_add2)
  743.  
  744.         other_team = Team('BLUE')
  745.         member_to_add1 = {'stoqn': 23}
  746.         other_team.add_member(**member_to_add1)
  747.  
  748.         merged_team = self.team + other_team
  749.         self.assertEqual('REDBLUE', merged_team.name)
  750.         self.assertEqual({'gogo': 25, 'stoqn': 23}, merged_team.members)
  751.  
  752.     def test__str__(self):
  753.         member_to_add = {'gogo': 25, 'pesho': 20}
  754.         self.team.add_member(**member_to_add)
  755.  
  756.         result = [f"Team name: RED", f"Member: gogo - 25-years old", f"Member: pesho - 20-years old"]
  757.         self.assertEqual('\n'.join(result), self.team.__str__())
  758.  
  759.  
  760. if __name__ == '__main__':
  761.     main()
  762.  
  763. ===================================================================================
  764. # Python OOP Retake Exam - 23 August 2021
  765. # https://judge.softuni.org/Contests/Practice/Index/3093#2
  766.  
  767.  
  768. from project.library import Library
  769. from unittest import TestCase, main
  770.  
  771.  
  772. class LibraryTests(TestCase):
  773.     NAME = 'NAME'
  774.  
  775.     def setUp(self) -> None:
  776.         self.library = Library(self.NAME)
  777.  
  778.     def test_init(self):
  779.         self.assertEqual('NAME', self.library.name)
  780.         self.assertEqual({}, self.library.readers)
  781.         self.assertEqual({}, self.library.books_by_authors)
  782.  
  783.     def test_wrong_name_raises(self):
  784.         with self.assertRaises(ValueError) as error:
  785.             new_lib = Library('')
  786.         self.assertEqual("Name cannot be empty string!", str(error.exception))
  787.  
  788.     def test_add_book_if_author_not_exist(self):
  789.         author = 'author1'
  790.         title = 'title1'
  791.         self.library.add_book(author, title)
  792.        
  793.         self.assertEqual({author: [title]}, self.library.books_by_authors)
  794.         self.assertEqual([title], self.library.books_by_authors[author])
  795.  
  796.     def test_add_book_if_author_exist_but_add_new_title(self):
  797.         author = 'author'
  798.         title = 'title'
  799.         new_title = 'new title'
  800.         self.library.add_book(author, title)
  801.         self.library.add_book(author, new_title)
  802.        
  803.         self.assertTrue(new_title in self.library.books_by_authors[author])
  804.  
  805.     def test_add_reader_if_reader_already_exist(self):
  806.         self.library.readers = {'gogo': []}
  807.         result = self.library.add_reader('gogo')
  808.        
  809.         self.assertEqual(f"gogo is already registered in the NAME library.", result)
  810.         self.assertTrue('gogo' in self.library.readers)
  811.  
  812.     def test_add_reader_if_reader_not_exist(self):
  813.         self.library.add_reader('gogo')
  814.        
  815.         self.assertTrue("gogo" in self.library.readers.keys())
  816.         self.assertEqual([], self.library.readers['gogo'])
  817.  
  818.     def test_rent_book_if_reader_not_registered(self):
  819.         reader_name = 'gogo'
  820.         book_author = 'author'
  821.         book_title = 'title'
  822.         result = self.library.rent_book(reader_name, book_author, book_title)
  823.        
  824.         self.assertEqual(f"gogo is not registered in the NAME Library.", result)
  825.         self.assertFalse(reader_name in self.library.readers.keys())
  826.  
  827.     def test_rent_book_if_library_not_have_any_of_this_authors_books(self):
  828.         reader_name = 'gogo'
  829.         book_author = 'author'
  830.         book_title = 'title'
  831.         self.library.add_reader(reader_name)
  832.         result = self.library.rent_book(reader_name, book_author, book_title)
  833.        
  834.         self.assertEqual(f"NAME Library does not have any author's books.", result)
  835.         self.assertFalse(book_author in self.library.books_by_authors.keys())
  836.  
  837.     def test_rent_book_if_book_title_not_in_authors_books(self):
  838.         reader_name = 'gogo'
  839.         book_author = 'author'
  840.         book_title = 'title'
  841.         self.library.add_reader(reader_name)
  842.         self.library.add_book(book_author, book_title)
  843.  
  844.         new_title = 'new title'
  845.         result = self.library.rent_book(reader_name, book_author, new_title)
  846.        
  847.         self.assertEqual(f"""NAME Library does not have author's "new title".""", result)
  848.         self.assertFalse(new_title in self.library.books_by_authors[book_author])
  849.  
  850.     def test_rent_book_successfully(self):
  851.         reader_name = 'gogo'
  852.         book_author = 'author'
  853.         book_title1 = 'title1'
  854.         book_title2 = 'title2'
  855.         self.library.add_reader(reader_name)
  856.         self.library.add_book(book_author, book_title1)
  857.         self.library.add_book(book_author, book_title2)
  858.  
  859.         self.library.rent_book(reader_name, book_author, book_title2)
  860.        
  861.         self.assertFalse(book_title2 in self.library.books_by_authors[book_author])
  862.         self.assertEqual([{'author': 'title2'}], self.library.readers[reader_name])
  863.  
  864.  
  865. if __name__ == '__main__':
  866.     main()
  867.  
  868. ===================================================================================
  869. # Python OOP Retake Exam - 22 Aug 2020
  870. # https://judge.softuni.org/Contests/Practice/Index/2472#2
  871.  
  872. from project.student_report_card import StudentReportCard
  873. from unittest import TestCase, main
  874.  
  875.  
  876. class StudentReportCardTests(TestCase):
  877.     STUDENT_NAME = 'Gosho'
  878.     SCHOOL_YEAR = 6
  879.  
  880.     def setUp(self) -> None:
  881.         self.card = StudentReportCard(self.STUDENT_NAME, self.SCHOOL_YEAR)
  882.  
  883.     def test_init(self):
  884.         self.assertEqual('Gosho', self.card.student_name)
  885.         self.assertEqual(6, self.card.school_year)
  886.         self.assertEqual({}, self.card.grades_by_subject)
  887.  
  888.     def test_init_edge_case_year_1(self):
  889.         new_student = StudentReportCard('student1', 1)
  890.         self.assertEqual('student1', new_student.student_name)
  891.         self.assertEqual(1, new_student.school_year)
  892.         self.assertEqual({}, new_student.grades_by_subject)
  893.  
  894.     def test_init_edge_case_year_12(self):
  895.         new_student = StudentReportCard('student2', 12)
  896.         self.assertEqual('student2', new_student.student_name)
  897.         self.assertEqual(12, new_student.school_year)
  898.         self.assertEqual({}, new_student.grades_by_subject)
  899.  
  900.     def test_wrong_student_name_raises(self):
  901.         with self.assertRaises(ValueError) as error:
  902.             self.card.student_name = ''
  903.         self.assertEqual("Student Name cannot be an empty string!", str(error.exception))
  904.  
  905.     def test_wrong_school_year_more_than_12_raises(self):
  906.         with self.assertRaises(ValueError) as error:
  907.             self.card.school_year = 15
  908.         self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
  909.  
  910.     def test_wrong_school_year_is_zero_raises(self):
  911.         with self.assertRaises(ValueError) as error:
  912.             self.card.school_year = 0
  913.         self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
  914.  
  915.     def test_wrong_school_year_is_negative_raises(self):
  916.         with self.assertRaises(ValueError) as error:
  917.             self.card.school_year = -5
  918.         self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
  919.  
  920.     def test_add_grade_without_subject(self):
  921.         subject = 'math'
  922.         grade = 5
  923.  
  924.         self.card.add_grade(subject, grade)
  925.         self.assertEqual([grade], self.card.grades_by_subject[subject])
  926.         self.assertTrue(subject in self.card.grades_by_subject)
  927.  
  928.     def test_add_grade_if_subject_exist(self):
  929.         subject = 'math'
  930.         grade1 = 5.50
  931.         grade2 = 4.50
  932.         self.card.add_grade(subject, grade1)
  933.         self.assertEqual(1, self.card.grades_by_subject[subject].__len__())
  934.         self.card.add_grade(subject, grade2)
  935.  
  936.         self.assertEqual([grade1, grade2], self.card.grades_by_subject[subject])
  937.         self.assertTrue(grade1 in self.card.grades_by_subject[subject])
  938.         self.assertTrue(grade2 in self.card.grades_by_subject[subject])
  939.         self.assertEqual(2, self.card.grades_by_subject[subject].__len__())
  940.  
  941.     def test_average_grade_return_proper_print_string(self):
  942.         self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
  943.         average_grade1 = (5 + 5) / 2
  944.         average_grade2 = (5 + 5) / 2
  945.         result = [f"math: {average_grade1:.2f}", f"info: {average_grade2:.2f}"]
  946.         self.assertEqual('\n'.join(result), self.card.average_grade_by_subject())
  947.  
  948.     def test_average_grade_for_all_subject_return_proper_print_string(self):
  949.         self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
  950.         final_result = f"Average Grade: 5.00"
  951.         self.assertEqual(final_result, self.card.average_grade_for_all_subjects())
  952.  
  953.     def test_repr(self):
  954.         self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
  955.         result = f"Name: Gosho\n" \
  956.                  f"Year: 6\n" \
  957.                  f"----------\n" \
  958.                  f"math: 5.00\n" \
  959.                  f"info: 5.00\n" \
  960.                  f"----------\n" \
  961.                  f"Average Grade: 5.00"
  962.         self.assertEqual(result, self.card.__repr__())
  963.  
  964.  
  965. if __name__ == '__main__':
  966.     main()
  967.  
  968.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement