def test_creation_when_no_database_exists(self): """ Create a database where none exists. :return: """ database_filename = "test.db" # Delete the test database if it exists. test_database = os.path.join(os.getcwd(), database_filename) if os.path.exists(test_database): os.remove(test_database) # Create the database object, build the database database = app.database.Database(database_filename) database.create_database() # Pull out the table names from the database we've created. column_names = extract_column_names(database_filename) # Assert that they are as expected: for column_name in app.database.database_columns: self.assertEqual( True, column_name in column_names, "Database creation process did not yield the column names expected. Missing: {0}".format(column_name) )
def test_interpreter_and_connector(self): """ Check that the interpreter can respond to messages from the connector. :return: """ if connection_name is None: self.skipTest("No connection available. Skipping test.") database_filename = "test.db" # Build our components and feed them through. database = app.database.Database(database_filename, overwrite=True) database.create_database() interpreter = app.interpreter.Interpreter(database=database) connector = app.connector.SerialConnector(connection_name) connector.connect() interpreter_input = [] interpreter_output = [] i = 0 for i in range(5): interpreter_input += [connector.retrieve_data()] print("Input: {0}".format(interpreter_input[-1])) try: interpreter_output += [interpreter.interpret(interpreter_input[-1])] print("Output: {0}".format(interpreter_output[-1])) except ValueError as e: if "ERROR 001" in e.args[0]: pass else: raise ValueError
def test_results_can_be_written_to_database(self): """ Use the write_to_database function to write a set of values to a database. """ database_filename = "test.db" # Delete the test database if it exists. test_database = os.path.join(os.getcwd(), database_filename) if os.path.exists(test_database): os.remove(test_database) # Create the database and write values to it database = app.database.Database(database_filename) database.create_database() database.write_to_database(app.test.DATA_MESSAGE_PARSED_DICT) # Retrieve the values and validate them with sqlite3.connect(database_filename) as conn: cur = conn.cursor() cur = cur.execute("SELECT * FROM data;") output_data = cur.fetchone() self.assertEqual( True, output_data == app.test.DATA_MESSAGE_OUT_OF_DATABASE, "Retrieve data from database did not match expected output." )
def test_creation_when_valid_database_exists_and_overwrite(self): """ Connect to, and validate a database. Two outcomes here: first of all, ensure that the database is valid. Second of all, ensure that the original database is unbesmirched, and the original database is destroyed. """ database_filename = "test.db" # Delete the test database if it exists. test_database = os.path.join(os.getcwd(), database_filename) if os.path.exists(test_database): os.remove(test_database) # Create our pre-existing, _valid_, database. database_creation_statement = """ CREATE TABLE data( row_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ID VARCHAR, Time DATETIME, Value REAL, Debug INTEGER ); """ database_insertion_values = ("Temperature", datetime.datetime.now(), 20.0, 1) with sqlite3.connect(database_filename) as conn: cur = conn.cursor() cur.execute(database_creation_statement) cur.execute("INSERT INTO data (ID, Time, Value, Debug) VALUES (?, ?, ?, ?);", database_insertion_values) conn.commit() original_data = cur.execute("SELECT * FROM data").fetchall() # Create the database object, build the database database = app.database.Database(database_filename, overwrite=True) database.create_database() # Pull out the table names from the database we've created. column_names = extract_column_names(database_filename) # Assert that they are as expected: for column_name in app.database.database_columns: self.assertEqual( True, column_name in column_names, "Database creation process did not yield the column names expected. Missing: {0}".format(column_name) ) # Assert that the existing data has been unmolested. with sqlite3.connect(database_filename) as conn: cur = conn.cursor() data_after_investigation = cur.execute("SELECT * FROM data").fetchall() self.assertEqual( True, original_data != data_after_investigation, "Data retrieved after investigating database did match original data in Data table." )
def test_interpreter_and_connector_full_response(self): """ Check that the interpreter can respond to messages from the connector, and write them back. :return: """ if connection_name is None: self.skipTest("No connection available. Skipping test.") database_filename = "test.db" # Build our components and feed them through. database = app.database.Database(database_filename, overwrite=True) database.create_database() interpreter = app.interpreter.Interpreter(database=database) connector = app.connector.SerialConnector(connection_name) connector.connect() interpreter_input = [] interpreter_output = [] for i in range(25): interpreter_input += [connector.retrieve_data()] print("Input: {0}".format(interpreter_input[-1])) try: interpreter_output += [interpreter.interpret(interpreter_input[-1])] print("Output: {0}".format(interpreter_output[-1])) if interpreter_output[-1] is not None: connector.write_data(interpreter_output[-1]) except ValueError as e: if "ERROR 001" in e.args[0]: pass else: raise ValueError with sqlite3.connect(database_filename) as conn: cur = conn.cursor() cur = cur.execute("SELECT * FROM data;") output_data = cur.fetchone() print(output_data)
def test_creation_when_invalid_database_exists_and_overwrite(self): """ Connect to, and validate a database. The only outcome here is that an error is raised. """ database_filename = "test.db" # Delete the test database if it exists. test_database = os.path.join(os.getcwd(), database_filename) if os.path.exists(test_database): os.remove(test_database) # Create our pre-existing, _invalid_, database. database_creation_statement = """ CREATE TABLE data( row_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ID VARCHAR, Time DATETIME ); """ with sqlite3.connect(database_filename) as conn: cur = conn.cursor() cur.execute(database_creation_statement) # Generate the database. database = app.database.Database(database_filename, overwrite=True) database.create_database() # Pull out the table names from the database we've created. column_names = extract_column_names(database_filename) # Assert that they are as expected: for column_name in app.database.database_columns: self.assertEqual( True, column_name in column_names, "Database creation process did not yield the column names expected. Missing: {0}".format(column_name) )
def test_interpreter_and_database_end_to_end(self): """ Build the database and interpreter, pass in a value, and check whether what we get out the end is right. :return: """ database_filename = "test.db" # Build our components and feed them through. database = app.database.Database(database_filename, overwrite=True) database.create_database() interpreter = app.interpreter.Interpreter(database=database) interpreter.interpret(app.test.DATA_MESSAGE) # Retrieve the values and validate them with sqlite3.connect(database_filename) as conn: cur = conn.cursor() cur = cur.execute("SELECT * FROM data;") output_data = cur.fetchone() self.assertEqual( True, output_data == app.test.DATA_MESSAGE_OUT_OF_DATABASE, "Retrieve data from database did not match expected output." )
def create_database(): database.db.init_app(app) with app.app_context(): database.create_database(app.config["SQLALCHEMY_DATABASE_URI"]) database.db.create_all()