Skip to content

Commit

Permalink
Add tests for MissingValueCode model & collection
Browse files Browse the repository at this point in the history
Issue #612
  • Loading branch information
robyngit committed Jul 21, 2023
1 parent 95ac205 commit 8d4bfc0
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 30 deletions.
13 changes: 9 additions & 4 deletions src/js/collections/metadata/eml/EMLMissingValueCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ define(["backbone", "models/metadata/eml211/EMLMissingValueCode"], function (
if (!objectDOM) return;
const $objectDOM = $(objectDOM);

// Get all of the missingValueCode nodes
const nodeName = "missingvaluecode";
const nodes = $objectDOM.filter(nodeName);
// Loop through each missingValueCode node
const opts = { parse: true };
for (var i = 0; i < $objectDOM.length; i++) {
const missingValueCodeNode = $objectDOM[i];
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
// Create a new missingValueCode model & add it to the collection
const attrs = { objectDOM: missingValueCodeNode };
const attrs = { objectDOM: node };
const missingValueCode = new EMLMissingValueCode(attrs, opts);
collection.add(missingValueCode);
}
Expand Down Expand Up @@ -71,7 +74,9 @@ define(["backbone", "models/metadata/eml211/EMLMissingValueCode"], function (
errors.push(model.validationError);
}
});
return errors.length ? errors : null;
// return errors.length ? errors : null;
// For now, if there is at least one error, just return the first one
return errors.length ? errors[0] : null;
},
}
);
Expand Down
41 changes: 15 additions & 26 deletions src/js/models/metadata/eml211/EMLMissingValueCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ define(["backbone"], function (Backbone) {
* @return {string} The XML string
*/
serialize: function () {
const xml = this.updateDOM().outerHTML;
const nodes = this.get("nodeOrder");
let xml = this.updateDOM().outerHTML;
const elNames = this.get("nodeOrder");
elNames.push(this.get("type"));
// replace lowercase node names with camelCase
nodes.forEach((node) => {
xml.replace(`<${node.toLowerCase()}>`, `<${node}>`);
xml.replace(`</${node.toLowerCase()}>`, `</${node}>`);
elNames.forEach((elName) => {
let elNameLower = elName.toLowerCase();
xml = xml.replace(`<${elNameLower}>`, `<${elName}>`);
xml = xml.replace(`</${elNameLower}>`, `</${elName}>`);
});
return xml;
},
Expand Down Expand Up @@ -91,7 +93,6 @@ define(["backbone"], function (Backbone) {
} else {
return $objectDOM[0];
}

},

/**
Expand All @@ -104,39 +105,27 @@ define(["backbone"], function (Backbone) {

/**
* Validate the model attributes
* @return {object} The validation errors, if any
* @return {object|undefined} The validation errors, if any
*/
validate: function () {
validate() {
if (this.isEmpty()) return undefined;

const errors = [];
const errors = {};

// Need a code and an explanation. Both must be non-empty strings.
let code = this.get("code");
let codeExplanation = this.get("codeExplanation");
if (
!code ||
!codeExplanation ||
typeof code !== "string" ||
typeof codeExplanation !== "string"
) {
errors.missingValueCode =
"Missing value code and explanation are required.";
return errors;
}
code = code.trim();
codeExplanation = codeExplanation.trim();
let code = this.get("code")?.trim();
let codeExplanation = this.get("codeExplanation")?.trim();

this.set("code", code);
this.set("codeExplanation", codeExplanation);

// Code must be a non-empty string
if (!code || !codeExplanation) {
errors.missingValueCode =
"Missing value code and explanation are required.";
"Both a missing value code and explanation are required.";
return errors;
}

return errors.length > 0 ? errors : undefined;
return Object.keys(errors).length > 0 ? errors : undefined;
},
}
);
Expand Down
2 changes: 2 additions & 0 deletions test/config/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"./js/specs/unit/models/metadata/eml211/EMLOtherEntity.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLParty.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLTemporalCoverage.spec.js",
"./js/specs/unit/collections/metadata/eml/EMLMissingValueCodes.spec.js",
"./js/specs/unit/models/metadata/eml211/EMLMissingValueCode.spec.js",
"./js/specs/unit/models/maps/assets/CesiumImagery.spec.js",
"./js/specs/unit/collections/maps/Geohashes.spec.js",
"./js/specs/unit/models/connectors/Filters-Map.spec.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
define([
"../../../../../../../../src/js/collections/metadata/eml/EMLMissingValueCodes",
], function (EMLMissingValueCodes) {
// Configure the Chai assertion library
var should = chai.should();
var expect = chai.expect;

describe("EMLMissingValueCodes Test Suite", function () {
/* Set up */
beforeEach(function () {
this.emlMissingValueCodes = new EMLMissingValueCodes();
});

/* Tear down */
afterEach(function () {
delete this.emlMissingValueCodes;
});

describe("Initialization", function () {
it("should create a EMLMissingValueCodes instance", function () {
new EMLMissingValueCodes().should.be.instanceof(EMLMissingValueCodes);
});
});

describe("Parsing", function () {
it("should parse an EMLMissingValueCodes from XML", function () {
var xmlString = `<missingValueCode>
<code>9999</code>
<codeExplanation>Sensor down</codeExplanation>
</missingValueCode>
<missingValueCode>
<code>9998</code>
<codeExplanation>Technician error</codeExplanation>
</missingValueCode>`;

this.emlMissingValueCodes.parse(xmlString);

this.emlMissingValueCodes.length.should.equal(2);
this.emlMissingValueCodes.at(0).get("code").should.equal("9999");
this.emlMissingValueCodes
.at(0)
.get("codeExplanation")
.should.equal("Sensor down");
this.emlMissingValueCodes.at(1).get("code").should.equal("9998");
this.emlMissingValueCodes
.at(1)
.get("codeExplanation")
.should.equal("Technician error");

});
});

describe("Validation", function () {
it("should validate valid EMLMissingValueCodes", function () {
this.emlMissingValueCodes.add({
code: "9999",
codeExplanation: "Sensor down",
})
var errors = this.emlMissingValueCodes.validate();

expect(errors).to.be.null;
});

it("should validate invalid EMLMissingValueCodes", function () {

this.emlMissingValueCodes.add({
code: "",
codeExplanation: "Sensor down",
})

var errors = this.emlMissingValueCodes.validate();

errors.should.be.an("object");
errors.should.have.property("missingValueCode");
errors.missingValueCode.should.be.a("string");

});
});

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
define([
"../../../../../../../../src/js/models/metadata/eml211/EMLMissingValueCode",
], function (EMLMissingValueCode) {
// Configure the Chai assertion library
var should = chai.should();
var expect = chai.expect;

describe("EMLMissingValueCode Test Suite", function () {
/* Set up */
beforeEach(function () {
this.emlMissingValueCode = new EMLMissingValueCode();
});

/* Tear down */
afterEach(function () {
delete this.emlMissingValueCode;
});

describe("Initialization", function () {
it("should create a EMLMissingValueCode instance", function () {
new EMLMissingValueCode().should.be.instanceof(EMLMissingValueCode);
});
});

describe("Parsing", function () {
it("should parse an EMLMissingValueCode from XML", function () {
var xmlString =
"<missingValueCode><code>9999</code><codeExplanation>Missing value</codeExplanation></missingValueCode>";

var emlMissingValueCode = new EMLMissingValueCode(
{
objectDOM: xmlString,
},
{ parse: true }
);
emlMissingValueCode.get("code").should.equal("9999");
emlMissingValueCode
.get("codeExplanation")
.should.equal("Missing value");
});
});

describe("Serializing", function () {
it("should serialize the EMLMissingValueCode to XML", function () {
var emlMissingValueCode = new EMLMissingValueCode({
code: "9999",
codeExplanation: "Missing value",
});
var xmlString = emlMissingValueCode.serialize();
xmlString.should.be.a("string");
xmlString.should.equal(
"<missingValueCode><code>9999</code><codeExplanation>Missing value</codeExplanation></missingValueCode>"
);
});
});

describe("Validation", function () {
it("should validate a valid EMLMissingValueCode", function () {
var emlMissingValueCode = new EMLMissingValueCode({
code: "9999",
codeExplanation: "Missing value",
});
var errors = emlMissingValueCode.validate();
expect(errors).to.be.undefined;
});

it("should not validate an invalid EMLMissingValueCode", function () {
var emlMissingValueCode = new EMLMissingValueCode({
code: "-999",
codeExplanation: "",
});
var errors = emlMissingValueCode.validate();
expect(errors).to.be.an("object");
expect(errors.missingValueCode).to.be.a("string");
});
});
});
});

0 comments on commit 8d4bfc0

Please sign in to comment.