source: anuga_core/source/anuga/utilities/test_system_tools.py @ 5079

Last change on this file since 5079 was 5072, checked in by ole, 17 years ago

Implemented function to obtain path for a python package and refactored two
unit tests to use it when reading in data files. This now works no matter
what the current working directory is.

File size: 3.5 KB
Line 
1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose, Float
6from Scientific.IO.NetCDF import NetCDFFile
7import zlib
8from os.path import join, split, sep
9
10from anuga.utilities.system_tools import *
11
12class Test_system_tools(unittest.TestCase):
13    def setUp(self):
14        pass
15
16    def tearDown(self):
17        pass
18
19    def test_user_name(self):
20        user = get_user_name()
21
22        # print user
23        assert isinstance(user, basestring), 'User name should be a string'
24
25    def test_host_name(self):
26        host = get_host_name()
27
28        # print host
29        assert isinstance(host, basestring), 'User name should be a string'       
30
31    def test_compute_checksum(self):
32        """test_compute_checksum(self):
33
34        Check that checksums on files are OK
35        """
36
37        from tempfile import mkstemp, mktemp
38
39        # Generate a text file
40        tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.')
41        fid = os.fdopen(tmp_fd, 'w+b')
42        string = 'My temp file with textual content. AAAABBBBCCCC1234'
43        fid.write(string)
44        fid.close()
45
46        # Have to apply the 64 bit fix here since we aren't comparing two
47        # files, but rather a string and a file.
48        ref_crc = safe_crc(string)
49
50        checksum = compute_checksum(tmp_name)
51        assert checksum == ref_crc
52
53        os.remove(tmp_name)
54       
55
56
57        # Binary file
58        tmp_fd , tmp_name = mkstemp(suffix='.tmp', dir='.')
59        fid = os.fdopen(tmp_fd, 'w+b')
60
61        string = 'My temp file with binary content. AAAABBBBCCCC1234'
62        fid.write(string)
63        fid.close()
64
65        ref_crc = safe_crc(string)
66        checksum = compute_checksum(tmp_name)
67
68        assert checksum == ref_crc
69
70        os.remove(tmp_name)       
71       
72        # Binary NetCDF File X 2 (use mktemp's name)
73
74        test_array = array([[7.0, 3.14], [-31.333, 0.0]])
75
76        # First file
77        filename1 = mktemp(suffix='.nc', dir='.')
78        fid = NetCDFFile(filename1, 'w')
79        fid.createDimension('two', 2)
80        fid.createVariable('test_array', Float,
81                           ('two', 'two'))
82        fid.variables['test_array'][:] = test_array
83        fid.close()
84
85        # Second file
86        filename2 = mktemp(suffix='.nc', dir='.')
87        fid = NetCDFFile(filename2, 'w')
88        fid.createDimension('two', 2)
89        fid.createVariable('test_array', Float,
90                           ('two', 'two'))
91        fid.variables['test_array'][:] = test_array
92        fid.close()
93
94       
95        checksum1 = compute_checksum(filename1)
96        checksum2 = compute_checksum(filename2)       
97        assert checksum1 == checksum2
98
99
100        os.remove(filename1)
101        os.remove(filename2)
102
103
104    def test_compute_checksum_real(self):
105        """test_compute_checksum(self):
106
107        Check that checksums on a png file is OK
108        """
109
110        # Get path where this test is run
111        path = get_pathname_from_package('anuga.utilities')
112       
113        filename = path + sep +  'crc_test_file.png'
114
115        ref_crc = 1203293305 # Computed on Windows box
116        checksum = compute_checksum(filename)
117
118        msg = 'Computed checksum = %s, should have been %s'\
119              %(checksum, ref_crc)
120        assert checksum == ref_crc, msg
121        #print checksum
122       
123       
124#-------------------------------------------------------------
125if __name__ == "__main__":
126    suite = unittest.makeSuite(Test_system_tools, 'test')
127    runner = unittest.TextTestRunner()
128    runner.run(suite)
129
130
131
132
Note: See TracBrowser for help on using the repository browser.