source: anuga_core/source/anuga/utilities/test_data_audit.py @ 5096

Last change on this file since 5096 was 5096, checked in by duncan, 16 years ago

bug fix

File size: 11.1 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose, Float
6from tempfile import mkstemp
7import os
8
9from data_audit import *
10
11class 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        #licfid.close()
77
78        try:
79            license_file_is_valid(license_filename, tmp_name)
80        except CRCMismatch:
81            pass
82        else:
83            msg = 'Should have raised bad CRC exception' 
84            raise Exception, msg       
85               
86        # Clean up
87        #licfid.close()
88        os.remove(license_filename)
89        try:
90            os.remove(tmp_name)       
91        except:
92            # FIXME(DSG) Windows seems to have a problem deleting this file
93            # This is a work-a-round. It doesn't fix the root problem
94            # It does delete the file though.
95            fid = open(tmp_name, 'a')       
96            string = 'Example data file'
97            fid.write(string)
98            fid.close()
99            os.remove(tmp_name) 
100       
101
102
103
104    def test_license_file_is_not_valid2(self):
105        """Basic test using an invalid XML file. This one
106        should fail on Not Publishable
107        """
108
109        # Generate invalid checksum example
110       
111        tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.')
112        fid = os.fdopen(tmp_fd, 'w')
113       
114        string = 'Example data file with textual content. AAAABBBBCCCC1234'
115        fid.write(string)
116        fid.close()
117       
118        # Create associated license file
119        basename, ext = os.path.splitext(tmp_name)
120        license_filename = basename + '.lic'
121   
122        licfid = open(license_filename, 'w')
123        xml_string = """<?xml version="1.0" encoding="iso-8859-1"?>
124
125  <ga_license_file>
126    <metadata>
127      <author>Ole Nielsen</author>
128      <svn_keywords>
129        <author>$Author: ole $</author> 
130        <date>$Date: 2008-01-21 18:58:15 +1100 (Mon, 21 Jan 2008) $</date>
131        <revision>$Revision$</revision>
132        <url>$URL: https://datamining.anu.edu.au/svn/ga/anuga_core/source/anuga/utilities/mainland_only.lic $</url>
133        <id>$Id: mainland_only.lic 4963 2008-01-21 07:58:15Z ole $</id>
134      </svn_keywords>
135    </metadata>
136    <datafile>
137      <filename>%s</filename>
138      <checksum>-1484449438</checksum>
139      <publishable>no</publishable>
140      <accountable>Jane Sexton</accountable>
141      <source>Unknown</source>
142      <IP_owner>Geoscience Australia</IP_owner>
143      <IP_info>This is a polygon comprising easting and northing locations</IP_info>
144    </datafile>
145
146  </ga_license_file>
147""" %tmp_name
148       
149        licfid.write(xml_string)
150        licfid.close()
151
152        licfid = open(license_filename)
153        #print licfid.read()
154
155
156        try:
157            license_file_is_valid(licfid, tmp_name)
158        except NotPublishable:
159            pass
160        else:
161            msg = 'Should have raised NotPublishable exception' 
162            raise Exception, msg       
163               
164        # Clean up
165        licfid.close()
166        os.remove(license_filename)
167
168        fid.close()       
169        try:
170            os.remove(tmp_name)       
171        except:
172            # FIXME(DSG) Windows seems to have a problem deleting this file
173            # This is a work-a-round. It doesn't fix the root problem
174            # It does delete the file though.
175            fid = open(tmp_name, 'a')       
176            string = 'Example data file'
177            fid.write(string)
178            fid.close()
179            os.remove(tmp_name) 
180
181
182
183
184    def test_license_file_is_not_valid3(self):
185        """Basic test using an invalid XML file. This one
186        should fail on Filename Mismatch
187        """
188
189       
190        tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.')
191        fid = os.fdopen(tmp_fd, 'w')
192       
193        string = 'Example data file with textual content. AAAABBBBCCCC1234'
194        fid.write(string)
195        fid.close()
196       
197        # Create associated license file
198        basename, ext = os.path.splitext(tmp_name)
199        license_filename = basename + '.lic'
200   
201        licfid = open(license_filename, 'w')
202        xml_string = """<?xml version="1.0" encoding="iso-8859-1"?>
203
204  <ga_license_file>
205    <metadata>
206      <author>Ole Nielsen</author>
207      <svn_keywords>
208        <author>$Author: ole $</author> 
209        <date>$Date: 2008-01-21 18:58:15 +1100 (Mon, 21 Jan 2008) $</date>
210        <revision>$Revision$</revision>
211        <url>$URL:$</url>
212        <id>$Id:$</id>
213      </svn_keywords>
214    </metadata>
215    <datafile>
216      <filename>%s</filename>
217      <checksum>-1484449438</checksum>
218      <publishable>Yes</publishable>
219      <accountable>Jane Sexton</accountable>
220      <source>Unknown</source>
221      <IP_owner>Geoscience Australia</IP_owner>
222      <IP_info>This is a polygon comprising easting and northing locations</IP_info>
223    </datafile>
224
225  </ga_license_file>
226""" %(basename + '.no_exist')
227
228       
229        licfid.write(xml_string)
230        licfid.close()
231
232        licfid = open(license_filename)
233        #print licfid.read()
234
235
236        try:
237            license_file_is_valid(licfid, basename + '.no_exist')
238        except FilenameMismatch:
239            pass
240        else:
241            msg = 'Should have raised FilenameMismatch exception' 
242            raise Exception, msg       
243               
244        # Clean up
245        licfid.close()
246        fid.close()
247        os.remove(license_filename)
248        os.remove(tmp_name)       
249
250
251
252
253    def test_license_file_is_valid(self):
254        """Basic test using an valid XML file
255        """
256       
257        # Generate valid example
258        tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.')
259        fid = os.fdopen(tmp_fd, 'w')       
260
261        string = 'Example data file with textual content. AAAABBBBCCCC1234'
262        fid.write(string)
263        fid.close()
264       
265        # Strip leading dir (./)
266        data_filename = os.path.split(tmp_name)[1]
267       
268        #print 'Name', data_filename
269       
270        # Create associated license file
271        basename, ext = os.path.splitext(tmp_name)
272        license_filename = basename + '.lic'
273   
274        licfid = open(license_filename, 'w')
275        xml_string = """<?xml version="1.0" encoding="iso-8859-1"?>
276
277  <ga_license_file>
278    <metadata>
279      <author>Ole Nielsen</author>
280      <svn_keywords>
281        <author>$Author$</author> 
282        <date>$Date$</date>
283        <revision>$Revision$</revision>
284        <url>$URL:$</url>
285        <id>$Id$</id>
286      </svn_keywords>
287    </metadata>
288    <datafile>
289      <filename>%s</filename>
290      <checksum>%s</checksum>
291      <publishable>Yes</publishable>
292      <accountable>Jane Sexton</accountable>
293      <source>Unknown</source>
294      <IP_owner>Geoscience Australia</IP_owner>
295      <IP_info>This is a test</IP_info>
296    </datafile>
297
298  </ga_license_file>
299""" %(data_filename, '-1484449438')
300
301        licfid.write(xml_string)
302        licfid.close()
303
304        licfid = open(license_filename)
305        license_file_is_valid(licfid, data_filename)
306        licfid.close()
307       
308        # Clean up
309        os.remove(license_filename)
310        os.remove(tmp_name)       
311       
312
313
314
315    def test_valid_license_file_with_multiple_files(self):
316        """Test of XML file with more than one datafile element.
317        """
318       
319        # Generate example files
320        tmp_fd , tmp_name = mkstemp(suffix='.asc', dir='.')
321        fid = os.fdopen(tmp_fd, 'w')       
322        string = 'Example data file with textual content. AAAABBBBCCCC1234'
323        fid.write(string)
324        fid.close()
325
326        # Derive filenames
327        basename, ext = os.path.splitext(tmp_name)
328        data_filename1 = basename + '.asc' 
329        data_filename2 = basename + '.prj'
330        license_filename = basename + '.lic'
331        #print data_filename1, data_filename2, license_filename         
332
333        # Write data to second data file
334        fid = open(data_filename2, 'w')       
335        string = 'Another example data file with text in it'
336        fid.write(string)
337        fid.close()       
338
339        # Create license file
340        licfid = open(license_filename, 'w')
341        xml_string = """<?xml version="1.0" encoding="iso-8859-1"?>
342
343  <ga_license_file>
344    <metadata>
345      <author>Ole Nielsen</author>
346      <svn_keywords>
347        <author>$Author$</author> 
348        <date>$Date$</date>
349        <revision>$Revision$</revision>
350        <url>$URL:$</url>
351        <id>$Id$</id>
352      </svn_keywords>
353    </metadata>
354    <datafile>
355      <filename>%s</filename>
356      <checksum>%s</checksum>
357      <publishable>Yes</publishable>
358      <accountable>Jane Sexton</accountable>
359      <source>Generated on the fly</source>
360      <IP_owner>Geoscience Australia</IP_owner>
361      <IP_info>This is a test</IP_info>
362    </datafile>
363    <datafile>
364      <filename>%s</filename>
365      <checksum>%s</checksum>
366      <publishable>Yes</publishable>
367      <accountable>Ole Nielsen</accountable>
368      <source>Generated on the fly</source>
369      <IP_owner>Geoscience Australia</IP_owner>
370      <IP_info>This is another test</IP_info>
371    </datafile>   
372  </ga_license_file>
373""" %(data_filename1, '-1484449438', data_filename2, '-1322430740')
374
375        licfid.write(xml_string)
376        licfid.close()
377
378        licfid = open(license_filename)
379        license_file_is_valid(licfid, data_filename1)
380        license_file_is_valid(licfid, data_filename2)       
381        licfid.close()
382               
383        # Clean up
384        os.remove(license_filename)
385        os.remove(data_filename1)
386        os.remove(data_filename2)
387
388       
389
390               
391       
392#-------------------------------------------------------------
393if __name__ == "__main__":
394    suite = unittest.makeSuite(Test_data_audit, 'test')
395    runner = unittest.TextTestRunner()
396    runner.run(suite)
397
398
399
400
Note: See TracBrowser for help on using the repository browser.