From 4aba6f6503549d5173fbe18d627794df6398c45f Mon Sep 17 00:00:00 2001 From: "lodenthal@uni-bielefeld.de" <lodenthal@uni-bielefeld.de> Date: Fri, 6 Apr 2018 10:44:30 +0300 Subject: [PATCH] commented test in db --- navipy/database/test.py | 139 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 126 insertions(+), 13 deletions(-) diff --git a/navipy/database/test.py b/navipy/database/test.py index 44dca8b..c073d3a 100644 --- a/navipy/database/test.py +++ b/navipy/database/test.py @@ -16,6 +16,14 @@ class TestCase(unittest.TestCase): self.mydb = database.DataBaseLoad(self.mydb_filename) def test_DataBase_init_(self): + """ + this test checks the initialization of a DataBase works + correctly. + it checks if correct errors are raised for: + - a database file with out .db ending is used for initialization + - wrong types are passed for the database name + i.e. integers, floats, none, nan + """ # filename must end with .db with self.assertRaises(NameError): DataBase('test') @@ -25,11 +33,18 @@ class TestCase(unittest.TestCase): DataBase(n) # only works if testdb was created before e.g. with DataBaseSave - with self.assertRaises(NameError): - DataBase('test') + # with self.assertRaises(NameError): + # DataBase('test') def test_DataBase_init_channel(self): - # filename must end with .db + """ + this test checks the initialization of a DataBase works + correctly. + it checks if correct errors are raised for: + - channels names, which are no strings or chars + - channel name is None value or nan + """ + # channels must be strings or char for n in [3, 8.7, None, np.nan]: with self.assertRaises(TypeError): DataBase(self.mydb_filename, n) @@ -37,18 +52,19 @@ class TestCase(unittest.TestCase): # db=DataBase(self.mydb_filename,[3,4]) with self.assertRaises(ValueError): DataBase(self.mydb_filename, [None, 2]) - # with self.assertRaises(ValueError): - # db=DataBase(self.mydb_filename,[4.5,'f']) - # filename must be string - for n in [2, 5.0, None, np.nan]: - with self.assertRaises(TypeError): - DataBase(n) - # not working, because DataBase does not create a new database with self.assertRaises(NameError): DataBase('test', ['R', 'B']) def test_table_exist(self): + """ + this test checks if the function table_exists works correctly + it checks if correct errors are raised for: + - a database name that are not of type string or char + i.e. integer, float, none, nan + - Attention: in this test the check for a table that existed + did not work correctly (False was returned) + """ # self.assertFalse(self.mydb.table_exist('testblubb')) for n in [2, 7.0, None, np.nan]: with self.assertRaises(TypeError): @@ -56,6 +72,16 @@ class TestCase(unittest.TestCase): # self.assertFalse(self.mydb.table_exist(self.mydb_filename)) def test_check_data_validity(self): + """ + this test checks the function data validity works + correctly. It should return true if the database + contains data in the row, that is checked for + it checks if correct errors are raised for: + - row numbers that are not integers. + i.e. float non, nan (must be integer) + - row is out of range; smaller or equal to 0 + - checks if true is returned for an exiting entry (row=1) + """ for n in [7.0, None, np.nan]: with self.assertRaises(TypeError): self.mydb.check_data_validity(n) @@ -65,6 +91,14 @@ class TestCase(unittest.TestCase): assert self.mydb.check_data_validity(1) def get_posid_test(self): + """ + this test checks the function get_posid works + correctly. + it checks if correct errors are raised for: + - posorient is missing an entry (no 'x' column) + - posorient contains nan or none values + - posorient is of wrong type (dict instead of pd.series) + """ conn = sqlite3.connect(self.mydb_filename) c = conn.cursor() c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) @@ -135,6 +169,14 @@ class TestCase(unittest.TestCase): self.mydb.get_posid(posorient2) def test_DataBaseLoad_init_(self): + """ + this test checks the function DataBaseLoad works + correctly. + it checks if correct errors are raised for: + - filename does not end with .db + - filename is not a string or char + i.e. integer, float, none, nan + """ # filename must end with .db with self.assertRaises(NameError): DataBaseLoad('test') @@ -149,6 +191,14 @@ class TestCase(unittest.TestCase): DataBaseLoad('test') def test_read_posorient(self): + """ + this test checks the function read_posorient works + correctly. + it checks if correct errors are raised for: + - posorient is missing an entry (no 'x' column) + - posorient contains nan or none values + - posorient is of wrong type (dict instead of pd.series) + """ conn = sqlite3.connect(self.mydb_filename) c = conn.cursor() c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) @@ -219,6 +269,15 @@ class TestCase(unittest.TestCase): self.mydb.read_posorient(posorient=posorient2) def test_read_posid_id(self): + """ + this test checks the function read_posorient works + correctly. + it checks if correct errors are raised for: + - rowid is out of range (<=0) + - rowid is of type char, none, nan, float + and checks if the returned entry for rowid 1 is correct + - that it all columns and correct values + """ conn = sqlite3.connect(self.mydb_filename) c = conn.cursor() c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) @@ -257,6 +316,18 @@ class TestCase(unittest.TestCase): assert posoriend2.alpha_2 == posorient.alpha_2 def test_scene_id(self): + """ + this test checks the function scene works + correctly. + it checks if correct errors are raised for: + - rowid is out of range (<=0) + - rowid is of type char, none, nan, float + + and checks if the returned entry for different + rows is correct + - has correct shape + - does not contain nans + """ for rowid in [0, -2]: with self.assertRaises(ValueError): # print("rowid",rowid) @@ -283,6 +354,14 @@ class TestCase(unittest.TestCase): self.assertTrue(image.shape[1] > 0) def test_scene_posorient(self): + """ + this test checks the function scene works + correctly. + it checks if correct errors are raised for: + - posorient is missing an entry (no 'x' column) + - posorient contains nan or none values + - posorient is of wrong type (dict instead of pd.series) + """ conn = sqlite3.connect(self.mydb_filename) c = conn.cursor() c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) @@ -357,6 +436,19 @@ class TestCase(unittest.TestCase): image = self.mydb.scene(posorient=posorient2) def test_denormalise_image(self): + """ + this test checks the function denormalise_image works + correctly. + it checks if correct errors are raised for: + - image has wrong type (list instead of np.ndarray) + - image does not have enough dimensions + - image contains nan values + - image has to many dimensions + - cminmax range is missing one channel + - cminmax range is empty pd.series + - cminmax range is dictionary + - cminmax contains nans + """ image = self.mydb.scene(rowid=1) image = np.squeeze(image) cminmaxrange = pd.Series(index=['R_min', 'R_max', 'R_range', @@ -458,6 +550,15 @@ class TestCase(unittest.TestCase): self.mydb.denormalise_image(imagecorrect, cminmaxrange) def test_DataBaseSave(self): + """ + this test checks the function DataBaseSaver works + correctly. + it checks if correct errors are raised for: + - the filename is of type integer + + checks for correct result if a new DataBaseSaver + is created (no error is thrown) + """ # should work and creat new database with tempfile.TemporaryDirectory() as folder: testdb_filename = folder + '/testdatabase.db' @@ -467,6 +568,14 @@ class TestCase(unittest.TestCase): database.DataBaseSave(filename=3) def test_normalise_image(self): + """ + this test checks the function normalise_image works + correctly. + it checks if correct errors are raised for: + - image is of wrong type (list) + - image has wrong dimensionality (too big, too small) + - image contains nan values + """ image = self.mydb.scene(rowid=1) image = np.squeeze(image) with tempfile.TemporaryDirectory() as folder: @@ -498,6 +607,13 @@ class TestCase(unittest.TestCase): loadDB.normalise_image(image2) def test_insert_replace(self): + """ + this test checks the function insert_replace works + correctly. + it checks if correct errors are raised for: + - filename is of type integer, float, nan or none + - filename does not exist in database/params are wrong + """ params = {} params['hight'] = 1.7 params['age'] = 20 @@ -509,9 +625,6 @@ class TestCase(unittest.TestCase): with self.assertRaises(TypeError): tmpmydb.insert_replace(name, params) - for name in [3, 7.5, np.nan, None]: - with self.assertRaises(TypeError): - tmpmydb.insert_replace(name, params) with self.assertRaises(sqlite3.OperationalError): tmpmydb.insert_replace('test', params) -- GitLab