Beispiel #1
0
def setup_fixture():
    """
    Index 2 documents.
    """
    reset()

    # save initial docs with paths already transformed
    mygene = SmartAPI(MYGENE_URL)
    mygene.raw = MYGENE_RAW
    mygene.username = '******'
    mygene.slug = 'mygene'
    mygene.save()

    mychem = SmartAPI(MYCHEM_URL)
    mychem.raw = MYCHEM_RAW
    mychem.username = '******'
    mychem.slug = 'mychem'
    mychem.save()

    # refresh index
    refresh()
Beispiel #2
0
def _restore(smartapis):
    if indices.exists():
        logging.error("Cannot write to an existing index.")
        return
    indices.reset()
    for smartapi in smartapis:
        logging.info(smartapi["url"])
        _smartapi = SmartAPI(smartapi["url"])
        _smartapi.username = smartapi["username"]
        _smartapi.slug = smartapi["slug"]
        _smartapi.date_created = datetime.fromisoformat(
            smartapi["date_created"])
        _smartapi.last_updated = datetime.fromisoformat(
            smartapi["last_updated"])
        _smartapi.raw = smartapi["raw"].encode()  # to bytes
        _smartapi.save()
Beispiel #3
0
def migrate():

    for doc in scan(Elasticsearch(ES_ORIGIN),
                    query={"query": {
                        "match_all": {}
                    }},
                    index="smartapi_oas3",
                    doc_type="api"):
        print(doc["_id"])
        if not doc['_source']['_meta'].get('_archived'):
            url = doc['_source']['_meta']['url']
            raw = decoder.decompress(
                base64.urlsafe_b64decode(doc['_source']['~raw']))

            smartapi = SmartAPI(url)
            smartapi.raw = raw
            smartapi.date_created = parser.parse(
                doc['_source']['_meta']['timestamp']).replace(
                    tzinfo=timezone.utc)
            smartapi.username = doc['_source']['_meta']['github_username']
            smartapi.slug = doc['_source']['_meta'].get('slug')
            smartapi.save()
    print()
Beispiel #4
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