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 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 = 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_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 append_new_entry(self, connection, values, column_names): """ Gets the required input from the user and appends the new values into the database. :param connection: Connection to the database file. :param list values: Values to add to the database. :param list column_names: Column names in the database to reference. """ self.logger.info('New data gathered:\n\t names: %s\n\t values: %s' % (values, column_names)) unique_id = db_api.add_new_row(connection=connection, table=self.table) values.append(unique_id) db_api.update_item(connection=connection, table=self.table, value_tuple=tuple(values), column_names=column_names) # not sure why this is needed but a unit test was failing because the nutrition get_new_data result somehow had # the unique_id appended even though nothing is returned in this function. No other tests showing this. values.pop(-1)