6
6
7
7
8
8
This is where you should write unit tests for your useful method. If you can't do so for any reason, mention so in your PR so I can help.
9
- """
9
+ """
10
+
11
+ import unittest
12
+ from _usefulibs import *
13
+
14
+ class TestUsefulibs (unittest .TestCase ):
15
+
16
+ def setUp (self ):
17
+ # For now, there are no setup scripts for tests. If you need to add one, remove this comment and add it.
18
+ pass
19
+
20
+ def test_reverse_string (self ):
21
+ """ @hamdivazim """
22
+
23
+ self .assertEqual (reverse_string ("abc" ), "cba" )
24
+ self .assertEqual (reverse_string ("123abcdef456" ), "654fedcba321" )
25
+
26
+ def test_loop_dict (self ):
27
+ """ @hamdivazim """
28
+ test_dict = {"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 }
29
+
30
+ self .assertEqual (loop_dict (test_dict ), [("a" , 1 , 0 ),("b" , 2 , 1 ),("c" , 3 , 2 ),("d" , 4 , 3 ),("e" , 5 , 4 ),])
31
+
32
+ index = 0
33
+ for key , val , i in loop_dict (test_dict ):
34
+ if index == 1 :
35
+ self .assertEqual (key , "b" )
36
+ self .assertEqual (val , 2 )
37
+ self .assertEqual (i , 1 )
38
+
39
+ index += 1
40
+
41
+ def test_find_nth_root (self ):
42
+ """ @hamdivazim """
43
+ self .assertEqual (find_nth_root (8 , 3 ), 2 )
44
+ self .assertEqual (find_nth_root (729 , 6 ), 3 )
45
+ self .assertEqual (find_nth_root (4 , - 2 ), 0.5 )
46
+ self .assertRaises (ValueError , find_nth_root , - 4 , - 2 )
47
+
48
+ def test_filter_by_string (self ):
49
+ """ @hamdivazim """
50
+ lst = ["I really do love apples!" , "Bananas are disgusting!" , "An apple a day keeps the doctor away!" , "You want a pear?" ]
51
+ lst2 = [1 , "I really do love apples!" , "Bananas are disgusting!" , "An apple a day keeps the doctor away!" , "You want a pear?" ]
52
+
53
+ self .assertEqual (filter_by_string (lst , "apple" )[1 ], "An apple a day keeps the doctor away!" )
54
+ self .assertRaises (TypeError , filter_by_string , lst2 , "apple" )
55
+
56
+ def test_filter_by_condition (self ):
57
+ """ @hamdivazim """
58
+ self .assertEqual (filter_by_condition ([0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ], "i % 2 == 0" ), [0 , 2 , 4 , 6 , 8 , 10 ])
59
+ self .assertEqual (filter_by_condition (["123abc" , "456def" , "123456" ], '"123" in i' ), ["123abc" , "123456" ])
60
+
61
+ def test_generate_random_string (self ):
62
+ """ @hamdivazim """
63
+ self .assertNotEqual (generate_random_string (18 ), generate_random_string (18 ))
64
+
65
+ def test_generateUUID (self ):
66
+ """ @hamdivazim """
67
+ self .assertNotEqual (generateUUID (), generateUUID ())
68
+
69
+
70
+ if __name__ == "__main__" :
71
+ unittest .main ()
0 commit comments