Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python OOP Exam - 14 August 2022
- # https://judge.softuni.org/Contests/Practice/Index/3558#2
- from project.bookstore import Bookstore
- from unittest import TestCase, main
- class BookstoreTests(TestCase):
- # def setUp(self) -> None:
- # books_limit = 10
- # self.shop = Bookstore(books_limit)
- # self.shop.availability_in_store_by_book_titles = {'title1': 5}
- # self.shop._Bookstore__total_sold_books = 5
- def test_init(self):
- store = Bookstore(10)
- self.assertEqual(10, store.books_limit)
- self.assertEqual({}, store.availability_in_store_by_book_titles)
- self.assertEqual(0, store._Bookstore__total_sold_books)
- def test_total_sold_books_property_returns_proper_result(self):
- store = Bookstore(10)
- store.availability_in_store_by_book_titles = {'title1': 5}
- store.sell_book('title1', 3)
- self.assertEqual(3, store.total_sold_books)
- def test_books_limit_returns_proper_result(self):
- store = Bookstore(10)
- self.assertEqual(10, store.books_limit)
- store.books_limit = 5
- self.assertEqual(5, store.books_limit)
- def test_books_limit_raises_if_value_is_zero_or_below_zero(self):
- with self.assertRaises(ValueError) as error:
- store = Bookstore(0)
- self.assertEqual(f"Books limit of 0 is not valid", str(error.exception))
- with self.assertRaises(ValueError) as error:
- store = Bookstore(-5)
- self.assertEqual(f"Books limit of -5 is not valid", str(error.exception))
- def test_len_returns_proper_result(self):
- store = Bookstore(5)
- self.assertEqual(0, store.__len__())
- store.availability_in_store_by_book_titles = {'book1': 3}
- self.assertEqual(3, store.__len__())
- store.availability_in_store_by_book_titles = {'book1': 5}
- self.assertEqual(5, store.__len__())
- def test_receive_book_raises_if_books_limit_reached(self):
- store = Bookstore(5)
- store.availability_in_store_by_book_titles = {'title1': 3}
- with self.assertRaises(Exception) as ex:
- store.receive_book('title1', 4)
- self.assertEqual("Books limit is reached. Cannot receive more books!", str(ex.exception))
- def test_receive_book_if_space_is_enough(self):
- store = Bookstore(10)
- store.availability_in_store_by_book_titles = {'title1': 2}
- store.receive_book('title1', 3)
- self.assertEqual(5, store.availability_in_store_by_book_titles['title1'])
- store.availability_in_store_by_book_titles = {'title1': 2}
- store.receive_book('title2', 3)
- self.assertEqual(3, store.availability_in_store_by_book_titles['title2'])
- store.availability_in_store_by_book_titles = {'title1': 2}
- self.assertEqual(f"8 copies of title1 are available in the bookstore."
- , store.receive_book('title1', 6))
- def test_sell_book_raises_if_book_doesnt_exist(self):
- store = Bookstore(10)
- with self.assertRaises(Exception) as ex:
- store.sell_book('title1', 1)
- self.assertEqual(f"Book title1 doesn't exist!", str(ex.exception))
- def test_sell_book_raises_if_not_enough_copies_to_sell(self):
- store = Bookstore(10)
- store.receive_book('title1', 2)
- with self.assertRaises(Exception) as ex:
- store.sell_book('title1', 4)
- self.assertEqual(f"title1 has not enough copies to sell. Left: 2", str(ex.exception))
- def test_sell_book_if_can_sell_successfully(self):
- store = Bookstore(10)
- store.receive_book('title1', 5)
- self.assertEqual(f"Sold 3 copies of title1", store.sell_book('title1', 3))
- self.assertEqual(2, store.availability_in_store_by_book_titles['title1'])
- self.assertEqual(3, store._Bookstore__total_sold_books)
- def test_str_method(self):
- store = Bookstore(10)
- store.availability_in_store_by_book_titles = {'book1': 5, 'book2': 3}
- result1 = f'Total sold books: 0\n'
- result1 += f"Current availability: 8\n"
- result1 += f" - book1: 5 copies\n"
- result1 += f" - book2: 3 copies\n"
- self.assertEqual(result1.rstrip(), str(store))
- if __name__ == '__main__':
- main()
- ===================================================================================
- #Python OOP Retake Exam - 22 August 2022
- # https://judge.softuni.org/Contests/Practice/Index/3591#2
- from project.shopping_cart import ShoppingCart
- from unittest import TestCase, main
- class ShoppingCartTests(TestCase):
- def test_init(self):
- shopping_cart = ShoppingCart('Name', 200)
- self.assertEqual('Name', shopping_cart.shop_name)
- self.assertEqual(200, shopping_cart.budget)
- self.assertEqual({}, shopping_cart.products)
- def test_setter_name_raises_wrong_name_starts_with_lower_letter(self):
- shopping_cart = ShoppingCart('Name', 200)
- with self.assertRaises(ValueError) as error:
- shopping_cart.shop_name = 'asd'
- self.assertEqual('Shop must contain only letters and must start with capital letter!', str(error.exception))
- def test_setter_name_raises_wrong_name_contains_not_isaplha(self):
- shopping_cart = ShoppingCart('Name', 200)
- with self.assertRaises(ValueError) as error:
- shopping_cart.shop_name = 'N123'
- self.assertEqual('Shop must contain only letters and must start with capital letter!', str(error.exception))
- def test_add_raises_if_product_cost_too_much(self):
- shopping_cart = ShoppingCart('Name', 200)
- with self.assertRaises(ValueError) as error:
- shopping_cart.add_to_cart('product1', 100)
- self.assertEqual(f"Product product1 cost too much!", str(error.exception))
- def test_add_if_added_successfully(self):
- shopping_cart = ShoppingCart('Name', 200)
- result = shopping_cart.add_to_cart('product1', 20)
- expected = f"product1 product was successfully added to the cart!"
- self.assertEqual(expected, result)
- self.assertEqual(20, shopping_cart.products['product1'])
- self.assertEqual({'product1': 20}, shopping_cart.products)
- def test_remove_return_proper_string(self):
- shopping_cart = ShoppingCart('Name', 200)
- shopping_cart.products = {'product1': 20}
- result = shopping_cart.remove_from_cart("product1")
- expected = f"Product product1 was successfully removed from the cart!"
- self.assertEqual(expected, result)
- self.assertEqual({}, shopping_cart.products)
- def test_remove_raises_if_product_not_in_cart(self):
- shopping_cart = ShoppingCart('Name', 200)
- shopping_cart.products = {'test1': 10}
- with self.assertRaises(ValueError) as error:
- shopping_cart.remove_from_cart('test2')
- expected = f"No product with name test2 in the cart!"
- self.assertEqual(expected, str(error.exception))
- self.assertEqual({'test1': 10}, shopping_cart.products)
- shopping_cart.products = {}
- self.assertEqual({}, shopping_cart.products)
- def test_buy_returns_proper_string(self):
- shopping_cart = ShoppingCart('Name', 200)
- shopping_cart.products = {'product1': 20, 'product2': 30}
- expected = shopping_cart.buy_products()
- result = f'Products were successfully bought! Total cost: 50.00lv.'
- self.assertEqual(expected, result)
- def test_buy_raises_if_no_enough_budget(self):
- shopping_cart = ShoppingCart('Name', 200)
- shopping_cart.products = {'product1': 150, 'product2': 60}
- with self.assertRaises(ValueError) as error:
- shopping_cart.buy_products()
- expected = f"Not enough money to buy the products! Over budget with 10.00lv!"
- self.assertEqual(expected, str(error.exception))
- def test_add_method(self):
- first = ShoppingCart('Test', 200)
- first.add_to_cart('from_first', 1)
- second = ShoppingCart('SecondTest', 100)
- second.add_to_cart('from_second', 2)
- merged = first.__add__(second)
- self.assertEqual('TestSecondTest', merged.shop_name)
- self.assertEqual(300, merged.budget)
- self.assertEqual({'from_first': 1, 'from_second': 2}, merged.products)
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Retake Exam - 18 April 2022
- # https://judge.softuni.org/Contests/Practice/Index/3431#2
- from project.plantation import Plantation
- from unittest import TestCase, main
- class PlantationTests(TestCase):
- def test_init(self):
- self.garden = Plantation(10)
- self.assertEqual(10, self.garden.size)
- self.assertEqual({}, self.garden.plants)
- self.assertEqual([], self.garden.workers)
- def test_raises_if_size_negative(self):
- self.garden = Plantation(5)
- with self.assertRaises(ValueError) as error:
- self.garden.size = -1
- self.assertEqual("Size must be positive number!", str(error.exception))
- def test_if_worker_is_already_hired(self):
- self.garden = Plantation(10)
- self.garden.hire_worker('Gosho')
- with self.assertRaises(ValueError) as error:
- self.garden.hire_worker('Gosho')
- self.assertEqual("Worker already hired!", str(error.exception))
- def test_hire_worker_returns_proper_string_when_successfully_added(self):
- self.garden = Plantation(10)
- result = self.garden.hire_worker('Gosho')
- self.assertEqual(len(self.garden.workers), 1)
- self.assertEqual(f"Gosho successfully hired.", result)
- def test_len_method(self):
- self.garden = Plantation(10)
- self.garden.hire_worker('Gosho')
- self.assertEqual(0, self.garden.__len__())
- self.garden.planting('Gosho', 'plant1')
- self.garden.planting('Gosho', 'plant2')
- self.assertEqual(2, self.garden.__len__())
- def test_len_method1(self):
- self.garden = Plantation(5)
- self.garden.hire_worker('Gosho')
- self.garden.hire_worker('Pesho')
- self.garden.plants['Gosho'] = ['plant1']
- self.garden.plants['Pesho'] = ['plant2']
- self.assertEqual(2, self.garden.__len__())
- def test_planting_raises_when_worker_not_in_workers_list_names(self):
- self.garden = Plantation(10)
- with self.assertRaises(ValueError) as error:
- self.garden.planting('Pesho', 'plant1')
- self.assertEqual(f"Worker with name Pesho is not hired!", str(error.exception))
- def test_planting_raises_when_plantation_is_full(self):
- self.garden = Plantation(1)
- self.garden.hire_worker('Gosho')
- self.garden.planting('Gosho', 'plant1')
- with self.assertRaises(ValueError) as error:
- self.garden.planting('Gosho', 'plant2')
- self.assertEqual("The plantation is full!", str(error.exception))
- def test_planting_worker_is_correct_and_make_first_planting_returns_proper_string(self):
- self.garden = Plantation(5)
- self.garden.hire_worker('Gosho')
- self.assertEqual(f"Gosho planted it's first plant1.", self.garden.planting('Gosho', 'plant1'))
- def test_planting_worker_is_correct_and_make_second_or_more_plantings_returns_proper_string(self):
- self.garden = Plantation(5)
- self.garden.hire_worker('Gosho')
- self.garden.planting('Gosho', 'plant1')
- self.assertEqual(len(self.garden.plants['Gosho']), 1)
- self.assertEqual(f"Gosho planted plant2.", self.garden.planting('Gosho', 'plant2'))
- def test_str_wrong_output(self):
- self.assertEqual(Plantation(2).__str__().strip(), 'Plantation size: 2')
- self.pl = Plantation(2)
- self.pl.hire_worker('Martin')
- self.pl.planting('Martin', 'Radishes')
- self.assertEqual(self.pl.__str__().strip(), 'Plantation size: 2\nMartin\nMartin planted: Radishes')
- def test_repr_wrong_output(self):
- self.assertEqual(Plantation(2).__repr__().strip(), 'Size: 2\nWorkers:')
- self.pl = Plantation(2)
- self.pl.hire_worker('Martin')
- self.pl.planting('Martin', 'Radishes')
- self.assertEqual(self.pl.__repr__().strip(), 'Size: 2\nWorkers: Martin')
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Exam - 10 April 2022
- # https://judge.softuni.org/Contests/Practice/Index/3426#2
- from project.movie import Movie
- from unittest import TestCase, main
- class MovieTests(TestCase):
- NAME = 'GOT'
- YEAR = 2000
- RATING = 10
- def setUp(self) -> None:
- self.movie = Movie(self.NAME, self.YEAR, self.RATING)
- def test_init(self):
- self.assertEqual('GOT', self.movie.name)
- self.assertEqual(2000, self.movie.year)
- self.assertEqual(10, self.movie.rating)
- self.assertEqual([], self.movie.actors)
- self.assertEqual(0, len(self.movie.actors))
- def test_wrong_name_validation_raises(self):
- with self.assertRaises(ValueError) as error:
- self.movie.name = ""
- self.assertEqual(str(error.exception), "Name cannot be an empty string!")
- def test_wrong_year_validation_raises(self):
- with self.assertRaises(ValueError) as error:
- self.movie.year = 1886
- self.assertEqual(str(error.exception), "Year is not valid!")
- def test_add_actor_when_actor_already_in_return_string(self):
- self.movie.actors = ['Neo']
- self.assertEqual(self.movie.add_actor('Neo'), f"Neo is already added in the list of actors!")
- def test_add_actor_when_actor_not_exist_(self):
- self.assertEqual(len(self.movie.actors), 0)
- self.movie.add_actor('Neo')
- self.assertEqual(len(self.movie.actors), 1)
- self.assertEqual(self.movie.actors, ['Neo'])
- self.movie.add_actor('Trinity')
- self.assertEqual(len(self.movie.actors), 2)
- self.assertEqual(self.movie.actors, ['Neo', 'Trinity'])
- def test_gt_method(self):
- self.another_movie = Movie('HOTD', 2020, 8)
- result1 = self.movie > self.another_movie
- result2 = self.another_movie > self.movie
- self.assertEqual(result1, f'"{self.movie.name}" is better than "{self.another_movie.name}"')
- self.assertEqual(result2, f'"{self.movie.name}" is better than "{self.another_movie.name}"')
- def test_repr_method(self):
- self.movie.add_actor('Neo')
- result = []
- result.append(f"Name: GOT")
- result.append(f"Year of Release: 2000")
- result.append(f"Rating: 10.00")
- result.append(f"Cast: Neo")
- self.assertEqual(self.movie.__repr__(), '\n'.join(result))
- if __name__ == "__main__":
- main()
- ===================================================================================
- # Python OOP Retake Exam - 23 August 2021
- # https://judge.softuni.org/Contests/Practice/Index/3093#2
- from project.library import Library
- from unittest import TestCase, main
- class LibraryTests(TestCase):
- NAME = 'name'
- def setUp(self) -> None:
- self.library = Library(self.NAME)
- def test_init(self):
- self.assertEqual('name', self.library.name)
- self.assertEqual({}, self.library.books_by_authors)
- self.assertEqual({}, self.library.readers)
- def test_wrong_name(self):
- with self.assertRaises(ValueError) as error:
- self.library.name = ''
- self.assertEqual('Name cannot be empty string!', str(error.exception))
- def test_add_book(self):
- author = 'author1'
- title1 = 'title1'
- title2 = 'title2'
- self.library.add_book(author, title1)
- self.assertEqual(True, author in self.library.books_by_authors)
- self.assertEqual(True, title1 in self.library.books_by_authors[author])
- self.assertEqual(1, len(self.library.books_by_authors[author]))
- self.library.add_book(author, title2)
- self.assertEqual(True, title2 in self.library.books_by_authors[author])
- self.assertEqual(2, len(self.library.books_by_authors[author]))
- def test_add_reader_when_readers_is_empty(self):
- reader1 = 'reader1'
- self.library.add_reader(reader1)
- self.assertEqual(True, reader1 in self.library.readers)
- def test_add_reader_when_reader_already_exist(self):
- reader1 = 'reader1'
- self.library.add_reader(reader1)
- result = self.library.add_reader(reader1)
- self.assertEqual(f"reader1 is already registered in the name library.", result)
- self.assertEqual(1, len(self.library.readers))
- self.assertTrue(True, reader1 in self.library.readers)
- def test_rent_book_if_reader_not_registered(self):
- reader1 = 'reader1'
- reader2 = 'reader2'
- author1 = 'author1'
- title1 = 'title1'
- self.library.readers = {reader2: []}
- result = self.library.rent_book(reader1, author1, title1)
- self.assertEqual(f"reader1 is not registered in the name Library.", result)
- self.assertFalse(False, reader1 in self.library.readers)
- self.assertEqual([], self.library.readers[reader2])
- self.assertEqual(1, self.library.readers.__len__())
- def test_rent_book_if_author_not_exist(self):
- reader1 = 'reader1'
- author1 = 'author1'
- author2 = 'author2'
- title1 = 'title1'
- title2 = 'title2'
- self.library.add_reader(reader1)
- self.library.books_by_authors = {author2: [title2]}
- result = self.library.rent_book(reader1, author1, title1)
- self.assertEqual(f"name Library does not have any author1's books.", result)
- self.assertFalse(False, author1 in self.library.books_by_authors)
- self.assertEqual([], self.library.readers[reader1])
- self.assertEqual([title2], self.library.books_by_authors[author2])
- def test_rent_book_if_books_title_doesnt_exist(self):
- reader1 = 'reader1'
- author1 = 'author1'
- title1 = 'title1'
- title2 = 'title2'
- self.library.add_reader(reader1)
- self.library.add_book(author1, title1)
- result = self.library.rent_book(reader1, author1, title2)
- self.assertEqual(f"""name Library does not have author1's "title2".""", result)
- self.assertEqual([title1], self.library.books_by_authors[author1])
- def test_rent_book_remove_book_successfully(self):
- reader1 = 'reader1'
- author1 = 'author1'
- title1 = 'title1'
- title2 = 'title2'
- self.library.add_reader(reader1)
- self.library.add_book(author1, title1)
- self.library.add_book(author1, title2)
- self.assertEqual([title1, title2], self.library.books_by_authors[author1])
- self.library.rent_book(reader1, author1, title1)
- self.assertEqual([title2], self.library.books_by_authors[author1])
- self.assertTrue(True, title2 not in self.library.books_by_authors[author1])
- self.assertTrue(True, title1 in self.library.readers[reader1])
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Exam - 15 August 2021
- # https://judge.softuni.org/Contests/Practice/Index/3066#2
- from project.pet_shop import PetShop
- from unittest import TestCase, main
- class PetShopTests(TestCase):
- NAME = 'shop'
- def setUp(self) -> None:
- self.shop = PetShop(self.NAME)
- def test_init(self):
- self.assertEqual('shop', self.shop.name)
- self.assertEqual({}, self.shop.food)
- self.assertEqual([], self.shop.pets)
- def test_add_food_raises_qty_0(self):
- with self.assertRaises(ValueError) as error:
- self.shop.add_food('dog_food', 0)
- self.assertEqual("Quantity cannot be equal to or less than 0", str(error.exception))
- def test_add_food_raises_if_qty_negative_number(self):
- with self.assertRaises(ValueError) as error:
- self.shop.add_food('dog_food', -10)
- self.assertEqual("Quantity cannot be equal to or less than 0", str(error.exception))
- def test_add_food_if_food_name_not_in_food_return_proper_string_when_added_successfully(self):
- food1 = 'dog food'
- self.assertFalse(food1 in self.shop.food)
- self.assertEqual(f"Successfully added 10.00 grams of dog food.",
- self.shop.add_food(food1, 10))
- self.assertEqual(10, self.shop.food[food1])
- def test_add_food_if_food_name_already_exist_return_proper_string_when_added_successfully(self):
- food1 = 'cat food'
- qty = 20
- self.shop.food = {food1: qty}
- self.assertTrue(food1 in self.shop.food)
- self.assertEqual(f"Successfully added 20.00 grams of cat food.",
- self.shop.add_food(food1, qty))
- def test_add_pet_raises_when_pet_name_already_exist(self):
- pet1 = 'dog'
- self.shop.pets = [pet1]
- with self.assertRaises(Exception) as ex:
- self.shop.add_pet(pet1)
- self.assertEqual("Cannot add a pet with the same name", str(ex.exception))
- self.assertEqual([pet1], self.shop.pets)
- def test_add_pet_returns_proper_string_if_add_is_successfully(self):
- pet1 = 'dog'
- pet2 = 'cat'
- self.assertEqual(f"Successfully added dog.", self.shop.add_pet(pet1))
- self.assertEqual([pet1], self.shop.pets)
- self.assertEqual(f"Successfully added cat.", self.shop.add_pet(pet2))
- self.assertEqual([pet1, pet2], self.shop.pets)
- def test_feed_pet_raises_if_pet_name_not_exist(self):
- food1 = 'dog food'
- name1 = 'gosho'
- name2 = 'pesho'
- self.shop.pets = [name2]
- with self.assertRaises(Exception) as ex:
- self.shop.feed_pet(food1, name1)
- self.assertEqual(f"Please insert a valid pet name", str(ex.exception))
- self.assertFalse(name1 in self.shop.pets)
- self.assertEqual([name2], self.shop.pets)
- def test_feed_pet_if_food_name_not_exist_return_proper_string(self):
- food1 = 'dog food'
- food2 = 'cat food'
- qty1 = 10
- pet_name1 = 'gosho'
- self.shop.pets = [pet_name1]
- self.shop.food = {food1: qty1}
- self.assertEqual(f'You do not have cat food', self.shop.feed_pet(food2, pet_name1))
- self.assertFalse(food2 in self.shop.food)
- self.assertEqual({food1: qty1}, self.shop.food)
- def test_feed_pet_if_qty_is_lower_than_100_return_proper_string(self):
- food1 = 'dog food'
- qty1 = 99
- pet_name1 = 'gosho'
- self.shop.pets = [pet_name1]
- self.shop.food = {food1: qty1}
- self.assertEqual("Adding food...", self.shop.feed_pet(food1, pet_name1))
- self.assertEqual(1099.00, self.shop.food[food1])
- def test_feed_pet_successfully_return_proper_string(self):
- food1 = 'dog food'
- qty1 = 150
- pet_name1 = 'gosho'
- self.shop.pets = [pet_name1]
- self.shop.food = {food1: qty1}
- self.assertEqual(f"gosho was successfully fed", self.shop.feed_pet(food1, pet_name1))
- self.assertEqual(50, self.shop.food[food1])
- def test_repr(self):
- pet_name1 = 'gosho'
- pet_name2 = 'pesho'
- self.shop.pets = [pet_name1, pet_name2]
- result = f'Shop shop:\nPets: gosho, pesho'
- self.assertEqual(result, self.shop.__repr__())
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Exam - 10 April 2021
- # https://judge.softuni.org/Contests/Practice/Index/2934#2
- from unittest import TestCase, main
- from project.train.train import Train
- class TrainTests(TestCase):
- TRAIN_FULL = "Train is full"
- PASSENGER_EXISTS = "Passenger {} Exists"
- PASSENGER_NOT_FOUND = "Passenger Not Found"
- PASSENGER_ADD = "Added passenger {}"
- PASSENGER_REMOVED = "Removed {}"
- ZERO_CAPACITY = 0
- def setUp(self) -> None:
- self.train = Train('train', 10)
- def test_init(self):
- self.assertEqual('train', self.train.name)
- self.assertEqual(10, self.train.capacity)
- self.assertEqual([], self.train.passengers)
- def test_add_raises_if_train_full(self):
- self.train.capacity = 1
- self.train.passengers = ['Gogo']
- with self.assertRaises(ValueError) as error:
- self.train.add('John')
- self.assertEqual(self.TRAIN_FULL, str(error.exception))
- self.assertEqual(1, self.train.passengers.__len__())
- self.assertEqual(['Gogo'], self.train.passengers)
- def test_add_raises_if_passenger_exist(self):
- self.train.capacity = 5
- self.train.passengers = ['Gogo', 'Pesho']
- with self.assertRaises(ValueError) as error:
- self.train.add('Gogo')
- self.assertEqual("Passenger Gogo Exists", str(error.exception))
- self.assertTrue('Gogo' in self.train.passengers)
- self.assertEqual(['Gogo', 'Pesho'], self.train.passengers)
- self.assertEqual(2, self.train.passengers.__len__())
- def test_add_when_added_successfully_return_proper_string(self):
- self.train.capacity = 5
- self.train.passengers = ['Gogo', 'Pesho']
- self.assertEqual("Added passenger Jivko", self.train.add('Jivko'))
- self.assertTrue('Jivko' in self.train.passengers)
- self.assertEqual(['Gogo', 'Pesho', 'Jivko'], self.train.passengers)
- self.assertEqual(3, self.train.passengers.__len__())
- def test_remove_passenger_raises_if_passanger_name_not_exist(self):
- self.train.passengers = ['Go', 'Sho']
- name1 = 'Pesho'
- with self.assertRaises(ValueError) as error:
- self.train.remove(name1)
- self.assertEqual("Passenger Not Found", str(error.exception))
- self.assertFalse(name1 in self.train.passengers)
- self.assertEqual(['Go', 'Sho'], self.train.passengers)
- self.assertEqual(2, self.train.passengers.__len__())
- def test_remove_successfully_removed_return_proper_string(self):
- self.train.passengers = ['Go', 'Sho']
- name1 = 'Go'
- self.assertEqual("Removed Go", self.train.remove(name1))
- self.assertFalse(name1 in self.train.passengers)
- self.assertEqual(['Sho'], self.train.passengers)
- self.assertEqual(1, self.train.passengers.__len__())
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Exam - 11 December 2021
- # https://judge.softuni.org/Contests/Practice/Index/3305#2
- from project.team import Team
- from unittest import TestCase, main
- class TeamTests(TestCase):
- NAME = "RED"
- def setUp(self) -> None:
- self.team = Team(self.NAME)
- def test_init(self):
- self.assertEqual('RED', self.team.name)
- self.assertEqual({}, self.team.members)
- def test_wrong_name_raises(self):
- with self.assertRaises(ValueError) as error:
- new_team = Team('team123')
- self.assertEqual("Team Name can contain only letters!", str(error.exception))
- def test_add_member(self):
- member_to_add = {'gogo': 25}
- result = self.team.add_member(**member_to_add)
- self.assertEqual(f"Successfully added: gogo", result)
- self.assertTrue('gogo' in self.team.members.keys())
- self.assertEqual(25, self.team.members['gogo'])
- def test_remove_member_if_member_not_exist(self):
- member_to_add = {'gogo': 25}
- self.team.add_member(**member_to_add)
- result = self.team.remove_member('pesho')
- self.assertEqual(f"Member with name pesho does not exist", result)
- self.assertFalse('pesho' in self.team.members)
- def test_remove_successfully(self):
- member_to_add = {'gogo': 25, 'pesho': 20}
- self.team.add_member(**member_to_add)
- result = self.team.remove_member('gogo')
- self.assertEqual(f"Member gogo removed", result)
- self.assertFalse('gogo' in self.team.members)
- def test_gt_(self):
- other_team = Team('BLUE')
- member_to_add1 = {'stoqn': 23}
- other_team.add_member(**member_to_add1)
- member_to_add2 = {'gogo': 25, 'pesho': 20}
- self.team.add_member(**member_to_add2)
- result1 = self.team > other_team # True
- result2 = other_team > self.team # False
- # self.assertTrue(result1) # WRONG !
- # self.assertFalse(result2) # WRONG !
- self.assertEqual(True, result1)
- self.assertEqual(False, result2)
- def test_len_(self):
- self.assertEqual(0, self.team.__len__())
- member_to_add = {'gogo': 25, 'pesho': 20}
- self.team.add_member(**member_to_add)
- self.assertEqual(2, self.team.__len__())
- self.assertTrue('gogo' in self.team.members)
- self.assertTrue('pesho' in self.team.members)
- self.assertEqual({'gogo': 25, 'pesho': 20}, self.team.members)
- def test__add__(self):
- member_to_add2 = {'gogo': 25}
- self.team.add_member(**member_to_add2)
- other_team = Team('BLUE')
- member_to_add1 = {'stoqn': 23}
- other_team.add_member(**member_to_add1)
- merged_team = self.team + other_team
- self.assertEqual('REDBLUE', merged_team.name)
- self.assertEqual({'gogo': 25, 'stoqn': 23}, merged_team.members)
- def test__str__(self):
- member_to_add = {'gogo': 25, 'pesho': 20}
- self.team.add_member(**member_to_add)
- result = [f"Team name: RED", f"Member: gogo - 25-years old", f"Member: pesho - 20-years old"]
- self.assertEqual('\n'.join(result), self.team.__str__())
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Retake Exam - 23 August 2021
- # https://judge.softuni.org/Contests/Practice/Index/3093#2
- from project.library import Library
- from unittest import TestCase, main
- class LibraryTests(TestCase):
- NAME = 'NAME'
- def setUp(self) -> None:
- self.library = Library(self.NAME)
- def test_init(self):
- self.assertEqual('NAME', self.library.name)
- self.assertEqual({}, self.library.readers)
- self.assertEqual({}, self.library.books_by_authors)
- def test_wrong_name_raises(self):
- with self.assertRaises(ValueError) as error:
- new_lib = Library('')
- self.assertEqual("Name cannot be empty string!", str(error.exception))
- def test_add_book_if_author_not_exist(self):
- author = 'author1'
- title = 'title1'
- self.library.add_book(author, title)
- self.assertEqual({author: [title]}, self.library.books_by_authors)
- self.assertEqual([title], self.library.books_by_authors[author])
- def test_add_book_if_author_exist_but_add_new_title(self):
- author = 'author'
- title = 'title'
- new_title = 'new title'
- self.library.add_book(author, title)
- self.library.add_book(author, new_title)
- self.assertTrue(new_title in self.library.books_by_authors[author])
- def test_add_reader_if_reader_already_exist(self):
- self.library.readers = {'gogo': []}
- result = self.library.add_reader('gogo')
- self.assertEqual(f"gogo is already registered in the NAME library.", result)
- self.assertTrue('gogo' in self.library.readers)
- def test_add_reader_if_reader_not_exist(self):
- self.library.add_reader('gogo')
- self.assertTrue("gogo" in self.library.readers.keys())
- self.assertEqual([], self.library.readers['gogo'])
- def test_rent_book_if_reader_not_registered(self):
- reader_name = 'gogo'
- book_author = 'author'
- book_title = 'title'
- result = self.library.rent_book(reader_name, book_author, book_title)
- self.assertEqual(f"gogo is not registered in the NAME Library.", result)
- self.assertFalse(reader_name in self.library.readers.keys())
- def test_rent_book_if_library_not_have_any_of_this_authors_books(self):
- reader_name = 'gogo'
- book_author = 'author'
- book_title = 'title'
- self.library.add_reader(reader_name)
- result = self.library.rent_book(reader_name, book_author, book_title)
- self.assertEqual(f"NAME Library does not have any author's books.", result)
- self.assertFalse(book_author in self.library.books_by_authors.keys())
- def test_rent_book_if_book_title_not_in_authors_books(self):
- reader_name = 'gogo'
- book_author = 'author'
- book_title = 'title'
- self.library.add_reader(reader_name)
- self.library.add_book(book_author, book_title)
- new_title = 'new title'
- result = self.library.rent_book(reader_name, book_author, new_title)
- self.assertEqual(f"""NAME Library does not have author's "new title".""", result)
- self.assertFalse(new_title in self.library.books_by_authors[book_author])
- def test_rent_book_successfully(self):
- reader_name = 'gogo'
- book_author = 'author'
- book_title1 = 'title1'
- book_title2 = 'title2'
- self.library.add_reader(reader_name)
- self.library.add_book(book_author, book_title1)
- self.library.add_book(book_author, book_title2)
- self.library.rent_book(reader_name, book_author, book_title2)
- self.assertFalse(book_title2 in self.library.books_by_authors[book_author])
- self.assertEqual([{'author': 'title2'}], self.library.readers[reader_name])
- if __name__ == '__main__':
- main()
- ===================================================================================
- # Python OOP Retake Exam - 22 Aug 2020
- # https://judge.softuni.org/Contests/Practice/Index/2472#2
- from project.student_report_card import StudentReportCard
- from unittest import TestCase, main
- class StudentReportCardTests(TestCase):
- STUDENT_NAME = 'Gosho'
- SCHOOL_YEAR = 6
- def setUp(self) -> None:
- self.card = StudentReportCard(self.STUDENT_NAME, self.SCHOOL_YEAR)
- def test_init(self):
- self.assertEqual('Gosho', self.card.student_name)
- self.assertEqual(6, self.card.school_year)
- self.assertEqual({}, self.card.grades_by_subject)
- def test_init_edge_case_year_1(self):
- new_student = StudentReportCard('student1', 1)
- self.assertEqual('student1', new_student.student_name)
- self.assertEqual(1, new_student.school_year)
- self.assertEqual({}, new_student.grades_by_subject)
- def test_init_edge_case_year_12(self):
- new_student = StudentReportCard('student2', 12)
- self.assertEqual('student2', new_student.student_name)
- self.assertEqual(12, new_student.school_year)
- self.assertEqual({}, new_student.grades_by_subject)
- def test_wrong_student_name_raises(self):
- with self.assertRaises(ValueError) as error:
- self.card.student_name = ''
- self.assertEqual("Student Name cannot be an empty string!", str(error.exception))
- def test_wrong_school_year_more_than_12_raises(self):
- with self.assertRaises(ValueError) as error:
- self.card.school_year = 15
- self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
- def test_wrong_school_year_is_zero_raises(self):
- with self.assertRaises(ValueError) as error:
- self.card.school_year = 0
- self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
- def test_wrong_school_year_is_negative_raises(self):
- with self.assertRaises(ValueError) as error:
- self.card.school_year = -5
- self.assertEqual("School Year must be between 1 and 12!", str(error.exception))
- def test_add_grade_without_subject(self):
- subject = 'math'
- grade = 5
- self.card.add_grade(subject, grade)
- self.assertEqual([grade], self.card.grades_by_subject[subject])
- self.assertTrue(subject in self.card.grades_by_subject)
- def test_add_grade_if_subject_exist(self):
- subject = 'math'
- grade1 = 5.50
- grade2 = 4.50
- self.card.add_grade(subject, grade1)
- self.assertEqual(1, self.card.grades_by_subject[subject].__len__())
- self.card.add_grade(subject, grade2)
- self.assertEqual([grade1, grade2], self.card.grades_by_subject[subject])
- self.assertTrue(grade1 in self.card.grades_by_subject[subject])
- self.assertTrue(grade2 in self.card.grades_by_subject[subject])
- self.assertEqual(2, self.card.grades_by_subject[subject].__len__())
- def test_average_grade_return_proper_print_string(self):
- self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
- average_grade1 = (5 + 5) / 2
- average_grade2 = (5 + 5) / 2
- result = [f"math: {average_grade1:.2f}", f"info: {average_grade2:.2f}"]
- self.assertEqual('\n'.join(result), self.card.average_grade_by_subject())
- def test_average_grade_for_all_subject_return_proper_print_string(self):
- self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
- final_result = f"Average Grade: 5.00"
- self.assertEqual(final_result, self.card.average_grade_for_all_subjects())
- def test_repr(self):
- self.card.grades_by_subject = {'math': [5, 5], 'info': [5, 5]}
- result = f"Name: Gosho\n" \
- f"Year: 6\n" \
- f"----------\n" \
- f"math: 5.00\n" \
- f"info: 5.00\n" \
- f"----------\n" \
- f"Average Grade: 5.00"
- self.assertEqual(result, self.card.__repr__())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement