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