This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
test_attrs.py
170 lines (140 loc) · 4.74 KB
/
test_attrs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
########################################################################
#
# License: BSD
# Created: August 17, 2012
# Author: Francesc Alted - francesc@blosc.io
#
########################################################################
import sys
import os, os.path
import struct
import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal
import bcolz
from bcolz.tests import common
from common import MayBeDiskTest
import unittest
class basicTest(MayBeDiskTest):
def getobject(self):
if self.flavor == 'carray':
obj = bcolz.zeros(10, dtype="i1", rootdir=self.rootdir)
assert type(obj) == bcolz.carray
elif self.flavor == 'ctable':
obj = bcolz.fromiter(((i,i*2) for i in range(10)), dtype='i2,f4',
count=10, rootdir=self.rootdir)
assert type(obj) == bcolz.ctable
return obj
def test00a(self):
"""Creating attributes in a new carray."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
self.assert_(cn.attrs['attr1'] == 'val1')
self.assert_(cn.attrs['attr2'] == 'val2')
self.assert_(cn.attrs['attr3'] == 'val3')
self.assert_(len(cn.attrs) == 3)
def test00b(self):
"""Accessing attributes in a opened carray."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
# Re-open the carray
if self.rootdir:
cn = bcolz.open(rootdir=self.rootdir)
self.assert_(cn.attrs['attr1'] == 'val1')
self.assert_(cn.attrs['attr2'] == 'val2')
self.assert_(cn.attrs['attr3'] == 'val3')
self.assert_(len(cn.attrs) == 3)
def test01a(self):
"""Removing attributes in a new carray."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
# Remove one of them
del cn.attrs['attr2']
self.assert_(cn.attrs['attr1'] == 'val1')
self.assert_(cn.attrs['attr3'] == 'val3')
self.assertRaises(KeyError, cn.attrs.__getitem__, 'attr2')
self.assert_(len(cn.attrs) == 2)
def test01b(self):
"""Removing attributes in a opened carray."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
# Reopen
if self.rootdir:
cn = bcolz.open(rootdir=self.rootdir)
# Remove one of them
del cn.attrs['attr2']
self.assert_(cn.attrs['attr1'] == 'val1')
self.assert_(cn.attrs['attr3'] == 'val3')
self.assertRaises(KeyError, cn.attrs.__getitem__, 'attr2')
self.assert_(len(cn.attrs) == 2)
def test01c(self):
"""Appending attributes in a opened carray."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
# Reopen
if self.rootdir:
cn = bcolz.open(rootdir=self.rootdir)
# Append attrs
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
self.assert_(cn.attrs['attr1'] == 'val1')
self.assert_(cn.attrs['attr2'] == 'val2')
self.assert_(cn.attrs['attr3'] == 'val3')
self.assert_(len(cn.attrs) == 3)
def test02(self):
"""Checking iterator in attrs accessor."""
cn = self.getobject()
# Some attrs
cn.attrs['attr1'] = 'val1'
cn.attrs['attr2'] = 'val2'
cn.attrs['attr3'] = 'val3'
count = 0
for key, val in cn.attrs:
if key == 'attr1':
self.assert_(val, 'val1')
if key == 'attr2':
self.assert_(val, 'val2')
if key == 'attr3':
self.assert_(val, 'val3')
count += 1
self.assert_(count, 3)
class carrayTest(basicTest):
flavor = "carray"
disk = False
class carrayDiskTest(basicTest):
flavor = "carray"
disk = True
class ctableTest(basicTest):
flavor = "ctable"
disk = False
class ctableDiskTest(basicTest):
flavor = "ctable"
disk = True
def suite():
theSuite = unittest.TestSuite()
theSuite.addTest(unittest.makeSuite(carrayTest))
theSuite.addTest(unittest.makeSuite(carrayDiskTest))
theSuite.addTest(unittest.makeSuite(ctableTest))
theSuite.addTest(unittest.makeSuite(ctableDiskTest))
return theSuite
if __name__ == "__main__":
unittest.main(defaultTest="suite")
## Local Variables:
## mode: python
## py-indent-offset: 4
## tab-width: 4
## fill-column: 72
## End: