Skip to content
Permalink
Browse files

added tests

  • Loading branch information...
nad2000 committed Oct 7, 2019
1 parent b117764 commit 7688a4e5dcb17b48c59f316a1d379314e3edc615
Showing with 141 additions and 12 deletions.
  1. +26 −7 orcid_hub/apis.py
  2. +7 −4 orcid_hub/models.py
  3. +108 −1 tests/test_apis.py
@@ -239,11 +239,10 @@ def api_response(self, query, exclude=None, only=None, recurse=False):
"""Create and return API response with pagination links."""
query = query.paginate(self.page, self.page_size)
records = [
r.to_dict(
recurse=recurse,
to_dashes=True,
exclude=exclude,
only=only) for r in query
r.to_dict(recurse=recurse,
to_dashes=True,
exclude=exclude,
only=only) for r in query
]
resp = yamlfy(records) if prefers_yaml() else jsonify(records)
resp.headers["Pagination-Page"] = self.page
@@ -301,7 +300,7 @@ def jsonify_task(self, task, include_records=True):
if request.method != "HEAD":
if task.task_type in [
TaskType.AFFILIATION, TaskType.FUNDING, TaskType.PEER_REVIEW,
TaskType.PROPERTY, TaskType.WORK, TaskType.OTHER_ID
TaskType.PROPERTY, TaskType.WORK, TaskType.OTHER_ID, TaskType.RESOURCE
]:
resp = jsonify(task.to_export_dict(include_records=include_records))
else:
@@ -450,6 +449,7 @@ def get(self, *args, **kwargs):
- FUNDING
- PEER_REVIEW
- PROPERTY
- RESOURCE
- WORK
created-at:
type: string
@@ -465,6 +465,8 @@ def get(self, *args, **kwargs):
enum:
- ACTIVE
- RESET
is-raw:
type: bool
parameters:
- name: "type"
in: "query"
@@ -476,6 +478,7 @@ def get(self, *args, **kwargs):
- FUNDING
- PEER_REVIEW
- PROPERTY
- RESOURCE
- WORK
- name: "status"
in: "query"
@@ -487,6 +490,19 @@ def get(self, *args, **kwargs):
enum:
- ACTIVE
- INACTIVE
- name: "with_records"
in: "query"
required: false
description: Indicates that the records should be included
default: false
type: "bool"
enum:
- true
- false
- yes
- no
- 1
- 0
- in: query
name: page
description: The number of the page of retrieved data starting counting from 1
@@ -516,6 +532,9 @@ def get(self, *args, **kwargs):
query = Task.select().where(Task.org_id == current_user.organisation_id)
task_type = request.args.get("type")
status = request.args.get("status")
with_records = request.args.get("with_records")
if with_records:
with_records = with_records.lower() not in ["0", "no", "false", "f"]
if task_type:
query = query.where(Task.task_type == TaskType[task_type.upper()].value)
if status:
@@ -528,7 +547,7 @@ def get(self, *args, **kwargs):
return self.api_response(
query,
exclude=[Task.created_by, Task.updated_by, Task.org],
recurse=False)
recurse=with_records)


class TaskAPI(TaskList):
@@ -1189,16 +1189,16 @@ def is_ready(self):
"""
return self.state == "ACTIVE" or self.records.whhere(self.record_model.is_active).exists()

def to_dict(self, to_dashes=True, recurse=False, exclude=None, include_records=True, only=None):
def to_dict(self, to_dashes=True, recurse=None, exclude=None, include_records=None, only=None):
"""Create a dict represenatation of the task suitable for serialization into JSON or YAML."""
# TODO: expand for the othe types of the tasks
task_dict = super().to_dict(
recurse=bool(False),
recurse=False,
to_dashes=to_dashes,
exclude=exclude,
only=only or [
Task.id, Task.filename, Task.task_type, Task.created_at, Task.updated_at,
Task.status
Task.status, Task.is_raw
])
# TODO: refactor for funding task to get records here not in API or export
if include_records and self.task_type not in [TaskType.FUNDING, TaskType.SYNC]:
@@ -4206,11 +4206,14 @@ def load(cls, data, task=None, task_id=None, filename=None, override=True,

def to_export_dict(self):
"""Map the common record parts to dict for export into JSON/YAML."""
return self.to_dict()

def to_dict(self, *args, **kwargs):
"""Map the common record parts to dict."""
d = json.loads(self.message)
d["invitees"] = [i.to_export_dict() for i in self.invitees]
return d


# class MessageRecordInvitee(BaseModel):
# """Link from a record to an invitee."""

@@ -19,6 +19,9 @@
from unittest.mock import patch, MagicMock
from tests import utils

# Test data directory
data_path = os.path.join(os.path.dirname(__file__), "data")


def test_plural():
"""Test noun pluralization."""
@@ -1501,7 +1504,6 @@ def test_proxy_get_profile(client):

def test_property_api(client, mocker):
"""Test property API in various formats."""
data_path = os.path.join(os.path.dirname(__file__), "data")
admin = client.data.get("admin")
resp = client.login(admin, follow_redirects=True)
resp = client.post("/load/researcher/properties",
@@ -1602,3 +1604,108 @@ def test_property_api(client, mocker):
create_or_update_other_name.assert_called_once()
create_or_update_keyword.assert_called_once()
create_or_update_address.assert_called()


def test_resource_api(client, mocker):
"""Test resource API in various formats."""
admin = client.data.get("admin")
resp = client.login(admin, follow_redirects=True)
resp = client.post("/load/task/RESOURCE",
data={
"file_": (open(os.path.join(data_path, "resources.csv"),
"rb"), "resources001.csv"),
},
follow_redirects=True)
assert resp.status_code == 200
assert Task.select().count() == 1

access_token = client.get_access_token()

resp = client.get("/api/v1/tasks?type=RESOURCE",
headers=dict(authorization=f"Bearer {access_token}"))
data = json.loads(resp.data)
task_id = int(data[0]["id"])

resp = client.get(f"/api/v1/resources/{task_id}",
headers=dict(authorization=f"Bearer {access_token}"))
data = json.loads(resp.data)
assert len(data["records"]) == 2
assert data["filename"] == "resources001.csv"

# del (data["id"])
# resp = client.post("/api/v1/properties/?filename=properties333.json",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=json.dumps(data))
# assert resp.status_code == 200
# assert Task.select().count() == 2

# records = data["records"]
# resp = client.post("/api/v1/properties/?filename=properties444.json",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=json.dumps(records))
# assert resp.status_code == 200
# assert Task.select().count() == 3

# resp = client.post("/api/v1/properties",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=json.dumps(records))
# assert resp.status_code == 200
# assert Task.select().count() == 4

# resp = client.post(f"/api/v1/properties/{task_id}",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=json.dumps(records))
# assert resp.status_code == 200
# assert Task.select().count() == 4

# resp = client.head(f"/api/v1/properties/{task_id}",
# headers=dict(authorization=f"Bearer {access_token}"))
# assert "Last-Modified" in resp.headers
# assert resp.status_code == 200

# resp = client.delete(f"/api/v1/properties/{task_id}",
# headers=dict(authorization=f"Bearer {access_token}"))

# resp = client.post("/api/v1/properties/?filename=properties333.csv",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="text/csv",
# data=open(os.path.join(data_path, "properties.csv")).read())
# assert resp.status_code == 200
# assert Task.select().count() == 4

# resp = client.post("/api/v1/properties/?filename=properties333.json",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=open(os.path.join(data_path, "properties.json")).read())
# assert resp.status_code == 200
# assert Task.select().count() == 5
# user = User.get(orcid="0000-0000-0000-00X3")
# OrcidToken.create(user=user, org=user.organisation, scopes="/person/update")
# get_profile = mocker.patch("orcid_hub.orcid_client.MemberAPIV3.get_record",
# return_value=utils.get_profile(user=user))
# send_email = mocker.patch("orcid_hub.utils.send_email")
# create_or_update_researcher_url = mocker.patch(
# "orcid_hub.orcid_client.MemberAPIV3.create_or_update_researcher_url")
# create_or_update_other_name = mocker.patch("orcid_hub.orcid_client.MemberAPIV3.create_or_update_other_name")
# create_or_update_address = mocker.patch("orcid_hub.orcid_client.MemberAPIV3.create_or_update_address")
# create_or_update_keyword = mocker.patch("orcid_hub.orcid_client.MemberAPIV3.create_or_update_keyword")
# for r in records:
# del(r["id"])
# r["is-active"] = True
# resp = client.post("/api/v1/properties/?filename=properties777.json",
# headers=dict(authorization=f"Bearer {access_token}"),
# content_type="application/json",
# data=json.dumps(records))
# assert resp.status_code == 200
# assert Task.select().count() == 6
# assert UserInvitation.select().count() == 7
# get_profile.assert_called()
# send_email.assert_called()
# create_or_update_researcher_url.assert_called_once()
# create_or_update_other_name.assert_called_once()
# create_or_update_keyword.assert_called_once()
# create_or_update_address.assert_called()

0 comments on commit 7688a4e

Please sign in to comment.
You can’t perform that action at this time.