11import os
22import re
3+ import traceback
34
45import django .test .simple
56from django .test import TestCase
67import ietf .settings
78import ietf .urls
89
910def run_tests (module_list , verbosity = 1 , extra_tests = []):
10- module_list .append (ietf .tests )
11+ module_list .append (ietf .urls )
12+ # If we append 'ietf.tests', we get it twice, first as itself, then
13+ # during the search for a 'tests' module ...
1114 return django .test .simple .run_tests (module_list , verbosity , extra_tests )
1215
1316def get_patterns (module ):
@@ -36,6 +39,7 @@ def setUp(self):
3639 self .client = Client ()
3740
3841 # find test urls
42+ self .testtuples = []
3943 self .testurls = []
4044 for root , dirs , files in os .walk (ietf .settings .BASE_DIR ):
4145 if "testurl.list" in files :
@@ -52,14 +56,15 @@ def setUp(self):
5256 code , testurl , goodurl = urlspec
5357 else :
5458 raise ValueError ("Expected 'HTTP_CODE TESTURL [GOODURL]' in %s line, found '%s'." % (filename , line ))
55- self .testurls += [ (code , testurl , goodurl ) ]
59+ self .testtuples += [ (code , testurl , goodurl ) ]
60+ self .testurls += [ testurl ]
5661 #print "(%s, %s, %s)" % (code, testurl, goodurl)
57- #print self.testurls
62+ #print self.testtuples
5863
5964 def testCoverage (self ):
6065 covered = []
6166 patterns = get_patterns (ietf .urls )
62- for code , testurl , goodurl in self .testurls :
67+ for code , testurl , goodurl in self .testtuples :
6368 for pattern in patterns :
6469 if re .match (pattern , testurl [1 :]):
6570 covered .append (pattern )
@@ -68,15 +73,46 @@ def testCoverage(self):
6873 #self.assertEqual(set(patterns), set(covered), "Not all the
6974 #application URLs has test cases. The missing are: %s" % (list(set(patterns) - set(covered))))
7075 if not set (patterns ) == set (covered ):
71- print "Not all the application URLs has test cases. The missing are: %s" % ("\n " .join (list (set (patterns ) - set (covered ))))
76+ #print "Not all the application URLs has test cases. The missing are: \n %s" % ("\n ".join(list(set(patterns) - set(covered))))
77+ print "Not all the application URLs has test cases."
7278
7379 def testUrls (self ):
74- for code , testurl , goodurl in self .testurls :
75- try :
76- response = self .client .get (testurl )
77- print "Got code %s for %s" % (response .status_code , testurl )
78- #self.assertEqual(response.status_code, code, "Unexpected response code (%s) for URL '%s'" % (response.status_code, testurl))
79- # TODO: Add comparison with goodurl
80- except :
81- print "Got exception for URL '%s'" % testurl
82- raise
80+ for code , testurl , goodurl in self .testtuples :
81+ if code in ["skip" , "Skip" ]:
82+ print "Skipping %s" % (testurl )
83+ else :
84+ try :
85+ response = self .client .get (testurl )
86+ print "Got code %s for %s" % (response .status_code , testurl )
87+ #self.assertEqual(response.status_code, code, "Unexpected response code (%s) for URL '%s'" % (response.status_code, testurl))
88+ # TODO: Add comparison with goodurl
89+ except :
90+ print "Exception for URL '%s'" % testurl
91+ traceback .print_exc ()
92+
93+ def testUrlsFallback (self ):
94+ patterns = get_patterns (ietf .urls )
95+ response_count = {}
96+ for pattern in patterns :
97+ if pattern .startswith ("^" ) and pattern .endswith ("$" ):
98+ url = "/" + pattern [1 :- 1 ]
99+ # if there is no variable parts in the url, test it
100+ if re .search ("^[-a-z0-9./_]*$" , url ) and not url in self .testurls and not url .startswith ("/admin/" ):
101+ try :
102+ response = self .client .get (url )
103+ if not response .status_code in response_count :
104+ response_count [response .status_code ] = 0
105+ response_count [response .status_code ] += 1
106+ if response .status_code != 200 :
107+ print "Bad code %s for %s" % (response .status_code , url )
108+ except :
109+ if not "Exc" in response_count :
110+ response_count ["Exc" ] = 0
111+ response_count ["Exc" ] += 1
112+ print "Exception for URL '%s'" % url
113+ traceback .print_exc ()
114+ else :
115+ print "Skipping %s" % (url )
116+ print "testUrlsFallback() response count:\n code count"
117+ for code in response_count :
118+ print " %s: %s " % (code , response_count [code ])
0 commit comments