2019-07-30 09:35:42 +02:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
|
2018-10-30 02:17:50 +02:00
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
const time = require('@joplin/lib/time').default;
|
2020-12-01 20:05:24 +02:00
|
|
|
const { fileContentEqual, setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('./test-utils.js');
|
2020-11-07 17:59:37 +02:00
|
|
|
const Folder = require('@joplin/lib/models/Folder.js');
|
|
|
|
const Note = require('@joplin/lib/models/Note.js');
|
|
|
|
const BaseItem = require('@joplin/lib/models/BaseItem.js');
|
|
|
|
const Resource = require('@joplin/lib/models/Resource.js');
|
|
|
|
const BaseModel = require('@joplin/lib/BaseModel').default;
|
|
|
|
const shim = require('@joplin/lib/shim').default;
|
2018-10-30 02:17:50 +02:00
|
|
|
|
|
|
|
async function allItems() {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folders = await Folder.all();
|
|
|
|
const notes = await Note.all();
|
2018-10-30 02:17:50 +02:00
|
|
|
return folders.concat(notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('models_BaseItem', function() {
|
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-10-31 02:35:57 +02:00
|
|
|
// This is to handle the case where a property is removed from a BaseItem table - in that case files in
|
|
|
|
// the sync target will still have the old property but we don't need it locally.
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should ignore properties that are present in sync file but not in database when serialising', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = await Folder.save({ title: 'folder1' });
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2018-10-31 02:35:57 +02:00
|
|
|
let serialized = await Folder.serialize(folder);
|
2019-07-30 09:35:42 +02:00
|
|
|
serialized += '\nignore_me: true';
|
2018-10-31 02:35:57 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const unserialized = await Folder.unserialize(serialized);
|
2018-10-31 02:35:57 +02:00
|
|
|
|
|
|
|
expect('ignore_me' in unserialized).toBe(false);
|
2018-10-30 02:17:50 +02:00
|
|
|
}));
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not modify title when unserializing', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: '' });
|
|
|
|
const folder2 = await Folder.save({ title: 'folder1' });
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const serialized1 = await Folder.serialize(folder1);
|
|
|
|
const unserialized1 = await Folder.unserialize(serialized1);
|
2019-07-30 09:35:42 +02:00
|
|
|
|
|
|
|
expect(unserialized1.title).toBe(folder1.title);
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const serialized2 = await Folder.serialize(folder2);
|
|
|
|
const unserialized2 = await Folder.unserialize(serialized2);
|
2019-07-30 09:35:42 +02:00
|
|
|
|
|
|
|
expect(unserialized2.title).toBe(folder2.title);
|
2019-05-06 22:25:14 +02:00
|
|
|
}));
|
2018-10-30 02:17:50 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should correctly unserialize note timestamps', (async () => {
|
2020-03-15 23:53:49 +02:00
|
|
|
const folder = await Folder.save({ title: 'folder' });
|
|
|
|
const note = await Note.save({ title: 'note', parent_id: folder.id });
|
2020-03-15 14:07:01 +02:00
|
|
|
|
2020-03-15 23:53:49 +02:00
|
|
|
const serialized = await Note.serialize(note);
|
|
|
|
const unserialized = await Note.unserialize(serialized);
|
2020-03-15 14:07:01 +02:00
|
|
|
|
|
|
|
expect(unserialized.created_time).toEqual(note.created_time);
|
|
|
|
expect(unserialized.updated_time).toEqual(note.updated_time);
|
|
|
|
expect(unserialized.user_created_time).toEqual(note.user_created_time);
|
|
|
|
expect(unserialized.user_updated_time).toEqual(note.user_updated_time);
|
|
|
|
}));
|
2020-04-30 17:56:47 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should serialize geolocation fields', (async () => {
|
2020-04-30 17:56:47 +02:00
|
|
|
const folder = await Folder.save({ title: 'folder' });
|
|
|
|
let note = await Note.save({ title: 'note', parent_id: folder.id });
|
|
|
|
note = await Note.load(note.id);
|
|
|
|
|
|
|
|
let serialized = await Note.serialize(note);
|
|
|
|
let unserialized = await Note.unserialize(serialized);
|
|
|
|
|
|
|
|
expect(unserialized.latitude).toEqual('0.00000000');
|
|
|
|
expect(unserialized.longitude).toEqual('0.00000000');
|
|
|
|
expect(unserialized.altitude).toEqual('0.0000');
|
|
|
|
|
|
|
|
await Note.updateGeolocation(note.id);
|
|
|
|
note = await Note.load(note.id);
|
|
|
|
|
|
|
|
serialized = await Note.serialize(note);
|
|
|
|
unserialized = await Note.unserialize(serialized);
|
|
|
|
|
|
|
|
expect(unserialized.latitude).toEqual(note.latitude);
|
|
|
|
expect(unserialized.longitude).toEqual(note.longitude);
|
|
|
|
expect(unserialized.altitude).toEqual(note.altitude);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should serialize and unserialize notes', (async () => {
|
2020-04-30 17:56:47 +02:00
|
|
|
const folder = await Folder.save({ title: 'folder' });
|
|
|
|
const note = await Note.save({ title: 'note', parent_id: folder.id });
|
|
|
|
await Note.updateGeolocation(note.id);
|
|
|
|
|
|
|
|
const noteBefore = await Note.load(note.id);
|
|
|
|
const serialized = await Note.serialize(noteBefore);
|
|
|
|
const noteAfter = await Note.unserialize(serialized);
|
|
|
|
|
|
|
|
expect(noteAfter).toEqual(noteBefore);
|
|
|
|
}));
|
2020-10-28 19:21:23 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should serialize and unserialize properties that contain new lines', (async () => {
|
2020-11-23 18:25:57 +02:00
|
|
|
const sourceUrl = `
|
|
|
|
https://joplinapp.org/ \\n
|
|
|
|
`;
|
|
|
|
|
|
|
|
const note = await Note.save({ title: 'note', source_url: sourceUrl });
|
2020-10-28 19:21:23 +02:00
|
|
|
|
|
|
|
const noteBefore = await Note.load(note.id);
|
|
|
|
const serialized = await Note.serialize(noteBefore);
|
|
|
|
const noteAfter = await Note.unserialize(serialized);
|
|
|
|
|
|
|
|
expect(noteAfter).toEqual(noteBefore);
|
|
|
|
}));
|
2020-11-23 18:25:57 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not serialize the note title and body', (async () => {
|
2020-11-23 18:25:57 +02:00
|
|
|
const note = await Note.save({ title: 'my note', body: `one line
|
|
|
|
two line
|
|
|
|
three line \\n no escape` });
|
|
|
|
|
|
|
|
const noteBefore = await Note.load(note.id);
|
|
|
|
const serialized = await Note.serialize(noteBefore);
|
|
|
|
expect(serialized.indexOf(`my note
|
|
|
|
|
|
|
|
one line
|
|
|
|
two line
|
|
|
|
three line \\n no escape`)).toBe(0);
|
|
|
|
}));
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|