|
1 | 1 | import { getTime, isAfter } from 'date-fns'
|
2 | 2 | import shortid from 'shortid'
|
3 | 3 |
|
| 4 | +import PageRequest, { UnpagedRequest } from '../../../clients/db/PageRequest' |
4 | 5 | import PatientRepository from '../../../clients/db/PatientRepository'
|
| 6 | +import SortRequest from '../../../clients/db/SortRequest' |
5 | 7 | import { patients } from '../../../config/pouchdb'
|
6 | 8 | import Patient from '../../../model/Patient'
|
7 | 9 |
|
@@ -224,4 +226,197 @@ describe('patient repository', () => {
|
224 | 226 | expect(allDocs.total_rows).toEqual(0)
|
225 | 227 | })
|
226 | 228 | })
|
| 229 | + |
| 230 | + describe('findAllPaged', () => { |
| 231 | + const patientsData = [ |
| 232 | + { _id: 'a', fullName: 'a', code: 'P-a', index: 'aP-a' }, |
| 233 | + { _id: 'b', fullName: 'b', code: 'P-b', index: 'bP-b' }, |
| 234 | + { _id: 'c', fullName: 'c', code: 'P-c', index: 'cP-c' }, |
| 235 | + ] |
| 236 | + |
| 237 | + afterEach(async () => { |
| 238 | + await removeAllDocs() |
| 239 | + }) |
| 240 | + |
| 241 | + beforeEach(async () => { |
| 242 | + await PatientRepository.createIndex() |
| 243 | + patientsData.forEach((patientData) => patients.put(patientData)) |
| 244 | + }) |
| 245 | + |
| 246 | + it('should find all patients in the database', async () => { |
| 247 | + const result = await PatientRepository.findAllPaged() |
| 248 | + |
| 249 | + expect(result.hasNext).toEqual(false) |
| 250 | + expect(result.hasPrevious).toEqual(false) |
| 251 | + expect(result.content).toHaveLength(patientsData.length) |
| 252 | + }) |
| 253 | + |
| 254 | + it('should find all patients in the database with sort request and page request', async () => { |
| 255 | + const sortRequest: SortRequest = { |
| 256 | + sorts: [{ field: 'index', direction: 'asc' }], |
| 257 | + } |
| 258 | + const pageRequest: PageRequest = { |
| 259 | + number: 1, |
| 260 | + size: 1, |
| 261 | + direction: 'next', |
| 262 | + nextPageInfo: undefined, |
| 263 | + previousPageInfo: undefined, |
| 264 | + } |
| 265 | + |
| 266 | + const result = await PatientRepository.findAllPaged(sortRequest, pageRequest) |
| 267 | + |
| 268 | + expect(result.content).toHaveLength(1) |
| 269 | + expect(result.hasNext).toEqual(true) |
| 270 | + expect(result.hasPrevious).toEqual(false) |
| 271 | + expect(result.pageRequest?.nextPageInfo).toEqual({ index: 'bP-b' }) |
| 272 | + }) |
| 273 | + |
| 274 | + it('page request less than number of records', async () => { |
| 275 | + const sortRequest: SortRequest = { |
| 276 | + sorts: [{ field: 'index', direction: 'asc' }], |
| 277 | + } |
| 278 | + |
| 279 | + const pageRequest: PageRequest = { |
| 280 | + number: 1, |
| 281 | + size: 4, |
| 282 | + direction: 'next', |
| 283 | + nextPageInfo: undefined, |
| 284 | + previousPageInfo: undefined, |
| 285 | + } |
| 286 | + |
| 287 | + const result = await PatientRepository.findAllPaged(sortRequest, pageRequest) |
| 288 | + |
| 289 | + expect(result.content).toHaveLength(patientsData.length) |
| 290 | + expect(result.hasNext).toEqual(false) |
| 291 | + expect(result.hasPrevious).toEqual(false) |
| 292 | + expect(result.pageRequest?.nextPageInfo).toBe(undefined) |
| 293 | + expect(result.pageRequest?.previousPageInfo).toBe(undefined) |
| 294 | + }) |
| 295 | + |
| 296 | + it('go till last page', async () => { |
| 297 | + const sortRequest: SortRequest = { |
| 298 | + sorts: [{ field: 'index', direction: 'asc' }], |
| 299 | + } |
| 300 | + const pageRequest1: PageRequest = { |
| 301 | + number: 1, |
| 302 | + size: 1, |
| 303 | + direction: 'next', |
| 304 | + nextPageInfo: undefined, |
| 305 | + previousPageInfo: undefined, |
| 306 | + } |
| 307 | + |
| 308 | + const result1 = await PatientRepository.findAllPaged(sortRequest, pageRequest1) |
| 309 | + |
| 310 | + const pageRequest2: PageRequest = { |
| 311 | + number: 2, |
| 312 | + size: 1, |
| 313 | + direction: 'next', |
| 314 | + nextPageInfo: result1.pageRequest?.nextPageInfo, |
| 315 | + previousPageInfo: undefined, |
| 316 | + } |
| 317 | + const result2 = await PatientRepository.findAllPaged(sortRequest, pageRequest2) |
| 318 | + |
| 319 | + expect(result2.hasPrevious).toBe(true) |
| 320 | + expect(result2.hasNext).toBe(true) |
| 321 | + |
| 322 | + const pageRequest3: PageRequest = { |
| 323 | + number: 2, |
| 324 | + size: 1, |
| 325 | + direction: 'next', |
| 326 | + nextPageInfo: result2.pageRequest?.nextPageInfo, |
| 327 | + previousPageInfo: undefined, |
| 328 | + } |
| 329 | + const result3 = await PatientRepository.findAllPaged(sortRequest, pageRequest3) |
| 330 | + |
| 331 | + expect(result3.content).toHaveLength(1) |
| 332 | + expect(result3.hasNext).toEqual(false) |
| 333 | + expect(result3.hasPrevious).toEqual(true) |
| 334 | + expect(result3.content.length).toEqual(1) |
| 335 | + expect(result3.pageRequest?.previousPageInfo).toEqual({ index: 'cP-c' }) |
| 336 | + }) |
| 337 | + |
| 338 | + it('go to previous page', async () => { |
| 339 | + const sortRequest: SortRequest = { |
| 340 | + sorts: [{ field: 'index', direction: 'asc' }], |
| 341 | + } |
| 342 | + const pageRequest1: PageRequest = { |
| 343 | + number: 1, |
| 344 | + size: 1, |
| 345 | + direction: 'next', |
| 346 | + nextPageInfo: undefined, |
| 347 | + previousPageInfo: undefined, |
| 348 | + } |
| 349 | + |
| 350 | + const result1 = await PatientRepository.findAllPaged(sortRequest, pageRequest1) |
| 351 | + |
| 352 | + const pageRequest2: PageRequest = { |
| 353 | + number: 2, |
| 354 | + size: 1, |
| 355 | + direction: 'next', |
| 356 | + nextPageInfo: result1.pageRequest?.nextPageInfo, |
| 357 | + previousPageInfo: undefined, |
| 358 | + } |
| 359 | + const result2 = await PatientRepository.findAllPaged(sortRequest, pageRequest2) |
| 360 | + |
| 361 | + expect(result2.hasPrevious).toBe(true) |
| 362 | + expect(result2.hasNext).toBe(true) |
| 363 | + |
| 364 | + const pageRequest3: PageRequest = { |
| 365 | + number: 1, |
| 366 | + size: 1, |
| 367 | + direction: 'previous', |
| 368 | + nextPageInfo: undefined, |
| 369 | + previousPageInfo: result2.pageRequest?.previousPageInfo, |
| 370 | + } |
| 371 | + const result3 = await PatientRepository.findAllPaged(sortRequest, pageRequest3) |
| 372 | + |
| 373 | + expect(result3.content).toHaveLength(1) |
| 374 | + expect(result3.hasNext).toEqual(true) |
| 375 | + expect(result3.hasPrevious).toEqual(false) |
| 376 | + expect(result3.content.length).toEqual(1) |
| 377 | + expect(result3.content[0].index).toEqual('aP-a') |
| 378 | + expect(result3.pageRequest?.nextPageInfo).toEqual({ index: 'bP-b' }) |
| 379 | + }) |
| 380 | + }) |
| 381 | + |
| 382 | + describe('searchPaged', () => { |
| 383 | + const patientsData = [ |
| 384 | + { _id: 'a', fullName: 'a', code: 'P-a', index: 'aP-a' }, |
| 385 | + { _id: 'b', fullName: 'b', code: 'P-b', index: 'bP-b' }, |
| 386 | + { _id: 'c', fullName: 'c', code: 'P-c', index: 'cP-c' }, |
| 387 | + ] |
| 388 | + |
| 389 | + afterEach(async () => { |
| 390 | + await removeAllDocs() |
| 391 | + }) |
| 392 | + |
| 393 | + beforeEach(async () => { |
| 394 | + await PatientRepository.createIndex() |
| 395 | + patientsData.forEach((patientData) => patients.put(patientData)) |
| 396 | + }) |
| 397 | + |
| 398 | + it('should search patient in the database', async () => { |
| 399 | + const result = await PatientRepository.searchPaged('a') |
| 400 | + |
| 401 | + expect(result.content).toHaveLength(1) |
| 402 | + expect(result.hasNext).toEqual(false) |
| 403 | + expect(result.hasPrevious).toEqual(false) |
| 404 | + |
| 405 | + expect(result.content.length).toEqual(1) |
| 406 | + }) |
| 407 | + |
| 408 | + it('should search patient in the database with sort request', async () => { |
| 409 | + const sortRequest: SortRequest = { |
| 410 | + sorts: [{ field: 'index', direction: 'asc' }], |
| 411 | + } |
| 412 | + |
| 413 | + const result = await PatientRepository.searchPaged('a', UnpagedRequest, sortRequest) |
| 414 | + |
| 415 | + expect(result.content).toHaveLength(1) |
| 416 | + expect(result.hasNext).toEqual(false) |
| 417 | + expect(result.hasPrevious).toEqual(false) |
| 418 | + |
| 419 | + expect(result.content.length).toEqual(1) |
| 420 | + }) |
| 421 | + }) |
227 | 422 | })
|
0 commit comments