Group strict-mode and non-strict-mode tests for Item::fromJSON()

This commit is contained in:
Dan Stillman 2020-02-09 12:44:40 -05:00
parent b41734924d
commit 11caa1b719

View file

@ -1754,18 +1754,6 @@ describe("Zotero.Item", function () {
assert.isFalse(item.inPublications);
});
it("should handle Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
extra: "Here's some extra text"
};
var item = new Zotero.Item();
item.fromJSON(json);
assert.equal(item.getField('extra'), json.extra);
});
// Not currently following this behavior
/*it("should move valid field in Extra to field if not set", function () {
var doi = '10.1234/abcd';
@ -1822,132 +1810,147 @@ describe("Zotero.Item", function () {
assert.equal(item.getField('extra'), json.extra);
});
it("should store unknown fields in Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
fooBar: "123",
testField: "test value"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo Bar: 123\nTest Field: test value');
describe("not-strict mode", function () {
it("should handle Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
extra: "Here's some extra text"
};
var item = new Zotero.Item();
item.fromJSON(json);
assert.equal(item.getField('extra'), json.extra);
});
it("should store unknown fields in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
fooBar: "123",
testField: "test value"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo Bar: 123\nTest Field: test value');
});
it("should replace unknown field in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
foo: "BBB",
extra: "Foo: AAA\nBar: CCC"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo: BBB\nBar: CCC');
});
it("should store invalid-for-type field in Extra", function () {
var json = {
itemType: "journalArticle",
title: "Test",
medium: "123"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Medium: 123');
});
it("should ignore invalid-for-type base-mapped field if valid-for-type base field is set in Extra", function () {
var json = {
itemType: "document",
publisher: "Foo", // Valid for 'document'
company: "Bar" // Not valid for 'document', but mapped to base field 'publisher'
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('publisher'), 'Foo');
assert.equal(item.getField('extra'), '');
});
it("shouldn't include base field or invalid base-mapped field in Extra if valid base-mapped field is set", function () {
var json = {
itemType: "audioRecording",
publisher: "A", // Base field, which will be overwritten by the valid base-mapped field
label: "B", // Valid base-mapped field, which should be stored
company: "C", // Invalid base-mapped field, which should be ignored
foo: "D" // Invalid other field, which should be added to Extra
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('label'), 'B');
assert.equal(item.getField('extra'), 'Foo: D');
});
});
it("should replace unknown field in Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
foo: "BBB",
extra: "Foo: AAA\nBar: CCC"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Foo: BBB\nBar: CCC');
});
it("should store invalid-for-type field in Extra in non-strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
medium: "123"
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('title'), 'Test');
assert.equal(item.getField('extra'), 'Medium: 123');
});
it("should ignore invalid-for-type base-mapped field if valid-for-type base field is set in Extra in non-strict mode", function () {
var json = {
itemType: "document",
publisher: "Foo", // Valid for 'document'
company: "Bar" // Not invalid for 'document', but mapped to base field 'publisher'
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('publisher'), 'Foo');
assert.equal(item.getField('extra'), '');
});
it("shouldn't include base field or invalid base-mapped field in Extra if valid base-mapped field is set in non-strict mode", function () {
var json = {
itemType: "audioRecording",
publisher: "A", // Base field, which will be overwritten by the valid base-mapped field
label: "B", // Valid base-mapped field, which should be stored
company: "C", // Invalid base-mapped field, which should be ignored
foo: "D" // Invalid other field, which should be added to Extra
};
var item = new Zotero.Item;
item.fromJSON(json);
assert.equal(item.getField('label'), 'B');
assert.equal(item.getField('extra'), 'Foo: D');
});
it("should throw on unknown field in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
foo: "Bar"
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Unknown field/);
});
it("should throw on invalid field for a given item type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
numPages: "123"
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid field/);
});
it("should throw on unknown creator type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "unknown"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Unknown creator type/);
});
it("should throw on invalid creator type for a given item type in strict mode", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "interviewee"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid creator type/);
describe("strict mode", function () {
it("should throw on unknown field", function () {
var json = {
itemType: "journalArticle",
title: "Test",
foo: "Bar"
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Unknown field/);
});
it("should throw on invalid field for a given item type", function () {
var json = {
itemType: "journalArticle",
title: "Test",
numPages: "123"
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid field/);
});
it("should throw on unknown creator type", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "unknown"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Unknown creator type/);
});
it("should throw on invalid creator type for a given item type", function () {
var json = {
itemType: "journalArticle",
title: "Test",
creators: [
{
firstName: "First",
lastName: "Last",
creatorType: "interviewee"
}
]
};
var item = new Zotero.Item;
var f = () => {
item.fromJSON(json, { strict: true });
};
assert.throws(f, /^Invalid creator type/);
});
});
it("should accept ISO 8601 dates", function* () {