From 1a1cad60ed7a0c81ea255cada7f32a363104355a Mon Sep 17 00:00:00 2001 From: Bryan Strassner Date: Mon, 6 Aug 2018 13:57:24 -0500 Subject: [PATCH] Change logging of sql to single line Cleans up Shipyard logs a bit, where queries were nice and easy to read, but multi-line and needlessly on a separate line from the logging headers. Change-Id: I2fdff634dad097ef30207edae5205cb6c7226602 --- .../shipyard_airflow/db/common_db.py | 12 ++++- .../tests/unit/db/__init__.py | 0 .../tests/unit/db/test_common_db.py | 53 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/bin/shipyard_airflow/tests/unit/db/__init__.py create mode 100644 src/bin/shipyard_airflow/tests/unit/db/test_common_db.py diff --git a/src/bin/shipyard_airflow/shipyard_airflow/db/common_db.py b/src/bin/shipyard_airflow/shipyard_airflow/db/common_db.py index 2111eaf5..3e77f4e6 100644 --- a/src/bin/shipyard_airflow/shipyard_airflow/db/common_db.py +++ b/src/bin/shipyard_airflow/shipyard_airflow/db/common_db.py @@ -109,7 +109,7 @@ class DbAccess: executes the supplied query and returns the array of dictionaries of the row results """ - LOG.debug('Query: %s', query) + LOG.debug('Query: %s', _query_single_line(query)) result_dict_list = [] if query is not None: with self.get_engine().connect() as connection: @@ -142,7 +142,15 @@ class DbAccess: """ Performs an update/insert/delete """ - LOG.debug('Query: %s', query) + LOG.debug('Query: %s', _query_single_line(query)) if query is not None: with self.get_engine().connect() as connection: return connection.execute(query, **kwargs) + + +def _query_single_line(query): + """Reformats a query string to remove newlines and extra spaces + + :param query: The query string to log + """ + return " ".join(query.split()) diff --git a/src/bin/shipyard_airflow/tests/unit/db/__init__.py b/src/bin/shipyard_airflow/tests/unit/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/bin/shipyard_airflow/tests/unit/db/test_common_db.py b/src/bin/shipyard_airflow/tests/unit/db/test_common_db.py new file mode 100644 index 00000000..bae4f604 --- /dev/null +++ b/src/bin/shipyard_airflow/tests/unit/db/test_common_db.py @@ -0,0 +1,53 @@ +# Copyright 2018 AT&T Intellectual Property. All other rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from shipyard_airflow.db import common_db + + +M_QUERY = """ +SELECT + junk_id, + junk_name +FROM + junk_table +WHERE + junk_name = "Junk" +""" + +M_RES = 'SELECT junk_id, junk_name FROM junk_table WHERE junk_name = "Junk"' + +M2_QUERY = """ + + + + +SELECT + things + + +FROM + + tables + +""" + +M2_RES = 'SELECT things FROM tables' + +S_QUERY = "SELECT 1 FROM dual" + + +class TestCommonDb(): + def test_single_line_query(self): + assert M_RES == common_db._query_single_line(M_QUERY) + assert M2_RES == common_db._query_single_line(M2_QUERY) + assert S_QUERY == common_db._query_single_line(S_QUERY)