unit_tests.py 12.1 KB
Newer Older
1 2 3 4
import os
import sys
import shutil
import unittest
5 6
from random import randint, uniform, choice
import string
7 8 9 10

from obitools3.obidms._obidms import OBIDMS


11 12
LINE_COUNT_FOR_TEST_COLUMN = 10000         # TODO randomize?
SMALLER_LINE_COUNT_FOR_TEST_COLUMN = 1000  # TODO randomize?
13
NB_ELEMENTS_PER_LINE = 10                  # TODO randomize?
14 15 16 17 18 19 20 21 22 23
DMS_NAME = "unit_test_dms"


def create_test_obidms():
    dms_name = DMS_NAME
    dms_dir_name = dms_name+'.obidms'
    dms = OBIDMS(dms_name)
    return (dms, dms_name, dms_dir_name)


24 25 26 27
def create_test_column(dms, data_type, multiple_elements_per_line=False):
    
    col_name = "unit_test_"+data_type

28
    if multiple_elements_per_line :
29
        elts_names = elements_names()
30 31
        col = dms.open_column(col_name, 
                              create=True, 
32
                              type=data_type, 
33
                              nb_elements_per_line=NB_ELEMENTS_PER_LINE,
34
                              elements_names=elts_names)
35
        return (col, col_name, elts_names)
36 37 38 39

    else :
        col = dms.open_column(col_name, 
                              create=True, 
40 41
                              type=data_type)
        return (col, col_name)
42

43 44

def elements_names():
45 46
    names = [str(i) for i in range(NB_ELEMENTS_PER_LINE)]
    return names
47 48 49 50 51 52 53 54 55 56 57


def random_obivalue(data_type):
    r = 1000000
    if data_type == "OBI_INT" :
        return randint(-r,r)
    elif data_type == "OBI_FLOAT" :
        return uniform(-r,r)
    elif data_type == "OBI_BOOL" :
        return randint(0,1)
    elif data_type == "OBI_CHAR" :
58 59 60
        return choice(string.ascii_lowercase)
    elif data_type == "OBI_STR" :
        length = randint(1,200)
61 62
        randoms = ''.join(choice(string.ascii_lowercase) for i in range(length))
        return randoms
63 64
    elif data_type == "OBI_SEQ" :
        length = randint(1,200)
65
        randoms = ''.join(choice("atgcryswkmdbhvn") for i in range(length))
66
        return randoms
67 68 69 70 71 72 73

class OBIDMS_Column_TestCase(unittest.TestCase):
    def tearDown(self):
        self.col.close()
        self.dms.close()
        shutil.rmtree(self.dms_dir_name, ignore_errors=True)
    def test_OBIDMS_column_type(self):
74
        assert self.col.get_data_type() == self.data_type, 'Wrong data type associated with column'
75 76
    def test_OBIDMS_column_cloning(self):
        for i in range(LINE_COUNT_FOR_TEST_COLUMN):
77
            self.col[i]= random_obivalue(self.data_type)
78 79 80 81 82 83 84
        self.col.close()
        clone = self.dms.open_column(self.col_name, clone=True)
        self.col = self.dms.open_column(self.col_name)
        assert clone.get_nb_lines_used() == self.col.get_nb_lines_used(), "Cloned column doesn't have the same number of lines used"
        i=0
        for i in range(clone.get_nb_lines_used()) :
            assert clone[i] == self.col[i], "Different value in original column and cloned column"
85
            assert clone[i] is not None, "None value"
86 87 88
        clone.close()
    def test_OBIDMS_column_set_and_get(self):
        for i in range(LINE_COUNT_FOR_TEST_COLUMN):
89
            v = random_obivalue(self.data_type)
90 91
            self.col[i] = v
            assert self.col[i] == v, "Different value than the set value"
92
            assert self.col[i] is not None, "None value"
93 94 95 96 97 98 99 100 101 102 103
    def test_OBIDMS_referring_column(self):
        for i in range(LINE_COUNT_FOR_TEST_COLUMN):
            self.col[i] = random_obivalue(self.data_type)
        ref_col = self.dms.open_column(self.col_name, referring=True)
        j = 0
        for i in range(LINE_COUNT_FOR_TEST_COLUMN):
            if i%2 :    # TODO randomize
                ref_col.grep_line(i)
                assert ref_col[j] == self.col[i], "Different value in original column and returned by referring column"
                assert ref_col[j] is not None, "None value"
                j+=1
104 105 106 107


class OBIDMS_Column_multiple_elements_TestCase(OBIDMS_Column_TestCase):
    def test_OBIDMS_column_cloning(self):
108
        pass
109
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
110 111
            v = {}
            for e in self.elts_names :
112
                v[e] = random_obivalue(self.data_type)
113
            self.col[i] = v
114 115 116 117 118 119
        self.col.close()
        clone = self.dms.open_column(self.col_name, clone=True)
        self.col = self.dms.open_column(self.col_name)
        assert clone.get_nb_lines_used() == self.col.get_nb_lines_used(), "Cloned column doesn't have the same number of lines used"
        i=0
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
120 121
            assert self.col[i] == clone[i], "Different value in original column and cloned column"
            assert self.col[i] is not None, "None value"
122
        clone.close()
123
    def test_OBIDMS_column_set_and_get_with_elements_names(self):
124 125
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
            for e in range(NB_ELEMENTS_PER_LINE) :
126
                v = random_obivalue(self.data_type)
127 128
                self.col.set_item(i, self.elts_names[e], v)
                assert self.col.get_item(i, self.elts_names[e]) == v, "Different value than the set value"
129
                assert self.col.get_item(i, self.elts_names[e]) is not None, "None value"
130 131 132 133
    def test_OBIDMS_column_set_and_get(self):
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
            v = {}
            for e in self.elts_names :
134
                v[e] = random_obivalue(self.data_type)
135 136 137
            self.col[i] = v
            assert self.col[i] == v, "Different value than the set value"
            assert self.col[i] is not None, "None value"
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    def test_OBIDMS_referring_column(self):
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
            v = {}
            for e in self.elts_names :
                v[e] = random_obivalue(self.data_type)
            self.col[i] = v
        ref_col = self.dms.open_column(self.col_name, referring=True)
        j = 0
        for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN):
            if i%2 :    # TODO randomize
                ref_col.grep_line(i)
                assert ref_col[j] == self.col[i], "Different value in original column and returned by referring column"
                assert ref_col[j] is not None, "None value"
                j+=1
        ref_col.close()
153 154 155 156


class OBIDMS_Column_OBI_INT_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
157
        self.data_type = 'OBI_INT'
158 159 160 161
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
162 163
        self.col_name = create_test_column(self.dms, 
                                           self.data_type)
164 165 166 167
       

class OBIDMS_Column_OBI_INT_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
168
        self.data_type = 'OBI_INT'
169 170 171 172 173
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
174 175 176
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
177 178 179 180


class OBIDMS_Column_OBI_FLOAT_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
181
        self.data_type = 'OBI_FLOAT'
182 183 184 185
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
186 187
        self.col_name = create_test_column(self.dms, 
                                           self.data_type)
188 189 190 191
       

class OBIDMS_Column_OBI_FLOAT_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
192
        self.data_type = 'OBI_FLOAT'
193 194 195 196 197
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
198 199 200
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
201 202 203 204


class OBIDMS_Column_OBI_BOOL_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
205
        self.data_type = 'OBI_BOOL'
206 207 208 209
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
210 211
        self.col_name = create_test_column(self.dms, 
                                           self.data_type)
212 213 214 215
       

class OBIDMS_Column_OBI_BOOL_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
216
        self.data_type = 'OBI_BOOL'
217 218 219 220 221
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
222 223 224
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
225 226 227 228


class OBIDMS_Column_OBI_CHAR_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
229
        self.data_type = 'OBI_CHAR'
230 231 232 233
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
234 235
        self.col_name = create_test_column(self.dms, 
                                                self.data_type)
236 237 238 239
       

class OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
240
        self.data_type = 'OBI_CHAR'
241 242 243 244 245
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
246 247 248
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
249 250


251 252
class OBIDMS_Column_OBI_STR_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
253
        self.data_type = 'OBI_STR'
254 255 256 257
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
258 259
        self.col_name = create_test_column(self.dms, 
                                           self.data_type)
260 261 262 263
       

class OBIDMS_Column_OBI_STR_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
264
        self.data_type = 'OBI_STR'
265 266 267 268 269
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
270 271 272
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
273

274 275
class OBIDMS_Column_OBI_SEQ_TestCase(OBIDMS_Column_TestCase):
    def setUp(self):
276
        self.data_type = 'OBI_SEQ'
277 278 279 280
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
281 282
        self.col_name = create_test_column(self.dms, 
                                           self.data_type)
283 284 285 286
       

class OBIDMS_Column_OBI_SEQ_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase):
    def setUp(self):
287
        self.data_type = 'OBI_SEQ'
288 289 290 291 292
        self.dms, \
        self.dms_name, \
        self.dms_dir_name = create_test_obidms()
        self.col, \
        self.col_name, \
293 294 295
        self.elts_names = create_test_column(self.dms, 
                                             self.data_type,
                                             multiple_elements_per_line=True)
296

297
if __name__ == '__main__': 
298 299 300 301 302 303 304 305 306
     unittest.main(verbosity=2, defaultTest=["OBIDMS_Column_OBI_INT_TestCase",
                                             "OBIDMS_Column_OBI_INT_multiple_elements_TestCase",
                                             "OBIDMS_Column_OBI_FLOAT_TestCase",
                                             "OBIDMS_Column_OBI_FLOAT_multiple_elements_TestCase",
                                             "OBIDMS_Column_OBI_BOOL_TestCase",
                                             "OBIDMS_Column_OBI_BOOL_multiple_elements_TestCase",
                                             "OBIDMS_Column_OBI_CHAR_TestCase",
                                             "OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase",
                                             "OBIDMS_Column_OBI_STR_TestCase",
307 308 309
                                             "OBIDMS_Column_OBI_STR_multiple_elements_TestCase",
                                             "OBIDMS_Column_OBI_SEQ_TestCase",
                                             "OBIDMS_Column_OBI_SEQ_multiple_elements_TestCase"])
310 311