Skip to content

Commit a71bad7

Browse files
committed
test: test cases corrected
1 parent e2d1ab0 commit a71bad7

File tree

1 file changed

+49
-154
lines changed

1 file changed

+49
-154
lines changed

tests/index.test.js

Lines changed: 49 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,32 @@ describe('ErrsolePostgres', () => {
185185

186186
describe('#createTables', () => {
187187
it('should create necessary tables', async () => {
188+
// Mock the query method to resolve for all calls
188189
poolMock.query.mockResolvedValue({});
189190

191+
// Call the function
190192
await errsolePostgres.createTables();
191193

192-
const createTableCalls = [
193-
expect.stringContaining('CREATE TABLE IF NOT EXISTS errsole_logs_v2'),
194-
expect.stringContaining('CREATE INDEX IF NOT EXISTS errsole_logs_v2_source_level_id_idx'),
195-
expect.stringContaining('CREATE INDEX IF NOT EXISTS errsole_logs_v2_source_level_timestamp_idx'),
196-
expect.stringContaining('CREATE INDEX IF NOT EXISTS errsole_logs_v2_hostname_pid_id_idx'),
197-
expect.stringContaining('CREATE TABLE IF NOT EXISTS errsole_users'),
198-
expect.stringContaining('CREATE TABLE IF NOT EXISTS errsole_config')
194+
// Capture all queries executed
195+
const executedQueries = poolMock.query.mock.calls.map(call => call[0]);
196+
// Define expected table creation queries
197+
const expectedQueries = [
198+
/CREATE TABLE IF NOT EXISTS errsole_logs_v3/,
199+
/CREATE INDEX IF NOT EXISTS .*errsole_logs_v3.*hostname.*source.*level.*timestamp.*id/,
200+
/CREATE INDEX IF NOT EXISTS .*errsole_logs_v3.*hostname.*timestamp.*id/,
201+
/CREATE INDEX IF NOT EXISTS .*errsole_logs_v3.*hostname/,
202+
/CREATE INDEX IF NOT EXISTS .*errsole_logs_v3.*timestamp.*id/,
203+
/CREATE INDEX IF NOT EXISTS .*errsole_logs_v3.*errsole_id/,
204+
/CREATE TABLE IF NOT EXISTS errsole_users/,
205+
/CREATE TABLE IF NOT EXISTS errsole_config/
199206
];
200207

201-
createTableCalls.forEach(call => {
202-
expect(poolMock.query).toHaveBeenCalledWith(call);
208+
// Ensure all expected queries were executed
209+
expectedQueries.forEach(expectedQuery => {
210+
expect(executedQueries.some(query => expectedQuery.test(query))).toBe(true);
203211
});
204212
});
213+
205214
it('should throw an error if table creation fails', async () => {
206215
const error = new Error('Query error');
207216
poolMock.query.mockRejectedValueOnce(error);
@@ -594,52 +603,12 @@ describe('ErrsolePostgres', () => {
594603
const result = await errsolePostgres.getLogs();
595604

596605
expect(poolMock.query).toHaveBeenCalledWith(
597-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2'),
606+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3'),
598607
[100]
599608
);
600609
expect(result).toEqual({ items: logs });
601610
});
602611

603-
it('should apply hostname and pid filters', async () => {
604-
const logs = [
605-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date(), level: 'info', message: 'test message' }
606-
];
607-
const filters = {
608-
hostname: 'localhost',
609-
pid: 1234,
610-
limit: 50
611-
};
612-
poolMock.query.mockResolvedValueOnce({ rows: logs });
613-
614-
const result = await errsolePostgres.getLogs(filters);
615-
616-
expect(poolMock.query).toHaveBeenCalledWith(
617-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE hostname = $1 AND pid = $2 ORDER BY id DESC LIMIT $3'),
618-
['localhost', 1234, 50]
619-
);
620-
expect(result).toEqual({ items: logs });
621-
});
622-
623-
it('should apply sources and levels filters', async () => {
624-
const logs = [
625-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date(), level: 'info', message: 'test message' }
626-
];
627-
const filters = {
628-
sources: ['test'],
629-
levels: ['info'],
630-
limit: 50
631-
};
632-
poolMock.query.mockResolvedValueOnce({ rows: logs });
633-
634-
const result = await errsolePostgres.getLogs(filters);
635-
636-
expect(poolMock.query).toHaveBeenCalledWith(
637-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE source = ANY($1) AND level = ANY($2) ORDER BY id DESC LIMIT $3'),
638-
[['test'], ['info'], 50]
639-
);
640-
expect(result).toEqual({ items: logs });
641-
});
642-
643612
it('should apply lt_id filter', async () => {
644613
const logs = [
645614
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date(), level: 'info', message: 'test message' }
@@ -653,7 +622,7 @@ describe('ErrsolePostgres', () => {
653622
const result = await errsolePostgres.getLogs(filters);
654623

655624
expect(poolMock.query).toHaveBeenCalledWith(
656-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE id < $1 ORDER BY id DESC LIMIT $2'),
625+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE id < $1 ORDER BY id DESC LIMIT $2'),
657626
[10, 50]
658627
);
659628
expect(result).toEqual({ items: logs });
@@ -672,7 +641,7 @@ describe('ErrsolePostgres', () => {
672641
const result = await errsolePostgres.getLogs(filters);
673642

674643
expect(poolMock.query).toHaveBeenCalledWith(
675-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE id > $1 ORDER BY id ASC LIMIT $2'),
644+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE id > $1 ORDER BY id ASC LIMIT $2'),
676645
[5, 50]
677646
);
678647
expect(result).toEqual({ items: logs });
@@ -686,54 +655,56 @@ describe('ErrsolePostgres', () => {
686655
lte_timestamp: new Date('2023-01-02T00:00:00Z'),
687656
limit: 50
688657
};
658+
689659
poolMock.query.mockResolvedValueOnce({ rows: logs });
690660

691661
const result = await errsolePostgres.getLogs(filters);
692662

693663
expect(poolMock.query).toHaveBeenCalledWith(
694-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE timestamp <= $1 ORDER BY id DESC LIMIT $2'),
664+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE timestamp <= $1 ORDER BY timestamp DESC, id DESC LIMIT $2'),
695665
[new Date('2023-01-02T00:00:00Z'), 50]
696666
);
697667
expect(result).toEqual({ items: logs });
698668
});
699669

700-
it('should apply level_json filter', async () => {
670+
it('should apply gte_timestamp filter', async () => {
701671
const logs = [
702-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date(), level: 'info', message: 'test message' }
672+
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date('2023-01-01T00:00:00Z'), level: 'info', message: 'test message' }
703673
];
704674
const filters = {
705-
level_json: [
706-
{ source: 'test', level: 'info' },
707-
{ source: 'another_test', level: 'warn' }
708-
],
675+
gte_timestamp: new Date('2023-01-01T00:00:00Z'),
709676
limit: 50
710677
};
678+
711679
poolMock.query.mockResolvedValueOnce({ rows: logs });
712680

713681
const result = await errsolePostgres.getLogs(filters);
714682

715683
expect(poolMock.query).toHaveBeenCalledWith(
716-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2'),
717-
expect.arrayContaining(['test', 'info', 'another_test', 'warn', 50])
684+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE timestamp >= $1 ORDER BY timestamp ASC, id ASC LIMIT $2'),
685+
[new Date('2023-01-01T00:00:00Z'), 50]
718686
);
719687
expect(result).toEqual({ items: logs });
720688
});
721689

722-
it('should apply gte_timestamp filter', async () => {
690+
it('should apply level_json filter', async () => {
723691
const logs = [
724-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date('2023-01-01T00:00:00Z'), level: 'info', message: 'test message' }
692+
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: new Date(), level: 'info', message: 'test message' }
725693
];
726694
const filters = {
727-
gte_timestamp: new Date('2023-01-01T00:00:00Z'),
695+
level_json: [
696+
{ source: 'test', level: 'info' },
697+
{ source: 'another_test', level: 'warn' }
698+
],
728699
limit: 50
729700
};
730701
poolMock.query.mockResolvedValueOnce({ rows: logs });
731702

732703
const result = await errsolePostgres.getLogs(filters);
733704

734705
expect(poolMock.query).toHaveBeenCalledWith(
735-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE timestamp >= $1 ORDER BY id ASC LIMIT $2'),
736-
[new Date('2023-01-01T00:00:00Z'), 50]
706+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3'),
707+
expect.arrayContaining(['test', 'info', 'another_test', 'warn', 50])
737708
);
738709
expect(result).toEqual({ items: logs });
739710
});
@@ -752,7 +723,7 @@ describe('ErrsolePostgres', () => {
752723
const result = await errsolePostgres.getLogs(filters);
753724

754725
expect(poolMock.query).toHaveBeenCalledWith(
755-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE id < $1 ORDER BY id DESC LIMIT $2'),
726+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE id < $1 ORDER BY id DESC LIMIT $2'),
756727
[10, 50]
757728
);
758729
expect(result.items).toEqual(logs.reverse());
@@ -885,7 +856,7 @@ describe('ErrsolePostgres', () => {
885856
await expect(errsolePostgres.getLogs()).rejects.toThrow('Query error');
886857

887858
expect(poolMock.query).toHaveBeenCalledWith(
888-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2'),
859+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3'),
889860
[100]
890861
);
891862
});
@@ -910,7 +881,7 @@ describe('ErrsolePostgres', () => {
910881

911882
expect(poolQuerySpy).toHaveBeenCalledWith(
912883
expect.stringContaining(
913-
'SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE ('
884+
'SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE ('
914885
),
915886
expect.arrayContaining(['source1', 'info', 'source2', 'error', 50])
916887
);
@@ -932,7 +903,7 @@ describe('ErrsolePostgres', () => {
932903
const result = await errsolePostgres.getLogs(filters);
933904

934905
expect(poolQuerySpy).toHaveBeenCalledWith(
935-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE ('),
906+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE ('),
936907
expect.arrayContaining([123, 50])
937908
);
938909
expect(result.items).toEqual(logs);
@@ -956,7 +927,7 @@ describe('ErrsolePostgres', () => {
956927
const result = await errsolePostgres.getLogs(filters);
957928

958929
expect(poolQuerySpy).toHaveBeenCalledWith(
959-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE ('),
930+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE ('),
960931
expect.arrayContaining(['source1', 'info', 123, 50])
961932
);
962933
expect(result.items).toEqual(logs);
@@ -976,7 +947,7 @@ describe('ErrsolePostgres', () => {
976947
const result = await errsolePostgres.getLogs(filters);
977948

978949
expect(poolQuerySpy).toHaveBeenCalledWith(
979-
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v2 WHERE ('),
950+
expect.stringContaining('SELECT id, hostname, pid, source, timestamp, level, message, errsole_id FROM errsole_logs_v3 WHERE ('),
980951
expect.arrayContaining(['source1', 'info', 123, 50])
981952
);
982953
expect(result.items).toEqual([]);
@@ -1013,82 +984,6 @@ describe('ErrsolePostgres', () => {
1013984
);
1014985
});
1015986

1016-
it('should search log entries with hostname filter', async () => {
1017-
poolMock.query.mockResolvedValueOnce({
1018-
rows: [
1019-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }
1020-
]
1021-
});
1022-
1023-
const logs = await errsolePostgres.searchLogs(['test'], { hostname: 'localhost' });
1024-
1025-
expect(logs).toEqual({
1026-
items: [{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }],
1027-
filters: { hostname: 'localhost', limit: 100 }
1028-
});
1029-
expect(poolMock.query).toHaveBeenCalledWith(
1030-
expect.any(String),
1031-
expect.arrayContaining(['%test%', 'localhost'])
1032-
);
1033-
});
1034-
1035-
it('should search log entries with pid filter', async () => {
1036-
poolMock.query.mockResolvedValueOnce({
1037-
rows: [
1038-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }
1039-
]
1040-
});
1041-
1042-
const logs = await errsolePostgres.searchLogs(['test'], { pid: 1234 });
1043-
1044-
expect(logs).toEqual({
1045-
items: [{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }],
1046-
filters: { pid: 1234, limit: 100 }
1047-
});
1048-
expect(poolMock.query).toHaveBeenCalledWith(
1049-
expect.any(String),
1050-
expect.arrayContaining(['%test%', 1234])
1051-
);
1052-
});
1053-
1054-
it('should search log entries with sources filter', async () => {
1055-
poolMock.query.mockResolvedValueOnce({
1056-
rows: [
1057-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }
1058-
]
1059-
});
1060-
1061-
const logs = await errsolePostgres.searchLogs(['test'], { sources: ['test'] });
1062-
1063-
expect(logs).toEqual({
1064-
items: [{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }],
1065-
filters: { sources: ['test'], limit: 100 }
1066-
});
1067-
expect(poolMock.query).toHaveBeenCalledWith(
1068-
expect.any(String),
1069-
expect.arrayContaining(['%test%', ['test']])
1070-
);
1071-
});
1072-
1073-
it('should search log entries with levels filter', async () => {
1074-
poolMock.query.mockResolvedValueOnce({
1075-
rows: [
1076-
{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }
1077-
]
1078-
});
1079-
1080-
const logs = await errsolePostgres.searchLogs(['test'], { levels: ['info'] });
1081-
1082-
expect(logs).toEqual({
1083-
items: [{ id: 1, hostname: 'localhost', pid: 1234, source: 'test', timestamp: '2023-01-01T00:00:00Z', level: 'info', message: 'test message' }],
1084-
filters: { levels: ['info'], limit: 100 }
1085-
});
1086-
expect(poolMock.query).toHaveBeenCalledWith(
1087-
expect.any(String),
1088-
expect.arrayContaining(['%test%', ['info']])
1089-
);
1090-
});
1091-
1092987
it('should search log entries with level_json filter', async () => {
1093988
poolMock.query.mockResolvedValueOnce({
1094989
rows: [
@@ -1399,7 +1294,7 @@ describe('ErrsolePostgres', () => {
13991294
const result = await errsolePostgres.searchLogs([], filters);
14001295

14011296
expect(poolQuerySpy).toHaveBeenCalledWith(
1402-
'SELECT id, hostname, pid, source, timestamp, level, message,errsole_id FROM errsole_logs_v2 WHERE (errsole_id = $1) ORDER BY id DESC LIMIT $1',
1297+
'SELECT id, hostname, pid, source, timestamp, level, message,errsole_id FROM errsole_logs_v3 WHERE (errsole_id = $1) ORDER BY id DESC LIMIT $1',
14031298
[123, 50]
14041299
);
14051300

@@ -1443,7 +1338,7 @@ describe('ErrsolePostgres', () => {
14431338

14441339
const result = await errsolePostgres.getMeta(1);
14451340

1446-
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v2 WHERE id = $1', [1]);
1341+
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v3 WHERE id = $1', [1]);
14471342
expect(result).toEqual({ item: logMeta });
14481343
});
14491344

@@ -1452,7 +1347,7 @@ describe('ErrsolePostgres', () => {
14521347

14531348
await expect(errsolePostgres.getMeta(999)).rejects.toThrow('Log entry not found.');
14541349

1455-
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v2 WHERE id = $1', [999]);
1350+
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v3 WHERE id = $1', [999]);
14561351
});
14571352

14581353
it('should handle errors during query execution', async () => {
@@ -1461,7 +1356,7 @@ describe('ErrsolePostgres', () => {
14611356

14621357
await expect(errsolePostgres.getMeta(1)).rejects.toThrow('Query error');
14631358

1464-
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v2 WHERE id = $1', [1]);
1359+
expect(poolMock.query).toHaveBeenCalledWith('SELECT id, meta FROM errsole_logs_v3 WHERE id = $1', [1]);
14651360
});
14661361
});
14671362

@@ -1980,7 +1875,7 @@ describe('ErrsolePostgres', () => {
19801875

19811876
const result = await errsolePostgres.deleteAllLogs();
19821877

1983-
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v2 RESTART IDENTITY CASCADE');
1878+
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v3 RESTART IDENTITY CASCADE');
19841879
expect(result).toEqual({});
19851880
});
19861881

@@ -1990,7 +1885,7 @@ describe('ErrsolePostgres', () => {
19901885

19911886
await expect(errsolePostgres.deleteAllLogs()).rejects.toThrow('Query error');
19921887

1993-
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v2 RESTART IDENTITY CASCADE');
1888+
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v3 RESTART IDENTITY CASCADE');
19941889
});
19951890
});
19961891
afterAll(() => {

0 commit comments

Comments
 (0)