class TestPanda(unittest.TestCase): def setUp(self, name, email, male): self.ivo = Panda("Ivo", "*****@*****.**", "male") self.name = "Ivo" self.email = "*****@*****.**" self.gender = "male" def test_init(self): self.assertTrue(isinstance(self.ivo, Panda)) self.assertEqual(self.ivo.name, self.name) self.assertEqual(self.ivo.email, self.email) self.assertEqual(self.ivo.gender, self.gender) with self.assertRaises(ValueError): self.ivo = Panda("Ivo", "ivopandamail.com", "male") def test_gender(self): self.assertTrue(self.ivo.isMale()) self.assertFalse(self.ivo.isFemale()) def test_str(self): self.assertEqual(str(self.ivo), self.ivo.name) def test_eq(self): other_panda = Panda("Rado", "*****@*****.**", "male") self.assertFalse(self.ivo == other_panda) def test_hash(self): self.assertTrue(isinstance(self.ivo.__hash__(), int))
class PandaTests(unittest.TestCase): def setUp(self): self.ivo = Panda('Ivo', '*****@*****.**', 'male') self.ivo_copy = Panda('Ivo', '*****@*****.**', 'male') self.roza = Panda('Rositsa', '*****@*****.**', 'female') self.smith = Panda('John', '*****@*****.**', 'male') self.smalling = Panda('Paul', '*****@*****.**', 'male') self.patrice = Panda('Evra', '*****@*****.**', 'male') self.girl = Panda('Girl', '*****@*****.**', 'female') def test_equal(self): self.assertNotEqual(self.ivo, self.roza) self.assertEqual(self.ivo, self.ivo_copy) def test_gender(self): self.assertTrue(self.girl.isFemale()) self.assertTrue(self.patrice.isMale()) def test_invalid_email(self): with self.assertRaises(InvalidEmailError): Panda('Ivo', '[email protected]', 'male') Panda('Ivo', '*****@*****.**', 'male') Panda('Ivo', 'nqkoisiBre.com', 'male') Panda('Ivo', 'nqkoisiBre@Abv.', 'male') Panda('Ivo', 'nqkoisiBre@Abv', 'male') def test_hash(self): """ Create three almost equal pandas and see if their hash function works as expected""" one = Panda('Ivo', '*****@*****.**', 'male') two = Panda('Ivo', '*****@*****.**', 'male') three = Panda('ivo', '*****@*****.**', 'male') four = Panda('shopa', '*****@*****.**', 'male') # create a set and see if all four pandas are there, if not, some have been overwritten pandas = {one, two, three, four} self.assertEqual(len(pandas), 4) # create a set again one_copy = Panda('Ivo', '*****@*****.**', 'male') pandas = {one, one_copy} # THESE SHOULD OVERWRITE self.assertNotEqual(len(pandas), 2) self.assertEqual(len(pandas), 1)