1+ from datetime import datetime , date
2+ import unittest
3+
4+
5+ #Exercise
6+
7+
8+
9+ def add (a , b ):
10+ if not isinstance (a , (int , float )) or not isinstance (b , (int , float )):
11+ raise TypeError ("The arguments must be of type int or float." )
12+ return a + b
13+
14+ class TestAdd (unittest .TestCase ):
15+ def test_add (self ):
16+ self .assertEqual (add (2 , 3 ), 5 )
17+ self .assertEqual (add (- 1 , - 1 ), - 2 )
18+ self .assertEqual (add (0 , 0 ), 0 )
19+ self .assertEqual (add (- 1 , 9 ), 8 )
20+ self .assertEqual (add (2.6 , 4.5 ), 7.1 )
21+ self .assertEqual (add (6.4 , 2 ), 8.4 )
22+
23+ def test_type_add (self ):
24+ with self .assertRaises (TypeError ):
25+ add ("3" , 7 )
26+ with self .assertRaises (TypeError ):
27+ add (5 , "6" )
28+ with self .assertRaises (TypeError ):
29+ add ("9" , "8" )
30+
31+
32+
33+ if __name__ == "__main__" :
34+ unittest .main ()
35+
36+ #Extra Exercise
37+
38+
39+
40+ class testUser_data (unittest .TestCase ):
41+ def setUp (self ) -> None :
42+ self .user_data = {
43+ "name" : "Juan" ,
44+ "age" : 29 ,
45+ "birth_date" : datetime .strptime ("23-02-1996" , "%d-%m-%y" ).date (),
46+ "languages" : ["Python" , "JavaScript" , "Java" ]
47+ }
48+
49+ def test_exist_fields (self ):
50+ self .assertIn ("name" , self .user_data )
51+ self .assertIn ("age" , self .user_data )
52+ self .assertIn ("birth_date" , self .user_data )
53+ self .assertIn ("languages" , self .user_data )
54+
55+ def test_user_gitdata_correct (self ):
56+ self .assertIsInstance (self .user_data ["name" ], str )
57+ self .assertIsInstance (self .user_data ["age" ], int )
58+ self .assertIsInstance (self .user_data ["birth_date" ], date )
59+ self .assertIsInstance (self .user_data ["languages" ], list )
60+
61+
62+ if __name__ == "__main__" :
63+ unittest .main ()
0 commit comments