Commit 8bd84c7c authored by Waleed Akbar's avatar Waleed Akbar
Browse files

Exception added

parent 58fd2c5a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -24,5 +24,5 @@ cd $PROJECTDIR/src
# python3 kpi_manager/tests/test_unitary.py

RCFILE=$PROJECTDIR/coverage/.coveragerc
python3 -m pytest --log-level=INFO --log-cli-level=INFO --verbose \
    kpi_manager/database/tests/KpiDBtests.py
 No newline at end of file
python3 -m pytest --log-level=DEBUG --log-cli-level=DEBUG --verbose \
    kpi_manager/tests/test_kpi_db.py
 No newline at end of file
+1 −3
Original line number Diff line number Diff line
@@ -14,10 +14,8 @@

import logging
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, Integer, String, Float, Text, ForeignKey
# from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float, Text
from sqlalchemy.orm import registry
from sqlalchemy.orm import sessionmaker, relationship
from common.proto.kpi_management_pb2 import KpiDescriptor

logging.basicConfig(level=logging.INFO)
+11 −10
Original line number Diff line number Diff line
@@ -12,14 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, time
from typing import List, Tuple
from sqlalchemy import select, and_
import logging
import sqlalchemy_utils
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from kpi_manager.database.KpiEngine import KpiEngine
from kpi_manager.database.KpiModel import Kpi as KpiModel
from common.method_wrappers.ServiceExceptions import ( 
    AlreadyExistsException, OperationFailedException)

LOGGER = logging.getLogger(__name__)
DB_NAME = "kpi"
@@ -31,8 +30,6 @@ class KpiDB:
            LOGGER.error('Unable to get SQLAlchemy DB Engine...')
            return False
        self.db_name = DB_NAME
        # self.drop_database(self.db_engine)          # added to test
        # self.create_database(self.db_engine)        # to add database 
        self.Session = sessionmaker(bind=self.db_engine)

    def create_database(self) -> None:
@@ -50,6 +47,7 @@ class KpiDB:
            LOGGER.debug("Tables created in the DB Name: {:}".format(self.db_name))
        except Exception as e:
            LOGGER.debug("Tables cannot be created in the kpi database. {:s}".format(str(e)))
            raise OperationFailedException ("Tables can't be created", extra_details=["unable to create table {:}".format(e)])

    def verify_tables(self):
        try:
@@ -70,10 +68,12 @@ class KpiDB:
        except Exception as e:
            session.rollback()
            if "psycopg2.errors.UniqueViolation" in str(e):
                LOGGER.debug(f"Unique key voilation: {row.__class__.__name__} table. {str(e)}")
                LOGGER.error(f"Unique key voilation: {row.__class__.__name__} table. {str(e)}")
                raise AlreadyExistsException(row.__class__.__name__, row,
                                             extra_details=["Unique key voilation: {:}".format(e)] )
            else:
                LOGGER.error(f"Failed to insert new row into {row.__class__.__name__} table. {str(e)}")
            return False
                raise OperationFailedException ("Deletion by column id", extra_details=["unable to delete row {:}".format(e)])
        finally:
            session.close()
    
@@ -90,7 +90,7 @@ class KpiDB:
        except Exception as e:
            session.rollback()
            LOGGER.debug(f"Failed to retrieve {model.__name__} ID. {str(e)}")
            raise
            raise OperationFailedException ("search by column id", extra_details=["unable to search row {:}".format(e)])
        finally:
            session.close()
    
@@ -108,6 +108,7 @@ class KpiDB:
        except Exception as e:
            session.rollback()
            LOGGER.error("Error deleting %s with %s %s: %s", model.__name__, col_name, id_to_search, e)
            raise OperationFailedException ("Deletion by column id", extra_details=["unable to delete row {:}".format(e)])
        finally:
            session.close()

@@ -148,6 +149,6 @@ class KpiDB:
            return result
        except Exception as e:
            LOGGER.error(f"Error fetching filtered rows from {model.__name__} table with filters {filter_object} ::: {e}")
            return []
            raise OperationFailedException ("Select by filter", extra_details=["unable to apply the filter {:}".format(e)])
        finally:
            session.close()
 No newline at end of file