smartclickhouse/test/test.nonci.ts
2024-06-14 16:33:00 +02:00

86 lines
2.4 KiB
TypeScript

import { expect, tap } from '@push.rocks/tapbundle';
import * as smartclickhouse from '../ts/index.js';
let testClickhouseDb: smartclickhouse.SmartClickHouseDb;
let table: smartclickhouse.TimeDataTable;
tap.test('first test', async () => {
testClickhouseDb = new smartclickhouse.SmartClickHouseDb({
url: 'http://localhost:8123',
database: 'test2',
unref: true,
});
});
tap.test('should start the clickhouse db', async () => {
await testClickhouseDb.start(true);
});
tap.test('should create a timedatatable', async (toolsArg) => {
table = await testClickhouseDb.getTable('analytics');
let i = 0;
while (i < 1000) {
await table.addData({
timestamp: Date.now(),
message: `hello this is a message ${i}`,
wow: 'hey',
deep: {
so: 'hello',
myArray: ['array1', 'array2'],
},
});
i++;
console.log(`logged ${i} of 1000 lines.`);
}
});
tap.test('should retrieve the last 10 entries', async () => {
const entries = await table.getLastEntries(10);
expect(entries.length).toEqual(10);
console.log(entries);
});
tap.test('should retrieve entries newer than a specific timestamp', async () => {
const timestamp = Date.now() - 60000; // 1 minute ago
const entries = await table.getEntriesNewerThan(timestamp);
expect(entries.length).toBeGreaterThan(0);
console.log(entries);
});
tap.test('should retrieve entries between two timestamps', async () => {
const startTimestamp = Date.now() - 120000; // 2 minutes ago
const endTimestamp = Date.now() - 60000; // 1 minute ago
const entries = await table.getEntriesBetween(startTimestamp, endTimestamp);
console.log(entries);
});
tap.test('should delete old entries', async () => {
await table.deleteOldEntries(0); // Delete all entries older than now
const entries = await table.getLastEntries(10);
expect(entries.length).toEqual(0);
});
tap.test('should delete the table', async () => {
await table.delete();
});
tap.test('should stream new entries', async (toolsArg) => {
const stream = table.streamNewEntries();
const subscription = stream.subscribe((entry) => {
console.log('New entry:', entry);
});
let i = 0;
while (i < 10) {
await table.addData({
timestamp: Date.now(),
message: `streaming message ${i}`,
});
i++;
await toolsArg.delayFor(1000); // Add a delay to simulate real-time data insertion
}
subscription.unsubscribe();
});
export default tap.start();