Skip to content

Commit

Permalink
Add EMLAnnotations validation unit tests + fix
Browse files Browse the repository at this point in the history
- Fix the findDuplicates method to return the correct result

Issue #2542
  • Loading branch information
robyngit committed Nov 4, 2024
1 parent 6ea1434 commit 8368b09
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/js/collections/metadata/eml/EMLAnnotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,27 @@ define(["underscore", "backbone", "models/metadata/eml211/EMLAnnotation"], (
* @since 0.0.0
*/
getDuplicates() {
const duplicates = [];
this.forEach((annotation) => {
const propertyURI = annotation.get("propertyURI");
const valueURI = annotation.get("valueURI");
const propertyLabel = annotation.get("propertyLabel");
const valueLabel = annotation.get("valueLabel");

const found = this.filter(
(a) =>
a.get("propertyURI") === propertyURI &&
a.get("valueURI") === valueURI &&
a.get("propertyLabel") === propertyLabel &&
a.get("valueLabel") === valueLabel &&
a.id !== annotation.id,
);

if (found.length) {
duplicates.push(...found);
const groups = {};
let duplicates = [];

// Group models by their serialized attributes
this.each((model) => {
// Serialize the model's attributes to create a unique key
const key = JSON.stringify(model.attributes);

// Group models by the serialized key
if (groups[key]) {
groups[key].push(model);
} else {
groups[key] = [model];
}
});

// Identify duplicates in each group
Object.values(groups).forEach((group) => {
if (group.length > 1) {
// Add all but one model from each group to the duplicates array
duplicates = duplicates.concat(group.slice(1));
}
});

Expand Down
33 changes: 33 additions & 0 deletions test/js/specs/unit/collections/metadata/eml/EMLAnnotations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,38 @@ define([
state.annotations.updateCanonicalDataset("http://example.com");
state.annotations.length.should.equal(3);
});

it("returns null if there are no validation errors", () => {
const errors = state.annotations.validate();
expect(errors).to.be.null;
});

it("shows validation errors if canonical dataset is not a valid URI", () => {
state.annotations.addCanonicalDatasetAnnotation("not-a-valid-uri");
const errors = state.annotations.validate();
expect(errors).to.be.an("array");
expect(errors.length).to.equal(1);
expect(errors[0].attr).to.equal("canonicalDataset");
});

it("removes duplicates during validation", () => {
state.annotations.add([
{
propertyLabel: "Property Label",
propertyURI: "http://example.com/property",
valueLabel: "Value Label",
valueURI: "http://example.com/value",
},
{
propertyLabel: "Property Label",
propertyURI: "http://example.com/property",
valueLabel: "Value Label",
valueURI: "http://example.com/value",
},
]);
const errors = state.annotations.validate();
expect(errors).to.be.null;
expect(state.annotations.length).to.equal(1);
});
});
});

0 comments on commit 8368b09

Please sign in to comment.