2015-05-19 05:05:05 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.DataObjectUtilities", function() {
|
2015-10-29 07:03:25 +00:00
|
|
|
describe("#patch()", function () {
|
|
|
|
it("should omit 'collections' if it doesn't exist", function* () {
|
|
|
|
var patchBase = {
|
|
|
|
collections: ['AAAAAAAA']
|
|
|
|
};
|
|
|
|
var obj = {};
|
|
|
|
obj = Zotero.DataObjectUtilities.patch(patchBase, obj);
|
|
|
|
assert.notProperty(obj, 'collections');
|
|
|
|
})
|
2018-04-22 21:27:33 +00:00
|
|
|
|
|
|
|
it("should include modified 'conditions'", function* () {
|
|
|
|
var patchBase = {
|
|
|
|
name: "Search",
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'is',
|
|
|
|
value: 'A'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
condition: 'language',
|
|
|
|
operator: 'is',
|
|
|
|
value: 'en'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var obj = {
|
|
|
|
name: "Search",
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'is',
|
|
|
|
value: 'B'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
condition: 'language',
|
|
|
|
operator: 'is',
|
|
|
|
value: 'en'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
obj = Zotero.DataObjectUtilities.patch(patchBase, obj);
|
|
|
|
assert.property(obj, 'conditions');
|
|
|
|
assert.equal(obj.conditions[0].value, 'B');
|
|
|
|
assert.equal(obj.conditions[1].value, 'en');
|
|
|
|
})
|
Type/field handling overhaul
This changes the way item types, item fields, creator types, and CSL
mappings are defined and handled, in preparation for updated types and
fields.
Instead of being predefined in SQL files or code, type/field info is
read from a bundled JSON file shared with other parts of the Zotero
ecosystem [1], referred to as the "global schema". Updates to the
bundled schema file are automatically applied to the database at first
run, allowing changes to be made consistently across apps.
When syncing, invalid JSON properties are now rejected instead of being
ignored and processed later, which will allow for schema changes to be
made without causing problems in existing clients. We considered many
alternative approaches, but this approach is by far the simplest,
safest, and most transparent to the user.
For now, there are no actual changes to types and fields, since we'll
first need to do a sync cut-off for earlier versions that don't reject
invalid properties.
For third-party code, the main change is that type and field IDs should
no longer be hard-coded, since they may not be consistent in new
installs. For example, code should use `Zotero.ItemTypes.getID('note')`
instead of hard-coding `1`.
[1] https://github.com/zotero/zotero-schema
2019-05-16 08:56:46 +00:00
|
|
|
|
2019-10-24 05:02:45 +00:00
|
|
|
it("should blank out deleted properties", function () {
|
Type/field handling overhaul
This changes the way item types, item fields, creator types, and CSL
mappings are defined and handled, in preparation for updated types and
fields.
Instead of being predefined in SQL files or code, type/field info is
read from a bundled JSON file shared with other parts of the Zotero
ecosystem [1], referred to as the "global schema". Updates to the
bundled schema file are automatically applied to the database at first
run, allowing changes to be made consistently across apps.
When syncing, invalid JSON properties are now rejected instead of being
ignored and processed later, which will allow for schema changes to be
made without causing problems in existing clients. We considered many
alternative approaches, but this approach is by far the simplest,
safest, and most transparent to the user.
For now, there are no actual changes to types and fields, since we'll
first need to do a sync cut-off for earlier versions that don't reject
invalid properties.
For third-party code, the main change is that type and field IDs should
no longer be hard-coded, since they may not be consistent in new
installs. For example, code should use `Zotero.ItemTypes.getID('note')`
instead of hard-coding `1`.
[1] https://github.com/zotero/zotero-schema
2019-05-16 08:56:46 +00:00
|
|
|
var patchBase = {
|
2019-10-24 05:02:45 +00:00
|
|
|
title: 'Test',
|
|
|
|
place: ''
|
Type/field handling overhaul
This changes the way item types, item fields, creator types, and CSL
mappings are defined and handled, in preparation for updated types and
fields.
Instead of being predefined in SQL files or code, type/field info is
read from a bundled JSON file shared with other parts of the Zotero
ecosystem [1], referred to as the "global schema". Updates to the
bundled schema file are automatically applied to the database at first
run, allowing changes to be made consistently across apps.
When syncing, invalid JSON properties are now rejected instead of being
ignored and processed later, which will allow for schema changes to be
made without causing problems in existing clients. We considered many
alternative approaches, but this approach is by far the simplest,
safest, and most transparent to the user.
For now, there are no actual changes to types and fields, since we'll
first need to do a sync cut-off for earlier versions that don't reject
invalid properties.
For third-party code, the main change is that type and field IDs should
no longer be hard-coded, since they may not be consistent in new
installs. For example, code should use `Zotero.ItemTypes.getID('note')`
instead of hard-coding `1`.
[1] https://github.com/zotero/zotero-schema
2019-05-16 08:56:46 +00:00
|
|
|
};
|
|
|
|
var obj = {};
|
|
|
|
obj = Zotero.DataObjectUtilities.patch(patchBase, obj);
|
2019-10-24 05:02:45 +00:00
|
|
|
assert.propertyVal(obj, 'title', '');
|
|
|
|
// place was already empty, so it shouldn't be included
|
|
|
|
assert.notProperty(obj, 'place');
|
Type/field handling overhaul
This changes the way item types, item fields, creator types, and CSL
mappings are defined and handled, in preparation for updated types and
fields.
Instead of being predefined in SQL files or code, type/field info is
read from a bundled JSON file shared with other parts of the Zotero
ecosystem [1], referred to as the "global schema". Updates to the
bundled schema file are automatically applied to the database at first
run, allowing changes to be made consistently across apps.
When syncing, invalid JSON properties are now rejected instead of being
ignored and processed later, which will allow for schema changes to be
made without causing problems in existing clients. We considered many
alternative approaches, but this approach is by far the simplest,
safest, and most transparent to the user.
For now, there are no actual changes to types and fields, since we'll
first need to do a sync cut-off for earlier versions that don't reject
invalid properties.
For third-party code, the main change is that type and field IDs should
no longer be hard-coded, since they may not be consistent in new
installs. For example, code should use `Zotero.ItemTypes.getID('note')`
instead of hard-coding `1`.
[1] https://github.com/zotero/zotero-schema
2019-05-16 08:56:46 +00:00
|
|
|
});
|
2022-08-22 05:11:11 +00:00
|
|
|
|
|
|
|
it("shouldn't include relations that haven't changed", function () {
|
|
|
|
var patchBase = {
|
|
|
|
title: "Old Title",
|
|
|
|
relations: {
|
|
|
|
"mendeleyDB:documentUUID": "6b97abe6-8e23-4471-b963-234cf26808b9"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var obj = {
|
|
|
|
title: "New Title",
|
|
|
|
relations: {
|
|
|
|
"mendeleyDB:documentUUID": "6b97abe6-8e23-4471-b963-234cf26808b9"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj = Zotero.DataObjectUtilities.patch(patchBase, obj);
|
|
|
|
assert.notProperty(obj, 'relations');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't include relations that only switched from string to array", function () {
|
|
|
|
var patchBase = {
|
|
|
|
title: "Old Title",
|
|
|
|
relations: {
|
|
|
|
"mendeleyDB:documentUUID": "6b97abe6-8e23-4471-b963-234cf26808b9"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var obj = {
|
|
|
|
title: "New Title",
|
|
|
|
relations: {
|
|
|
|
"mendeleyDB:documentUUID": [
|
|
|
|
"6b97abe6-8e23-4471-b963-234cf26808b9"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj = Zotero.DataObjectUtilities.patch(patchBase, obj);
|
|
|
|
assert.notProperty(obj, 'relations');
|
|
|
|
});
|
2015-10-29 07:03:25 +00:00
|
|
|
})
|
|
|
|
|
2015-05-19 05:05:05 +00:00
|
|
|
describe("#diff()", function () {
|
2015-07-20 06:10:23 +00:00
|
|
|
// This is mostly covered by syncLocal::_reconcileChanges() tests, but we test some
|
|
|
|
// additional things here
|
|
|
|
describe("items", function () {
|
|
|
|
//
|
|
|
|
// Fields
|
|
|
|
//
|
|
|
|
describe("fields", function () {
|
|
|
|
it("should not show empty items as different", function* () {
|
|
|
|
var id1, id2, json1, json2;
|
2020-07-05 10:20:01 +00:00
|
|
|
yield Zotero.DB.executeTransaction(async function () {
|
2015-07-20 06:10:23 +00:00
|
|
|
var item = new Zotero.Item('book');
|
2020-07-05 10:20:01 +00:00
|
|
|
id1 = await item.save();
|
Deasyncification :back: :cry:
While trying to get translation and citing working with asynchronously
generated data, we realized that drag-and-drop support was going to
be...problematic. Firefox only supports synchronous methods for
providing drag data (unlike, it seems, the DataTransferItem interface
supported by Chrome), which means that we'd need to preload all relevant
data on item selection (bounded by export.quickCopy.dragLimit) and keep
the translate/cite methods synchronous (or maintain two separate
versions).
What we're trying instead is doing what I said in #518 we weren't going
to do: loading most object data on startup and leaving many more
functions synchronous. Essentially, this takes the various load*()
methods described in #518, moves them to startup, and makes them operate
on entire libraries rather than individual objects.
The obvious downside here (other than undoing much of the work of the
last many months) is that it increases startup time, potentially quite a
lot for larger libraries. On my laptop, with a 3,000-item library, this
adds about 3 seconds to startup time. I haven't yet tested with larger
libraries. But I'm hoping that we can optimize this further to reduce
that delay. Among other things, this is loading data for all libraries,
when it should be able to load data only for the library being viewed.
But this is also fundamentally just doing some SELECT queries and
storing the results, so it really shouldn't need to be that slow (though
performance may be bounded a bit here by XPCOM overhead).
If we can make this fast enough, it means that third-party plugins
should be able to remain much closer to their current designs. (Some
things, including saving, will still need to be made asynchronous.)
2016-03-07 21:05:51 +00:00
|
|
|
json1 = item.toJSON();
|
2015-07-20 06:10:23 +00:00
|
|
|
|
|
|
|
var item = new Zotero.Item('book');
|
2020-07-05 10:20:01 +00:00
|
|
|
id2 = await item.save();
|
Deasyncification :back: :cry:
While trying to get translation and citing working with asynchronously
generated data, we realized that drag-and-drop support was going to
be...problematic. Firefox only supports synchronous methods for
providing drag data (unlike, it seems, the DataTransferItem interface
supported by Chrome), which means that we'd need to preload all relevant
data on item selection (bounded by export.quickCopy.dragLimit) and keep
the translate/cite methods synchronous (or maintain two separate
versions).
What we're trying instead is doing what I said in #518 we weren't going
to do: loading most object data on startup and leaving many more
functions synchronous. Essentially, this takes the various load*()
methods described in #518, moves them to startup, and makes them operate
on entire libraries rather than individual objects.
The obvious downside here (other than undoing much of the work of the
last many months) is that it increases startup time, potentially quite a
lot for larger libraries. On my laptop, with a 3,000-item library, this
adds about 3 seconds to startup time. I haven't yet tested with larger
libraries. But I'm hoping that we can optimize this further to reduce
that delay. Among other things, this is loading data for all libraries,
when it should be able to load data only for the library being viewed.
But this is also fundamentally just doing some SELECT queries and
storing the results, so it really shouldn't need to be that slow (though
performance may be bounded a bit here by XPCOM overhead).
If we can make this fast enough, it means that third-party plugins
should be able to remain much closer to their current designs. (Some
things, including saving, will still need to be made asynchronous.)
2016-03-07 21:05:51 +00:00
|
|
|
json2 = item.toJSON();
|
2015-07-20 06:10:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
2015-05-20 07:41:22 +00:00
|
|
|
|
2016-01-17 21:55:34 +00:00
|
|
|
yield Zotero.Items.erase([id1, id2]);
|
2015-07-20 06:10:23 +00:00
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
it("should not show empty strings as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
title: ""
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
title: ""
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
it("should not show empty string and undefined as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
title: ""
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
place: ""
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
//
|
|
|
|
// Creators
|
|
|
|
//
|
|
|
|
describe("creators", function () {
|
|
|
|
it("should not show identical creators as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
creators: [
|
|
|
|
{
|
|
|
|
name: "Center for History and New Media",
|
|
|
|
creatorType: "author"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
creators: [
|
|
|
|
{
|
|
|
|
creatorType: "author",
|
|
|
|
name: "Center for History and New Media"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not show an empty creators array and a missing one as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
creators: []
|
|
|
|
};
|
|
|
|
var json2 = {};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
|
|
|
|
var json1 = {};
|
|
|
|
var json2 = {
|
|
|
|
creators: []
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
|
2016-05-17 06:33:53 +00:00
|
|
|
describe("notes", function () {
|
|
|
|
it("should ignore sanitization changes", function* () {
|
|
|
|
var json1 = {
|
2016-05-17 06:44:00 +00:00
|
|
|
note: "<p>\u00a0</p>"
|
2016-05-17 06:33:53 +00:00
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
note: "<p> </p>"
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
//
|
|
|
|
// Relations
|
|
|
|
//
|
|
|
|
describe("relations", function () {
|
|
|
|
it("should not show an empty relations object and a missing one as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
relations: {}
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
|
|
|
|
var json1 = {};
|
|
|
|
var json2 = {
|
|
|
|
relations: {}
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
})
|
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
//
|
|
|
|
// Tags
|
|
|
|
//
|
|
|
|
describe("tags", function () {
|
|
|
|
it("should not show manual tags with or without 'type' property as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "Foo"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "Foo",
|
|
|
|
type: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
|
2015-07-20 06:10:23 +00:00
|
|
|
it("should show tags of different types as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "Foo"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "Foo",
|
|
|
|
type: 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
assert.sameDeepMembers(
|
|
|
|
changes,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
field: "tags",
|
|
|
|
op: "member-remove",
|
|
|
|
value: {
|
|
|
|
tag: "Foo"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "tags",
|
|
|
|
op: "member-add",
|
|
|
|
value: {
|
|
|
|
tag: "Foo",
|
|
|
|
type: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
})
|
2015-05-20 07:41:22 +00:00
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
|
|
|
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
2015-07-20 06:10:23 +00:00
|
|
|
// Searches
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
|
|
|
//
|
2015-07-20 06:10:23 +00:00
|
|
|
// Search conditions
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
2015-07-20 06:10:23 +00:00
|
|
|
describe("searches", function () {
|
|
|
|
describe("conditions", function () {
|
|
|
|
it("should not show an empty conditions object and a missing one as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
conditions: {}
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
|
|
|
|
var json1 = {};
|
|
|
|
var json2 = {
|
|
|
|
conditions: {}
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
/*it("should not show an empty conditions object and a missing one as different", function () {
|
|
|
|
var json1 = {
|
|
|
|
conditions: []
|
|
|
|
};
|
|
|
|
var json2 = {
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: 'title',
|
|
|
|
operator: 'contains',
|
|
|
|
value: 'test'
|
2015-05-20 07:41:22 +00:00
|
|
|
}
|
2015-07-20 06:10:23 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
|
|
|
|
var json1 = {};
|
|
|
|
var json2 = {
|
|
|
|
conditions: {}
|
|
|
|
};
|
|
|
|
var changes = Zotero.DataObjectUtilities.diff(json1, json2);
|
|
|
|
Zotero.debug(changes);
|
|
|
|
assert.lengthOf(changes, 0);
|
|
|
|
})*/
|
2015-05-20 07:41:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
describe("#applyChanges()", function () {
|
|
|
|
//
|
|
|
|
// Fields
|
|
|
|
//
|
|
|
|
describe("fields", function () {
|
|
|
|
it("should set added/modified field values", function () {
|
|
|
|
var json = {
|
|
|
|
title: "A"
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "title",
|
|
|
|
op: "add",
|
|
|
|
value: "B"
|
|
|
|
},
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "date",
|
|
|
|
op: "modify",
|
|
|
|
value: "2015-05-19"
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.equal(json.title, "B");
|
|
|
|
assert.equal(json.date, "2015-05-19");
|
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
|
|
|
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
|
|
|
// Collections
|
|
|
|
//
|
|
|
|
describe("collections", function () {
|
|
|
|
it("should add a collection", function () {
|
|
|
|
var json = {
|
|
|
|
collections: ["AAAAAAAA"]
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "collections",
|
|
|
|
op: "member-add",
|
|
|
|
value: "BBBBBBBB"
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameMembers(json.collections, ["AAAAAAAA", "BBBBBBBB"]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not duplicate an existing collection", function () {
|
|
|
|
var json = {
|
|
|
|
collections: ["AAAAAAAA"]
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "collections",
|
|
|
|
op: "member-add",
|
|
|
|
value: "AAAAAAAA"
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameMembers(json.collections, ["AAAAAAAA"]);
|
|
|
|
assert.lengthOf(json.collections, 1);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove a collection", function () {
|
|
|
|
var json = {
|
|
|
|
collections: ["AAAAAAAA"]
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "collections",
|
|
|
|
op: "member-remove",
|
|
|
|
value: "AAAAAAAA"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.lengthOf(json.collections, 0);
|
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
|
|
|
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
|
|
|
// Relations
|
|
|
|
//
|
|
|
|
describe("relations", function () {
|
|
|
|
it("should add a predicate and object to an empty relations object", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {}
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-add",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ["A"] }]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should add a predicate and object to a missing relations object", function () {
|
|
|
|
var json = {};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-add",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ["A"] }]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should add an object to an existing predicate string", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: 'A1'
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-add",
|
2015-05-19 05:05:05 +00:00
|
|
|
value: {
|
2015-05-20 07:41:22 +00:00
|
|
|
key: "a",
|
|
|
|
value: "A2"
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ["A1", "A2"] }]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should add an object to an existing predicate array", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: ['A1']
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-add",
|
2015-05-19 05:05:05 +00:00
|
|
|
value: {
|
2015-05-20 07:41:22 +00:00
|
|
|
key: "a",
|
|
|
|
value: "A2"
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ['A1', 'A2'] }]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should ignore a removal for an missing relations object", function () {
|
|
|
|
var json = {};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.notProperty(json, 'relations');
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should ignore a removal for a missing relations predicate", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {}
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.lengthOf(Object.keys(json.relations), 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should ignore a removal for a missing object", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: ['A1']
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ['A1'] }]);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove a predicate and object string from a relations object", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: "A"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.lengthOf(Object.keys(json.relations), 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove a predicate and object array from a relations object", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: ["A"]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.lengthOf(Object.keys(json.relations), 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove an object from an existing predicate array", function () {
|
|
|
|
var json = {
|
|
|
|
relations: {
|
|
|
|
a: ['A1', 'A2']
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "relations",
|
|
|
|
op: "property-member-remove",
|
|
|
|
value: {
|
|
|
|
key: "a",
|
|
|
|
value: "A2"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers([json.relations], [{ a: ["A1"] }]);
|
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
|
|
|
|
2015-05-20 07:41:22 +00:00
|
|
|
//
|
|
|
|
// Tags
|
|
|
|
//
|
|
|
|
describe("tags", function () {
|
|
|
|
it("should add a tag", function () {
|
|
|
|
var json = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = [
|
2015-05-19 05:05:05 +00:00
|
|
|
{
|
2015-05-20 07:41:22 +00:00
|
|
|
field: "tags",
|
|
|
|
op: "member-add",
|
|
|
|
value: {
|
|
|
|
tag: "B"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers(
|
|
|
|
json.tags,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: "B"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should not duplicate an existing tag", function () {
|
|
|
|
var json = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "tags",
|
|
|
|
op: "member-add",
|
|
|
|
value: {
|
|
|
|
tag: "A"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers(
|
|
|
|
json.tags,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
assert.lengthOf(json.tags, 1);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove a tag", function () {
|
|
|
|
var json = {
|
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "tags",
|
|
|
|
op: "member-remove",
|
|
|
|
value: {
|
|
|
|
tag: "A"
|
|
|
|
}
|
2015-05-19 05:05:05 +00:00
|
|
|
}
|
2015-05-20 07:41:22 +00:00
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.lengthOf(json.tags, 0);
|
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
2015-07-20 06:10:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Search conditions
|
|
|
|
//
|
|
|
|
describe("conditions", function () {
|
|
|
|
it("should add a condition", function () {
|
|
|
|
var json = {
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "conditions",
|
|
|
|
op: "member-add",
|
|
|
|
value: {
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "B"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers(
|
|
|
|
json.conditions,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "B"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should remove a condition", function () {
|
|
|
|
var json = {
|
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "A"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "B"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
var changes = [
|
|
|
|
{
|
|
|
|
field: "conditions",
|
|
|
|
op: "member-remove",
|
|
|
|
value: {
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "B"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
Zotero.DataObjectUtilities.applyChanges(json, changes);
|
|
|
|
assert.sameDeepMembers(
|
|
|
|
json.conditions,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
condition: "title",
|
|
|
|
op: "contains",
|
|
|
|
value: "A"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
})
|
|
|
|
})
|
2015-05-19 05:05:05 +00:00
|
|
|
})
|
|
|
|
})
|