Advertisement
Kamend1

Problem 3 December 2021 OOP

Mar 26th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.03 KB | None | 0 0
  1. from project.team import Team
  2. from unittest import TestCase, main
  3.  
  4.  
  5. class TestTeam(TestCase):
  6.  
  7.     def setUp(self):
  8.         self.test_team = Team("Test")
  9.         self.test_other_team = Team("Other")
  10.  
  11.     def test_init_works_correctly(self):
  12.         self.assertEqual("Test", self.test_team.name)
  13.         self.assertEqual({}, self.test_team.members)
  14.  
  15.     def test_setter_raises_error_for_name(self):
  16.         with self.assertRaises(ValueError) as ve:
  17.             self.wrong_name = Team("123")
  18.  
  19.         self.assertEqual("Team Name can contain only letters!", str(ve.exception))
  20.  
  21.     def test_name_setter(self):
  22.         self.test_team.name = "TestTest"
  23.         self.assertEqual("TestTest", self.test_team.name)
  24.  
  25.     def test_add_members_override(self):
  26.         members_to_add = {"Pesho": 10, "Gosho": 20}
  27.         result = self.test_team.add_member(**members_to_add)
  28.         expected_result = "Successfully added: Pesho, Gosho"
  29.         self.assertEqual(expected_result, result)
  30.         self.assertEqual(members_to_add, self.test_team.members)
  31.         more_members = {"Pesho": 23, "Tosho": 15}
  32.         result = self.test_team.add_member(**more_members)
  33.         expected_result = "Successfully added: Tosho"
  34.         expected_members = {"Pesho": 10, "Gosho": 20, "Tosho": 15}
  35.         self.assertEqual(expected_result, result)
  36.         self.assertEqual(expected_members, self.test_team.members)
  37.  
  38.     def test_add_members(self):
  39.         members_to_add = {"Pesho": 10, "Gosho": 20}
  40.         result = self.test_team.add_member(**members_to_add)
  41.         expected_result = "Successfully added: Pesho, Gosho"
  42.         self.assertEqual(expected_result, result)
  43.         self.assertEqual(members_to_add, self.test_team.members)
  44.  
  45.     def test_add_members_empty_dictionary(self):
  46.         members_to_add = {}
  47.         result = self.test_team.add_member(**members_to_add)
  48.         expected_result = "Successfully added: "
  49.         self.assertEqual(expected_result, result)
  50.         self.assertEqual({}, self.test_team.members)
  51.  
  52.     def test_remove_member_success(self):
  53.         members_to_add = {"Pesho": 10, "Gosho": 20}
  54.         self.test_team.add_member(**members_to_add)
  55.         result = self.test_team.remove_member("Pesho")
  56.         expected_result = "Member Pesho removed"
  57.         members_to_check = {"Gosho": 20}
  58.         self.assertEqual(expected_result, result)
  59.         self.assertEqual(members_to_check, self.test_team.members)
  60.  
  61.     def test_remove_member_name_does_not_exist(self):
  62.         result = self.test_team.remove_member("Tosho")
  63.         expected_result = "Member with name Tosho does not exist"
  64.         self.assertEqual(expected_result, result)
  65.         self.assertEqual({}, self.test_team.members)
  66.  
  67.     def test_greater_then_method_return_false(self):
  68.         members_to_add_1 = {"Pesho": 10, "Gosho": 20}
  69.         members_to_add_2 = {"Tosho": 15}
  70.         self.test_other_team.add_member(**members_to_add_1)
  71.         self.test_team.add_member(**members_to_add_2)
  72.         self.assertEqual(False, len(self.test_team.members) > len(self.test_other_team.members))
  73.  
  74.     def test_greater_then_method_return_true(self):
  75.         members_to_add_1 = {"Pesho": 10, "Gosho": 20}
  76.         members_to_add_2 = {"Tosho": 15}
  77.         self.test_team.add_member(**members_to_add_1)
  78.         self.test_other_team.add_member(**members_to_add_2)
  79.         self.assertEqual(True, len(self.test_team.members) > len(self.test_other_team.members))
  80.  
  81.     def test_greater_then_equal_returns_false_if_equal_lengths(self):
  82.         members_to_add_1 = {"Pesho": 10}
  83.         members_to_add_2 = {"Tosho": 15}
  84.         self.test_team.add_member(**members_to_add_1)
  85.         self.test_other_team.add_member(**members_to_add_2)
  86.         self.assertEqual(False, len(self.test_team.members) > len(self.test_other_team.members))
  87.  
  88.     def test_len_method(self):
  89.         self.assertEqual(0, len(self.test_team.members))
  90.         members_to_add = {"Pesho": 10, "Gosho": 20}
  91.         self.test_team.add_member(**members_to_add)
  92.         self.assertEqual(2, len(self.test_team.members))
  93.  
  94.     def test_add_method_concatenate_two_instances(self):
  95.         members_to_add = {"Tosho": 10}
  96.         members_to_add_other = {"Pesho": 10, "Gosho": 20}
  97.         self.test_team.add_member(**members_to_add)
  98.         self.test_other_team.add_member(**members_to_add_other)
  99.         members = {"Tosho": 10, "Pesho": 10, "Gosho": 20}
  100.         new_team = self.test_team + self.test_other_team
  101.         self.assertEqual("TestOther", new_team.name)
  102.         self.assertEqual(members, new_team.members)
  103.  
  104.     def test_string_method_for_class(self):
  105.         members = {"Tosho": 10, "Pesho": 10, "Gosho": 20}
  106.         expected_result = ("Team name: Test" +
  107.                            "\n" + "Member: Gosho - 20-years old" +
  108.                            "\n" + "Member: Pesho - 10-years old" +
  109.                            "\n" + "Member: Tosho - 10-years old"
  110.                            )
  111.         self.test_team.add_member(**members)
  112.         self.assertEqual(expected_result, str(self.test_team))
  113.  
  114.  
  115. if __name__ == '__main__':
  116.     main()
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement