def test_when_select_and_columns_none_and_where_defined_then_sql_executed( db, cursor_mock ): db._cursors["transactions"] = cursor_mock where = "power_level > 9000" db.select("transactions", "transactions", columns=None, where=where) cursor_mock.assert_has_calls( [ call.execute("SELECT * FROM transactions WHERE power_level > 9000;"), call.execute().fetchall(), ] )
def test_when_select_and_columns_defined_and_where_none_then_sql_executed( db, cursor_mock ): db._cursors["transactions"] = cursor_mock columns = ["name", "yeet-ness"] db.select("transactions", "transactions", columns=columns, where=None) cursor_mock.assert_has_calls( [ call.execute("SELECT name, yeet-ness FROM transactions;"), call.execute().fetchall(), ] )
def test_execute_and_fetch(slot): norm_conn = slot._normal_conn mock_cur = MagicMock() norm_conn.cursor = Mock(return_value=mock_cur) slot._execute_and_fetch('SQL SQL STATEMENT', 1, 2, 3) call.execute('SQL SQL STATEMENT', (1, 2, 3)) in mock_cur.method_calls assert call.__enter__().fetchall() in mock_cur.mock_calls mock_cur.reset_mock() slot._execute_and_fetch('SQL SQL STATEMENT') call.execute('SQL SQL STATEMENT') in mock_cur.method_calls assert call.__enter__().fetchall() in mock_cur.mock_calls
def test_when_select_and_columns_defined_and_where_defined_then_sql_executed( db, cursor_mock ): db._cursors["transactions"] = cursor_mock columns = ["name", "yeet-ness"] where = "power_level > 9000" db.select("transactions", "transactions", columns=columns, where=where) cursor_mock.assert_has_calls( [ call.execute( "SELECT name, yeet-ness FROM transactions WHERE power_level > 9000;" ), call.execute().fetchall(), ] )
def test_when_insert_and_db_good_and_table_good_then_sql_called( db, cursor_mock, insert_data ): db._cursors["transactions"] = cursor_mock db.insert("transactions", "transactions", insert_data) cursor_mock.assert_has_calls( [ call.execute( "INSERT INTO transactions(institution, account, tran_id, tran_type, amount, narrative, " 'date_posted) VALUES("matts_fully_sick_bank", "multi-billion_dollar_savings", "42069", ' '"CREDIT", -135000.00, "sweet ass tesla", "20600707103000");' ) ] )
def test_docker_compose_up_down(self, mock_SimpleCommand, mock_isfile): mock_isfile.return_value = True dcomp_script_obj = DockerComposeScript( "tests/execute/resources/docker-compose.yml") dcomp_killable_script_obj = DockerComposeScriptKillable( dcomp_script_obj) mock_SimpleCommand.return_value.status = 0 result = dcomp_script_obj.run() commads = dcomp_script_obj.get_result() mock_SimpleCommand.return_value.execute.assert_called_once() expected_constr_call = [ call(['docker-compose', 'up', '-d'], 'tests/execute/resources') ] self.assertEqual(mock_SimpleCommand.call_args_list, expected_constr_call) self.assertTrue(result) self.assertEqual(len(commads), 1) #kill services result = dcomp_killable_script_obj.kill() commads = dcomp_killable_script_obj.get_result() self.assertTrue(result) self.assertEqual(len(commads), 1) expected_execute_calls = [call.execute(), call.execute()] self.assertEqual(mock_SimpleCommand.return_value.method_calls, expected_execute_calls) expected_constr_call = [ call(['docker-compose', 'up', '-d'], 'tests/execute/resources'), call(['docker-compose', 'down'], 'tests/execute/resources') ] self.assertEqual(mock_SimpleCommand.call_args_list, expected_constr_call)
def test_init_database(self): # Mock the sqlite3 and database_connection object. Make the sqlite3.connect return the mocked connection sqlite3=MagicMock() conn=MagicMock() conn.execute.return_value=True sqlite3.connect.return_value=conn #Make the bbbDAQ use the mocked sqlite3 sys.modules['sqlite3'] = sqlite3 import bbbDAQ reload (bbbDAQ) # Assert a connection is not created when we want to log the info to the console. created_conn=bbbDAQ.init_database('shell') self.assertEqual(created_conn, None) # Asserts when we want to save the adquired info to a database created_conn=bbbDAQ.init_database('test.db') self.assertEqual(created_conn, conn) sqlite3.connect.assert_called_with('test.db', check_same_thread=False) conn.assert_has_calls([call.execute(mock.ANY)]*3)
def test_when_select_and_columns_none_and_where_none_then_sql_executed(db, cursor_mock): db._cursors["transactions"] = cursor_mock db.select("transactions", "transactions", columns=None, where=None) cursor_mock.assert_has_calls( [call.execute("SELECT * FROM transactions;"), call.execute().fetchall()] )