@@ -102,52 +102,68 @@ def note(string):
102102 print now .strftime (" %Y-%m-%d_%H:%M" ), "+%ds" % (now - prev_note_time ).seconds
103103 prev_note_time = datetime .utcnow ()
104104
105+ def module_setup (module ):
106+ # get selected prefixes, if any
107+ module .prefixes = os .environ .get ("URLPREFIX" , "" ).split ()
108+
109+ # find test urls
110+ module .testtuples = []
111+ module .testurls = []
112+ module .diffchunks = []
113+ for root , dirs , files in os .walk (settings .BASE_DIR ):
114+ if "testurl.list" in files :
115+ module .testtuples += read_testurls (root + "/testurl.list" )
116+ if "testurls.list" in files :
117+ module .testtuples += read_testurls (root + "/testurls.list" )
118+ module .testurls = [ tuple [1 ] for tuple in module .testtuples ]
119+
120+ # find diff chunks
121+ testdir = os .path .abspath (settings .BASE_DIR + "/../test/diff/" )
122+ for item in os .listdir (testdir ):
123+ path = testdir + "/" + item
124+ if item .startswith ("generic-" ) and os .path .isfile (path ):
125+ chunk = filetext (path ).strip ()
126+ chunk = re .sub (r"([\[\]().|+*?])" , r"\\\1" , chunk )
127+ # @@ -27,0 \+23,1 @@
128+ chunk = re .sub (r"(?m)^@@ -\d+,(\d+) \\\+\d+,(\d+) @@$" , r"@@ -\d+,\1 \+\d+,\2 @@" , chunk )
129+ #print "*** Installing diff chunk:"
130+ #print chunk
131+ module .diffchunks .append (chunk )
132+
133+ # extract application urls:
134+ module .patterns = get_patterns (ietf .urls )
135+
136+ # apply prefix filters
137+ module .patterns = [ pattern for pattern in module .patterns for prefix in module .prefixes if re .match (prefix , pattern ) ]
138+ module .testtuples = [ tuple for tuple in module .testtuples for prefix in module .prefixes if re .match (prefix , tuple [1 ][1 :]) ]
139+
140+ # Use the default database for the url tests, instead of the test database
141+ module .testdb = settings .DATABASE_NAME
142+ connection .close ()
143+ settings .DATABASE_NAME = startup_database
144+ # Install updated fixtures:
145+ # Also has the side effect of creating the database connection.
146+ management .syncdb (verbosity = 1 , interactive = False )
147+ connection .close ()
148+ settings .DATABASE_NAME = module .testdb
149+ connection .cursor ()
150+
151+
152+
105153class UrlTestCase (TestCase ):
154+
155+ def __init__ (self , * args , ** kwargs ):
156+ TestCase .__init__ (self , * args , ** kwargs )
157+
158+
106159 def setUp (self ):
107160 from django .test .client import Client
108161 self .client = Client ()
109162
110- # get selected prefixes, if any
111- self .prefixes = os .environ .get ("URLPREFIX" , "" ).split ()
112-
113- # find test urls
114- self .testtuples = []
115- self .testurls = []
116- self .diffchunks = []
117- for root , dirs , files in os .walk (settings .BASE_DIR ):
118- if "testurl.list" in files :
119- self .testtuples += read_testurls (root + "/testurl.list" )
120- if "testurls.list" in files :
121- self .testtuples += read_testurls (root + "/testurls.list" )
122- self .testurls = [ tuple [1 ] for tuple in self .testtuples ]
123-
124- # find diff chunks
125- testdir = os .path .abspath (settings .BASE_DIR + "/../test/diff/" )
126- for item in os .listdir (testdir ):
127- path = testdir + "/" + item
128- if item .startswith ("generic-" ) and os .path .isfile (path ):
129- chunk = filetext (path ).strip ()
130- chunk = re .sub (r"([\[\]().|+*?])" , r"\\\1" , chunk )
131- # @@ -27,0 \+23,1 @@
132- chunk = re .sub (r"(?m)^@@ -\d+,(\d+) \\\+\d+,(\d+) @@$" , r"@@ -\d+,\1 \+\d+,\2 @@" , chunk )
133- #print "*** Installing diff chunk:"
134- #print chunk
135- self .diffchunks .append (chunk )
136-
137- # extract application urls:
138- self .patterns = get_patterns (ietf .urls )
139-
140- # apply prefix filters
141- self .patterns = [ pattern for pattern in self .patterns for prefix in self .prefixes if re .match (prefix , pattern ) ]
142- self .testtuples = [ tuple for tuple in self .testtuples for prefix in self .prefixes if re .match (prefix , tuple [1 ][1 :]) ]
143-
144- # Use the default database for the url tests, instead of the test database
145163 self .testdb = settings .DATABASE_NAME
146164 connection .close ()
147165 settings .DATABASE_NAME = startup_database
148- # Install updated fixtures:
149- # Also has the side effect of creating the database connection.
150- management .syncdb (verbosity = 1 , interactive = False )
166+ connection .cursor ()
151167
152168 def tearDown (self ):
153169 # Revert to using the test database
@@ -157,16 +173,16 @@ def tearDown(self):
157173
158174 def testCoverage (self ):
159175 covered = []
160- for codes , testurl , goodurl in self .testtuples :
161- for pattern in self .patterns :
176+ for codes , testurl , goodurl in module .testtuples :
177+ for pattern in module .patterns :
162178 if re .match (pattern , testurl [1 :]):
163179 covered .append (pattern )
164180 # We should have at least one test case for each url pattern declared
165181 # in our Django application:
166182 #self.assertEqual(set(patterns), set(covered), "Not all the
167183 #application URLs has test cases. The missing are: %s" % (list(set(patterns) - set(covered))))
168- if not set (self .patterns ) == set (covered ):
169- missing = list (set (self .patterns ) - set (covered ))
184+ if not set (module .patterns ) == set (covered ):
185+ missing = list (set (module .patterns ) - set (covered ))
170186 print "Not all the application URLs has test cases, there are %d missing." % (len (missing ))
171187 print "The ones missing are: "
172188 for pattern in missing :
@@ -270,7 +286,7 @@ def sorted(l):
270286 contextlines = 0
271287 difflist = list (unified_diff (goodtext , testtext , master , url , "" , "" , contextlines , lineterm = "" ))
272288 diff = "\n " .join (difflist )
273- for chunk in self .diffchunks :
289+ for chunk in module .diffchunks :
274290 #print "*** Checking for chunk:", chunk[:24]
275291 while re .search (chunk , diff ):
276292 #print "*** Removing chunk of %s lines" % (len(chunk.split("\n")))
@@ -321,20 +337,20 @@ def sorted(l):
321337
322338 def testUrlsList (self ):
323339 note ("\n Testing specified URLs:" )
324- self .doUrlsTest (self .testtuples )
340+ self .doUrlsTest (module .testtuples )
325341
326342 def testRedirectsList (self ):
327343 note ("\n Testing specified Redirects:" )
328- self .doRedirectsTest (self .testtuples )
344+ self .doRedirectsTest (module .testtuples )
329345
330346 def testUrlsFallback (self ):
331347 note ("\n Fallback: Test access to URLs which don't have an explicit test entry:" )
332348 lst = []
333- for pattern in self .patterns :
349+ for pattern in module .patterns :
334350 if pattern .startswith ("^" ) and pattern .endswith ("$" ):
335351 url = "/" + pattern [1 :- 1 ]
336352 # if there is no variable parts in the url, test it
337- if re .search ("^[-a-z0-9./_]*$" , url ) and not url in self .testurls and not url .startswith ("/admin/" ):
353+ if re .search ("^[-a-z0-9./_]*$" , url ) and not url in module .testurls and not url .startswith ("/admin/" ):
338354 lst .append ((["200" ], url , None ))
339355 else :
340356 #print "No fallback test for %s" % (url)
@@ -343,3 +359,9 @@ def testUrlsFallback(self):
343359 lst .append ((["Skip" ], pattern , None ))
344360
345361 self .doUrlsTest (lst )
362+
363+
364+ class Module :
365+ pass
366+ module = Module ()
367+ module_setup (module )
0 commit comments