akshitkrnagpal / akshitkrnagpal/keyv-dataloader
Improve test coverage for edge cases and cache failures
- Dominant language
- TypeScript
- Stars
- 1
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
While reviewing the test suite, I noticed some gaps in coverage, particularly around error handling and edge cases that could occur in production.
### Issues
1. Missing tests for concurrent cache access patterns
2. No tests for cache connection timeouts or failures
3. Limited coverage of edge cases with unusual input types
### Proposed Tests to Add
1. **Concurrent operations test**:
```typescript
test('should handle concurrent operations correctly', async () => {
const testBatchLoadFn = jest.fn(async (keys) => {
// Add a slight delay to simulate db access
await new Promise(resolve => setTimeout(resolve, 50));
return keys.map(key => `Value for ${key}`);
});
const loader = new KeyvDataLoader({
batchLoadFn: testBatchLoadFn,
ttl: 1000,
});
// Run many loads concurrently
const promises = Array.from({ length: 100 }, (_, i) =>
loader.load(`key-${i % 10}`) // Only 10 unique keys to test deduplication
);
const results = await Promise.all(promises);
// Verify results are correct
expect(results.length).toBe(100);
// Verify batch function was called efficiently
// Should be called at most 10 times (once per unique key)
expect(testBatchLoadFn.mock.calls.length).toBeLessThanOrEqual(10);
});
```
2. **Cache failure test**:
```typescript
test('should handle cache failures gracefully', async () => {
// Create a mock Keyv implementation that throws on operations
const failingCache = {
get: jest.fn().mockRejectedValue(new Error('Cache failure')),
set: jest.fn().mockRejectedValue(new Error('Cache failure')),
delete: jest.fn().mockRejectedValue(new Error('Cache failure')),
clear: jest.fn().mockRejectedValue(new Error('Cache failure')),
};
// Override the Keyv constructor to return our failing implementation
jest.mock('keyv', () => {
return jest.fn().mockImplementation(() => failingCache);
});
const testBatchLoadFn = jest.fn(async (keys) => {
return keys.map(key => `Value for ${key}`);
});
const loader = new KeyvDataLoader({
batchLoadFn: testBatchLoadFn,
ttl: 1000,
});
// Should still work even with cache failures
const result = await loader.load('test-key');
expect(result).toBe('Value for test-key');
// Batch function should be called because cache failed
expect(testBatchLoadFn).toHaveBeenCalled();
});
```
3. **Edge case tests for unusual inputs**:
```typescript
test('should handle special characters in cache keys', async () => {
const loader = new KeyvDataLoader({
batchLoadFn: async (keys) => keys.map(key => `Value for ${key}`),
ttl: 1000,
});
const specialKeys = [
'key with spaces',
'key:with:colons',
'key/with/slashes',
'key+with+plus',
'key&with&ersand',
'key#with#hash',
'key?with?question',
JSON.stringify({complex: 'object', with: {nested: 'values'}}),
];
for (const key of specialKeys) {
const result = await loader.load(key);
expect(result).toBe(`Value for ${key}`);
// Load again to verify caching works
const cachedResult = await loader.load(key);
expect(cachedResult).toBe(`Value for ${key}`);
}
});
```
These additional tests would help ensure the library works correctly in more real-world scenarios and improves the overall reliability of the codebase.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.