smartclickhouse/test/test.nonci.ts

104 lines
3.2 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 () => {
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() - 5000; // 5 seconds ago
const entries = await table.getEntriesBetween(startTimestamp, endTimestamp);
expect(entries.length).toBeGreaterThan(0);
console.log(entries);
});
tap.test('should delete old entries', async (toolsArg) => {
// Ensure there are entries before deletion
let entries = await table.getLastEntries(1000);
expect(entries.length).toBeGreaterThan(100);
console.log('Entries before deletion:', entries.length);
await table.deleteOldEntries(0); // Delete all entries older than now
// Add a delay to ensure the delete operation completes
await new Promise(resolve => setTimeout(resolve, 5000));
// Verify the entries are deleted
entries = await table.getLastEntries(1000);
console.log('Entries after deletion:', entries.length);
expect(entries.length).toBeLessThan(100);
await toolsArg.delayFor(5000);
});
tap.test('should stream new entries', async (toolsArg) => {
const stream = table.watchNewEntries();
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();
});
tap.test('should delete the table', async () => {
await table.delete();
// Verify table deletion
const result = await testClickhouseDb.clickhouseHttpClient.queryPromise(`
SHOW TABLES FROM ${testClickhouseDb.options.database} LIKE '${table.options.tableName}'
`);
console.log('Table exists after deletion:', result);
expect(result.length).toEqual(0);
});
export default tap.start();