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