Skip to content

Commit 38f116e

Browse files
committed
test(postgresql): add test cases for deleteAllLogs function
1 parent 6d4b736 commit 38f116e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/index.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,37 @@ describe('ErrsolePostgres', () => {
19621962
});
19631963
});
19641964

1965+
describe('#deleteAllLogs', () => {
1966+
let poolQuerySpy;
1967+
1968+
beforeEach(() => {
1969+
poolQuerySpy = jest.spyOn(poolMock, 'query');
1970+
poolMock.query.mockClear(); // Clear any previous calls
1971+
});
1972+
1973+
afterEach(() => {
1974+
jest.clearAllMocks();
1975+
});
1976+
1977+
it('should delete all logs and reset the table identity', async () => {
1978+
poolMock.query.mockResolvedValueOnce({});
1979+
1980+
const result = await errsolePostgres.deleteAllLogs();
1981+
1982+
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v2 RESTART IDENTITY CASCADE');
1983+
expect(result).toEqual({});
1984+
});
1985+
1986+
it('should throw an error if the query fails', async () => {
1987+
const error = new Error('Query error');
1988+
poolMock.query.mockRejectedValueOnce(error);
1989+
1990+
await expect(errsolePostgres.deleteAllLogs()).rejects.toThrow('Query error');
1991+
1992+
expect(poolQuerySpy).toHaveBeenCalledWith('TRUNCATE TABLE errsole_logs_v2 RESTART IDENTITY CASCADE');
1993+
});
1994+
});
1995+
19651996
afterAll(() => {
19661997
cronJob.stop();
19671998
clearInterval(errsolePostgres.flushIntervalId);

0 commit comments

Comments
 (0)