elasticsearch/test/test.nonci.ts

90 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2023-08-29 09:11:25 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
import { Qenv } from '@push.rocks/qenv';
2023-07-04 07:13:14 +00:00
import * as elasticsearch from '../ts/index.js';
2018-01-24 23:38:07 +00:00
2023-08-01 10:24:22 +00:00
let testElasticLog: elasticsearch.ElsSmartlogDestination<any>;
2023-08-01 10:38:53 +00:00
let testElasticDoc: elasticsearch.ElasticDoc;
2018-01-24 23:38:07 +00:00
tap.test('first test', async () => {
2023-08-01 10:24:22 +00:00
testElasticLog = new elasticsearch.ElsSmartlogDestination({
2023-07-05 13:31:00 +00:00
indexPrefix: 'testprefix',
2018-11-10 00:48:44 +00:00
indexRetention: 7,
2023-07-05 13:31:00 +00:00
node: 'http://localhost:9200',
2023-07-05 15:27:24 +00:00
auth: {
username: 'elastic',
password: 'YourPassword'
}
2018-08-12 14:42:09 +00:00
});
2023-08-01 10:24:22 +00:00
expect(testElasticLog).toBeInstanceOf(elasticsearch.ElsSmartlogDestination);
2018-08-12 14:42:09 +00:00
});
2018-01-24 23:38:07 +00:00
2023-07-05 13:31:00 +00:00
tap.test('should send a message to Elasticsearch', async () => {
await testElasticLog.log({
2018-11-07 10:38:53 +00:00
timestamp: Date.now(),
2018-11-03 22:45:21 +00:00
type: 'increment',
level: 'info',
context: {
company: 'Lossless GmbH',
companyunit: 'lossless.cloud',
containerName: 'testcontainer',
environment: 'test',
runtime: 'node',
2023-07-04 07:13:14 +00:00
zone: 'ship.zone',
2018-11-03 22:45:21 +00:00
},
2023-07-04 07:13:14 +00:00
message: 'GET https://myroute.to.a.cool.destination/sorare?hello=there',
correlation: null,
2018-08-12 14:42:09 +00:00
});
});
2018-01-24 23:38:07 +00:00
2023-08-01 10:38:53 +00:00
tap.test('should create an ElasticDoc instance', async () => {
testElasticDoc = new elasticsearch.ElasticDoc({
index: 'testindex',
node: 'http://localhost:9200',
auth: {
username: 'elastic',
password: 'YourPassword'
}
});
expect(testElasticDoc).toBeInstanceOf(elasticsearch.ElasticDoc);
});
tap.test('should add and update documents in a piping session', async () => {
2023-08-29 07:18:15 +00:00
await testElasticDoc.startPipingSession({});
2023-08-25 14:06:52 +00:00
await testElasticDoc.pipeDocument({
docId: '1',
timestamp: new Date().toISOString(),
doc: { name: 'doc1' }
});
await testElasticDoc.pipeDocument({
docId: '2',
timestamp: new Date().toISOString(),
doc: { name: 'doc2' }
});
await testElasticDoc.pipeDocument({
docId: '1',
timestamp: new Date().toISOString(),
doc: { name: 'updated doc1' }
});
2023-08-01 10:38:53 +00:00
});
tap.test('should delete documents not part of the piping session', async () => {
await testElasticDoc.endPipingSession();
});
2023-08-02 01:11:17 +00:00
tap.test('should take and store snapshot', async () => {
await testElasticDoc.takeSnapshot(async (iterator, prevSnapshot) => {
const aggregationData = [];
for await (const doc of iterator) {
// Sample aggregation: counting documents
aggregationData.push(doc);
}
const snapshot = {
date: new Date().toISOString(),
aggregationData,
};
return snapshot;
});
});
2018-08-12 14:42:09 +00:00
tap.start();