1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose, Float |
---|
6 | from tempfile import mkstemp |
---|
7 | import os |
---|
8 | |
---|
9 | from data_audit import * |
---|
10 | |
---|
11 | class Test_data_audit(unittest.TestCase): |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | def test_license_file_is_not_valid1(self): |
---|
19 | """Basic test using an invalid XML file. This one |
---|
20 | should fail on bad CRC checksum |
---|
21 | """ |
---|
22 | |
---|
23 | # Generate invalid checksum example |
---|
24 | |
---|
25 | tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.') |
---|
26 | fid = os.fdopen(tmp_fd, 'w') |
---|
27 | |
---|
28 | string = 'Example data file with textual content. AAAABBBBCCCC1234' |
---|
29 | fid.write(string) |
---|
30 | fid.close() |
---|
31 | |
---|
32 | # Create associated license file |
---|
33 | basename, ext = os.path.splitext(tmp_name) |
---|
34 | license_filename = basename + '.lic' |
---|
35 | |
---|
36 | licfid = open(license_filename, 'w') |
---|
37 | xml_string = """<?xml version="1.0" encoding="iso-8859-1"?> |
---|
38 | |
---|
39 | <ga_license_file> |
---|
40 | <metadata> |
---|
41 | <author>Ole Nielsen</author> |
---|
42 | <svn_keywords> |
---|
43 | <author>$Author: ole $</author> |
---|
44 | <date>$Date: 2008-01-21 18:58:15 +1100 (Mon, 21 Jan 2008) $</date> |
---|
45 | <revision>$Revision$</revision> |
---|
46 | <url>$URL: https://datamining.anu.edu.au/svn/ga/anuga_core/source/anuga/utilities/mainland_only.lic $</url> |
---|
47 | <id>$Id: mainland_only.lic 4963 2008-01-21 07:58:15Z ole $</id> |
---|
48 | </svn_keywords> |
---|
49 | </metadata> |
---|
50 | <datafile> |
---|
51 | <filename>%s</filename> |
---|
52 | <checksum>-111111</checksum> |
---|
53 | <publishable>Yes</publishable> |
---|
54 | <accountable>Jane Sexton</accountable> |
---|
55 | <source>Unknown</source> |
---|
56 | <IP_owner>Geoscience Australia</IP_owner> |
---|
57 | <IP_info>This is a polygon comprising easting and northing locations |
---|
58 | tracing parts of the coastline at Dampier WA as well as a rectangular area inland. |
---|
59 | This is used to specifically set the onshore initial condition in a tsunami scenario |
---|
60 | and here, it is used with a unit test in test_polygon.py. |
---|
61 | |
---|
62 | The coastline was derived from Maritime Boundaries which is a public dataset. However, |
---|
63 | rumour has it that some of it was digitised from a Landgate supplied image. |
---|
64 | |
---|
65 | The origin and license issues are still undecided</IP_info> |
---|
66 | </datafile> |
---|
67 | |
---|
68 | </ga_license_file> |
---|
69 | """ %tmp_name |
---|
70 | |
---|
71 | licfid.write(xml_string) |
---|
72 | licfid.close() |
---|
73 | |
---|
74 | licfid = open(license_filename) |
---|
75 | #print licfid.read() |
---|
76 | |
---|
77 | try: |
---|
78 | license_file_is_valid(licfid) |
---|
79 | except CRCMismatch: |
---|
80 | pass |
---|
81 | else: |
---|
82 | msg = 'Should have raised bad CRC exception' |
---|
83 | raise Exception, msg |
---|
84 | |
---|
85 | # Clean up |
---|
86 | licfid.close() |
---|
87 | os.remove(license_filename) |
---|
88 | try: |
---|
89 | os.remove(tmp_name) |
---|
90 | except: |
---|
91 | # FIXME(Ole)QThis doesn't work on Windows for some reason |
---|
92 | pass |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | def test_license_file_is_not_valid2(self): |
---|
98 | """Basic test using an invalid XML file. This one |
---|
99 | should fail on Not Publishable |
---|
100 | """ |
---|
101 | |
---|
102 | # Generate invalid checksum example |
---|
103 | |
---|
104 | tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.') |
---|
105 | fid = os.fdopen(tmp_fd, 'w') |
---|
106 | |
---|
107 | string = 'Example data file with textual content. AAAABBBBCCCC1234' |
---|
108 | fid.write(string) |
---|
109 | fid.close() |
---|
110 | |
---|
111 | # Create associated license file |
---|
112 | basename, ext = os.path.splitext(tmp_name) |
---|
113 | license_filename = basename + '.lic' |
---|
114 | |
---|
115 | licfid = open(license_filename, 'w') |
---|
116 | xml_string = """<?xml version="1.0" encoding="iso-8859-1"?> |
---|
117 | |
---|
118 | <ga_license_file> |
---|
119 | <metadata> |
---|
120 | <author>Ole Nielsen</author> |
---|
121 | <svn_keywords> |
---|
122 | <author>$Author: ole $</author> |
---|
123 | <date>$Date: 2008-01-21 18:58:15 +1100 (Mon, 21 Jan 2008) $</date> |
---|
124 | <revision>$Revision$</revision> |
---|
125 | <url>$URL: https://datamining.anu.edu.au/svn/ga/anuga_core/source/anuga/utilities/mainland_only.lic $</url> |
---|
126 | <id>$Id: mainland_only.lic 4963 2008-01-21 07:58:15Z ole $</id> |
---|
127 | </svn_keywords> |
---|
128 | </metadata> |
---|
129 | <datafile> |
---|
130 | <filename>%s</filename> |
---|
131 | <checksum>-1484449438</checksum> |
---|
132 | <publishable>no</publishable> |
---|
133 | <accountable>Jane Sexton</accountable> |
---|
134 | <source>Unknown</source> |
---|
135 | <IP_owner>Geoscience Australia</IP_owner> |
---|
136 | <IP_info>This is a polygon comprising easting and northing locations</IP_info> |
---|
137 | </datafile> |
---|
138 | |
---|
139 | </ga_license_file> |
---|
140 | """ %tmp_name |
---|
141 | |
---|
142 | licfid.write(xml_string) |
---|
143 | licfid.close() |
---|
144 | |
---|
145 | licfid = open(license_filename) |
---|
146 | #print licfid.read() |
---|
147 | |
---|
148 | |
---|
149 | try: |
---|
150 | license_file_is_valid(licfid) |
---|
151 | except NotPublishable: |
---|
152 | pass |
---|
153 | else: |
---|
154 | msg = 'Should have raised NotPublishable exception' |
---|
155 | raise Exception, msg |
---|
156 | |
---|
157 | # Clean up |
---|
158 | licfid.close() |
---|
159 | os.remove(license_filename) |
---|
160 | |
---|
161 | fid.close() |
---|
162 | try: |
---|
163 | os.remove(tmp_name) |
---|
164 | except: |
---|
165 | # FIXME(Ole)QThis doesn't work on Windows for some reason |
---|
166 | pass |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | def test_license_file_is_not_valid3(self): |
---|
172 | """Basic test using an invalid XML file. This one |
---|
173 | should fail on Filename Mismatch |
---|
174 | """ |
---|
175 | |
---|
176 | |
---|
177 | tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.') |
---|
178 | fid = os.fdopen(tmp_fd, 'w') |
---|
179 | |
---|
180 | string = 'Example data file with textual content. AAAABBBBCCCC1234' |
---|
181 | fid.write(string) |
---|
182 | fid.close() |
---|
183 | |
---|
184 | # Create associated license file |
---|
185 | basename, ext = os.path.splitext(tmp_name) |
---|
186 | license_filename = basename + '.lic' |
---|
187 | |
---|
188 | licfid = open(license_filename, 'w') |
---|
189 | xml_string = """<?xml version="1.0" encoding="iso-8859-1"?> |
---|
190 | |
---|
191 | <ga_license_file> |
---|
192 | <metadata> |
---|
193 | <author>Ole Nielsen</author> |
---|
194 | <svn_keywords> |
---|
195 | <author>$Author: ole $</author> |
---|
196 | <date>$Date: 2008-01-21 18:58:15 +1100 (Mon, 21 Jan 2008) $</date> |
---|
197 | <revision>$Revision$</revision> |
---|
198 | <url>$URL:$</url> |
---|
199 | <id>$Id:$</id> |
---|
200 | </svn_keywords> |
---|
201 | </metadata> |
---|
202 | <datafile> |
---|
203 | <filename>%s</filename> |
---|
204 | <checksum>-1484449438</checksum> |
---|
205 | <publishable>Yes</publishable> |
---|
206 | <accountable>Jane Sexton</accountable> |
---|
207 | <source>Unknown</source> |
---|
208 | <IP_owner>Geoscience Australia</IP_owner> |
---|
209 | <IP_info>This is a polygon comprising easting and northing locations</IP_info> |
---|
210 | </datafile> |
---|
211 | |
---|
212 | </ga_license_file> |
---|
213 | """ %(basename + '.no_exist') |
---|
214 | |
---|
215 | |
---|
216 | licfid.write(xml_string) |
---|
217 | licfid.close() |
---|
218 | |
---|
219 | licfid = open(license_filename) |
---|
220 | #print licfid.read() |
---|
221 | |
---|
222 | |
---|
223 | try: |
---|
224 | license_file_is_valid(licfid) |
---|
225 | except FilenameMismatch: |
---|
226 | pass |
---|
227 | else: |
---|
228 | msg = 'Should have raised FilenameMismatch exception' |
---|
229 | raise Exception, msg |
---|
230 | |
---|
231 | # Clean up |
---|
232 | licfid.close() |
---|
233 | fid.close() |
---|
234 | os.remove(license_filename) |
---|
235 | os.remove(tmp_name) |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | def test_license_file_is_valid(self): |
---|
241 | """Basic test using an valid XML file |
---|
242 | """ |
---|
243 | |
---|
244 | # Generate valid example |
---|
245 | tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.') |
---|
246 | fid = os.fdopen(tmp_fd, 'w') |
---|
247 | |
---|
248 | string = 'Example data file with textual content. AAAABBBBCCCC1234' |
---|
249 | fid.write(string) |
---|
250 | fid.close() |
---|
251 | |
---|
252 | # Strip leading dir (./) |
---|
253 | data_filename = os.path.split(tmp_name)[1] |
---|
254 | |
---|
255 | #print 'Name', data_filename |
---|
256 | |
---|
257 | # Create associated license file |
---|
258 | basename, ext = os.path.splitext(tmp_name) |
---|
259 | license_filename = basename + '.lic' |
---|
260 | |
---|
261 | licfid = open(license_filename, 'w') |
---|
262 | xml_string = """<?xml version="1.0" encoding="iso-8859-1"?> |
---|
263 | |
---|
264 | <ga_license_file> |
---|
265 | <metadata> |
---|
266 | <author>Ole Nielsen</author> |
---|
267 | <svn_keywords> |
---|
268 | <author>$Author$</author> |
---|
269 | <date>$Date$</date> |
---|
270 | <revision>$Revision$</revision> |
---|
271 | <url>$URL:$</url> |
---|
272 | <id>$Id$</id> |
---|
273 | </svn_keywords> |
---|
274 | </metadata> |
---|
275 | <datafile> |
---|
276 | <filename>%s</filename> |
---|
277 | <checksum>%s</checksum> |
---|
278 | <publishable>Yes</publishable> |
---|
279 | <accountable>Jane Sexton</accountable> |
---|
280 | <source>Unknown</source> |
---|
281 | <IP_owner>Geoscience Australia</IP_owner> |
---|
282 | <IP_info>This is a test</IP_info> |
---|
283 | </datafile> |
---|
284 | |
---|
285 | </ga_license_file> |
---|
286 | """ %(data_filename, '-1484449438') |
---|
287 | |
---|
288 | licfid.write(xml_string) |
---|
289 | licfid.close() |
---|
290 | |
---|
291 | licfid = open(license_filename) |
---|
292 | license_file_is_valid(licfid)#, verbose=True) |
---|
293 | licfid.close() |
---|
294 | |
---|
295 | # Clean up |
---|
296 | os.remove(license_filename) |
---|
297 | os.remove(tmp_name) |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | def test_valid_license_file_with_multiple_files(self): |
---|
303 | """Test of XML file with more than one datafile element. |
---|
304 | """ |
---|
305 | |
---|
306 | # Generate example files |
---|
307 | tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.') |
---|
308 | fid = os.fdopen(tmp_fd, 'w') |
---|
309 | string = 'Example data file with textual content. AAAABBBBCCCC1234' |
---|
310 | fid.write(string) |
---|
311 | fid.close() |
---|
312 | |
---|
313 | # Derive filenames |
---|
314 | basename, ext = os.path.splitext(tmp_name) |
---|
315 | data_filename1 = basename + '.asc' |
---|
316 | data_filename2 = basename + '.prj' |
---|
317 | license_filename = basename + '.lic' |
---|
318 | #print data_filename1, data_filename2, license_filename |
---|
319 | |
---|
320 | # Write data to second data file |
---|
321 | fid = open(data_filename2, 'w') |
---|
322 | string = 'Another example data file with text in it' |
---|
323 | fid.write(string) |
---|
324 | fid.close() |
---|
325 | |
---|
326 | # Create license file |
---|
327 | licfid = open(license_filename, 'w') |
---|
328 | xml_string = """<?xml version="1.0" encoding="iso-8859-1"?> |
---|
329 | |
---|
330 | <ga_license_file> |
---|
331 | <metadata> |
---|
332 | <author>Ole Nielsen</author> |
---|
333 | <svn_keywords> |
---|
334 | <author>$Author$</author> |
---|
335 | <date>$Date$</date> |
---|
336 | <revision>$Revision$</revision> |
---|
337 | <url>$URL:$</url> |
---|
338 | <id>$Id$</id> |
---|
339 | </svn_keywords> |
---|
340 | </metadata> |
---|
341 | <datafile> |
---|
342 | <filename>%s</filename> |
---|
343 | <checksum>%s</checksum> |
---|
344 | <publishable>Yes</publishable> |
---|
345 | <accountable>Jane Sexton</accountable> |
---|
346 | <source>Generated on the fly</source> |
---|
347 | <IP_owner>Geoscience Australia</IP_owner> |
---|
348 | <IP_info>This is a test</IP_info> |
---|
349 | </datafile> |
---|
350 | <datafile> |
---|
351 | <filename>%s</filename> |
---|
352 | <checksum>%s</checksum> |
---|
353 | <publishable>Yes</publishable> |
---|
354 | <accountable>Ole Nielsen</accountable> |
---|
355 | <source>Generated on the fly</source> |
---|
356 | <IP_owner>Geoscience Australia</IP_owner> |
---|
357 | <IP_info>This is another test</IP_info> |
---|
358 | </datafile> |
---|
359 | </ga_license_file> |
---|
360 | """ %(data_filename1, '-1484449438', data_filename2, '-1322430740') |
---|
361 | |
---|
362 | licfid.write(xml_string) |
---|
363 | licfid.close() |
---|
364 | |
---|
365 | licfid = open(license_filename) |
---|
366 | license_file_is_valid(licfid)#, verbose=True) |
---|
367 | licfid.close() |
---|
368 | |
---|
369 | # Clean up |
---|
370 | os.remove(license_filename) |
---|
371 | os.remove(data_filename1) |
---|
372 | os.remove(data_filename2) |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | |
---|
378 | #------------------------------------------------------------- |
---|
379 | if __name__ == "__main__": |
---|
380 | suite = unittest.makeSuite(Test_data_audit, 'test') |
---|
381 | runner = unittest.TextTestRunner() |
---|
382 | runner.run(suite) |
---|
383 | |
---|
384 | |
---|
385 | |
---|
386 | |
---|