From 9ffa29f658350e674bde5dec0a9f8232ce4afe4c Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 10 Nov 2021 12:03:45 +0000 Subject: [PATCH] Chore: Add tests for storage driver loading --- packages/server/src/models/ItemModel.ts | 10 ++-- .../items/storage/loadStorageDriver.test.ts | 50 +++++++++++++++++++ ...iverFromConfig.ts => loadStorageDriver.ts} | 0 .../server/src/utils/testing/testUtils.ts | 2 +- 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 packages/server/src/models/items/storage/loadStorageDriver.test.ts rename packages/server/src/models/items/storage/{storageDriverFromConfig.ts => loadStorageDriver.ts} (100%) diff --git a/packages/server/src/models/ItemModel.ts b/packages/server/src/models/ItemModel.ts index 87680b83dba..a5f4bee4f79 100644 --- a/packages/server/src/models/ItemModel.ts +++ b/packages/server/src/models/ItemModel.ts @@ -11,7 +11,7 @@ import StorageDriverBase, { Context } from './items/storage/StorageDriverBase'; import { DbConnection } from '../db'; import { Config, StorageDriverConfig, StorageDriverMode } from '../utils/types'; import { NewModelFactoryHandler } from './factory'; -import storageDriverFromConfig from './items/storage/storageDriverFromConfig'; +import loadStorageDriver from './items/storage/loadStorageDriver'; const mimeUtils = require('@joplin/lib/mime-utils.js').mime; @@ -78,11 +78,11 @@ export default class ItemModel extends BaseModel { return Object.keys(databaseSchema[this.tableName]).filter(f => f !== 'content'); } - private async storageDriverFromConfig(config: StorageDriverConfig): Promise { + private async loadStorageDriver(config: StorageDriverConfig): Promise { let driver = ItemModel.storageDrivers_.get(config); if (!driver) { - driver = await storageDriverFromConfig(config, this.db); + driver = await loadStorageDriver(config, this.db); ItemModel.storageDrivers_.set(config, driver); } @@ -90,12 +90,12 @@ export default class ItemModel extends BaseModel { } public async storageDriver(): Promise { - return this.storageDriverFromConfig(this.storageDriverConfig_); + return this.loadStorageDriver(this.storageDriverConfig_); } public async storageDriverFallback(): Promise { if (!this.storageDriverConfigFallback_) return null; - return this.storageDriverFromConfig(this.storageDriverConfigFallback_); + return this.loadStorageDriver(this.storageDriverConfigFallback_); } public async checkIfAllowed(user: User, action: AclAction, resource: Item = null): Promise { diff --git a/packages/server/src/models/items/storage/loadStorageDriver.test.ts b/packages/server/src/models/items/storage/loadStorageDriver.test.ts new file mode 100644 index 00000000000..49bad61a142 --- /dev/null +++ b/packages/server/src/models/items/storage/loadStorageDriver.test.ts @@ -0,0 +1,50 @@ +import { afterAllTests, beforeAllDb, beforeEachDb, db, expectThrow, models } from '../../../utils/testing/testUtils'; +import { StorageDriverType } from '../../../utils/types'; +import loadStorageDriver from './loadStorageDriver'; + +describe('loadStorageDriver', function() { + + beforeAll(async () => { + await beforeAllDb('loadStorageDriver'); + }); + + afterAll(async () => { + await afterAllTests(); + }); + + beforeEach(async () => { + await beforeEachDb(); + }); + + test('should load a driver and assign an ID to it', async function() { + { + const newDriver = await loadStorageDriver({ type: StorageDriverType.Memory }, db()); + expect(newDriver.storageId).toBe(1); + expect((await models().storage().count())).toBe(1); + } + + { + const newDriver = await loadStorageDriver({ type: StorageDriverType.Filesystem, path: '/just/testing' }, db()); + expect(newDriver.storageId).toBe(2); + expect((await models().storage().count())).toBe(2); + } + }); + + test('should not record the same storage connection twice', async function() { + await db()('storages').insert({ + connection_string: 'Type=Database', + updated_time: Date.now(), + created_time: Date.now(), + }); + + await expectThrow(async () => + await db()('storages').insert({ + connection_string: 'Type=Database', + updated_time: Date.now(), + created_time: Date.now(), + }) + ); + }); + +}); + diff --git a/packages/server/src/models/items/storage/storageDriverFromConfig.ts b/packages/server/src/models/items/storage/loadStorageDriver.ts similarity index 100% rename from packages/server/src/models/items/storage/storageDriverFromConfig.ts rename to packages/server/src/models/items/storage/loadStorageDriver.ts diff --git a/packages/server/src/utils/testing/testUtils.ts b/packages/server/src/utils/testing/testUtils.ts index 0f227cbb215..f4113aa05ae 100644 --- a/packages/server/src/utils/testing/testUtils.ts +++ b/packages/server/src/utils/testing/testUtils.ts @@ -436,7 +436,7 @@ export async function expectThrow(asyncFn: Function, errorCode: any = undefined) if (!hasThrown) { expect('not throw').toBe('throw'); - } else if (thrownError.code !== errorCode) { + } else if (errorCode !== undefined && thrownError.code !== errorCode) { console.error(thrownError); expect(`error code: ${thrownError.code}`).toBe(`error code: ${errorCode}`); } else {