def read_names(filepath): csv = open(filepath) csv.readline() names = [] participants = {} for line in csv: names.append(line.strip("\n")) csv.close() for i in range(len(names)): name_info = names[i].split(",") if len(name_info) == 4: person = Person(i, name_info[0], name_info[1], name_info[2], name_info[3]) elif len(name_info) == 3: person = Person(i, name_info[0], name_info[1], name_info[2]) else: person = Person(i, name_info[0], name_info[1]) participants[person.id_num] = person all_names = set() duplicate_names = set() for key in participants: if participants[key].get_name() in all_names: duplicate_names.add(participants[key].get_name()) all_names.add(participants[key].get_name()) for key in participants: if participants[key].get_name() in duplicate_names: participants[key].set_duplicate() return participants
class TestPerson(unittest.TestCase): """针对人类的测试""" def setUp(self): self.test_group = Person() def test_get_name(self): result = self.test_group.get_name(5) expected_result = "Ee" self.assertEqual(expected_result, result) def test_get_age(self): result = self.test_group.get_age(7) expected_result = 17 self.assertEqual(expected_result, result) def test_get_gender(self): result = self.test_group.get_gender(8) expected_result = "female" self.assertEqual(expected_result, result) def test_get_persons_number(self): result = self.test_group.get_persons_number() expected_result = 14 self.assertEqual(expected_result, result) def test_get_person_info(self): result = self.test_group.get_person_info(10) expected_result = ['Jj', 'male', 20] self.assertEqual(expected_result, result)
def run(): # Make a new person frank = Person("Frank") # Make frank say something frank.speak("Hi, I'm an instance of a Person object. Whoo hooo!") Boy = Person("Eric") Boy.speak("Hello!") Girl = Person("Cindy") Girl.speak("Meow!") Man = Person("John") Man.speak("Yay, it's Friday!") Woman = Person("Rachel") Woman.speak("I'm ready for the weekend!")
def testAgeInFloat(self): person = Person('Ivan Ivanov', date(1901, 1, 1)) with self.assertRaises(ValueError): person.age_in(1.25)
class TestJosephus(unittest.TestCase): """针对约瑟夫类的测试""" def setUp(self): self.test_peoples = Person() self.test_peoples_info = self.test_peoples.get_persons_info() self.test_josephus = Josephus(2, 5) def test_append_1(self): result = self.test_josephus.appends(self.test_peoples_info) expected_result = [['Aa', 'male', 11], ['Bb', 'female', 12], ['Cc', 'female', 13], ['Dd', 'male', 14], ['Ee', 'male', 15], ['Ff', 'male', 16], ['Gg', 'male', 17], ['Hh', 'female', 18], ['Ii', 'male', 19], ['Jj', 'male', 20], ['Kk', 'female', 21], ['Ll', 'female', 22], ['Nn', 'female', 23], ['Mm', 'male', 24]] self.assertEqual(expected_result, result) def test_append_2(self): person_1 = Person('Aa', 'male', 10).list_person_info() result = self.test_josephus.appends(person_1) expected_result = [['Aa', 'male', 10]] self.assertEqual(expected_result, result) def test_erase(self): self.test_josephus.appends(self.test_peoples_info) result = self.test_josephus.erase(2) expected_result = ['Bb', 'female', 12] self.assertEqual(expected_result, result) def test_killed_order(self): self.test_josephus.appends(self.test_peoples_info) result = self.test_josephus.killed_order() expected_result = [['Ff', 'male', 16], ['Kk', 'female', 21], ['Bb', 'female', 12], ['Hh', 'female', 18], ['Mm', 'male', 24], ['Gg', 'male', 17], ['Aa', 'male', 11], ['Jj', 'male', 20], ['Ee', 'male', 15], ['Dd', 'male', 14], ['Ii', 'male', 19], ['Nn', 'female', 23], ['Cc', 'female', 13]] self.assertEqual(expected_result, result) def test_get_survivor(self): self.test_josephus.appends(self.test_peoples_info) result = self.test_josephus.get_survivor() expected_result = [['Ll', 'female', 22]] self.assertEqual(expected_result, result) def test_iterator_iter(self): self.test_josephus.appends(self.test_peoples_info) result = [] for person in self.test_josephus: result.append(person) expected_result = [['Ff', 'male', 16], ['Kk', 'female', 21], ['Bb', 'female', 12], ['Hh', 'female', 18], ['Mm', 'male', 24], ['Gg', 'male', 17], ['Aa', 'male', 11], ['Jj', 'male', 20], ['Ee', 'male', 15], ['Dd', 'male', 14], ['Ii', 'male', 19], ['Nn', 'female', 23], ['Cc', 'female', 13], ['Ll', 'female', 22]] self.assertEqual(expected_result, result) def test_iterator_yield(self): self.test_josephus.appends(self.test_peoples_info) result = [] for person in self.test_josephus.iterator_josephus(): result.append(person) expected_result = [['Ff', 'male', 16], ['Kk', 'female', 21], ['Bb', 'female', 12], ['Hh', 'female', 18], ['Mm', 'male', 24], ['Gg', 'male', 17], ['Aa', 'male', 11], ['Jj', 'male', 20], ['Ee', 'male', 15], ['Dd', 'male', 14], ['Ii', 'male', 19], ['Nn', 'female', 23], ['Cc', 'female', 13], ['Ll', 'female', 22]] self.assertEqual(expected_result, result)
def setUp(self): self.test_peoples = Person() self.test_peoples_info = self.test_peoples.get_persons_info() self.test_josephus = Josephus(2, 5)
def testEmptyAtributFullName(self): with self.assertRaises(ValueError): Person(birth_date=date(1902, 1, 1))
def test_append_2(self): person_1 = Person('Aa', 'male', 10).list_person_info() result = self.test_josephus.appends(person_1) expected_result = [['Aa', 'male', 10]] self.assertEqual(expected_result, result)
def testAgeInYearMinus(self): person = Person('Ivan Ivanov', date(1901, 1, 1)) with self.assertRaises(ValueError): person.age_in(-1900)
def testAgeInYearNoParam(self): person = Person('Ivan Ivanov', date(1901, 1, 1)) res = person.age_in() expect = datetime.now().year - 1901 self.assertEqual(res, expect)
def testAgeIn(self): person = Person('Ivan Ivanov', date(1901, 1, 1)) self.assertEqual(1, person.age_in(1902))
def setUp(self): self.test_group = Person()
def testGetSurname(self): person = Person('Ivan Ivanov', date(1901, 1, 1)) self.assertEqual('Ivanov', person.get_surname())
def testAtributBirthDateNotDate(self): with self.assertRaises(AttributeError): Person(full_name='Ivanov Ivan', birth_date=1)
def testAtributBirthDateEmpty(self): with self.assertRaises(ValueError): Person(full_name='Ivanov Ivan')
def testAtributFullNameDigits(self): with self.assertRaises(ValueError): Person(full_name='333 666', birth_date=date(1902, 1, 1))
def testAtributFullNameThreeWord(self): with self.assertRaises(ValueError): Person(full_name='Ivanov Ivan Ivanovich')