1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose, Float |
---|
6 | from Scientific.IO.NetCDF import NetCDFFile |
---|
7 | |
---|
8 | from system_tools import * |
---|
9 | |
---|
10 | class Test_system_tools(unittest.TestCase): |
---|
11 | def setUp(self): |
---|
12 | pass |
---|
13 | |
---|
14 | def tearDown(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def test_user_name(self): |
---|
18 | user = get_user_name() |
---|
19 | |
---|
20 | # print user |
---|
21 | assert isinstance(user, basestring), 'User name should be a string' |
---|
22 | |
---|
23 | def test_host_name(self): |
---|
24 | host = get_host_name() |
---|
25 | |
---|
26 | # print host |
---|
27 | assert isinstance(host, basestring), 'User name should be a string' |
---|
28 | |
---|
29 | def test_compute_checksum(self): |
---|
30 | """test_compute_checksum(self): |
---|
31 | |
---|
32 | Check that checksums on files are OK |
---|
33 | """ |
---|
34 | |
---|
35 | import zlib |
---|
36 | from tempfile import NamedTemporaryFile, mkstemp, mktemp |
---|
37 | |
---|
38 | # Generate a text file |
---|
39 | fid = open(mktemp(suffix='.tmp', |
---|
40 | dir='.'), 'w') |
---|
41 | string = 'My temp file with textual content. AAAABBBBCCCC1234' |
---|
42 | fid.write(string) |
---|
43 | fid.close() |
---|
44 | |
---|
45 | |
---|
46 | ref_crc = zlib.crc32(string) |
---|
47 | checksum = compute_checksum(fid.name) |
---|
48 | |
---|
49 | assert checksum == ref_crc |
---|
50 | |
---|
51 | os.remove(fid.name) |
---|
52 | |
---|
53 | # Binary file |
---|
54 | fid = open(mktemp(suffix='.tmp', |
---|
55 | dir='.'), 'wb') |
---|
56 | string = 'My temp file with textual content. AAAABBBBCCCC1234' |
---|
57 | fid.write(string) |
---|
58 | fid.close() |
---|
59 | |
---|
60 | ref_crc = zlib.crc32(string) |
---|
61 | checksum = compute_checksum(fid.name) |
---|
62 | |
---|
63 | assert checksum == ref_crc |
---|
64 | os.remove(fid.name) |
---|
65 | |
---|
66 | # Binary NetCDF File X 2 |
---|
67 | |
---|
68 | test_array = array([[7.0, 3.14], [-31.333, 0.0]]) |
---|
69 | |
---|
70 | # First file |
---|
71 | filename1 = mktemp(suffix='.nc', dir='.') |
---|
72 | fid = NetCDFFile(filename1, 'w') |
---|
73 | fid.createDimension('two', 2) |
---|
74 | fid.createVariable('test_array', Float, |
---|
75 | ('two', 'two')) |
---|
76 | fid.variables['test_array'][:] = test_array |
---|
77 | fid.close() |
---|
78 | |
---|
79 | # Second file |
---|
80 | filename2 = mktemp(suffix='.nc', dir='.') |
---|
81 | fid = NetCDFFile(filename2, 'w') |
---|
82 | fid.createDimension('two', 2) |
---|
83 | fid.createVariable('test_array', Float, |
---|
84 | ('two', 'two')) |
---|
85 | fid.variables['test_array'][:] = test_array |
---|
86 | fid.close() |
---|
87 | |
---|
88 | |
---|
89 | checksum1 = compute_checksum(filename1) |
---|
90 | checksum2 = compute_checksum(filename2) |
---|
91 | assert checksum1 == checksum2 |
---|
92 | |
---|
93 | |
---|
94 | os.remove(filename1) |
---|
95 | os.remove(filename2) |
---|
96 | |
---|
97 | |
---|
98 | #------------------------------------------------------------- |
---|
99 | if __name__ == "__main__": |
---|
100 | suite = unittest.makeSuite(Test_system_tools, 'test') |
---|
101 | runner = unittest.TextTestRunner() |
---|
102 | runner.run(suite) |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|