1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
This commit is contained in:
Laurent Cozic 2017-06-14 20:59:02 +01:00
parent 226353ecb7
commit a0400a5e38
5 changed files with 215 additions and 51 deletions

View File

@ -15,42 +15,54 @@ import { sprintf } from 'sprintf-js';
import { _ } from 'src/locale.js';
import { NoteFolderService } from 'src/services/note-folder-service.js';
const vorpal = require('vorpal')();
// import { ItemSyncTime } from 'src/models/item-sync-time.js';
let db = new Database(new DatabaseDriverNode());
db.setDebugEnabled(false);
// const vorpal = require('vorpal')();
// let fileDriver = new FileApiDriverLocal();
// let fileApi = new FileApi('/home/laurent/Temp/TestImport', fileDriver);
// let db = new Database(new DatabaseDriverNode());
// db.setDebugEnabled(false);
// db.open({ name: '/home/laurent/Temp/test.sqlite3' }).then(() => {
// BaseModel.db_ = db;
// return ItemSyncTime.setTime(123, 789);
// }).then((r) => {
// console.info(r);
// }).catch((error) => {
// console.error(error);
// });
// // let fileDriver = new FileApiDriverLocal();
// // let fileApi = new FileApi('/home/laurent/Temp/TestImport', fileDriver);
// // let synchronizer = new Synchronizer(db, fileApi);
// let fileDriver = new FileApiDriverMemory();
// let fileApi = new FileApi('/root', fileDriver);
// let synchronizer = new Synchronizer(db, fileApi);
let fileDriver = new FileApiDriverMemory();
let fileApi = new FileApi('/root', fileDriver);
let synchronizer = new Synchronizer(db, fileApi);
fileApi.mkdir('test').then(() => {
return fileApi.mkdir('test2');
}).then(() => {
return fileApi.put('test/un', 'abcd1111').then(fileApi.put('test/deux', 'abcd2222'));
}).then(() => {
return fileApi.list();
}).then((items) => {
//console.info(items);
}).then(() => {
return fileApi.delete('test/un');
}).then(() => {
return fileApi.get('test/deux').then((content) => { console.info(content); });
}).then(() => {
return fileApi.list('test', true);
}).then((items) => {
console.info(items);
}).catch((error) => {
console.error(error);
}).then(() => {
process.exit();
});
// fileApi.mkdir('test').then(() => {
// return fileApi.mkdir('test2');
// }).then(() => {
// return fileApi.put('test/un', 'abcd1111').then(fileApi.put('test/deux', 'abcd2222'));
// }).then(() => {
// return fileApi.list();
// }).then((items) => {
// //console.info(items);
// }).then(() => {
// return fileApi.delete('test/un');
// }).then(() => {
// return fileApi.get('test/deux').then((content) => { console.info(content); });
// }).then(() => {
// return fileApi.list('test', true);
// }).then((items) => {
// console.info(items);
// }).catch((error) => {
// console.error(error);
// }).then(() => {
// process.exit();
// });

View File

@ -2,7 +2,8 @@
CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
rm -f "$CLIENT_DIR/tests-build/src"
mkdir -p "$CLIENT_DIR/tests-build"
mkdir -p "$CLIENT_DIR/tests-build/data"
ln -s "$CLIENT_DIR/build/src" "$CLIENT_DIR/tests-build"
npm run build && NODE_PATH="$CLIENT_DIR/tests-build/" npm test tests-build/synchronizer.js
# npm run build && NODE_PATH="$CLIENT_DIR/tests-build/" npm test tests-build/synchronizer.js
npm run build && NODE_PATH="$CLIENT_DIR/tests-build/" npm test tests-build/services/note-folder-service.js

View File

@ -0,0 +1,89 @@
import { time } from 'src/time-utils.js';
import { Note } from 'src/models/note.js';
import { Folder } from 'src/models/folder.js';
import { promiseChain } from 'src/promise-chain.js';
import { NoteFolderService } from 'src/services/note-folder-service.js';
import { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi } from 'test-utils.js';
describe('NoteFolderServices', function() {
beforeEach(function(done) {
setupDatabaseAndSynchronizer(done);
});
function createNotes(parentId, id = 1) {
let notes = [];
if (id === 1) {
notes.push({ parent_id: parentId, title: 'note one', body: 'content of note one' });
notes.push({ parent_id: parentId, title: 'note two', body: 'content of note two' });
} else {
throw new Error('Invalid ID: ' + id);
}
let output = [];
let chain = [];
for (let i = 0; i < notes.length; i++) {
chain.push(() => {
return Note.save(notes[i]).then((note) => {
output.push(note);
return output;
});
});
}
return promiseChain(chain, []);
}
function createFolders(id = 1) {
let folders = [];
if (id === 1) {
folders.push({ title: 'myfolder1' });
folders.push({ title: 'myfolder2' });
folders.push({ title: 'myfolder3' });
} else {
throw new Error('Invalid ID: ' + id);
}
let output = [];
let chain = [];
for (let i = 0; i < folders.length; i++) {
chain.push(() => {
return Folder.save(folders[i]).then((folder) => {
output.push(folder);
return output;
});
});
}
return promiseChain(chain, []);
}
it('should retrieve sync items', function(done) {
createFolders().then((folders) => {
return createNotes(folders[0].id);
}).then(() => {
return NoteFolderService.itemsThatNeedSync().then((context) => {
expect(context.items.length).toBe(2);
expect(context.hasMore).toBe(true);
return context;
});
}).then((context) => {
return NoteFolderService.itemsThatNeedSync(context, 2).then((context) => {
expect(context.items.length).toBe(2);
expect(context.hasMore).toBe(true);
return context;
});
}).then((context) => {
return NoteFolderService.itemsThatNeedSync(context, 2).then((context) => {
expect(context.items.length).toBe(1);
expect(context.hasMore).toBe(false);
return context;
});
}).then(() => {
done();
}).catch((error) => {
console.error(error);
});
});
});

View File

@ -1,14 +1,8 @@
import { Synchronizer } from 'src/synchronizer.js';
import { FileApi } from 'src/file-api.js';
import { FileApiDriverMemory } from 'src/file-api-driver-memory.js';
import { time } from 'src/time-utils.js';
import { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi } from 'test-utils.js';
describe('Synchronizer syncActions', function() {
let fileDriver = new FileApiDriverMemory();
let fileApi = new FileApi('/root', fileDriver);
let synchronizer = new Synchronizer(null, fileApi);
// Note: set 1 matches set 1 of createRemoteItems()
function createLocalItems(id, updatedTime, lastSyncTime) {
let output = [];
@ -25,10 +19,10 @@ describe('Synchronizer syncActions', function() {
if (!updatedTime) updatedTime = time.unix();
if (id === 1) {
return fileApi.format()
.then(() => fileApi.mkdir('test'))
.then(() => fileApi.put('test/un', 'abcd'))
.then(() => fileApi.list('', true))
return fileApi().format()
.then(() => fileApi().mkdir('test'))
.then(() => fileApi().put('test/un', 'abcd'))
.then(() => fileApi().list('', true))
.then((items) => {
for (let i = 0; i < items.length; i++) {
items[i].updatedTime = updatedTime;
@ -40,11 +34,15 @@ describe('Synchronizer syncActions', function() {
}
}
beforeEach(function(done) {
setupDatabaseAndSynchronizer(done);
});
it('should create remote items', function() {
let localItems = createLocalItems(1, time.unix(), 0);
let remoteItems = [];
let actions = synchronizer.syncActions(localItems, remoteItems, []);
let actions = synchronizer().syncActions(localItems, remoteItems, []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -57,7 +55,7 @@ describe('Synchronizer syncActions', function() {
createRemoteItems(1).then((remoteItems) => {
let lastSyncTime = time.unix() + 1000;
let localItems = createLocalItems(1, lastSyncTime + 1000, lastSyncTime);
let actions = synchronizer.syncActions(localItems, remoteItems, []);
let actions = synchronizer().syncActions(localItems, remoteItems, []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -82,7 +80,7 @@ describe('Synchronizer syncActions', function() {
createRemoteItems(1).then((remoteItems) => {
let localItems = createLocalItems(1, time.unix() + 1000, time.unix() - 1000);
let actions = synchronizer.syncActions(localItems, remoteItems, []);
let actions = synchronizer().syncActions(localItems, remoteItems, []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -97,7 +95,7 @@ describe('Synchronizer syncActions', function() {
it('should create local file', function(done) {
createRemoteItems(1).then((remoteItems) => {
let localItems = [];
let actions = synchronizer.syncActions(localItems, remoteItems, []);
let actions = synchronizer().syncActions(localItems, remoteItems, []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -113,7 +111,7 @@ describe('Synchronizer syncActions', function() {
createRemoteItems(1).then((remoteItems) => {
let localItems = createLocalItems(1, time.unix(), time.unix());
let deletedItemPaths = [localItems[0].path, localItems[1].path];
let actions = synchronizer.syncActions([], remoteItems, deletedItemPaths);
let actions = synchronizer().syncActions([], remoteItems, deletedItemPaths);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -129,7 +127,7 @@ describe('Synchronizer syncActions', function() {
let lastSyncTime = time.unix();
createRemoteItems(1, lastSyncTime - 1000).then((remoteItems) => {
let localItems = createLocalItems(1, lastSyncTime - 1000, lastSyncTime);
let actions = synchronizer.syncActions(localItems, [], []);
let actions = synchronizer().syncActions(localItems, [], []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {
@ -145,7 +143,7 @@ describe('Synchronizer syncActions', function() {
let lastSyncTime = time.unix();
createRemoteItems(1, lastSyncTime + 1000).then((remoteItems) => {
let localItems = createLocalItems(1, lastSyncTime - 1000, lastSyncTime);
let actions = synchronizer.syncActions(localItems, remoteItems, []);
let actions = synchronizer().syncActions(localItems, remoteItems, []);
expect(actions.length).toBe(2);
for (let i = 0; i < actions.length; i++) {

View File

@ -0,0 +1,64 @@
import fs from 'fs-extra';
import { Database } from 'src/database.js';
import { DatabaseDriverNode } from 'src/database-driver-node.js';
import { BaseModel } from 'src/base-model.js';
import { Synchronizer } from 'src/synchronizer.js';
import { FileApi } from 'src/file-api.js';
import { FileApiDriverMemory } from 'src/file-api-driver-memory.js';
let database_ = null;
let synchronizer_ = null;
let fileApi_ = null;
function setupDatabase(done) {
if (database_) {
let queries = [
'DELETE FROM changes',
'DELETE FROM notes',
'DELETE FROM folders',
'DELETE FROM item_sync_times',
];
return database_.transactionExecBatch(queries).then(() => {
if (done) done();
});
}
const filePath = __dirname + '/data/test.sqlite';
return fs.unlink(filePath).catch(() => {
// Don't care if the file doesn't exist
}).then(() => {
//console.info('Opening database ' + filePath);
database_ = new Database(new DatabaseDriverNode());
database_.setDebugEnabled(false);
return database_.open({ name: filePath }).then(() => {
BaseModel.db_ = database_;
return setupDatabase(done);
});
});
}
function setupDatabaseAndSynchronizer(done) {
return setupDatabase().then(() => {
if (!synchronizer_) {
let fileDriver = new FileApiDriverMemory();
fileApi_ = new FileApi('/root', fileDriver);
synchronizer_ = new Synchronizer(db(), fileApi);
}
done();
});
}
function db() {
return database_;
}
function synchronizer() {
return synchronizer_;
}
function fileApi() {
return fileApi_;
}
export { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi };