-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.coverage.test.ts.new
More file actions
146 lines (124 loc) · 5.4 KB
/
services.coverage.test.ts.new
File metadata and controls
146 lines (124 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
process.env.NODE_ENV = 'test';
import { describe, it, expect } from '@jest/globals';
import {
createDisaster,
getAllDisasters,
countDisasters,
getDisasterById,
updateDisaster,
deleteDisaster,
findDisastersNear,
bulkInsertDisasters,
bulkUpdateDisasters,
} from './services/disaster.service';
// Mock the PostgreSQL pool for service coverage testing
jest.mock('pg', () => ({
Pool: jest.fn().mockImplementation(() => ({
query: jest.fn().mockResolvedValue({ rows: [], rowCount: 0 }),
end: jest.fn().mockResolvedValue(undefined),
})),
}));
describe('Disaster service coverage', () => {
it('should export expected functions', () => {
expect(typeof createDisaster).toBe('function');
expect(typeof getAllDisasters).toBe('function');
expect(typeof getDisasterById).toBe('function');
expect(typeof updateDisaster).toBe('function');
expect(typeof deleteDisaster).toBe('function');
expect(typeof findDisastersNear).toBe('function');
expect(typeof bulkInsertDisasters).toBe('function');
expect(typeof bulkUpdateDisasters).toBe('function');
expect(typeof countDisasters).toBe('function');
});
it('getAllDisasters: should handle both legacy and paginated calls', async () => {
// Mock implementation returns empty results for coverage
const result = await getAllDisasters();
expect(Array.isArray(result) || result.hasOwnProperty('data')).toBe(true);
});
it('countDisasters: should handle counting with and without filter', async () => {
// Test both with and without filter
const count1 = await countDisasters();
const count2 = await countDisasters({ type: 'fire' });
expect(typeof count1).toBe('number');
expect(typeof count2).toBe('number');
});
it('findDisastersNear: should handle both object and tuple parameters', async () => {
// Test both overloads for coverage
const result1 = await findDisastersNear({ lat: 1, lng: 2, distance: 3 });
const result2 = await findDisastersNear(2, 1, 3);
expect(Array.isArray(result1)).toBe(true);
expect(Array.isArray(result2)).toBe(true);
});
it('bulkInsertDisasters: should handle bulk insert operations', async () => {
const disasters = [
{
type: 'fire',
location: { type: 'Point', coordinates: [1, 2] },
date: '2025-01-01',
description: '',
status: 'active',
},
];
const result = await bulkInsertDisasters(disasters);
expect(Array.isArray(result)).toBe(true);
});
it('bulkUpdateDisasters: should handle bulk update operations', async () => {
const updates = [{ id: 1, type: 'fire', status: 'contained' }];
const result = await bulkUpdateDisasters(updates);
expect(result).toBeDefined();
});
it('individual CRUD operations: should handle basic operations', async () => {
const disasterInput = {
type: 'fire',
location: { type: 'Point', coordinates: [1, 2] },
date: '2025-01-01',
description: 'Test',
status: 'active',
};
// Test create
const created = await createDisaster(disasterInput);
expect(created).toBeDefined();
// Test get by ID
const found = await getDisasterById(1);
expect(found === null || typeof found === 'object').toBe(true);
// Test update
const updated = await updateDisaster(1, disasterInput);
expect(updated === null || typeof updated === 'object').toBe(true);
// Test delete
const deleted = await deleteDisaster(1);
expect(deleted === null || typeof deleted === 'object').toBe(true);
});
it('createDisaster: should throw on missing required fields', async () => {
await expect(createDisaster({} as any)).rejects.toThrow();
await expect(createDisaster({ type: '', location: null, date: '', status: '' } as any)).rejects.toThrow();
});
it('getAllDisasters: should handle invalid pagination and filter', async () => {
const result = await getAllDisasters({ skip: -1, limit: 0, filter: { type: 'fire', status: 'active', dateFrom: 'notadate', dateTo: 'notadate' } });
expect(Array.isArray(result)).toBe(true);
});
it('getDisasterById: should return null for invalid/empty id', async () => {
await expect(getDisasterById('')).resolves.toBeNull();
await expect(getDisasterById('not-a-uuid')).resolves.toBeNull();
});
it('updateDisaster: should return null for invalid id or no fields', async () => {
await expect(updateDisaster('', {})).resolves.toBeNull();
await expect(updateDisaster('not-a-uuid', {})).resolves.toBeNull();
});
it('deleteDisaster: should return false for invalid id', async () => {
await expect(deleteDisaster('')).resolves.toBe(false);
await expect(deleteDisaster('not-a-uuid')).resolves.toBe(false);
});
it('bulkInsertDisasters: should return [] for empty array', async () => {
await expect(bulkInsertDisasters([])).resolves.toEqual([]);
});
it('bulkUpdateDisasters: should handle empty and invalid updates', async () => {
const result = await bulkUpdateDisasters([]);
expect(result).toEqual({ matchedCount: 0, modifiedCount: 0 });
const result2 = await bulkUpdateDisasters([{ id: '', type: 'fire' } as any]);
expect(result2.matchedCount).toBe(1);
});
it('findDisastersNear: should handle invalid input', async () => {
await expect(findDisastersNear({ lat: NaN, lng: NaN, distance: NaN })).resolves.toEqual([]);
await expect(findDisastersNear({ lat: 9999, lng: 9999, distance: 1 })).resolves.toEqual([]);
});
});