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);
|
|
|
|
});
|
|
|
|
|
2024-06-14 14:56:39 +00:00
|
|
|
tap.test('should create a timedatatable', async () => {
|
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
|
2024-06-14 14:56:39 +00:00
|
|
|
const endTimestamp = Date.now() - 5000; // 5 seconds ago
|
2024-06-14 14:33:00 +00:00
|
|
|
const entries = await table.getEntriesBetween(startTimestamp, endTimestamp);
|
2024-06-14 14:56:39 +00:00
|
|
|
expect(entries.length).toBeGreaterThan(0);
|
2024-06-14 14:33:00 +00:00
|
|
|
console.log(entries);
|
|
|
|
});
|
|
|
|
|
2024-06-14 14:56:39 +00:00
|
|
|
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);
|
|
|
|
|
2024-06-14 14:33:00 +00:00
|
|
|
await table.deleteOldEntries(0); // Delete all entries older than now
|
2024-06-14 14:56:39 +00:00
|
|
|
// 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);
|
2024-06-14 14:33:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should stream new entries', async (toolsArg) => {
|
2024-06-14 15:02:28 +00:00
|
|
|
const stream = table.watchNewEntries();
|
2024-06-14 14:33:00 +00:00
|
|
|
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-23 11:33:53 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-06-14 14:33:00 +00:00
|
|
|
export default tap.start();
|