Skip to content

Commit

Permalink
test: disable test_codemeta, add test_metadata
Browse files Browse the repository at this point in the history
and resolve some edge case bugs with metadata generation.

test_codemeta was primarily checking to make sure that codemeta was
conforming to the expected schema, and this is implicit now

we may still want some test module that uses hypothesis, but it would be
even more useful to do this at a higher level e.g. create a bunch of
codebase+releases and see if anything goes wrong downstream
  • Loading branch information
sgfost committed Jan 16, 2025
1 parent 9cbc597 commit b6d93cd
Show file tree
Hide file tree
Showing 6 changed files with 659 additions and 561 deletions.
2 changes: 1 addition & 1 deletion django/core/settings/e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
SHARE_DIR = path.realpath("/shared/e2e")
LIBRARY_ROOT = path.join(SHARE_DIR, "library")
LIBRARY_PREVIOUS_ROOT = path.join(SHARE_DIR, ".latest")
REPOSITORY_ROOT = path.join(BASE_DIR, "repository")
REPOSITORY_ROOT = path.join(SHARE_DIR, "repository")
BACKUP_ROOT = path.join(SHARE_DIR, "backups")
BORG_ROOT = path.join(BACKUP_ROOT, "repo")
EXTRACT_ROOT = path.join(SHARE_DIR, "extract")
Expand Down
2 changes: 1 addition & 1 deletion django/core/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SHARE_DIR = path.realpath("library/tests/tmp")
LIBRARY_ROOT = path.join(SHARE_DIR, "library")
LIBRARY_PREVIOUS_ROOT = path.join(SHARE_DIR, ".latest")
REPOSITORY_ROOT = path.join(BASE_DIR, "repository")
REPOSITORY_ROOT = path.join(SHARE_DIR, "repository")
BACKUP_ROOT = path.join(SHARE_DIR, "backups")
BORG_ROOT = path.join(BACKUP_ROOT, "repo")
EXTRACT_ROOT = path.join(SHARE_DIR, "extract")
Expand Down
10 changes: 6 additions & 4 deletions django/library/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def to_textual_creative_work(cls, text: str) -> dict:

@classmethod
def license_to_creative_work(cls, license) -> dict:
return {
creative_work_license = {
"@type": "CreativeWork",
"name": license.name,
"url": license.url,
}
if license.url:
creative_work_license["url"] = license.url
return creative_work_license

@classmethod
def url_to_datafeed(cls, url: str) -> dict:
Expand Down Expand Up @@ -168,7 +170,7 @@ def _convert_codebase(cls, codebase) -> CodeMeta:
codebase.all_nonauthor_contributors, "contributor"
)
or None,
dateCreated=codebase.date_created.date(),
dateCreated=codebase.date_created.date() if codebase.date_created else None,
datePublished=(
codebase.last_published_on.date()
if codebase.last_published_on
Expand Down Expand Up @@ -219,7 +221,7 @@ def _convert_release(cls, release) -> CodeMeta:
copyrightYear=(
release.last_published_on.year if release.last_published_on else None
),
dateCreated=release.date_created.date(),
dateCreated=release.date_created.date() if release.date_created else None,
dateModified=(
release.last_modified.date() if release.last_modified else None
),
Expand Down
37 changes: 20 additions & 17 deletions django/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,29 +751,29 @@ def publication_year(self):
self.last_published_on.year if self.last_published_on else date.today().year
)

@cached_property
@property
def all_contributors(self):
return Contributor.objects.filter(
id__in=ReleaseContributor.objects.for_codebase(self).values(
"contributor_id"
)
)

@cached_property
@property
def all_author_contributors(self):
return Contributor.objects.filter(
id__in=ReleaseContributor.objects.authors()
.for_codebase(self)
.values("contributor_id")
)

@cached_property
@property
def all_nonauthor_contributors(self):
return self.all_contributors.exclude(
id__in=self.all_author_contributors.values("id")
)

@cached_property
@property
def all_citable_contributors(self):
return Contributor.objects.filter(
id__in=ReleaseContributor.objects.citable()
Expand Down Expand Up @@ -1028,14 +1028,16 @@ def create_release(
self.save()
return release

def save(self, rebuild_metadata=True, **kwargs):
def save(self, rebuild_metadata=True, rebuild_release_metadata=True, **kwargs):
"""save the codebase and optionally rebuild metadata by updating codemeta_snapshot.
If rebuild_release_metadata is True, all releases will be saved to trigger metadata updates"""
if rebuild_metadata:
logger.debug("Building codemeta for codebase: %s", self)
self.codemeta_snapshot = self.codemeta.dict(serialize=True)
super().save(**kwargs)
# saving releases will trigger metadata rebuilding and updating
# the fs and git mirror if one exists
if rebuild_metadata:
if rebuild_metadata and rebuild_release_metadata:
for release in self.releases.all():
release.save(rebuild_metadata=True)

Expand Down Expand Up @@ -1774,12 +1776,11 @@ def _publish(self):
codebase.last_published_on = now
if codebase.first_published_at is None:
codebase.first_published_at = now
codebase.save(rebuild_metadata=False)
# normally, rebuilding metadata is asynchronous and automatic but
# here we need to build it synchronously after setting everything
self.codemeta_snapshot = self.codemeta.dict(serialize=True)
self.save(rebuild_metadata=False)
self.get_fs_api().rebuild(metadata_only=True)
self.save(defer_fs=False)
# and then rebuild the codebase metadata
codebase.save(rebuild_metadata=True, rebuild_release_metadata=False)

@transaction.atomic
def unpublish(self):
Expand Down Expand Up @@ -1838,11 +1839,10 @@ def set_version_number(self, version_number):
)
self.version_number = version_number

def cache_codemeta(self):
self.codemeta_snapshot = self.codemeta.dict(serialize=True)
self.save(rebuild_metadata=False)

def save(self, rebuild_metadata=True, **kwargs):
def save(self, rebuild_metadata=True, defer_fs=True, **kwargs):
"""save the release and optionally rebuild metadata by updating codemeta_snapshot
and rebuilding the filesystem metadata. If defer_fs is True (default), the filesystem rebuild
will be deferred to an async task"""
if not rebuild_metadata:
super().save(**kwargs)
else:
Expand All @@ -1852,9 +1852,12 @@ def save(self, rebuild_metadata=True, **kwargs):
super().save(**kwargs)

if old_codemeta != self.codemeta_snapshot:
from .tasks import update_fs_release_metadata
if defer_fs:
from .tasks import update_fs_release_metadata

update_fs_release_metadata(self.id)
update_fs_release_metadata(self.id)
else:
self.get_fs_api().rebuild(metadata_only=True)

@classmethod
def get_indexed_objects(cls):
Expand Down
Loading

0 comments on commit b6d93cd

Please sign in to comment.