Skip to content

Commit

Permalink
fix: testing AASIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Oct 27, 2024
1 parent cafb6e5 commit 7384632
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 20 deletions.
8 changes: 2 additions & 6 deletions projects/aas-server/src/app/aas-index/aas-index-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export class AASIndexFactory {
try {
const url = new URL(variable.AAS_INDEX);
if (url.protocol === 'mysql:') {
const index = new MySqlIndex(keywordDirectory, variable);
logger.info(`AAS index connected to ${urlToString(url)}.`);
return index;
return new MySqlIndex(logger, variable, keywordDirectory);
}

throw new Error(`${urlToString(url)} is a not supported AAS index.`);
Expand All @@ -43,8 +41,6 @@ export class AASIndexFactory {

const dbFile = path.join(variable.CONTENT_ROOT, 'db.json');
const db = new Low<LowDbData>(new JSONFile(dbFile), { documents: [], endpoints: [], elements: [] });
const index = new LowDbIndex(db, keywordDirectory, variable);
logger.info('Using internal AAS index.');
return index;
return new LowDbIndex(logger, variable, db, keywordDirectory);
}
}
2 changes: 1 addition & 1 deletion projects/aas-server/src/app/aas-index/aas-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { LangString } from '../types/aas-v2.js';
export abstract class AASIndex {
protected constructor(private readonly keywordDirectory: KeywordDirectory) {}

public abstract getCount(query?: string): Promise<number>;
public abstract getCount(endpoint?: string): Promise<number>;

public abstract getEndpoints(): Promise<AASEndpoint[]>;

Expand Down
21 changes: 18 additions & 3 deletions projects/aas-server/src/app/aas-index/lowdb/lowdb-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,37 @@ import { LowDbData, LowDbDocument, LowDbElement } from './lowdb-types.js';
import { decodeBase64Url, encodeBase64Url } from '../../convert.js';
import { PagedResult } from '../../types/paged-result.js';
import { KeywordDirectory } from '../keyword-directory.js';
import { Logger } from '../../logging/logger.js';

export class LowDbIndex extends AASIndex {
private readonly promise: Promise<void>;

public constructor(
private readonly logger: Logger,
private readonly variable: Variable,
private readonly db: Low<LowDbData>,
keywordDirectory: KeywordDirectory,
private readonly variable: Variable,
) {
super(keywordDirectory);

this.promise = this.initialize();
this.promise.then(() => logger.info('Using internal AAS index.')).catch(error => logger.error(error));
}

public override getCount(): Promise<number> {
return Promise.resolve(this.db.data.documents.length);
public override async getCount(endpoint?: string): Promise<number> {
await this.promise;
if (endpoint === undefined) {
return this.db.data.documents.length;
}

let count = 0;
this.db.data.documents.forEach(item => {
if (item.endpoint === endpoint) {
++count;
}
});

return count;
}

public override async getEndpoints(): Promise<AASEndpoint[]> {
Expand Down
34 changes: 29 additions & 5 deletions projects/aas-server/src/app/aas-index/mysql/mysql-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,46 @@ import { MySqlQuery } from './mysql-query.js';
import { DocumentCount, MySqlDocument, MySqlEndpoint } from './mysql-types.js';
import { PagedResult } from '../../types/paged-result.js';
import { KeywordDirectory } from '../keyword-directory.js';
import { Logger } from '../../logging/logger.js';
import { urlToString } from '../../convert.js';

export class MySqlIndex extends AASIndex {
private readonly connection: Promise<Connection>;

public constructor(
keywordDirectory: KeywordDirectory,
private readonly logger: Logger,
private readonly variable: Variable,
keywordDirectory: KeywordDirectory,
connection?: Connection,
) {
super(keywordDirectory);

this.connection = this.initialize();
if (connection === undefined) {
this.connection = this.initialize();
this.connection
.then(() => {
logger.info(`AAS index connected to ${urlToString(this.variable.AAS_INDEX)}.`);
})
.catch(error => {
this.logger.error(error);
});
} else {
this.connection = new Promise(resolve => resolve(connection));
}
}

public override async getCount(): Promise<number> {
return (await (await this.connection).query<DocumentCount[]>('SELECT COUNT(*) FROM `documents` AS count'))[0][0]
.count;
public override async getCount(endpoint?: string): Promise<number> {
if (endpoint === undefined) {
return (
await (await this.connection).query<DocumentCount[]>('SELECT COUNT(*) FROM `documents` AS count')
)[0][0].count;
}

return (
await (
await this.connection
).query<DocumentCount[]>('SELECT COUNT(*) FROM `documents` WHERE endpoint = ? AS count', [endpoint])
)[0][0].count;
}

public override async getEndpoints(): Promise<AASEndpoint[]> {
Expand Down
6 changes: 5 additions & 1 deletion projects/aas-server/src/app/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export function parseUrl(url: string): URL {
}
}

export function urlToString(url: URL | string): string {
export function urlToString(url: URL | string | undefined): string {
if (url === undefined) {
return '';
}

const temp = new URL(url);
temp.password = '';
temp.username = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import { Variable } from '../../../app/variable.js';
import { LowDbIndex } from '../../../app/aas-index/lowdb/lowdb-index.js';
import { LowDbData } from '../../../app/aas-index/lowdb/lowdb-types.js';
import { KeywordDirectory } from 'projects/aas-server/src/app/aas-index/keyword-directory.js';
import { Logger } from '../../../app/logging/logger.js';

describe('LowDbIndex', () => {
let index: LowDbIndex;
let db: jest.Mocked<Low<LowDbData>>;
let logger: jest.Mocked<Logger>;
let variable: jest.Mocked<Variable>;
let keywords: jest.Mocked<KeywordDirectory>;
let dbData: LowDbData;
Expand All @@ -36,16 +38,17 @@ describe('LowDbIndex', () => {
beforeEach(() => {
db = createSpyObj<Low<LowDbData>>(['read', 'write'], { data: dbData });
db.read.mockResolvedValue();
logger = createSpyObj<Logger>(['error', 'info']);
variable = createSpyObj<Variable>([], { ENDPOINTS: [] });
keywords = createSpyObj<KeywordDirectory>(['containedKeyword', 'toString']);
index = new LowDbIndex(db, keywords, variable);
index = new LowDbIndex(logger, variable, db, keywords);
});

it('should create', () => {
expect(index).toBeTruthy();
});

describe('getContainerDocuments', () => {
describe('nextPage', () => {
it('returns all documents that belongs to a container', async () => {
const result = await index.nextPage('Samples', undefined);
expect(result.result).toEqual(db.data.documents.filter(document => document.endpoint === 'Samples'));
Expand Down
56 changes: 54 additions & 2 deletions projects/aas-server/src/test/aas-index/mysql/mysql-index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,60 @@
*
*****************************************************************************/

import { describe, it } from '@jest/globals';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { Connection } from 'mysql2/promise';
import { createSpyObj } from 'fhg-jest';
import { MySqlIndex } from '../../../app/aas-index/mysql/mysql-index.js';
import { Logger } from '../../../app/logging/logger.js';
import { Variable } from '../../../app/variable.js';
import { KeywordDirectory } from '../../../app/aas-index/keyword-directory.js';
import { DocumentCount } from '../../../app/aas-index/mysql/mysql-types.js';

describe('MySqlIndex', () => {
it.todo('MySqlIndex');
let index: MySqlIndex;
let logger: jest.Mocked<Logger>;
let variable: jest.Mocked<Variable>;
let connection: jest.Mocked<Connection>;
let keywords: jest.Mocked<KeywordDirectory>;

beforeEach(() => {
logger = createSpyObj<Logger>(['error', 'info']);
variable = createSpyObj<Variable>({}, { ENDPOINTS: [] });
keywords = createSpyObj<KeywordDirectory>(['containedKeyword', 'toString']);
connection = createSpyObj<Connection>(['query']);
index = new MySqlIndex(logger, variable, keywords, connection);
});

it('should be created', () => {
expect(index).toBeTruthy();
});

describe('getCount', () => {
it('returns the total number of documents', async () => {
const result: DocumentCount = {
constructor: { name: 'RowDataPacket' },
count: 42,
};

connection.query.mockResolvedValue([[result], []]);
const count = await index.getCount();
expect(connection.query).toBeCalledWith('SELECT COUNT(*) FROM `documents` AS count');
expect(count).toEqual(42);
});

it('returns the total number of documents of the specified Endpoint', async () => {
const result: DocumentCount = {
constructor: { name: 'RowDataPacket' },
count: 42,
};

connection.query.mockResolvedValue([[result], []]);
const count = await index.getCount('Samples');
expect(connection.query).toBeCalledWith('SELECT COUNT(*) FROM `documents` WHERE endpoint = ? AS count', [
'Samples',
]);

expect(count).toEqual(42);
});
});
});

0 comments on commit 7384632

Please sign in to comment.