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

Last change on this file since 5039 was 5039, checked in by ole, 16 years ago

fiddled

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