Skip to content

Commit

Permalink
fixtures: Fix a bug where the update_record was removing the bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
psaiz committed Dec 9, 2024
1 parent 1176ce5 commit f7fcde6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cernopendata/modules/fixtures/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ def update_record(pid, data, skip_files):
FileInstance.query.filter_by(id=o.file_id).delete()
FileIndexMetadata.delete_by_record(record=record)
# This is to ensure that fields that do not appear in the new data
# are not just maintained from the previous version
# are not just kept from the previous version
for k in list(record.keys()):
if k == "_bucket":
continue
if skip_files and k in ["files", "_files", "file_indices", "_file_indices"]:
continue
del record[k]
Expand Down
50 changes: 48 additions & 2 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import pytest
from invenio_files_rest.models import Location
from invenio_indexer.api import RecordIndexer
from invenio_pidstore.models import PersistentIdentifier

from cernopendata.modules.fixtures.cli import create_record
from cernopendata.api import FileIndexMetadata, RecordFilesWithIndex
from cernopendata.modules.fixtures.cli import create_record, update_record
from cernopendata.modules.records.utils import record_file_page


def test_file_index(app, database, search):
def test_file(app, database, search):
"""Checking that records can be inserted"""
data = {
"$schema": app.extensions["invenio-jsonschemas"].path_to_url(
Expand Down Expand Up @@ -66,3 +68,47 @@ def test_file_index(app, database, search):
)
response = json.loads(req.response[0])
assert response["files"][0]["uri"] == "root://foo/bar"
record.commit()
data2 = {
"$schema": app.extensions["invenio-jsonschemas"].path_to_url(
"records/record-v1.0.0.json"
),
"recid": "71114",
"date_published": "2024",
"experiment": ["ALICE"],
"publisher": "CERN Open Data Portal",
"title": "File modified",
"type": {
"primary": "Dataset",
"secondary": ["Derived"],
},
"files": [
{"checksum": "adler32:9719fd6a", "size": 1053, "uri": "root://foo/bar"}
],
}
pid = PersistentIdentifier.get("recid", data["recid"])
# The first time that an update is done, it usually works
record = update_record(pid, data2, False)
record.commit()

data3 = {
"$schema": app.extensions["invenio-jsonschemas"].path_to_url(
"records/record-v1.0.0.json"
),
"recid": "71114",
"date_published": "2024",
"experiment": ["ALICE"],
"publisher": "CERN Open Data Portal",
"title": "File modified",
"type": {
"primary": "Dataset",
"secondary": ["Derived"],
},
"files": [
{"checksum": "adler32:9719fd6a", "size": 1053, "uri": "root://foo/bar"}
],
}
# Let's make sure that a new update still works. This used to fail because the first update
# was removing the bucket
record = update_record(pid, data3, False)
record.commit()

0 comments on commit f7fcde6

Please sign in to comment.