Exemplo n.º 1
0
def test_no_sqlalchemy() -> None:

    with pytest.raises(ServiceUnavailable):
        connector.get_instance()

    log.warning("Skipping {} tests: service not available", CONNECTOR)
    return None
Exemplo n.º 2
0
def test_sqlalchemy(app: Flask) -> None:

    log.info("Executing {} tests", CONNECTOR)

    if not connector.SQLAlchemy.is_mysql():
        with pytest.raises(ServiceUnavailable):
            connector.get_instance(host="invalidhostname", port="123")

    with pytest.raises(ServiceUnavailable):
        connector.get_instance(user="******")

    obj = connector.get_instance()
    assert obj is not None

    with pytest.raises(AttributeError, match=r"Model InvalidModel not found"):
        obj.InvalidModel

    obj.disconnect()

    # a second disconnect should not raise any error
    obj.disconnect()

    # Create new connector with short expiration time
    obj = connector.get_instance(expiration=2, verification=1)
    obj_id = id(obj)
    obj_db_id = id(obj.db)

    # Connector is expected to be still valid
    obj = connector.get_instance(expiration=2, verification=1)
    assert id(obj) == obj_id

    time.sleep(1)

    # The connection should have been checked and should be still valid
    obj = connector.get_instance(expiration=2, verification=1)
    assert id(obj) == obj_id

    time.sleep(1)

    obj = connector.get_instance(expiration=2, verification=1)
    # With alchemy the connection object remains the same...
    assert id(obj) != obj_id
    assert id(obj.db) == obj_db_id

    assert obj.is_connected()
    obj.disconnect()
    assert not obj.is_connected()

    # ... close connection again ... nothing should happen
    obj.disconnect()
Exemplo n.º 3
0
        def do_queries(self, value: str) -> None:

            neo4j_enabled = Connector.check_availability("neo4j")
            sql_enabled = Connector.check_availability("sqlalchemy")
            mysql_enabled = sql_enabled and sqlalchemy.SQLAlchemy.is_mysql()
            postgres_enabled = sql_enabled and not sqlalchemy.SQLAlchemy.is_mysql(
            )

            # This is just a stub... to be completed
            if neo4j_enabled:
                graph = neo4j.get_instance()

                graph.cypher(
                    "MATCH (g: Group) WHERE g.shortname = $value return g.shortname",
                    value=value,
                )

                graph.Group.nodes.get_or_none(shortname=value)

            elif postgres_enabled:
                sql = sqlalchemy.get_instance()

                t = sqlalchemy.text(
                    'SELECT * FROM "group" WHERE shortname = :value')
                sql.db.engine.execute(t, value=value)

                sql.Group.query.filter_by(shortname=value).first()

            elif mysql_enabled:
                sql = sqlalchemy.get_instance()

                t = sqlalchemy.text(
                    "SELECT * FROM `group` WHERE shortname = :value")
                sql.db.engine.execute(t, value=value)

                sql.Group.query.filter_by(shortname=value).first()
Exemplo n.º 4
0
    def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:

        neo4j_enabled = Connector.check_availability("neo4j")
        sqlalchemy_enabled = Connector.check_availability("sqlalchemy")
        if neo4j_enabled:
            from neomodel import db as neo4j_db

        if sqlalchemy_enabled:
            # thanks to connectors cache this should always match the
            # same instance that will be used from inside the endpoint
            from restapi.connectors import sqlalchemy

            alchemy_db = sqlalchemy.get_instance()

        try:

            if neo4j_enabled:
                neo4j_db.begin()

            # Transaction is already open...
            # if sqlalchemy_enabled:
            #     pass

            out = func(self, *args, **kwargs)

            if neo4j_enabled:
                neo4j_db.commit()

            if sqlalchemy_enabled:
                alchemy_db.session.commit()

            return out
        except Exception as e:
            log.debug("Rolling backend database transaction")
            try:

                if neo4j_enabled:
                    neo4j_db.rollback()

                if sqlalchemy_enabled:
                    alchemy_db.session.rollback()

            except Exception as sub_ex:  # pragma: no cover
                log.warning("Exception raised during rollback: {}", sub_ex)
            raise e
Exemplo n.º 5
0
def test_sqlalchemy(app: Flask) -> None:

    if not Connector.check_availability(CONNECTOR):

        try:
            obj = connector.get_instance()
            pytest.fail("No exception raised")  # pragma: no cover
        except ServiceUnavailable:
            pass

        log.warning("Skipping {} tests: service not available", CONNECTOR)
        return None

    log.info("Executing {} tests", CONNECTOR)

    if not connector.SQLAlchemy.is_mysql():
        try:
            connector.get_instance(host="invalidhostname", port=123)

            pytest.fail("No exception raised on unavailable service"
                        )  # pragma: no cover
        except ServiceUnavailable:
            pass

    try:
        connector.get_instance(user="******")

        pytest.fail(
            "No exception raised on unavailable service")  # pragma: no cover
    except ServiceUnavailable:
        pass

    obj = connector.get_instance()
    assert obj is not None

    try:
        obj.InvalidModel
        pytest.fail("No exception raised on InvalidModel")  # pragma: no cover
    except AttributeError as e:
        assert str(e) == "Model InvalidModel not found"

    obj.disconnect()

    # a second disconnect should not raise any error
    obj.disconnect()

    # Create new connector with short expiration time
    obj = connector.get_instance(expiration=2, verification=1)
    obj_id = id(obj)
    obj_db_id = id(obj.db)

    # Connector is expected to be still valid
    obj = connector.get_instance(expiration=2, verification=1)
    assert id(obj) == obj_id

    time.sleep(1)

    # The connection should have been checked and should be still valid
    obj = connector.get_instance(expiration=2, verification=1)
    assert id(obj) == obj_id

    time.sleep(1)

    obj = connector.get_instance(expiration=2, verification=1)
    # With alchemy the connection object remains the same...
    assert id(obj) != obj_id
    assert id(obj.db) == obj_db_id

    assert obj.is_connected()
    obj.disconnect()
    assert not obj.is_connected()

    # ... close connection again ... nothing should happens
    obj.disconnect()