Esempio n. 1
0
def test_forge_anonymous(capsys):
    f = Forge(anonymous=True)
    # Test search
    assert len(
        f.search("mdf.source_name:ab_initio_solute_database",
                 advanced=True,
                 limit=300)) == 300

    # Test aggregation
    assert len(f.aggregate("mdf.source_name:nist_xps_db")) > 10000

    # Error on auth-only functions
    # http_download
    assert f.http_download({})["success"] is False
    out, err = capsys.readouterr()
    assert "Error: Anonymous HTTP download not yet supported." in out
    # globus_download
    assert f.globus_download({})["success"] is False
    out, err = capsys.readouterr()
    assert "Error: Anonymous Globus Transfer not supported." in out
    # http_stream
    res = f.http_stream({})
    assert next(res)["success"] is False
    out, err = capsys.readouterr()
    assert "Error: Anonymous HTTP download not yet supported." in out
    with pytest.raises(StopIteration):
        next(res)
Esempio n. 2
0
def test_forge_http_download(capsys):
    f = Forge(index="mdf")
    # Simple case
    f.http_download(example_result1)
    assert os.path.exists("./test_fetch.txt")

    # Test conflicting filenames
    f.http_download(example_result1)
    assert os.path.exists("./test_fetch(1).txt")
    f.http_download(example_result1)
    assert os.path.exists("./test_fetch(2).txt")
    os.remove("./test_fetch.txt")
    os.remove("./test_fetch(1).txt")
    os.remove("./test_fetch(2).txt")

    # With dest and preserve_dir, and tuple of results
    dest_path = os.path.expanduser("~/mdf")
    f.http_download(([example_result1], {
        "info": None
    }),
                    dest=dest_path,
                    preserve_dir=True)
    assert os.path.exists(os.path.join(dest_path, "test", "test_fetch.txt"))
    os.remove(os.path.join(dest_path, "test", "test_fetch.txt"))
    os.rmdir(os.path.join(dest_path, "test"))

    # With multiple files
    f.http_download(example_result2, dest=dest_path)
    assert os.path.exists(os.path.join(dest_path, "test_fetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "test_multifetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "petrel_fetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "petrel_multifetch.txt"))
    os.remove(os.path.join(dest_path, "test_fetch.txt"))
    os.remove(os.path.join(dest_path, "test_multifetch.txt"))
    os.remove(os.path.join(dest_path, "petrel_fetch.txt"))
    os.remove(os.path.join(dest_path, "petrel_multifetch.txt"))

    f.http_download(example_result3, dest=dest_path)
    assert os.path.exists(os.path.join(dest_path, "test_fetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "test_multifetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "petrel_fetch.txt"))
    assert os.path.exists(os.path.join(dest_path, "petrel_multifetch.txt"))
    os.remove(os.path.join(dest_path, "test_fetch.txt"))
    os.remove(os.path.join(dest_path, "test_multifetch.txt"))
    os.remove(os.path.join(dest_path, "petrel_fetch.txt"))
    os.remove(os.path.join(dest_path, "petrel_multifetch.txt"))

    # Too many files
    assert f.http_download(list(range(10001)))["success"] is False
    out, err = capsys.readouterr()
    assert "Too many results supplied. Use globus_download()" in out

    # "Missing" files
    f.http_download(example_result_missing)
    out, err = capsys.readouterr()
    assert not os.path.exists("./should_not_exist.txt")
    assert (
        "Error 404 when attempting to access "
        "'https://data.materialsdatafacility.org/test/should_not_exist.txt'"
    ) in out

    # No datasets
    f.http_download(example_dataset)
    out, err = capsys.readouterr()
    assert not os.path.exists(os.path.join(dest_path, "petrel_fetch.txt"))
    assert (
        "Skipping datset entry for 'foobar_v1': Cannot download dataset over HTTPS. "
        "Use globus_download() for datasets.") in out

    # Bad resource_type
    f.http_download(example_bad_resource)
    out, err = capsys.readouterr()
    assert "Error: Found unknown resource_type 'foobar'. Skipping entry." in out