Files
Pond-Level-Sensor/python_driver/test.py
2017-11-16 17:55:49 -06:00

98 lines
3.7 KiB
Python

"""Test the functions."""
import unittest
import calibration_db as db
import sqlite3
TEST_HEIGHT = 1.0
TEST_VOLUME = 1000.0
class TestDatabaseMethods(unittest.TestCase):
"""Class for testing database methods."""
def test_01_setuptable(self):
"""Test the setup_calibration_table function."""
try:
db.setup_calibration_table(drop_first=True)
cursor = db.SQLITE_CONN.cursor()
cursor.execute("SELECT * FROM cal_data")
cursor.fetchone()
self.assertTrue(True, msg="Table is accessible.")
except sqlite3.OperationalError:
self.assertTrue(False, msg="Table is not accessible.")
def test_02_getemptytable(self):
"""Test that an empty table is returned if no entries."""
self.assertListEqual(db.get_calibration_data(), [], msg="Calibration table is empty.")
def test_03_insertcalibrationdata(self):
"""Test the insert_calibration_data function."""
db.insert_calibration_data(TEST_HEIGHT, TEST_VOLUME)
cursor = db.SQLITE_CONN.cursor()
cursor.execute("SELECT * FROM cal_data")
result = cursor.fetchone()
self.assertEqual(result[1], TEST_HEIGHT, msg="Height fetched OK")
self.assertEqual(result[2], TEST_VOLUME, msg="Volume fetched OK")
def test_04_getcaldatasingle(self):
"""Test the get_calibration_data function for a single row."""
inserted_row = db.insert_calibration_data(TEST_HEIGHT, TEST_VOLUME)
fetched_row = db.get_calibration_data(single_entry=inserted_row)
self.assertEqual(fetched_row[1], TEST_HEIGHT, msg="Height fetched OK")
self.assertEqual(fetched_row[2], TEST_VOLUME, msg="Volume fetched OK")
def test_05_get_caldataall(self):
"""Test the get_calibration_data function for all rows."""
db.insert_calibration_data(TEST_HEIGHT, TEST_VOLUME)
fetched_data = db.get_calibration_data()
self.assertEqual(len(fetched_data), 3, msg="Should have 3 rows of data.")
def test_06_deletecaldata(self):
"""Test the delete_calibration_data function."""
cal_data = db.get_calibration_data()
start_len = len(cal_data)
db.delete_calibration_data(cal_data[-1][0])
end_len = len(db.get_calibration_data())
self.assertEqual(start_len - 1, end_len, msg="Length of data array is good after delete. {} = {}".format(start_len - 1, end_len))
def test_07_checkscaledata(self):
"""Test the scaling function."""
expected = [
[0.5, 0.5],
[1.5, 1.5],
[2.5, 3.0],
[3.5, 6.0],
[4.5, 12.0],
[5.5, 24.0],
[6.5, 48.0],
[7.5, 96.0],
[8.5, 192.0],
[9.5, 384.0],
[10.5, 640.0],
]
db.setup_calibration_table(drop_first=True)
db.insert_calibration_data(1.0, 1.0)
db.insert_calibration_data(2.0, 2.0)
db.insert_calibration_data(3.0, 4.0)
db.insert_calibration_data(4.0, 8.0)
db.insert_calibration_data(5.0, 16.0)
db.insert_calibration_data(6.0, 32.0)
db.insert_calibration_data(7.0, 64.0)
db.insert_calibration_data(8.0, 128.0)
db.insert_calibration_data(9.0, 256.0)
db.insert_calibration_data(10.0, 512.0)
for i in range(0, len(expected)):
self.assertEqual(db.get_volume_for_height(expected[i][0]), expected[i][1], msg="Volume equal. {} == {}".format(db.get_volume_for_height(expected[i][0]), expected[i][1]))
@classmethod
def tearDownClass(cls):
"""Tear down function."""
db.setup_calibration_table(drop_first=True)
if __name__ == '__main__':
unittest.main()