1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/CliClient/tests/services_KvStore.js
Laurent Cozic 71efff6827
Linter update (#1777)
* Update eslint config

* Applied linter to lib

* Applied eslint config to CliClient/app

* Removed prettier due to https://github.com/prettier/prettier/pull/4765

* First pass on test units

* Applied linter config to test units

* Applied eslint config to clipper

* Applied to plugin dir

* Applied to root of ElectronClient

* Applied on RN root

* Applied on CLI root

* Applied on Clipper root

* Applied config to tools

* test hook

* test hook

* test hook

* Added pre-commit hook

* Applied rule no-trailing-spaces

* Make sure root packages are installed when installing sub-dir

* Added doc
2019-07-30 09:35:42 +02:00

110 lines
3.1 KiB
JavaScript

/* eslint-disable no-unused-vars */
require('app-module-path').addPath(__dirname);
const { asyncTest, fileContentEqual, setupDatabase, revisionService, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
const KvStore = require('lib/services/KvStore.js');
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
function setupStore() {
const store = KvStore.instance();
store.setDb(db());
return store;
}
describe('services_KvStore', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);
done();
});
it('should set and get values', asyncTest(async () => {
const store = setupStore();
await store.setValue('a', 123);
expect(await store.value('a')).toBe(123);
await store.setValue('a', 123);
expect(await store.countKeys()).toBe(1);
expect(await store.value('a')).toBe(123);
await store.setValue('a', 456);
expect(await store.countKeys()).toBe(1);
expect(await store.value('a')).toBe(456);
await store.setValue('b', 789);
expect(await store.countKeys()).toBe(2);
expect(await store.value('a')).toBe(456);
expect(await store.value('b')).toBe(789);
}));
it('should set and get values with the right type', asyncTest(async () => {
const store = setupStore();
await store.setValue('string', 'something');
await store.setValue('int', 123);
expect(await store.value('string')).toBe('something');
expect(await store.value('int')).toBe(123);
}));
it('should increment values', asyncTest(async () => {
const store = setupStore();
await store.setValue('int', 1);
const newValue = await store.incValue('int');
expect(newValue).toBe(2);
expect(await store.value('int')).toBe(2);
expect(await store.incValue('int2')).toBe(1);
expect(await store.countKeys()).toBe(2);
}));
it('should handle non-existent values', asyncTest(async () => {
const store = setupStore();
expect(await store.value('nope')).toBe(null);
}));
it('should delete values', asyncTest(async () => {
const store = setupStore();
await store.setValue('int', 1);
expect(await store.countKeys()).toBe(1);
await store.deleteValue('int');
expect(await store.countKeys()).toBe(0);
await store.deleteValue('int'); // That should not throw
}));
it('should increment in an atomic way', asyncTest(async () => {
const store = setupStore();
await store.setValue('int', 0);
const promises = [];
for (let i = 0; i < 20; i++) {
promises.push(store.incValue('int'));
}
await Promise.all(promises);
expect(await store.value('int')).toBe(20);
}));
it('should search by prefix', asyncTest(async () => {
const store = setupStore();
await store.setValue('testing:1', 1);
await store.setValue('testing:2', 2);
const results = await store.searchByPrefix('testing:');
expect(results.length).toBe(2);
const numbers = results.map(r => r.value).sort();
expect(numbers[0]).toBe(1);
expect(numbers[1]).toBe(2);
}));
});