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 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_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_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_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 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 test_get_max_lift_updates_nominal(self): """ Updates the max lift values for the weight lifting procedure. """ self.input_values = ['9', '100', '200'] result, names = self.procedure.get_max_lift_updates() self.assertEqual(result, [100, 200]) table = 'max_lifts' db_api.create_table(connection=self.connection, table=table, query=Constants.max_lifts_query) unique_id = db_api.add_new_row(connection=self.connection, table=table) result.append(unique_id) db_api.update_item(connection=self.connection, table=table, value_tuple=tuple(result), column_names=names)
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 __run_procedure(self): """ Performs procedure operations. """ prompt = self.procedure_prompt_text if self.procedure_prompt_text is not None else Constants.user_prompt while True: input_text = input(prompt) if input_text == '1': self.logger.info('Adding a new entry with user entry') db_api.create_table(connection=self.connection, table=self.procedure.table, query=self.procedure.query) self.procedure.get_new_data(connection=self.connection) elif input_text == '2': self.logger.info('Adding new entries from file') db_api.create_table(connection=self.connection, table=self.procedure.table, query=self.procedure.query) self.procedure.get_new_data_from_file( connection=self.connection) elif input_text == '3': self.logger.info("Creating plots for user") self.procedure.view_data(connection=self.connection) pass elif input_text == '4': self.logger.info("Dumping tables to csv files") db_api.table_to_csv(connection=self.connection, table=self.procedure.table) elif input_text == '5': if isinstance(self.procedure, WeightLiftingProcedure): self.procedure.get_max_lift_updates() print("Please enter a valid option.") elif input_text == 'q': break else: print("Please enter a valid option.")