Ejemplo n.º 1
0
def test_sanity_pass_relationship():
    """
    See database sanity check understands about relationships and don't
    deem them as missing column.
    """
    engine = mc.connect_to_mc_testing_db().engine
    conn = engine.connect()
    trans = conn.begin()

    Session = sessionmaker(bind=engine)
    session = Session()

    Base, RelationTestModel, RelationTestModel2 = gen_relation_models()
    try:
        Base.metadata.drop_all(engine, tables=[RelationTestModel.__table__,
                                               RelationTestModel2.__table__])
    except sqlalchemy.exc.NoSuchTableError:
        pass

    Base.metadata.create_all(engine, tables=[RelationTestModel.__table__,
                                             RelationTestModel2.__table__])

    try:
        assert is_sane_database(Base, session) is True
    finally:
        Base.metadata.drop_all(engine)
Ejemplo n.º 2
0
def test_sanity_pass_declarative(db_name=test_db):
    """
    See database sanity check understands about relationships and don't deem
    them as missing column.
    """

    engine = create_engine(db_name)
    conn = engine.connect()
    trans = conn.begin()

    Session = sessionmaker(bind=engine)
    session = Session()

    Base, DeclarativeTestModel = gen_declarative()
    try:
        Base.metadata.drop_all(engine, tables=[DeclarativeTestModel.__table__])
    except sqlalchemy.exc.NoSuchTableError:
        pass

    Base.metadata.create_all(engine, tables=[DeclarativeTestModel.__table__])

    try:
        assert is_sane_database(Base, session) is True
    finally:
        Base.metadata.drop_all(engine)
Ejemplo n.º 3
0
    def __init__(self, db_name=default_db):
        self.Base = automap_base()
        super(DB_automap, self).__init__(db_name=db_name)
        self.Base.prepare(self.engine, reflect=True)
        self.DBSession.configure(bind=self.engine)

        # initialization should fail if the automapped database does not
        # match the delarative base
        session = self.DBSession()
        assert is_sane_database(DEC_BASE, session)
Ejemplo n.º 4
0
def test_sanity_table_missing():
    """See check fails when there is a missing table"""
    engine = mc.connect_to_mc_testing_db().engine
    conn = engine.connect()
    trans = conn.begin()

    Base, SaneTestModel = gen_test_model()
    Session = sessionmaker(bind=engine)
    session = Session()

    try:
        Base.metadata.drop_all(engine, tables=[SaneTestModel.__table__])
    except sqlalchemy.exc.NoSuchTableError:
        pass

    assert is_sane_database(Base, session) is False
Ejemplo n.º 5
0
def test_sanity_column_missing():
    """See check fails when there is a missing table"""
    engine = mc.connect_to_mc_testing_db().engine
    conn = engine.connect()
    trans = conn.begin()

    Session = sessionmaker(bind=engine)
    session = Session()
    Base, SaneTestModel = gen_test_model()
    try:
        Base.metadata.drop_all(engine, tables=[SaneTestModel.__table__])
    except sqlalchemy.exc.NoSuchTableError:
        pass
    Base.metadata.create_all(engine, tables=[SaneTestModel.__table__])

    # Delete one of the columns
    engine.execute("ALTER TABLE sanity_check_test DROP COLUMN id")

    assert is_sane_database(Base, session) is False
Ejemplo n.º 6
0
def test_sanity_pass():
    """
    See database sanity check completes when tables and columns are created.
    """
    engine = mc.connect_to_mc_testing_db().engine
    conn = engine.connect()
    trans = conn.begin()

    Base, SaneTestModel = gen_test_model()
    Session = sessionmaker(bind=engine)
    session = Session()
    try:
        Base.metadata.drop_all(engine, tables=[SaneTestModel.__table__])
    except sqlalchemy.exc.NoSuchTableError:
        pass

    Base.metadata.create_all(engine, tables=[SaneTestModel.__table__])

    try:
        assert is_sane_database(Base, session) is True
    finally:
        Base.metadata.drop_all(engine)
Ejemplo n.º 7
0
#! /usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
# Copyright 2016 the HERA Collaboration
# Licensed under the 2-clause BSD license.

from __future__ import absolute_import, division, print_function

from hera_mc import MCDeclarativeBase, mc
from hera_mc.db_check import is_sane_database

parser = mc.get_mc_argument_parser()
args = parser.parse_args()

try:
    db = mc.connect_to_mc_db(args)
except RuntimeError as e:
    raise SystemExit(str(e))

# If the specified database is in "testing" mode, we won't have actually
# checked anything yet. It doesn't hurt to double-check if the DB is
# in production mode, so let's just check again.

with db.sessionmaker() as session:
    if not is_sane_database(MCDeclarativeBase, session):
        raise SystemExit('database {0} does not match expected schema'.format(db.engine.url))
Ejemplo n.º 8
0
 def test_db_sane(self):
     from hera_mc.db_check import is_sane_database
     assert is_sane_database(self.test_db.Base, self.real_session)