def make_test_group(): sergey = Student("Сергей", "Кабанов", "man", date(1989, 4, 26), None, None, 1) masha = Student("Маша", "Абстрактная", "woman", date(1998, 3, 8), 3, 151, -inf) dictator = Student("Гвидо", "ван Россум", "man", date(1900, 1, 1), None, None, inf) return Group([dictator, sergey, masha])
def test_init(self): p = self._get_galina() self.assertTrue(hasattr(p, "group")) self.assertTrue(hasattr(p, "skill")) with self.assertRaises(ValueError): Student("a", "b", "c", "1990/4/12", 1, 1) with self.assertRaises(ValueError): Student("a", "b", "c", 1990, 1, 1) with self.assertRaises(ValueError): Student("a", "b", "c", [], 1, 1)
def test_str(self): s = self._get_galina() self.assertEqual( str(s), "Galina Moskovskaya, female, 26 years, 161 group, 5 skill" ) s = Student('Ivan', 'Ivanov', 'male', date(1989, 4, 26), 1, 1) self.assertEqual( str(s), "Ivan Ivanov, male, 29 years, 1 group, 1 skill" ) self.assertEqual( Student('A', 'B', 'male', date(1989, 4, 26), 1, 2).__str__(), "A B, male, 29 years, 1 group, 2 skill" ) self.assertEqual( str(Student('Cap', 'Ter', 'male', date(1989, 4, 26), 11, 3)), "Cap Ter, male, 29 years, 11 group, 3 skill" ) for i, c in enumerate(string.printable): s.name = c + c s.surname = c s.skill = i self.assertEqual( str(s), f"{c + c} {c}, male, 29 years, 1 group, {i} skill" )
def test_sort_by_age_and_skill(self): s1 = Student("a", "a", "a", date(1993, 1, 1), 1, 4) s2 = Student("a", "a", "a", date(1993, 1, 1), 1, 2) s3 = Student("a", "a", "a", date(1994, 1, 1), 1, 3) g = Group([s1, s2, s3]) g.sort_by_age_and_skill() self.assertEqual(str(g), str(Group([s3, s2, s1]))) g.sort_by_age_and_skill(reverse=True) self.assertEqual(str(g), str(Group([s1, s2, s3]))) s4 = Student("a", "a", "a", date(1994, 1, 1), 1, 7) for group in permutations([s1, s2, s3, s4]): g = Group(group) g.sort_by_age_and_skill() self.assertEqual(str(g), str(Group([s3, s4, s2, s1]))) g.sort_by_age_and_skill(reverse=True) self.assertEqual(str(g), str(Group([s1, s2, s4, s3])))
def test_sort_by_ages(self): s1 = Student("a", "a", "a", date(1997, 1, 1), 1, 1) s2 = Student("a", "a", "a", date(1993, 1, 1), 1, 1) s3 = Student("a", "a", "a", date(1994, 1, 1), 1, 1) g = Group([s1, s2, s3]) g.sort_by_age() self.assertEqual(str(g), str(Group([s1, s3, s2]))) g.sort_by_age(reverse=True) self.assertEqual(str(g), str(Group([s2, s3, s1]))) s4 = Student("a", "a", "a", date(1992, 1, 1), 1, 1) for group in permutations([s1, s2, s3, s4]): g = Group(group) g.sort_by_age() self.assertEqual(str(g), str(Group([s1, s3, s2, s4]))) g.sort_by_age(reverse=True) self.assertEqual(str(g), str(Group([s4, s2, s3, s1])))
def test_eq(self): s = self._get_galina() self.assertEqual(s, s) self.assertNotEqual( s, Student("Galina", "Moskovskaya", "female", date(1992, 4, 12), 161, 6)) self.assertNotEqual( s, Student("Galina", "A", "female", date(1992, 4, 12), 161, 5)) self.assertNotEqual( s, Student("Galina", "Moskovskaya", "female", date(1992, 4, 11), 11, 6)) self.assertNotEqual( s, Student("Galina", "Moskovskaya", "female", date(1992, 2, 12), 161, 6)) self.assertNotEqual( s, Student("Galina", "Moskovskaya", "female", date(1992, 4, 12), 162, 6)) self.assertNotEqual( s, Student("Galina", "Moskoviskaya", "female", date(1992, 4, 12), 161, 6))
def _get_galina(): return Student("Galina", "Moskovskaya", "female", date(1992, 4, 12), 161, 5)
def _get_group(): return [ Student("a", "a", "a", date(1997, 1, 1), 1, 1), Student("a", "a", "a", date(1993, 1, 1), 1, 1), Student("a", "a", "a", date(1994, 1, 1), 1, 1) ]