Exemplo n.º 1
0
def test_fetch_software_from_github_archive():
    # we "fetch" a local ZIP file to simulate a Zenodo record created from a
    # GitHub repository via the Zenodo-GitHub integration
    with zenodo_archive() as zen_path:
        mock_response = BytesIO(
            json.dumps(
                {
                    "files": [
                        {
                            "filename": "some_dir/afake.zip",
                            "links": {"download": "file://{}".format(zen_path)},
                        }
                    ],
                    "metadata": {"upload_type": "software"},
                }
            ).encode("utf-8")
        )

        def mock_urlopen(self, req):
            if isinstance(req, Request):
                return mock_response
            else:
                return urlopen(req)

        with patch.object(Zenodo, "_urlopen", new=mock_urlopen):
            zen = Zenodo()

            with TemporaryDirectory() as d:
                output = []
                for l in zen.fetch({"record": "1234"}, d):
                    output.append(l)

                unpacked_files = set(os.listdir(d))
                expected = set(["some-other-file.txt", "some-file.txt"])
                assert expected == unpacked_files
def test_fetch_software_from_github_archive(requests_mock):
    # we "fetch" a local ZIP file to simulate a Zenodo record created from a
    # GitHub repository via the Zenodo-GitHub integration
    with zenodo_archive() as zen_path:
        mock_response = {
            "files": [{
                "filename": "some_dir/afake.zip",
                "links": {
                    "download": "file://{}".format(zen_path)
                },
            }],
            "metadata": {
                "upload_type": "other"
            },
        }
        requests_mock.get("https://zenodo.org/api/records/1234",
                          json=mock_response)
        requests_mock.get("file://{}".format(zen_path),
                          content=open(zen_path, "rb").read())

        zen = Zenodo()
        spec = {"host": test_zen.hosts[0], "record": "1234"}

        with TemporaryDirectory() as d:
            output = []
            for l in zen.fetch(spec, d):
                output.append(l)

            unpacked_files = set(os.listdir(d))
            expected = set(["some-other-file.txt", "some-file.txt"])
            assert expected == unpacked_files
def test_fetch_software(requests_mock):
    # we "fetch" a local ZIP file to simulate a Zenodo software record with a
    # ZIP file in it
    with zenodo_archive() as zen_path:
        mock_response = {
            "files": [{
                # this is the difference to the GitHub generated one,
                # the ZIP file isn't in a directory
                "filename": "afake.zip",
                "links": {
                    "download": "file://{}".format(zen_path)
                },
            }],
            "metadata": {
                "upload_type": "software"
            },
        }
        requests_mock.get("https://zenodo.org/api/records/1234",
                          json=mock_response)
        requests_mock.get("file://{}".format(zen_path),
                          content=open(zen_path, "rb").read())

        with TemporaryDirectory() as d:
            zen = Zenodo()
            spec = spec = {"host": test_zen.hosts[0], "record": "1234"}
            output = []
            for l in zen.fetch(spec, d):
                output.append(l)

            unpacked_files = set(os.listdir(d))
            expected = set(["some-other-file.txt", "some-file.txt"])
            assert expected == unpacked_files
Exemplo n.º 4
0
def test_fetch_software():
    # we "fetch" a local ZIP file to simulate a Zenodo software record with a
    # ZIP file in it
    with zenodo_archive() as zen_path:
        mock_response = BytesIO(
            json.dumps({
                "files": [{
                    # this is the difference to the GitHub generated one,
                    # the ZIP file isn't in a directory
                    "filename": "afake.zip",
                    "links": {
                        "download": "file://{}".format(zen_path)
                    },
                }],
                "metadata": {
                    "upload_type": "software"
                },
            }).encode("utf-8"))

        def mock_urlopen(self, req):
            if isinstance(req, Request):
                return mock_response
            else:
                return urlopen(req)

        with patch.object(Zenodo, "_urlopen", new=mock_urlopen):
            with TemporaryDirectory() as d:
                zen = Zenodo()
                spec = spec = {
                    "host": {
                        "hostname": [
                            "https://zenodo.org/record/",
                            "http://zenodo.org/record/",
                        ],
                        "api":
                        "https://zenodo.org/api/records/",
                        "filepath":
                        "files",
                        "filename":
                        "filename",
                        "download":
                        "links.download",
                        "type":
                        "metadata.upload_type",
                    },
                    "record": "1234",
                }
                output = []
                for l in zen.fetch(spec, d):
                    output.append(l)

                unpacked_files = set(os.listdir(d))
                expected = set(["some-other-file.txt", "some-file.txt"])
                assert expected == unpacked_files
def test_fetch_data():
    # we "fetch" a local ZIP file to simulate a Zenodo data record
    with zenodo_archive() as a_zen_path:
        with zenodo_archive() as b_zen_path:
            mock_response = BytesIO(
                json.dumps({
                    "files": [
                        {
                            "filename": "afake.zip",
                            "links": {
                                "download": "file://{}".format(a_zen_path)
                            },
                        },
                        {
                            "filename": "bfake.zip",
                            "links": {
                                "download": "file://{}".format(b_zen_path)
                            },
                        },
                    ],
                    "metadata": {
                        "upload_type": "data"
                    },
                }).encode("utf-8"))

            def mock_urlopen(self, req):
                if isinstance(req, Request):
                    return mock_response
                else:
                    return urlopen(req)

            with patch.object(Zenodo, "urlopen", new=mock_urlopen):
                with TemporaryDirectory() as d:
                    zen = Zenodo()
                    spec = {"host": test_zen.hosts[0], "record": "1234"}
                    output = []
                    for l in zen.fetch(spec, d):
                        output.append(l)

                    unpacked_files = set(os.listdir(d))
                    # ZIP files shouldn't have been unpacked
                    expected = {"bfake.zip", "afake.zip"}
                    assert expected == unpacked_files
def test_fetch_data(requests_mock):
    # we "fetch" a local ZIP file to simulate a Zenodo data record
    with zenodo_archive() as a_zen_path:
        with zenodo_archive() as b_zen_path:
            mock_response = {
                "files": [
                    {
                        "filename": "afake.zip",
                        "links": {
                            "download": "file://{}".format(a_zen_path)
                        },
                    },
                    {
                        "filename": "bfake.zip",
                        "links": {
                            "download": "file://{}".format(b_zen_path)
                        },
                    },
                ],
                "metadata": {
                    "upload_type": "data"
                },
            }
            requests_mock.get("https://zenodo.org/api/records/1234",
                              json=mock_response)
            requests_mock.get("file://{}".format(a_zen_path),
                              content=open(a_zen_path, "rb").read())
            requests_mock.get("file://{}".format(b_zen_path),
                              content=open(b_zen_path, "rb").read())

            with TemporaryDirectory() as d:
                zen = Zenodo()
                spec = {"host": test_zen.hosts[0], "record": "1234"}
                output = []
                for l in zen.fetch(spec, d):
                    output.append(l)

                unpacked_files = set(os.listdir(d))
                # ZIP files shouldn't have been unpacked
                expected = {"bfake.zip", "afake.zip"}
                assert expected == unpacked_files