Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/cache/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?',
Expand All @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down