Skip to content

Commit

Permalink
add affiliated_institutions replationship field with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Jul 22, 2024
1 parent 0befa48 commit ae989aa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/preprints/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ class PreprintSerializer(TaxonomizableSerializerMixin, MetricsSerializerMixin, J
),
)

affiliated_institutions = RelationshipField(
related_view='preprints:preprints-institutions',
related_view_kwargs={'preprint_id': '<_id>'},
self_view='preprints:preprints-institutions',
self_view_kwargs={'preprint_id': '<_id>'},
read_only=False,
required=False,
allow_null=True,
)

links = LinksField(
{
'self': 'get_preprint_url',
Expand Down
15 changes: 15 additions & 0 deletions api_tests/preprints/views/test_preprint_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
ProjectFactory,
SubjectFactory,
PreprintProviderFactory,
InstitutionFactory
)
from website.settings import DOI_FORMAT, CROSSREF_URL

Expand Down Expand Up @@ -58,6 +59,10 @@ class TestPreprintDetail:
def preprint(self, user):
return PreprintFactory(creator=user)

@pytest.fixture()
def institution(self):
return InstitutionFactory(creator=user)

@pytest.fixture()
def preprint_pre_mod(self, user):
return PreprintFactory(reviews_workflow='pre-moderation', is_published=False, creator=user)
Expand Down Expand Up @@ -215,6 +220,16 @@ def test_preprint_embed_identifiers(self, app, user, preprint, url):
link = res.json['data']['relationships']['identifiers']['links']['related']['href']
assert f'{url}identifiers/' in link

def test_return_affiliated_institutions(self, app, user, preprint, institution, url):
"""
Confirmation test for the the new preprint affiliated institutions feature
"""
preprint.affiliated_institutions.add(institution)
res = app.get(url)
assert res.status_code == 200
relationship_link = res.json['data']['relationships']['affiliated_institutions']['links']['related']['href']
assert f'/v2/preprints/{preprint._id}/institutions/' in relationship_link


@pytest.mark.django_db
class TestPreprintDelete:
Expand Down
14 changes: 14 additions & 0 deletions api_tests/preprints/views/test_preprint_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
AuthUserFactory,
SubjectFactory,
PreprintProviderFactory,
InstitutionFactory,
)
from tests.base import ApiTestCase, capture_signals
from website.project import signals as project_signals
Expand Down Expand Up @@ -145,6 +146,7 @@ class TestPreprintList(ApiTestCase):
def setUp(self):
super().setUp()
self.user = AuthUserFactory()
self.institution = InstitutionFactory()

self.preprint = PreprintFactory(creator=self.user)
self.url = f'/{API_BASE}preprints/'
Expand Down Expand Up @@ -184,6 +186,18 @@ def test_withdrawn_preprints_list(self):
assert pp._id not in user_res_ids
assert pp._id in mod_res_ids

def test_return_affiliated_institutions(self):
"""
Confirmation test for the the new preprint affiliated institutions feature
"""
self.preprint.affiliated_institutions.add(self.institution)
res = self.app.get(self.url)
assert len(res.json['data']) == 1
assert res.status_code == 200
assert res.content_type == 'application/vnd.api+json'
relationship_link = res.json['data'][0]['relationships']['affiliated_institutions']['links']['related']['href']
assert f'/v2/preprints/{self.preprint._id}/institutions/' in relationship_link


class TestPreprintsListFiltering(PreprintsListFilteringMixin):

Expand Down

0 comments on commit ae989aa

Please sign in to comment.