1
2
3
4
5
6
7
8
9 """Unit tests for PyMVPA Hamster"""
10
11 import os
12 import unittest
13 from tempfile import mktemp
14 import numpy as N
15
16 from mvpa.misc.io.hamster import *
17
19
21
22
23 self.failUnlessRaises(ValueError, Hamster, "1", 2)
24
25 self.failUnlessRaises(ValueError, Hamster, "1", bu=123)
26
27 self.failUnlessRaises(ValueError, Hamster, 1)
28
29 self.failUnlessRaises(ValueError, Hamster, dump=123)
30
31 self.failUnlessRaises(IOError, Hamster, "/dev/ZUMBARGAN123")
32
33 hh=Hamster(budda=1, z=[123], fuga="123"); hh.h1=123;
34 delattr(hh, 'budda')
35 self.failUnless(`hh` == "Hamster(fuga='123', h1=123, z=[123])")
36
37
39 ex1 = """eins zwei drei
40 0 1 2
41 3 4 5
42 """
43 ex2 = {'d1': N.random.normal(size=(4,4))}
44
45 hamster = Hamster(ex1=ex1)
46 hamster.d = ex2
47 hamster.boo = HamsterHelperTests
48
49 total_dict = {'ex1' : ex1,
50 'd' : ex2,
51 'boo' : HamsterHelperTests}
52 self.failUnless(hamster.asdict() == total_dict)
53 self.failUnless(set(hamster.registered) == set(['ex1', 'd', 'boo']))
54
55 filename = mktemp('mvpa', 'test')
56 filename_gz = filename + '.gz'
57 filename_bogusgz = filename + '_bogus.gz'
58
59
60 hamster.dump(filename)
61 hamster.dump(filename_gz)
62
63 hamster.dump(filename_bogusgz, compresslevel=0)
64 self.failUnless(hamster.asdict() == total_dict)
65
66
67 gzplain = gzip.open(filename)
68 self.failUnlessRaises(IOError, gzplain.readlines)
69 gzipped = gzip.open(filename_gz)
70 discard = gzipped.readlines()
71 gzbogus = gzip.open(filename_bogusgz)
72 self.failUnlessRaises(IOError, gzbogus.readlines)
73
74
75 hamster2 = Hamster(filename)
76
77
78 k = hamster.__dict__.keys();
79 k2 = hamster2.__dict__.keys();
80 self.failUnless(set(k) == set(k2))
81
82
83 self.failUnless(hamster.ex1 is hamster.ex1)
84 self.failUnless(not (hamster.ex1 is hamster2.ex1))
85
86
87 self.failUnless(hamster.ex1 == hamster2.ex1)
88
89 self.failUnless(hamster.d.keys() == hamster2.d.keys())
90 self.failUnless((hamster.d['d1'] == hamster2.d['d1']).all())
91
92
93 self.failUnless(hamster.boo == hamster2.boo)
94
95 self.failUnless(hamster.boo is hamster2.boo)
96
97
98 os.remove(filename)
99 os.remove(filename_gz)
100 os.remove(filename_bogusgz)
101
103 ex1 = """eins zwei drei
104 0 1 2
105 3 4 5
106 """
107 ex2 = {'d1': N.random.normal(size=(4,4))}
108
109 h = Hamster(ex1=ex1)
110 h.ex2 = ex2
111 self.failUnless(hasattr(h, 'ex2'))
112 h.ex2 = None
113 self.failUnless(h.ex2 is None)
114 h.ex2 = 123
115 self.failUnless(h.ex2 == 123)
116 h.has_key = 123
117
118
119
122
123
124 if __name__ == '__main__':
125 import runner
126