def test_asset_types_register(postgresql: connection):
    client = _init_test_client(postgresql)

    # normal asset type registration
    asset_type_id = 'file'
    asset_type_description = 'Data assets provided as downloadable file'

    response = client.post('/asset_types/register',
                           json={
                               'id': asset_type_id,
                               'description': asset_type_description
                           })

    assert response.status_code == 201

    cur: cursor = postgresql.cursor()
    cur.execute(f'SELECT * FROM topio_asset_type WHERE id=%s;',
                (asset_type_id, ))
    results = cur.fetchall()
    cur.close()
    assert len(results) == 1
    assert results[0][1] == asset_type_description

    # existing asset type registration
    response = client.post('/asset_types/register',
                           json={
                               'id': asset_type_id,
                               'description': asset_type_description
                           })

    assert response.status_code == 200

    # registration of asset type with broken asset type ID (contains spaces)
    asset_type_id = 'this is broken'
    asset_type_description = \
        'This is a broken asset type with spaces in its identifier string'

    response = client.post('/asset_types/register',
                           json={
                               'id': asset_type_id,
                               'description': asset_type_description
                           })

    assert response.status_code == 422

    cur: cursor = postgresql.cursor()
    cur.execute(f'SELECT * FROM topio_asset_type WHERE id=%s;',
                (asset_type_id, ))
    results = cur.fetchall()
    cur.close()
    assert len(results) == 0
예제 #2
0
def test_postgres_load_two_files(postgresql_load_2: connection) -> None:
    """Check postgresql fixture can load two files."""
    cur = postgresql_load_2.cursor()
    cur.execute(SELECT_Q)
    results = cur.fetchall()
    assert len(results) == 2
    cur.close()
예제 #3
0
def test_postgres_load_one_file(postgresql_load_1: connection) -> None:
    """Check postgresql fixture can load one file."""
    cur = postgresql_load_1.cursor()
    cur.execute(SELECT_Q)
    results = cur.fetchall()
    assert len(results) == 1
    cur.close()
def test_template_database(postgres_with_template: connection, _: int) -> None:
    """Check that the database structure gets recreated out of a template."""
    with postgres_with_template.cursor() as cur:
        cur.execute("SELECT * FROM stories")
        res = cur.fetchall()
        assert len(res) == 4
        cur.execute("TRUNCATE stories")
        cur.execute("SELECT * FROM stories")
        res = cur.fetchall()
        assert len(res) == 0
def test_postgres_docker_load(postgres_with_schema: connection) -> None:
    """
    Check main postgres fixture
    """
    with postgres_with_schema.cursor() as cur:
        # Query for public.tokens since the eidastats changes postgres' search_path to ''.
        # The search path by default is public, but without it,
        # every schema has to be written explicitly.
        cur.execute("select * from public.tokens")
        print(cur.fetchall())
예제 #6
0
def test_two_postgreses(postgresql: connection,
                        postgresql2: connection) -> None:
    """Check two postgresql fixtures on one test."""
    cur = postgresql.cursor()
    cur.execute(MAKE_Q)
    postgresql.commit()
    cur.close()

    cur = postgresql2.cursor()
    cur.execute(MAKE_Q)
    postgresql2.commit()
    cur.close()
def _init_test_client(postgresql: connection) -> TestClient:
    mock_engine = create_engine(name_or_url='postgresql://',
                                connect_args=postgresql.get_dsn_parameters())

    async def get_mock_db():
        MockSessionLocal = \
            sessionmaker(autocommit=False, autoflush=False, bind=mock_engine)
        db = MockSessionLocal()

        try:
            yield db
        finally:
            db.close()

    app.dependency_overrides[ompid.get_db] = get_mock_db
    Base.metadata.create_all(mock_engine)

    return TestClient(app)
예제 #8
0
def test_postgres_terminate_connection(postgresql2: connection,
                                       _: int) -> None:
    """
    Test that connections are terminated between tests.

    And check that only one exists at a time.
    """

    with postgresql2.cursor() as cur:

        def check_if_one_connection() -> None:
            cur.execute("SELECT * FROM pg_stat_activity "
                        "WHERE backend_type = 'client backend';")
            existing_connections = cur.fetchall()
            assert (
                len(existing_connections) == 1
            ), f"there is always only one connection, {existing_connections}"

        retry(check_if_one_connection,
              timeout=120,
              possible_exception=AssertionError)
예제 #9
0
def test_1(postgres: connection) -> None:
    with postgres.cursor() as cur:
        cur.execute("SELECT * FROM general_journal ORDER BY id;")
        res = cur.fetchall()
예제 #10
0
def test_main_postgres(postgresql: connection) -> None:
    """Check main postgresql fixture."""
    cur = postgresql.cursor()
    cur.execute(MAKE_Q)
    postgresql.commit()
    cur.close()
예제 #11
0
def test_1(postgres: connection) -> None:
    with postgres.cursor() as cur:
        cur.execute("SELECT * FROM general_ledger ORDER BY id;")
        res = cur.fetchall()
        assert len(res) == 3
예제 #12
0
def test_custom_isolation_level(postgres_isolation_level: connection) -> None:
    """Check that a client fixture with a custom isolation level works."""
    cur = postgres_isolation_level.cursor()
    cur.execute("SELECT 1")
    assert cur.fetchone() == (1, )
예제 #13
0
def test_postgres_options(postgres_max_conns: connection) -> None:
    """Check that max connections (-N 42) is honored."""
    cur = postgres_max_conns.cursor()
    cur.execute("SHOW max_connections")
    assert cur.fetchone() == ("42", )
def test_users_register(postgresql: connection):
    client = _init_test_client(postgresql)

    # normal user -----------------------------------------
    user_name = 'User ABC'
    user_namespace = 'abc'
    response = client.post('/users/register',
                           json={
                               'name': user_name,
                               'user_namespace': user_namespace
                           })

    # no errors were raised
    assert response.status_code == 201

    # user data can be found in the database
    user_id: int = json.loads(response.content)['id']
    cur: cursor = postgresql.cursor()
    cur.execute(
        f'SELECT * FROM topio_user '
        f'WHERE id=%s AND name=%s AND user_namespace=%s;',
        (user_id, user_name, user_namespace))
    results = cur.fetchall()
    cur.close()
    assert len(results) == 1

    # existing user ---------------------------------------
    response = client.post('/users/register',
                           json={
                               'name': user_name,
                               'user_namespace': user_namespace
                           })

    # no errors were raised
    assert response.status_code == 200

    # user with broken namespace (contains whitespace) ----
    user_name = 'User DEF'
    user_namespace = 'this is broken'
    response = client.post('/users/register',
                           json={
                               'name': user_name,
                               'user_namespace': user_namespace
                           })

    # should cause a client error 422 (Unprocessable Entity)...
    assert response.status_code == 422

    # ...and it should be a value error
    error_type = json.loads(response.content)['detail'][0]['type']
    assert error_type == 'value_error'

    # ...and no DB write should have happened
    cur: cursor = postgresql.cursor()
    cur.execute(
        f'SELECT * FROM topio_user '
        f'WHERE id=%s AND name=%s AND user_namespace=%s;',
        (user_id, user_name, user_namespace))
    results = cur.fetchall()
    cur.close()
    assert len(results) == 0
def test_assets_register(postgresql: connection):
    client = _init_test_client(postgresql)

    # register asset owner
    owner_name = 'User ABC'
    owner_namespace = 'abc'
    response = client.post('/users/register',
                           json={
                               'name': owner_name,
                               'user_namespace': owner_namespace
                           })
    owner_id = json.loads(response.content)['id']

    # register asset type
    asset_type_id = 'file'
    asset_type_description = 'Data assets provided as downloadable file'

    client.post('/asset_types/register',
                json={
                    'id': asset_type_id,
                    'description': asset_type_description
                })

    asset_1_local_id = 'hdfs://foo.bar.ttl'
    asset_1_description = 'A Turtle HDFS file'

    response = client.post('/assets/register',
                           json={
                               'local_id': asset_1_local_id,
                               'owner_id': owner_id,
                               'asset_type': asset_type_id,
                               'description': asset_1_description
                           })

    assert response.status_code == 200

    # {
    #   "local_id":"hdfs://foo.bar.ttl",
    #   "owner_id":1,
    #   "asset_type":"file",
    #   "description":"A Turtle HDFS file",
    #   "topio_id":"topio.abc.1.file"
    # }
    asset_1_topio_id = json.loads(response.content)['topio_id']
    asset_1_id = json.loads(response.content)['id']

    assert asset_1_topio_id == \
        TOPIO_ID_SCHEMA.format(**{
            'owner_namespace': owner_namespace,
            'asset_id': asset_1_id,
            'asset_type': asset_type_id})

    cur: cursor = postgresql.cursor()
    cur.execute(f'SELECT * FROM topio_asset WHERE id=%s;', (asset_1_id, ))
    results = cur.fetchall()
    cur.close()

    assert len(results) == 1

    # [(1, 'hdfs://foo.bar.ttl', 1, 'file', 'A Turtle HDFS file')]
    assert results[0][0] == asset_1_id
    assert results[0][1] == asset_1_local_id
    assert results[0][2] == owner_id
    assert results[0][3] == asset_type_id
    assert results[0][4] == asset_1_description

    # asset without local ID and description
    response = client.post('/assets/register',
                           json={
                               'owner_id': owner_id,
                               'asset_type': asset_type_id
                           })

    assert response.status_code == 200

    # {
    #   "local_id":null,
    #   "owner_id":1,
    #   "asset_type":"file",
    #   "description":null,
    #   "id":2,
    #   "topio_id":"topio.abc.2.file"
    # }
    asset_2_topio_id = json.loads(response.content)['topio_id']
    asset_2_id = json.loads(response.content)['id']

    assert asset_2_topio_id == \
        TOPIO_ID_SCHEMA.format(**{
            'owner_namespace': owner_namespace,
            'asset_id': asset_2_id,
            'asset_type': asset_type_id})

    cur: cursor = postgresql.cursor()
    cur.execute(f'SELECT * FROM topio_asset WHERE id=%s;', (asset_2_id, ))
    results = cur.fetchall()
    cur.close()

    assert len(results) == 1

    # [(2, None, 1, 'file', None)]
    assert results[0][0] == asset_2_id
    assert results[0][1] is None
    assert results[0][2] == owner_id
    assert results[0][3] == asset_type_id
    assert results[0][4] is None