From dbed3d9d1d17948cc2cfe8486db0415793382bad Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Fri, 9 Oct 2015 15:42:57 +0200 Subject: [PATCH] New module for unit testing with PyUnit --- python/obitools3/unit_tests.py | 230 +++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 python/obitools3/unit_tests.py diff --git a/python/obitools3/unit_tests.py b/python/obitools3/unit_tests.py new file mode 100644 index 0000000..ac9795d --- /dev/null +++ b/python/obitools3/unit_tests.py @@ -0,0 +1,230 @@ +import os +import sys +import shutil +import unittest +from random import randint, uniform + +from obitools3.obidms._obidms import OBIDMS + + +LINE_COUNT_FOR_TEST_COLUMN = 100000 # TODO randomize? +SMALLER_LINE_COUNT_FOR_TEST_COLUMN = 10000 # TODO randomize? +NB_ELEMENTS_PER_LINE = 50 # TODO randomize? +DMS_NAME = "unit_test_dms" +DATA_TYPES = ['OBI_INT', 'OBI_FLOAT', 'OBI_BOOL', 'OBI_CHAR'] + + +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) + + +def create_test_column(dms, data_type_code, multiple_elements_per_line=False): + data_types = DATA_TYPES + data_type_code = data_type_code + data_type_str = data_types[data_type_code-1] + col_name = "test_col"+data_type_str + + if multiple_elements_per_line : + elts_names = elements_names().split(";") + col = dms.open_column(col_name, + create=True, + data_type=data_type_code, + nb_elements_per_line=NB_ELEMENTS_PER_LINE, + elements_names=elements_names()) + return (col, col_name, elts_names, data_type_str) + + else : + col = dms.open_column(col_name, + create=True, + data_type=data_type_code) + return (col, col_name, data_type_str) + + +def elements_names(): + names = "" + for i in range(NB_ELEMENTS_PER_LINE): + names = names + str(i) + ";" + return names + + +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" : + nucs = 'atgc' + return bytes(nucs[randint(0,3)], 'utf-8') + elif data_type == "OBI_IDX" : + return randint(0,r) + + +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): + assert self.col.get_data_type() == self.data_type_str, 'Wrong data type associated with column' + def test_OBIDMS_column_cloning(self): + for i in range(LINE_COUNT_FOR_TEST_COLUMN): + self.col[i]= random_obivalue(self.data_type_str) + 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" + clone.close() + def test_OBIDMS_column_set_and_get(self): + for i in range(LINE_COUNT_FOR_TEST_COLUMN): + v = random_obivalue(self.data_type_str) + self.col[i] = v + assert self.col[i] == v, "Different value than the set value" + + +class OBIDMS_Column_multiple_elements_TestCase(OBIDMS_Column_TestCase): + def test_OBIDMS_column_cloning(self): + for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN): + for e in range(NB_ELEMENTS_PER_LINE) : + self.col.set_item(i, self.elts_names[e], random_obivalue(self.data_type_str)) + 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): + for e in range(NB_ELEMENTS_PER_LINE) : + assert clone.get_item(i, self.elts_names[e]) == self.col.get_item(i, self.elts_names[e]), "Different value in original column and cloned column" + clone.close() + def test_OBIDMS_column_set_and_get(self): + for i in range(SMALLER_LINE_COUNT_FOR_TEST_COLUMN): + for e in range(NB_ELEMENTS_PER_LINE) : + v = random_obivalue(self.data_type_str) + 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" + + + +class OBIDMS_Column_OBI_INT_TestCase(OBIDMS_Column_TestCase): + def setUp(self): + self.data_type_code = 1 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code) + + +class OBIDMS_Column_OBI_INT_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase): + def setUp(self): + self.data_type_code = 1 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.elts_names, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code, + multiple_elements_per_line=True) + + +class OBIDMS_Column_OBI_FLOAT_TestCase(OBIDMS_Column_TestCase): + def setUp(self): + self.data_type_code = 2 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code) + + +class OBIDMS_Column_OBI_FLOAT_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase): + def setUp(self): + self.data_type_code = 2 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.elts_names, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code, + multiple_elements_per_line=True) + + +class OBIDMS_Column_OBI_BOOL_TestCase(OBIDMS_Column_TestCase): + def setUp(self): + self.data_type_code = 3 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code) + + +class OBIDMS_Column_OBI_BOOL_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase): + def setUp(self): + self.data_type_code = 3 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.elts_names, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code, + multiple_elements_per_line=True) + + +class OBIDMS_Column_OBI_CHAR_TestCase(OBIDMS_Column_TestCase): + def setUp(self): + self.data_type_code = 4 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code) + + +class OBIDMS_Column_OBI_CHAR_multiple_elements_TestCase(OBIDMS_Column_multiple_elements_TestCase): + def setUp(self): + self.data_type_code = 4 + self.dms, \ + self.dms_name, \ + self.dms_dir_name = create_test_obidms() + self.col, \ + self.col_name, \ + self.elts_names, \ + self.data_type_str = create_test_column(self.dms, + self.data_type_code, + multiple_elements_per_line=True) + + +if __name__ == '__main__': + 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"]) + +