Skip to content

Commit

Permalink
Chore: Add tests for storage driver loading
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Nov 10, 2021
1 parent 7431da9 commit 9ffa29f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/server/src/models/ItemModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,24 +78,24 @@ export default class ItemModel extends BaseModel<Item> {
return Object.keys(databaseSchema[this.tableName]).filter(f => f !== 'content');
}

private async storageDriverFromConfig(config: StorageDriverConfig): Promise<StorageDriverBase> {
private async loadStorageDriver(config: StorageDriverConfig): Promise<StorageDriverBase> {
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);
}

return driver;
}

public async storageDriver(): Promise<StorageDriverBase> {
return this.storageDriverFromConfig(this.storageDriverConfig_);
return this.loadStorageDriver(this.storageDriverConfig_);
}

public async storageDriverFallback(): Promise<StorageDriverBase> {
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<void> {
Expand Down
50 changes: 50 additions & 0 deletions packages/server/src/models/items/storage/loadStorageDriver.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
})
);
});

});

2 changes: 1 addition & 1 deletion packages/server/src/utils/testing/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9ffa29f

Please sign in to comment.