11#!/usr/bin/env -S python3 -i
22
3- from os import path
4-
53import argparse
64import importlib
75import inspect
8- import os
96import sys
7+ import os
8+ from os import path
109
1110FAIL_COLOR = '\033 [91m'
1211OK_COLOR = '\033 [92m'
1312WARN_COLOR = '\033 [93m'
1413
14+
1515def run_sanity_check (test_dir ):
16+ ''' Udacity file to check students test cases '''
1617
17- # assert path.isdir(test_dir), FAIL_COLOR+f"No direcotry named {test_dir} found in {os.getcwd()}"
18+ assert path .isdir (test_dir ), FAIL_COLOR + f"No directory named { test_dir } found in { os .getcwd ()} "
1819 print ('This script will perform a sanity test to ensure your code meets the criteria in the rubric.\n ' )
1920 print ('Please enter the path to the file that contains your test cases for the GET() and POST() methods' )
2021 print ('The path should be something like abc/def/test_xyz.py' )
@@ -26,98 +27,93 @@ def run_sanity_check(test_dir):
2627 module_name = path .splitext (path .basename (filepath ))[0 ]
2728 module = importlib .import_module (module_name )
2829
29-
30- test_function_names = list ( filter ( lambda x : inspect .isfunction (getattr (module ,x )) and not x .startswith ('__' ), dir (module )))
31-
32- test_functions_for_get = list ( filter ( lambda x : inspect .getsource (getattr (module ,x )).find ('.get(' ) != - 1 , test_function_names ))
33- test_functions_for_post = list (filter (lambda x : inspect . getsource ( getattr ( module , x )). find ( '.post(' ) != - 1 , test_function_names ))
34-
30+ test_function_names = list ( filter (
31+ lambda x : inspect .isfunction (getattr (module , x )) and not x .startswith ('__' ), dir (module )))
32+ test_functions_for_get = list ( filter (
33+ lambda x : inspect .getsource (getattr (module , x )).find ('.get(' ) != - 1 , test_function_names ))
34+ test_functions_for_post = list (filter (
35+ lambda x : inspect . getsource ( getattr ( module , x )). find ( '.post(' ) != - 1 , test_function_names ))
3536
3637 print ("\n ============= Sanity Check Report ===========" )
3738 SANITY_TEST_PASSING = True
3839 WARNING_COUNT = 1
3940
40- ## GET()
41+ # GET()
4142 TEST_FOR_GET_METHOD_RESPONSE_CODE = False
4243 TEST_FOR_GET_METHOD_RESPONSE_BODY = False
4344 if not test_functions_for_get :
44- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
45+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
4546 WARNING_COUNT += 1
46- print (FAIL_COLOR + "No test cases were detected for the GET() method." )
47- print (FAIL_COLOR + "\n Please make sure you have a test case for the GET method.\
47+ print (FAIL_COLOR + "No test cases were detected for the GET() method." )
48+ print (FAIL_COLOR + "\n Please make sure you have a test case for the GET method.\
4849 This MUST test both the status code as well as the contents of the request object.\n " )
4950 SANITY_TEST_PASSING = False
50-
5151 else :
5252 for func in test_functions_for_get :
53- source = inspect .getsource (getattr (module ,func ))
53+ source = inspect .getsource (getattr (module , func ))
5454 if source .find ('.status_code' ) != - 1 :
5555 TEST_FOR_GET_METHOD_RESPONSE_CODE = True
5656 if (source .find ('.json' ) != - 1 ) or (source .find ('json.loads' ) != - 1 ):
57- TEST_FOR_GET_METHOD_RESPONSE_BODY = True
58-
57+ TEST_FOR_GET_METHOD_RESPONSE_BODY = True
5958
6059 if not TEST_FOR_GET_METHOD_RESPONSE_CODE :
61- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
60+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
6261 WARNING_COUNT += 1
63- print (FAIL_COLOR + \
62+ print (FAIL_COLOR +
6463 "Your test case for GET() does not seem to be testing the response code.\n " )
65-
64+
6665 if not TEST_FOR_GET_METHOD_RESPONSE_BODY :
67- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
66+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
6867 WARNING_COUNT += 1
69- print (FAIL_COLOR + \
68+ print (FAIL_COLOR +
7069 "Your test case for GET() does not seem to be testing the CONTENTS of the response.\n " )
7170
72-
73-
74- ## POST()
71+ # POST()
7572 TEST_FOR_POST_METHOD_RESPONSE_CODE = False
7673 TEST_FOR_POST_METHOD_RESPONSE_BODY = False
7774 COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT = 0
7875
7976 if not test_functions_for_post :
80- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
77+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
8178 WARNING_COUNT += 1
82- print (FAIL_COLOR + "No test cases were detected for the POST() method." )
83- print (FAIL_COLOR + "Please make sure you have TWO test cases for the POST() method." +
84- "\n One test case for EACH of the possible inferences (results/outputs) of the ML model.\n " )
79+ print (FAIL_COLOR + "No test cases were detected for the POST() method." )
80+ print (FAIL_COLOR + "Please make sure you have TWO test cases for the POST() method." +
81+ "\n One test case for EACH of the possible inferences (results/outputs) of the ML model.\n " )
8582 SANITY_TEST_PASSING = False
8683 else :
8784 if len (test_functions_for_post ) == 1 :
8885 print (f"[{ WARNING_COUNT } ]" )
8986 WARNING_COUNT += 1
90- print (FAIL_COLOR + "Only one test case was detected for the POST() method." )
91- print (FAIL_COLOR + "Please make sure you have two test cases for the POST() method." +
92- "\n One test case for EACH of the possible inferences (results/outputs) of the ML model.\n " )
87+ print (FAIL_COLOR + "Only one test case was detected for the POST() method." )
88+ print (FAIL_COLOR + "Please make sure you have two test cases for the POST() method." +
89+ "\n One test case for EACH of the possible inferences (results/outputs) of the ML model.\n " )
9390 SANITY_TEST_PASSING = False
9491
9592 for func in test_functions_for_post :
96- source = inspect .getsource (getattr (module ,func ))
93+ source = inspect .getsource (getattr (module , func ))
9794 if source .find ('.status_code' ) != - 1 :
9895 TEST_FOR_POST_METHOD_RESPONSE_CODE = True
9996 if (source .find ('.json' ) != - 1 ) or (source .find ('json.loads' ) != - 1 ):
100- TEST_FOR_POST_METHOD_RESPONSE_BODY = True
97+ TEST_FOR_POST_METHOD_RESPONSE_BODY = True
10198 COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT += 1
10299
103100 if not TEST_FOR_POST_METHOD_RESPONSE_CODE :
104- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
101+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
105102 WARNING_COUNT += 1
106- print (FAIL_COLOR + \
103+ print (FAIL_COLOR +
107104 "One or more of your test cases for POST() do not seem to be testing the response code.\n " )
105+
108106 if not TEST_FOR_POST_METHOD_RESPONSE_BODY :
109- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
107+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
110108 WARNING_COUNT += 1
111- print (FAIL_COLOR + \
109+ print (FAIL_COLOR +
112110 "One or more of your test cases for POST() do not seem to be testing the contents of the response.\n " )
113111
114112 if len (test_functions_for_post ) >= 2 and COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT < 2 :
115- print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
113+ print (FAIL_COLOR + f"[{ WARNING_COUNT } ]" )
116114 WARNING_COUNT += 1
117- print (FAIL_COLOR + \
118- "You do not seem to have TWO separate test cases, one for each possible prediction that your model can make." )
119-
120-
115+ print (FAIL_COLOR + "You do not seem to have TWO separate test cases, "
116+ "one for each possible prediction that your model can make." )
121117
122118 SANITY_TEST_PASSING = SANITY_TEST_PASSING and \
123119 TEST_FOR_GET_METHOD_RESPONSE_CODE and \
@@ -127,11 +123,10 @@ def run_sanity_check(test_dir):
127123 COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT >= 2
128124
129125 if SANITY_TEST_PASSING :
130- print (OK_COLOR + "Your test cases look good!" )
131-
132- print (WARN_COLOR + "This is a heuristic based sanity testing and cannot guarantee the correctness of your code." )
133- print (WARN_COLOR + "You should still check your work against the rubric to ensure you meet the criteria." )
126+ print (OK_COLOR + "Your test cases look good!" )
134127
128+ print (WARN_COLOR + "This is a heuristic based sanity testing and cannot guarantee the correctness of your code." )
129+ print (WARN_COLOR + "You should still check your work against the rubric to ensure you meet the criteria." )
135130
136131
137132if __name__ == "__main__" :
@@ -143,4 +138,3 @@ def run_sanity_check(test_dir):
143138 help = 'Name of the directory that has test files.' )
144139 args = parser .parse_args ()
145140 run_sanity_check (args .test_dir )
146-
0 commit comments