Skip to content

Commit

Permalink
Add test for default version, fix formatting of date
Browse files Browse the repository at this point in the history
  • Loading branch information
koutst committed Oct 30, 2024
1 parent 710a8d4 commit e704e8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/paper/serializers/paper_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,23 @@ def get_version_list(self, paper) -> list:
}
]

paper_versions = PaperVersion.objects.filter(
base_doi=paper_version.base_doi
).order_by("version")
paper_versions = (
PaperVersion.objects.filter(base_doi=paper_version.base_doi)
.select_related("paper")
.order_by("version")
)
latest_version = paper_versions.last()

# Return a list of version pointing to the paper_id
return [
{
"version": version.version,
"paper_id": version.paper.id,
"published_date": paper.paper_publish_date,
"published_date": (
version.paper.paper_publish_date.strftime("%Y-%m-%d")
if version.paper.paper_publish_date
else None
),
"message": version.message,
"is_latest": version.version == latest_version.version,
}
Expand Down Expand Up @@ -1093,17 +1099,23 @@ def get_version_list(self, paper) -> list:
}
]

paper_versions = PaperVersion.objects.filter(
base_doi=paper_version.base_doi
).order_by("version")
paper_versions = (
PaperVersion.objects.filter(base_doi=paper_version.base_doi)
.select_related("paper")
.order_by("version")
)
latest_version = paper_versions.last()

# Return a list of version pointing to the paper_id
return [
{
"version": version.version,
"paper_id": version.paper.id,
"published_date": paper.paper_publish_date,
"published_date": (
version.paper.paper_publish_date.strftime("%Y-%m-%d")
if version.paper.paper_publish_date
else None
),
"message": version.message,
"is_latest": version.version == latest_version.version,
}
Expand Down
16 changes: 16 additions & 0 deletions src/paper/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ def test_hubs_field_is_optional(self):
serialized = PaperSerializer(data=paper_dict)
self.assertTrue(serialized.is_valid())

def test_paper_serializer_default_paper_version(self):
paper = helpers.create_paper(title="Serialized Paper Title")
serialized = PaperSerializer(paper)
self.assertEqual(serialized.data["version"], 1)
self.assertEqual(
serialized.data["version_list"],
[
{
"version": 1,
"paper_id": paper.id,
"published_date": paper.paper_publish_date,
"is_latest": True,
}
],
)

def test_paper_serializer_paper_versions(self):
paper = helpers.create_paper(title="Serialized Paper Title")
PaperVersion.objects.create(
Expand Down

0 comments on commit e704e8e

Please sign in to comment.