Skip to content
Snippets Groups Projects
test_general_data_routes.py 1.86 KiB
import contextlib
import json
import jsonschema

DEFAULT_REQUEST_HEADERS = {
    "Content-type": "application/json",
    "Accept": ["application/json"]
}


def test_get_routers(client):
    version_schema = {
        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "array",
        "items": {"type": "string"}
    }

    rv = client.post(
        "data/routers",
        headers=DEFAULT_REQUEST_HEADERS)
    assert rv.status_code == 200
    response = json.loads(rv.data.decode("utf-8"))
    jsonschema.validate(response, version_schema)
    assert response


def test_pop_info(client, mocker):
    """
    just check the correct method is called, but mock out all sql access
    """

    expected_pop_info = {
        'C': 'bogus connection',
        'E': 'bogus equipment name'
    }

    @contextlib.contextmanager
    def mocked_connection(ignored):
        yield expected_pop_info['C']

    mocker.patch(
        'inventory_provider.db.db.connection', mocked_connection)
    mocker.patch(
        'inventory_provider.db.opsdb.lookup_pop_info',
        lambda c, e: {'C': c, 'E': e})

    rv = client.get(
        '/data/pop/{E}'.format(**expected_pop_info),
        headers=DEFAULT_REQUEST_HEADERS)

    assert rv.status_code == 200
    assert rv.is_json
    response = json.loads(rv.data.decode('utf-8'))

    assert response == expected_pop_info


def test_pop_not_found(client, mocker):
    """
    just check the correct method is called, but mock out all sql access
    """

    @contextlib.contextmanager
    def mocked_connection(ignored):
        yield 'xyz'

    mocker.patch(
        'inventory_provider.db.db.connection', mocked_connection)
    mocker.patch(
        'inventory_provider.db.opsdb.lookup_pop_info',
        lambda a, b: None)

    rv = client.get(
        '/data/pop/aabbcc',
        headers=DEFAULT_REQUEST_HEADERS)

    assert rv.status_code == 404