Skip to content

Commit

Permalink
Update tests (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Sep 14, 2023
1 parent 5ee911e commit a078d41
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/zenodo_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def create(self, data: Data, paths: Paths) -> requests.Response:
json=data,
params={"access_token": self.access_token},
)
if res.status_code == 400:
raise ValueError(res.text)
res.raise_for_status()

res_json = res.json()
Expand Down
61 changes: 26 additions & 35 deletions tests/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from uuid import uuid4

import pystow
import requests

from zenodo_client import Creator, Metadata, Zenodo

Expand All @@ -16,50 +16,49 @@
TEXT_V2 = "this is some v2 test text\noh yeah baby"
TEXT_V3 = "this is some v3 test text\noh yeah baby"

ACCESS_TOKEN = pystow.get_config("zenodo", "sandbox_api_token") or pystow.get_config("zenodo:sandbox", "api_token")
CREATOR = Creator(
name="Hoyt, Charles Tapley",
affiliation="Northeastern University",
orcid="0000-0003-4423-4370",
# Note: zenodo has some problem validating my GND. Skip it for now
# gnd="1203140533",
)


@unittest.skipUnless(ACCESS_TOKEN, reason="Missing Zenodo sandbox API token")
class TestLifecycle(unittest.TestCase):
"""Test case for zenodo client."""

def setUp(self) -> None:
"""Set up the test case with a zenodo client."""
self.zenodo = Zenodo(sandbox=True, access_token=ACCESS_TOKEN)
self.zenodo = Zenodo(sandbox=True)
self.key = f"test-{uuid4()}"
self._directory = tempfile.TemporaryDirectory()
self.directory = Path(self._directory.name).resolve()

self.path = self.directory.joinpath("test.txt")
self.path.write_text(TEXT_V1)

def tearDown(self) -> None:
"""Tear down the test case."""
self._directory.cleanup()

def test_connect(self):
"""Test connection works."""
r = requests.get(self.zenodo.depositions_base, params={"access_token": self.zenodo.access_token})
self.assertEqual(200, r.status_code, msg=r.text)

def test_create(self):
"""Test creating a record."""
data = Metadata(
title="Test Upload",
upload_type="dataset",
description="test description",
creators=[
Creator(
name="Hoyt, Charles Tapley",
affiliation="Harvard Medical School",
orcid="0000-0003-4423-4370",
),
],
)
path = self.directory.joinpath("test.txt")
path.write_text(TEXT_V1)

logger.info(f"wrote data to {path}")
res = self.zenodo.ensure(
key=self.key,
data=data,
paths=path,
creators=[CREATOR],
)

res = self.zenodo.ensure(key=self.key, data=data, paths=self.path)
res_json = res.json()
# print(f"\n\nSEE V1 ON ZENODO: {res_json['links']['record_html']}")

self.assertEqual(True, res_json["submitted"])
self.assertEqual("done", res_json["state"])
self.assertEqual("dataset", res_json["metadata"]["upload_type"])
Expand All @@ -68,15 +67,15 @@ def test_create(self):

deposition_id = res_json["record_id"]
# print(f"DEPOSITION ID: {deposition_id}")
path.write_text(TEXT_V2)
self.path.write_text(TEXT_V2)

res = self.zenodo.update(deposition_id, paths=path)
res = self.zenodo.update(deposition_id, paths=self.path)
res_json = res.json()
# print(f"SEE V2 ON ZENODO: {res_json['links']['record_html']}")
self.assertEqual(f"{data.version}-1", res_json["metadata"]["version"])

path.write_text(TEXT_V3)
res = self.zenodo.update(deposition_id, paths=path)
self.path.write_text(TEXT_V3)
res = self.zenodo.update(deposition_id, paths=self.path)
res_json = res.json()
# print(f"SEE V3 ON ZENODO: {res_json['links']['record_html']}")
self.assertEqual(f"{data.version}-2", res_json["metadata"]["version"])
Expand All @@ -94,18 +93,10 @@ def test_create_no_orcid(self):
),
],
)
path = self.directory.joinpath("test.txt")
path.write_text(TEXT_V1)

logger.info(f"wrote data to {path}")
res = self.zenodo.ensure(
key=self.key,
data=data,
paths=path,
)

res = self.zenodo.ensure(key=self.key, data=data, paths=self.path)
res_json = res.json()
# print(f"\n\nSEE V1 ON ZENODO: {res_json['links']['record_html']}")

self.assertEqual(True, res_json["submitted"])
self.assertEqual("done", res_json["state"])
self.assertEqual("dataset", res_json["metadata"]["upload_type"])
Expand Down

0 comments on commit a078d41

Please sign in to comment.