def test_create_table_nominal(self): """ Creates a test database with nominal values. """ db_api.create_table( connection=db_api.create_connection(db_path=self.db_path), table=self.table, query=self.query) tables = db_api.get_table_names(connection=db_api.create_connection( db_path=self.db_path)) self.assertTrue(self.table in tables)
def test_create_table_no_date_query(self): """ Tries to create a test database without the required column name date. """ self.query = "ID integer PRIMARY KEY ASC NOT NULL" db_api.create_table( connection=db_api.create_connection(db_path=self.db_path), table=self.table, query=self.query) tables = db_api.get_table_names(connection=db_api.create_connection( db_path=self.db_path)) self.assertFalse(self.table in tables)
def test_create_table_already_exists(self): """ Tries to create a table that already exists. """ db_api.create_table( connection=db_api.create_connection(db_path=self.db_path), table=self.table, query=self.query) db_api.create_table( connection=db_api.create_connection(db_path=self.db_path), table=self.table, query=self.query) self.assertTrue(os.path.exists(self.db_path))
def setUp(self): """ Initializes unit test variables. """ self.logs_dir = tempfile.mkdtemp() self.connection = db_api.create_connection(db_path=os.path.join(self.logs_dir, 'test_database.db')) self.procedure = body_weight.BodyWeightProcedure(output_dir=self.logs_dir) self.input_values = [] def mock_input(_): """ Fake input function in order to test input calls in unit tests. """ return self.input_values.pop(0) body_weight.input = mock_input procedure.input = mock_input db_api.create_table(connection=self.connection, table=self.procedure.table, query=self.procedure.query) for _ in range(5): unique_id = db_api.add_new_row(connection=self.connection, table=self.procedure.table) db_api.update_item(connection=self.connection, table=self.procedure.table, value_tuple=(100, unique_id), column_names=['body_weight'])
def setUp(self): """ Initializes unit test variables. """ self.logs_dir = tempfile.mkdtemp() self.connection = db_api.create_connection(db_path=os.path.join(self.logs_dir, 'test_database.db')) self.procedure = nutrition.NutritionProcedure(output_dir=self.logs_dir) self.input_values = [] def mock_input(_): """ Fake input function in order to test input calls in unit tests. """ return self.input_values.pop(0) nutrition.input = mock_input db_api.create_table(connection=self.connection, table=self.procedure.table, query=self.procedure.query) for _ in range(1, 10): unique_id = db_api.add_new_row(connection=self.connection, table=self.procedure.table) db_api.update_item(connection=self.connection, table=self.procedure.table, value_tuple=(1, 2, 3, 4, 5, unique_id), column_names=[a[0] for a in Constants.nutrition_query_tuple])
def test_get_all_table_items_nominal(self): """ Gets all entries within the specified table. """ connection = db_api.create_connection(db_path=self.db_path) db_api.create_table(connection=connection, table=self.table, query=self.query) unique_id = db_api.add_new_row(connection=connection, table=self.table) unique_id_2 = db_api.add_new_row(connection=connection, table=self.table) db_api.update_item(connection=connection, table=self.table, value_tuple=('a', 5, unique_id), column_names=['text_item', 'number_item']) db_api.update_item(connection=connection, table=self.table, value_tuple=('b', 10, unique_id_2), column_names=['text_item', 'number_item']) result = db_api.get_all_table_entries(connection=connection, table=self.table) self.assertEqual(result[0][0], 1) self.assertEqual(result[1][0], 2) self.assertEqual(result[1][3], 10) self.assertEqual(result[1][2], 'b')
def test_create_table_bad_path(self): """ Tries to create a test database with invalid path. """ db_api.create_table(connection=self.logs_dir, table=self.table, query=self.query) tables = db_api.get_table_names(connection=db_api.create_connection( db_path=self.db_path)) self.assertFalse(self.table in tables)
def test_create_table_no_id_query(self): """ Tries to create a test database without the required column name ID. """ self.query = "date text" db_api.create_table(connection=self.db_path, table=self.table, query=self.query) tables = db_api.get_table_names(connection=db_api.create_connection( db_path=self.db_path)) self.assertFalse(self.table in tables)
def test_add_new_row_nominal(self): """ Creates a default row. """ connection = db_api.create_connection(db_path=self.db_path) db_api.create_table(connection=connection, table=self.table, query=self.query) unique_id = db_api.add_new_row(connection=connection, table=self.table) result = db_api.get_all_table_entries(connection=connection, table=self.table) self.assertEqual(unique_id, 1) self.assertEqual(result[0][0], 1)
def setUp(self): self.logs_dir = tempfile.mkdtemp() self.connection = db_api.create_connection(db_path=os.path.join(self.logs_dir, 'test_database.db')) self.table = 'test_table' self.query = ("ID integer PRIMARY KEY ASC NOT NULL," "date text," "item integer") db_api.create_table(connection=self.connection, table=self.table, query=self.query) for _ in range(1, 10): unique_id = db_api.add_new_row(connection=self.connection, table=self.table) db_api.update_item(connection=self.connection, table=self.table, value_tuple=(100, unique_id), column_names=['item'])
def test_update_item_nominal(self): """ Updates database file at the specified column and table. """ connection = db_api.create_connection(db_path=self.db_path) db_api.create_table(connection=connection, table=self.table, query=self.query) unique_id = db_api.add_new_row(connection=connection, table=self.table) db_api.update_item(connection=connection, table=self.table, value_tuple=('a', 5, unique_id), column_names=['text_item', 'number_item']) result = db_api.get_all_table_entries(connection=connection, table=self.table) self.assertEqual(result[0][0], 1) self.assertEqual(result[0][2], 'a') self.assertEqual(result[0][3], 5)
def test_table_to_csv(self): """ Outputs all of the specified table to a csv file. """ connection = db_api.create_connection(db_path=self.db_path) db_api.create_table(connection=connection, table=self.table, query=self.query) unique_id = db_api.add_new_row(connection=connection, table=self.table) db_api.update_item(connection=connection, table=self.table, value_tuple=('a', 5, unique_id), column_names=['text_item', 'number_item']) name = db_api.table_to_csv(connection=connection, table=self.table, output_dir=self.logs_dir) self.assertTrue(os.path.exists(name)) self.assertEqual(os.path.join(self.logs_dir, '%s.csv' % self.table), name)
def test_get_table_columns_nominal(self): """ Gets the entries as a dictionary at the specified columns. """ connection = db_api.create_connection(db_path=self.db_path) db_api.create_table(connection=connection, table=self.table, query=self.query) unique_id = db_api.add_new_row(connection=connection, table=self.table) db_api.update_item(connection=connection, table=self.table, value_tuple=('a', 5, unique_id), column_names=['text_item', 'number_item']) result = db_api.get_table_columns_dict( connection=connection, table=self.table, column_names=['text_item', 'number_item']) self.assertEqual(result['text_item'][0], 'a') self.assertEqual(result['number_item'][0], 5)
def setUp(self): """ Initializes unit test variables. """ self.logs_dir = tempfile.mkdtemp() self.connection = db_api.create_connection( db_path=os.path.join(self.logs_dir, 'test_database.db')) self.procedure = weight_lifting.WeightLiftingProcedure() self.input_values = [] def mock_input(_): """ Fake input function in order to test input calls in unit tests. """ return self.input_values.pop(0) weight_lifting.input = mock_input db_api.create_table(connection=self.connection, table=self.procedure.table, query=self.procedure.query)
def __init__(self, database_path=None, log_name='application_log.log', backup_path=os.getcwd(), config_path=os.getcwd()): """ Setup for application. :param str database_path: Optional database location if not default. :param str log_name: Optional name for log file. :param str backup_path: Path to output the backup database file. :param str config_path: Path to the config file. """ self.database_path = database_path if database_path is not None else Constants.database_path self.connection = db_api.create_connection(db_path=self.database_path) self.procedure = None self.procedure_prompt_text = None self.backup_path = backup_path self.config_path = config_path logging.basicConfig(filename=log_name, level=logging.INFO) self.logger = logging.getLogger(__name__)
def create_connection_bad_path(self): """ A return of None shall occur when a bad path is given. """ connection = db_api.create_connection(connection=self.logs_dir) self.assertTrue(connection is None)
def create_connection_nominal(self): """ A standard database connection shall be created when provided a valid path. """ connection = db_api.create_connection(connection=self.db_path) self.assertTrue(connection is not None)