Exemple #1
0
class TestTeam(TestCase):
    def setUp(self):
        self.team = Team("TestName")

    def test_initializing_all_attributes(self):
        self.assertEqual("TestName", self.team.name)
        self.assertDictEqual({}, self.team.members)

    def test_initializing_name_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.team.name = "Name12"

        self.assertEqual("Team Name can contain only letters!",
                         str(ex.exception))

    def test_add_member(self):
        self.team.members = {}
        x = self.team.add_member(Ivan=10, Gergi=20)
        self.assertDictEqual({"Ivan": 10, "Gergi": 20}, self.team.members)
        self.assertEqual("Successfully added: Ivan, Gergi", x)

        y = self.team.add_member(Ivan=10)
        self.assertDictEqual({"Ivan": 10, "Gergi": 20}, self.team.members)
        self.assertEqual("Successfully added: ", y)

    def test_remove_member(self):
        self.team.members = {"Ivan": 20}
        x = self.team.remove_member("Ivan")
        self.assertDictEqual({}, self.team.members)
        self.assertEqual("Member Ivan removed", x)

    def test_remove_member_not_exist(self):
        self.team.members = {"Ivan": 20}
        x = self.team.remove_member("Georgi")
        self.assertDictEqual({"Ivan": 20}, self.team.members)
        self.assertEqual("Member with name Georgi does not exist", x)

    def test_gt_method(self):
        team1 = Team("FirstTeam")
        team2 = Team("SecondTeam")
        team2.members = {"Ivan": 20}

        x = team1 < team2
        y = team1 > team2
        self.assertEqual(True, x)
        self.assertEqual(False, y)

    def test_len_method(self):
        team2 = Team("SecondTeam")
        team2.members = {"Ivan": 20}

        x = len(team2)
        self.assertEqual(1, x)

    def test_add_method(self):
        team1 = Team("FirstTeam")
        team2 = Team("SecondTeam")
        team1.members = {"Gergi": 10}
        team2.members = {"Ivan": 20}

        x = team1 + team2
        self.assertEqual("FirstTeamSecondTeam", x.name)
        self.assertEqual({"Gergi": 10, "Ivan": 20}, x.members)

    def test_str_method(self):
        self.team.members = {"Test": 10, "sTest": 20}
        self.assertEqual(
            f"Team name: TestName\n"
            f"Member: sTest - 20-years old\n"
            f"Member: Test - 10-years old", str(self.team))
Exemple #2
0
class TeamTests(TestCase):
    __TEAM_NAME = "TestName"
    __TEAM_NAME1 = "TestNameFirst"
    __MEMBERS = {
        "First": 5,
        "Second": 6,
    }
    __MEMBERS1 = {
        "Third": 7,
        "Fourth": 8,
        "Fifth": 9,
    }

    def setUp(self) -> None:
        self.team = Team(self.__TEAM_NAME)
        self.team1 = Team(self.__TEAM_NAME1)
        self.team.add_member(**self.__MEMBERS)
        self.team1.add_member(**self.__MEMBERS1)

    def test_structure_initialization(self):
        team = Team(self.__TEAM_NAME)
        self.assertEqual(self.__TEAM_NAME, self.team.name)
        self.assertDictEqual({}, team.members)
        with self.assertRaises(ValueError) as ex:
            Team("T3stNam3")
        expected = f"Team Name can contain only letters!"
        self.assertEqual(expected, str(ex.exception))

    def test_remove_member(self):
        expected = f"Member with name Zero does not exist"
        result = self.team.remove_member("Zero")
        self.assertDictEqual(self.__MEMBERS, self.team.members)
        self.assertEqual(expected, result)

        expected = f"Member First removed"
        result = self.team.remove_member("First")
        self.assertDictEqual({"Second": 6}, self.team.members)
        self.assertEqual(expected, result)

    def test_greater_than_between_two_instances_of_team(self):
        expected = True
        result = self.team1 > self.team
        self.assertEqual(expected, result)

        expected = False
        result = self.team > self.team1
        self.assertEqual(expected, result)

    def test_len_method_on_team_instance(self):
        self.assertEqual(2, len(self.team))

    def test_adding_two_instances_of_team(self):
        expected = Team(f"{self.__TEAM_NAME}{self.__TEAM_NAME1}")
        expected.add_member(**self.__MEMBERS)
        expected.add_member(**self.__MEMBERS1)
        result = self.team + self.team1
        self.assertEqual(expected.name, result.name)
        self.assertDictEqual(expected.members, result.members)

    def test_team_string_representation(self):
        expected = [f"Team name: {self.team.name}"]
        members = list(
            sorted(self.team.members.items(), key=lambda x: (-x[1], x[0])))
        expected.extend(
            [f"Member: {x[0]} - {x[1]}-years old" for x in members])
        expected = '\n'.join(expected)
        result = str(self.team)
        self.assertEqual(expected, result)