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

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

Started work on unit testing of data_audit

File size: 4.4 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose, Float
6from tempfile import NamedTemporaryFile
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_valid(self):
19        """Basic test using an invalid XML file
20        """
21
22        # Generate invalid example
23       
24        fid = NamedTemporaryFile(mode='w',
25                                 suffix='.asc',
26                                 dir='.')
27        string = 'Example data file with textual content. AAAABBBBCCCC1234'
28        fid.write(string)
29        fid.flush()
30       
31        # Create associated license file
32        basename, ext = os.path.splitext(fid.name)
33        license_filename = basename + '.lic'
34   
35        #print fid.name, license_filename
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>mainland_only.csv</filename>
52      <checksum>-1661725548</checksum>
53      <publishable>No</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"""
70        licfid.write(xml_string)
71        licfid.close()
72
73        licfid = open(license_filename)
74        #print licfid.read()
75       
76        try:
77            license_file_is_valid(licfid)
78        except CRCMismatch:
79            pass
80        else:
81            msg = 'Should have raised bad CRC exception' 
82            raise Exception, msg       
83               
84        # Clean up
85        licfid.close()
86        fid.close()
87        os.remove(license_filename)
88       
89
90    def NOtest_license_file_is_valid(self):
91        """Basic test using an valid XML file
92        """
93
94        # FIXME(Ole): NOT FINISHED
95       
96        # Generate valid example
97       
98        fid = NamedTemporaryFile(mode='w',
99                                 suffix='.asc',
100                                 dir='.')
101        string = 'Example data file with textual content. AAAABBBBCCCC1234'
102        fid.write(string)
103        fid.flush()
104       
105        # Strip leading dir (./)
106        data_filename = os.path.split(fid.name)[1]
107       
108        print 'Name', data_filename
109       
110        # Create associated license file
111        basename, ext = os.path.splitext(fid.name)
112        license_filename = basename + '.lic'
113   
114        licfid = open(license_filename, 'w')
115        xml_string = """<?xml version="1.0" encoding="iso-8859-1"?>
116
117  <ga_license_file>
118    <metadata>
119      <author>Ole Nielsen</author>
120      <svn_keywords>
121        <author>$Author$</author> 
122        <date>$Date$</date>
123        <revision>$Revision$</revision>
124        <url>$URL:$</url>
125        <id>$Id$</id>
126      </svn_keywords>
127    </metadata>
128    <datafile>
129      <filename>%s</filename>
130      <checksum>%s</checksum>
131      <publishable>Yes</publishable>
132      <accountable>Jane Sexton</accountable>
133      <source>Unknown</source>
134      <IP_owner>Geoscience Australia</IP_owner>
135      <IP_info>This is a test</IP_info>
136    </datafile>
137
138  </ga_license_file>
139""" %(data_filename, '000')
140
141        licfid.write(xml_string)
142        licfid.close()
143
144        licfid = open(license_filename)
145        #print licfid.read()
146       
147        #print fid.name, license_filename
148       
149        print os.listdir('.')
150        license_file_is_valid(licfid, verbose=True)
151               
152        # Clean up
153        licfid.close()
154        fid.close()
155        os.remove(license_filename)
156       
157               
158       
159#-------------------------------------------------------------
160if __name__ == "__main__":
161    suite = unittest.makeSuite(Test_data_audit, 'test')
162    runner = unittest.TextTestRunner()
163    runner.run(suite)
164
165
166
167
Note: See TracBrowser for help on using the repository browser.