22
33from ..algorithm import grading_function
44
5+
56class TestGradingFunction (unittest .TestCase ):
67 """
7- TestCase Class used to test the algorithm.
8- ---
9- Tests are used here to check that the algorithm written
10- is working as it should.
11-
12- It's best practise to write these tests first to get a
13- kind of 'specification' for how your algorithm should
14- work, and you should run these tests before committing
15- your code to AWS.
16-
17- Read the docs on how to use unittest here:
18- https://docs.python.org/3/library/unittest.html
19-
20- Use grading_function() to check your algorithm works
21- as it should.
8+ TestCase Class used to test the algorithm.
9+ ---
10+ Tests are used here to check that the algorithm written
11+ is working as it should.
12+
13+ It's best practise to write these tests first to get a
14+ kind of 'specification' for how your algorithm should
15+ work, and you should run these tests before committing
16+ your code to AWS.
17+
18+ Read the docs on how to use unittest here:
19+ https://docs.python.org/3/library/unittest.html
20+
21+ Use grading_function() to check your algorithm works
22+ as it should.
2223 """
23- def test_returns_is_correct_true (self ):
24- body = {}
24+
25+ def test_evaluate_as_int (self ):
26+ body = {"answer" : "45" , "response" : "45" , "params" : {"type" : "int" }}
2527
2628 response = grading_function (body )
2729
2830 self .assertEqual (response .get ("is_correct" ), True )
31+ self .assertEqual (response .get ("error" , False ), False )
32+
33+ def test_invalid_int (self ):
34+ body = {"answer" : "0" , "response" : "1.0" , "params" : {"type" : "int" }}
35+
36+ response = grading_function (body )
37+
38+ self .assertIsNotNone (response .get ("error" , None ))
39+
40+ def test_evaluate_as_float (self ):
41+ body = {"answer" : "4.80" , "response" : "4.8" , "params" : {"type" : "float" }}
42+
43+ response = grading_function (body )
44+
45+ self .assertEqual (response .get ("is_correct" ), True )
46+ self .assertEqual (response .get ("error" , False ), False )
47+
48+ def test_invalid_float (self ):
49+ body = {"answer" : "abc" , "response" : "1" , "params" : {"type" : "int" }}
50+
51+ response = grading_function (body )
52+
53+ self .assertIsNotNone (response .get ("error" , None ))
54+
55+ def test_evaluate_as_string (self ):
56+ body = {
57+ "answer" : "dogs" ,
58+ "response" : "dogs" ,
59+ "params" : {"type" : "str" },
60+ }
61+
62+ response = grading_function (body )
63+
64+ self .assertEqual (response .get ("is_correct" ), True )
65+ self .assertEqual (response .get ("error" , False ), False )
66+
67+ def test_evaluate_as_string_incorrect (self ):
68+ body = {
69+ "answer" : "1.0" ,
70+ "response" : "1" ,
71+ "params" : {"type" : "str" },
72+ }
73+
74+ response = grading_function (body )
75+
76+ self .assertEqual (response .get ("is_correct" ), False )
77+
78+ def test_evaluate_as_dict (self ):
79+ body = {
80+ "answer" : {"a" : 1 , "b" : 2 },
81+ "response" : {"b" : 2 , "a" : 1 },
82+ "params" : {"type" : "dict" },
83+ }
84+
85+ response = grading_function (body )
86+
87+ self .assertEqual (response .get ("is_correct" ), True )
88+ self .assertEqual (response .get ("error" , False ), False )
89+
2990
3091if __name__ == "__main__" :
31- unittest .main ()
92+ unittest .main ()
0 commit comments