diff --git a/src/cache/index.test.ts b/src/cache/index.test.ts index 8438ab0..78ba118 100644 --- a/src/cache/index.test.ts +++ b/src/cache/index.test.ts @@ -38,6 +38,18 @@ describe('Cache Module', () => { expect(result).toBeNull() }) + it('should return null for internal data sources', async () => { + mockDataSource.source = 'internal' + const result = await beforeQueryCache({ + sql: 'SELECT * FROM users', + params: [], + dataSource: mockDataSource, + }) + + expect(result).toBeNull() + expect(mockDataSource.rpc.executeQuery).not.toHaveBeenCalled() + }) + it('should return null if query has parameters', async () => { const result = await beforeQueryCache({ sql: 'SELECT * FROM users WHERE id = ?', @@ -58,6 +70,18 @@ describe('Cache Module', () => { expect(result).toBeNull() }) + it('should return null if no cached row is present', async () => { + vi.mocked(mockDataSource.rpc.executeQuery).mockResolvedValue([]) + + const result = await beforeQueryCache({ + sql: 'SELECT * FROM users', + params: [], + dataSource: mockDataSource, + }) + + expect(result).toBeNull() + }) + it('should return cached result if present and valid', async () => { const cachedData = { timestamp: new Date().toISOString(), @@ -111,6 +135,19 @@ describe('Cache Module', () => { expect(mockDataSource.rpc.executeQuery).not.toHaveBeenCalled() }) + it('should not cache internal data source results', async () => { + mockDataSource.source = 'internal' + + await afterQueryCache({ + sql: 'SELECT * FROM users', + params: [], + result: [{ id: 1, name: 'John' }], + dataSource: mockDataSource, + }) + + expect(mockDataSource.rpc.executeQuery).not.toHaveBeenCalled() + }) + it('should not cache modifying queries (UPDATE)', async () => { await afterQueryCache({ sql: 'UPDATE users SET name = "John" WHERE id = 1', @@ -153,6 +190,30 @@ describe('Cache Module', () => { params: expect.any(Array), }) }) + + it('should swallow cache write errors', async () => { + const consoleErrorSpy = vi + .spyOn(console, 'error') + .mockImplementation(() => {}) + vi.mocked(mockDataSource.rpc.executeQuery).mockRejectedValue( + new Error('database unavailable') + ) + + const result = await afterQueryCache({ + sql: 'SELECT * FROM users', + params: [], + result: [{ id: 1, name: 'John' }], + dataSource: mockDataSource, + }) + + expect(result).toBeUndefined() + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'Error in cache operation:', + expect.any(Error) + ) + + consoleErrorSpy.mockRestore() + }) }) describe('hasModifyingStatement', () => {