Ejemplo n.º 1
0
def openapi():
    yield '5f5141cbae5ca099d3f420f9c42c94cf'
    refresh()
    try:  # teardown
        APIDoc.get('5f5141cbae5ca099d3f420f9c42c94cf').delete()
    except elasticsearch.exceptions.NotFoundError:
        pass
Ejemplo n.º 2
0
    def delete(self):

        try:
            APIDoc.get(self._id).delete()
        except ESNotFoundError as err:
            raise NotFoundError() from err

        return self._id
Ejemplo n.º 3
0
    def get(cls, _id):

        try:
            doc = APIDoc.get(_id)
        except ESNotFoundError as err:
            raise NotFoundError from err

        obj = cls(doc._meta.url)
        obj.raw = decoder.decompress(doc._raw)

        obj.username = doc._meta.username
        obj.slug = doc._meta.slug

        obj.date_created = doc._meta.date_created
        obj.last_updated = doc._meta.last_updated

        obj.uptime = APIMonitorStatus(
            obj, (
                doc._status.uptime_status, 
                doc._status.uptime_msg
            ),
            doc._status.uptime_ts
        )

        obj.webdoc = APIRefreshStatus(
            obj, doc._status.refresh_status,
            doc._status.refresh_ts
        )

        return obj
Ejemplo n.º 4
0
def test_uptime_status():
    mygene = SmartAPI.get(MYGENE_ID)
    assert mygene.uptime.status[0] is None

    mygene.uptime.update(('good', None))
    assert mygene.uptime.status[0] == 'good'
    mygene.save()
    refresh()
    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.uptime_status == 'good'

    mygene.uptime.update((None, None))
    assert mygene.uptime.status[0] is None
    mygene.save()
    refresh()
    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.uptime_status is None
Ejemplo n.º 5
0
def myvariant():

    with open(os.path.join(dirname, 'myvariant.es.json'), 'r') as file:
        MYVARIANT_ES = json.load(file)
    with open(os.path.join(dirname, 'myvariant.yml'), 'rb') as file:
        MYVARIANT_RAW = file.read()
    MYVARIANT_ID = MYVARIANT_ES.pop("_id")

    myvariant = APIDoc(meta={'id': MYVARIANT_ID}, **MYVARIANT_ES)
    myvariant._raw = decoder.compress(MYVARIANT_RAW)
    myvariant.save()

    refresh()
    yield MYVARIANT_ID
    refresh()

    try:
        APIDoc.get(MYVARIANT_ID).delete()
    except elasticsearch.exceptions.NotFoundError:
        pass
Ejemplo n.º 6
0
def test_uptime_update():

    mygene = SmartAPI.get(MYGENE_ID)
    mygene.check()  # minimum api document
    assert mygene.uptime.status[0] == 'unknown'  # TODO VERIFY THIS IS IN FACT CORRECT

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.uptime_status == 'unknown'

    mychem = SmartAPI.get(MYCHEM_ID)
    mychem.check()  # full api document
    assert mychem.uptime.status[0] == 'unknown'

    mychem.save()
    refresh()

    mychem_doc = APIDoc.get(MYCHEM_ID)
    assert mychem_doc._status.uptime_status == 'unknown'
Ejemplo n.º 7
0
def test_refresh_update():

    mychem = SmartAPI.get(MYCHEM_ID)
    assert mychem.webdoc.status is None

    # NOTE
    # the following update and the updates thereafter will be applied
    # https://github.com/NCATS-Tangerine/translator-api-registry/commit/b01baa5

    mychem.refresh()
    assert mychem.webdoc.status == 299  # new version
    assert mychem.webdoc.timestamp > datetime(2020, 1, 1, tzinfo=timezone.utc)
    _ts0 = mychem.webdoc.timestamp

    mychem.refresh()
    assert mychem.webdoc.status == 200  # already latest
    assert mychem.webdoc.timestamp >= _ts0  # could be cached internally

    mychem.save()
    refresh()
    mychem_doc = APIDoc.get(MYCHEM_ID)
    assert mychem_doc._status.refresh_status == 200
Ejemplo n.º 8
0
def test_refresh_status():

    with open(os.path.join(dirname, 'mygene_full.yml'), 'rb') as file:
        MYGENE_FULL = file.read()

    mygene = SmartAPI.get(MYGENE_ID)  # minimum
    assert mygene.webdoc.status is None
    assert 'components' not in mygene

    mygene.webdoc.update(File(200, MYGENE_FULL, None, None))  # new content

    assert mygene.webdoc.status == 299  # updated
    assert 'components' in mygene
    assert mygene.webdoc.timestamp > datetime(2020, 1, 1)
    _ts0 = mygene.webdoc.timestamp

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.refresh_status == 299
    assert 'components' in mygene_doc

    mygene.webdoc.update(File(200, MYGENE_FULL, None, None))  # no change

    assert mygene.webdoc.status == 200  # latest
    assert 'components' in mygene
    assert mygene.webdoc.timestamp > _ts0

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.refresh_status == 200
    assert 'components' in mygene_doc

    mygene.webdoc.update(File(404, None, None, None))  # link broken

    assert mygene.webdoc.status == 404
    assert 'components' in mygene  # do not affect main copy

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.refresh_status == 404
    assert 'components' in mygene_doc

    mygene.webdoc.update(File(200, MYGENE_FULL, None, None))  # link back working

    assert mygene.webdoc.status == 200  # latest
    assert 'components' in mygene

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.refresh_status == 200
    assert 'components' in mygene_doc

    mygene.webdoc.update(File(200, b'{"openapi":"3.0.0"}', None, None))  # invalid

    assert mygene.webdoc.status == 499  # invalid
    assert 'components' in mygene  # do not affect main copy

    mygene.save()
    refresh()

    mygene_doc = APIDoc.get(MYGENE_ID)
    assert mygene_doc._status.refresh_status == 499
    assert 'components' in mygene_doc
Ejemplo n.º 9
0
def test_save(openapi):
    """
    SmartAPI.slug.validate(slug)
    smartapi.slug
    smartapi.save()
    """
    _t0 = datetime.now(timezone.utc)
    time.sleep(0.1)
    URL = "http://example.com/valid.json"
    with pytest.raises(ValueError):
        SmartAPI.slug.validate("a")
    with pytest.raises(ValueError):
        SmartAPI.slug.validate("AAA")
    with pytest.raises(ValueError):
        SmartAPI.slug.validate("www")
    with pytest.raises(ValueError):
        SmartAPI.slug.validate("^_^")
    with open(os.path.join(dirname, './validate/openapi-pass.json'), 'rb') as file:
        raw = file.read()
        smartapi = SmartAPI(URL)
        with pytest.raises(ControllerError):
            smartapi.raw = None
        smartapi.raw = raw
        smartapi.slug = "mygene"
        smartapi.validate()
        with pytest.raises(ControllerError):
            smartapi.save()
        smartapi.username = "******"
        with pytest.raises(ConflictError):
            smartapi.save()
        smartapi.slug = "mychem"
        with pytest.raises(ConflictError):
            smartapi.save()
        smartapi.slug = "openapi"
        smartapi.save()
        refresh()
        assert SmartAPI.find("openapi") == smartapi._id
        assert smartapi.date_created > _t0
        assert smartapi.last_updated > _t0
        assert smartapi.date_created == smartapi.last_updated
        apidoc = APIDoc.get(smartapi._id)
        assert apidoc._meta.date_created == smartapi.date_created
        assert apidoc._meta.last_updated == smartapi.last_updated
        _t1 = smartapi.date_created
        smartapi.save()  # no change
        refresh()
        assert SmartAPI.find("openapi") == smartapi._id
        assert smartapi.date_created == _t1
        assert smartapi.last_updated == _t1
        assert smartapi.date_created == smartapi.last_updated
        apidoc = APIDoc.get(smartapi._id)
        assert apidoc._meta.date_created == smartapi.date_created
        assert apidoc._meta.last_updated == smartapi.last_updated
        smartapi.slug = None
        smartapi.save()
        refresh()
        assert not SmartAPI.find("openapi")
        found = SmartAPI.get(openapi)
        assert dict(smartapi) == dict(found)
        assert smartapi.username == found.username
        assert smartapi.slug == found.slug
        assert smartapi.url == found.url
        assert smartapi.date_created == _t1
        assert smartapi.last_updated == _t1
        assert smartapi.date_created == smartapi.last_updated
        apidoc = APIDoc.get(smartapi._id)
        assert apidoc._meta.date_created == smartapi.date_created
        assert apidoc._meta.last_updated == smartapi.last_updated
        smartapi.raw = raw  # should trigger ts update
        smartapi.save()
        refresh()
        assert smartapi.date_created == _t1
        assert smartapi.last_updated > _t1
        apidoc = APIDoc.get(smartapi._id)
        assert apidoc._meta.date_created == smartapi.date_created
        assert apidoc._meta.last_updated == smartapi.last_updated