fix: parametrize location reader and update test
This commit is contained in:
@@ -21,6 +21,7 @@ import path from 'path';
|
||||
|
||||
import { Database } from '../database';
|
||||
import { getVoidLogger } from '../../../../packages/backend-common/src/logging/voidLogger';
|
||||
import { ReaderOutput } from '../ingestion/types';
|
||||
|
||||
describe('DatabaseLocationsCatalog', () => {
|
||||
const database = knex({
|
||||
@@ -34,14 +35,29 @@ describe('DatabaseLocationsCatalog', () => {
|
||||
let db: Database;
|
||||
let catalog: DatabaseLocationsCatalog;
|
||||
|
||||
const mockLocationReader = {
|
||||
read: async (type: string, target: string): Promise<ReaderOutput[]> => {
|
||||
if (type !== 'valid_type') {
|
||||
throw new Error(`Unknown location type ${type}`);
|
||||
}
|
||||
if (target === 'valid_target') {
|
||||
return Promise.resolve([{ type: 'data', data: {} }]);
|
||||
}
|
||||
throw new Error(
|
||||
`Can't read location at ${target} with error: Something is broken`,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
await database.migrate.latest({
|
||||
directory: path.resolve(__dirname, '../database/migrations'),
|
||||
loadExtensions: ['.ts'],
|
||||
});
|
||||
db = new Database(database, getVoidLogger());
|
||||
catalog = new DatabaseLocationsCatalog(db);
|
||||
catalog = new DatabaseLocationsCatalog(db, mockLocationReader);
|
||||
});
|
||||
|
||||
it('resolves to location with id', async () => {
|
||||
return expect(
|
||||
catalog.addLocation({ type: 'valid_type', target: 'valid_target' }),
|
||||
|
||||
@@ -16,16 +16,16 @@
|
||||
|
||||
import { Database } from '../database';
|
||||
import { AddLocation, Location, LocationsCatalog } from './types';
|
||||
import { LocationReaders } from '../ingestion';
|
||||
import { LocationReader } from '../ingestion';
|
||||
|
||||
export class DatabaseLocationsCatalog implements LocationsCatalog {
|
||||
constructor(private readonly database: Database) {}
|
||||
constructor(
|
||||
private readonly database: Database,
|
||||
private readonly reader: LocationReader,
|
||||
) {}
|
||||
|
||||
async addLocation(location: AddLocation): Promise<Location> {
|
||||
const outputs = await LocationReaders.create().read(
|
||||
location.type,
|
||||
location.target,
|
||||
);
|
||||
const outputs = await this.reader.read(location.type, location.target);
|
||||
outputs.forEach(output => {
|
||||
if (output.type === 'error') {
|
||||
throw new Error(
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// @ts-nocheck
|
||||
import { LocationSource, LocationReader, ReaderOutput } from '../types';
|
||||
|
||||
export class LocationReaders implements LocationReader {
|
||||
static create(): LocationReader {
|
||||
return {
|
||||
read: (type, target) => {
|
||||
if (type !== 'valid_type') {
|
||||
throw new Error(`Unknown location type ${type}`);
|
||||
}
|
||||
if (target === 'valid_target') {
|
||||
return Promise.resolve([{ type: 'data', data: {} }]);
|
||||
}
|
||||
throw new Error(
|
||||
`Can't read location at ${target} with error: Something is broken`,
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
// eslint-disable-next-line
|
||||
constructor(private readonly sources: Record<string, LocationSource>) {}
|
||||
|
||||
// eslint-disable-next-line
|
||||
async read(type: string, target: string): Promise<ReaderOutput[]> {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user