smartclickhouse/test/test.nonci.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-06-14 14:33:00 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-14 13:29:23 +00:00
import * as smartclickhouse from '../ts/index.js';
2022-03-07 14:49:47 +00:00
let testClickhouseDb: smartclickhouse.SmartClickHouseDb;
2024-06-14 14:33:00 +00:00
let table: smartclickhouse.TimeDataTable;
2022-03-07 14:49:47 +00:00
tap.test('first test', async () => {
testClickhouseDb = new smartclickhouse.SmartClickHouseDb({
2022-07-27 20:42:08 +00:00
url: 'http://localhost:8123',
2022-03-07 14:49:47 +00:00
database: 'test2',
2022-07-30 16:03:17 +00:00
unref: true,
2022-03-07 14:49:47 +00:00
});
});
tap.test('should start the clickhouse db', async () => {
await testClickhouseDb.start(true);
});
2022-07-27 20:42:08 +00:00
tap.test('should create a timedatatable', async (toolsArg) => {
2024-06-14 14:33:00 +00:00
table = await testClickhouseDb.getTable('analytics');
2022-03-07 14:49:47 +00:00
let i = 0;
2024-06-14 14:33:00 +00:00
while (i < 1000) {
2022-03-07 14:49:47 +00:00
await table.addData({
timestamp: Date.now(),
message: `hello this is a message ${i}`,
wow: 'hey',
deep: {
2022-03-08 14:12:51 +00:00
so: 'hello',
2022-08-05 11:31:11 +00:00
myArray: ['array1', 'array2'],
},
2022-03-07 14:49:47 +00:00
});
i++;
2024-06-14 14:33:00 +00:00
console.log(`logged ${i} of 1000 lines.`);
2022-03-07 14:49:47 +00:00
}
});
2024-06-14 14:33:00 +00:00
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();
});
2022-03-07 14:49:47 +00:00
2024-06-14 14:33:00 +00:00
export default tap.start();