|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { EagerResult, newError, Record, Result, ResultSummary } from '../src' |
| 18 | +import { EagerResult, Integer, newError, Record, Result, ResultSummary } from '../src' |
19 | 19 | import resultTransformers from '../src/result-transformers'
|
20 | 20 | import ResultStreamObserverMock from './utils/result-stream-observer.mock'
|
21 | 21 |
|
@@ -268,6 +268,168 @@ describe('resultTransformers', () => {
|
268 | 268 | })
|
269 | 269 | })
|
270 | 270 |
|
| 271 | + describe('.summary()', () => { |
| 272 | + describe('with a valid result', () => { |
| 273 | + it('should return a ResultSummary', async () => { |
| 274 | + const resultStreamObserverMock = new ResultStreamObserverMock() |
| 275 | + const query = 'Query' |
| 276 | + const params = { a: 1 } |
| 277 | + const meta = { db: 'adb' } |
| 278 | + const result = new Result(Promise.resolve(resultStreamObserverMock), query, params) |
| 279 | + const keys = ['a', 'b'] |
| 280 | + const rawRecord1 = [1, 2] |
| 281 | + const rawRecord2 = [3, 4] |
| 282 | + resultStreamObserverMock.onKeys(keys) |
| 283 | + resultStreamObserverMock.onNext(rawRecord1) |
| 284 | + resultStreamObserverMock.onNext(rawRecord2) |
| 285 | + resultStreamObserverMock.onCompleted(meta) |
| 286 | + |
| 287 | + const summary: ResultSummary = await resultTransformers.summary()(result) |
| 288 | + |
| 289 | + expect(summary).toEqual( |
| 290 | + new ResultSummary(query, params, meta) |
| 291 | + ) |
| 292 | + }) |
| 293 | + |
| 294 | + it('should cancel stream', async () => { |
| 295 | + const meta = { db: 'adb' } |
| 296 | + const resultStreamObserverMock = new ResultStreamObserverMock() |
| 297 | + const cancelSpy = jest.spyOn(resultStreamObserverMock, 'cancel') |
| 298 | + cancelSpy.mockImplementation(() => resultStreamObserverMock.onCompleted(meta)) |
| 299 | + const query = 'Query' |
| 300 | + const params = { a: 1 } |
| 301 | + const result = new Result(Promise.resolve(resultStreamObserverMock), query, params) |
| 302 | + const keys = ['a', 'b'] |
| 303 | + const rawRecord1 = [1, 2] |
| 304 | + const rawRecord2 = [3, 4] |
| 305 | + resultStreamObserverMock.onKeys(keys) |
| 306 | + resultStreamObserverMock.onNext(rawRecord1) |
| 307 | + resultStreamObserverMock.onNext(rawRecord2) |
| 308 | + |
| 309 | + const summary: ResultSummary = await resultTransformers.summary()(result) |
| 310 | + |
| 311 | + expect(cancelSpy).toHaveBeenCalledTimes(1) |
| 312 | + expect(summary).toEqual( |
| 313 | + new ResultSummary(query, params, meta) |
| 314 | + ) |
| 315 | + }) |
| 316 | + |
| 317 | + it('should return a ResultSummary<number>', async () => { |
| 318 | + const resultStreamObserverMock = new ResultStreamObserverMock() |
| 319 | + const query = 'Query' |
| 320 | + const params = { a: 1 } |
| 321 | + const meta = { db: 'adb' } |
| 322 | + const result = new Result(Promise.resolve(resultStreamObserverMock), query, params) |
| 323 | + const keys = ['model', 'year'] |
| 324 | + const rawRecord1 = ['Beautiful Sedan', 1987] |
| 325 | + const rawRecord2 = ['Hot Hatch', 1995] |
| 326 | + |
| 327 | + resultStreamObserverMock.onKeys(keys) |
| 328 | + resultStreamObserverMock.onNext(rawRecord1) |
| 329 | + resultStreamObserverMock.onNext(rawRecord2) |
| 330 | + resultStreamObserverMock.onCompleted(meta) |
| 331 | + const summary = await resultTransformers.summary<number>()(result) |
| 332 | + |
| 333 | + const typeAssertionNumber: ResultSummary<number> = summary |
| 334 | + // @ts-expect-error |
| 335 | + const typeAssertionInteger: ResultSummary<Integer> = summary |
| 336 | + // @ts-expect-error |
| 337 | + const typeAssertionBigInt: ResultSummary<bigint> = summary |
| 338 | + |
| 339 | + expect(typeAssertionNumber).toEqual( |
| 340 | + new ResultSummary<Integer>(query, params, meta) |
| 341 | + ) |
| 342 | + |
| 343 | + expect(typeAssertionInteger).toEqual( |
| 344 | + new ResultSummary<Integer>(query, params, meta) |
| 345 | + ) |
| 346 | + |
| 347 | + expect(typeAssertionBigInt).toEqual( |
| 348 | + new ResultSummary<Integer>(query, params, meta) |
| 349 | + ) |
| 350 | + }) |
| 351 | + |
| 352 | + it('should return a ResultSummary<bigint>', async () => { |
| 353 | + const resultStreamObserverMock = new ResultStreamObserverMock() |
| 354 | + const query = 'Query' |
| 355 | + const params = { a: 1 } |
| 356 | + const meta = { db: 'adb' } |
| 357 | + const result = new Result(Promise.resolve(resultStreamObserverMock), query, params) |
| 358 | + const keys = ['model', 'year'] |
| 359 | + const rawRecord1 = ['Beautiful Sedan', 1987] |
| 360 | + const rawRecord2 = ['Hot Hatch', 1995] |
| 361 | + |
| 362 | + resultStreamObserverMock.onKeys(keys) |
| 363 | + resultStreamObserverMock.onNext(rawRecord1) |
| 364 | + resultStreamObserverMock.onNext(rawRecord2) |
| 365 | + resultStreamObserverMock.onCompleted(meta) |
| 366 | + const summary = await resultTransformers.summary<bigint>()(result) |
| 367 | + |
| 368 | + const typeAssertionBigInt: ResultSummary<bigint> = summary |
| 369 | + // @ts-expect-error |
| 370 | + const typeAssertionNumber: ResultSummary<number> = summary |
| 371 | + // @ts-expect-error |
| 372 | + const typeAssertionInteger: ResultSummary<Integer> = summary |
| 373 | + |
| 374 | + expect(typeAssertionNumber).toEqual( |
| 375 | + new ResultSummary<Integer>(query, params, meta) |
| 376 | + ) |
| 377 | + |
| 378 | + expect(typeAssertionInteger).toEqual( |
| 379 | + new ResultSummary<Integer>(query, params, meta) |
| 380 | + ) |
| 381 | + |
| 382 | + expect(typeAssertionBigInt).toEqual( |
| 383 | + new ResultSummary<Integer>(query, params, meta) |
| 384 | + ) |
| 385 | + }) |
| 386 | + |
| 387 | + it('should return a ResultSummary<Integer>', async () => { |
| 388 | + const resultStreamObserverMock = new ResultStreamObserverMock() |
| 389 | + const query = 'Query' |
| 390 | + const params = { a: 1 } |
| 391 | + const meta = { db: 'adb' } |
| 392 | + const result = new Result(Promise.resolve(resultStreamObserverMock), query, params) |
| 393 | + const keys = ['model', 'year'] |
| 394 | + const rawRecord1 = ['Beautiful Sedan', 1987] |
| 395 | + const rawRecord2 = ['Hot Hatch', 1995] |
| 396 | + |
| 397 | + resultStreamObserverMock.onKeys(keys) |
| 398 | + resultStreamObserverMock.onNext(rawRecord1) |
| 399 | + resultStreamObserverMock.onNext(rawRecord2) |
| 400 | + resultStreamObserverMock.onCompleted(meta) |
| 401 | + const summary = await resultTransformers.summary<Integer>()(result) |
| 402 | + |
| 403 | + const typeAssertionInteger: ResultSummary<Integer> = summary |
| 404 | + // @ts-expect-error |
| 405 | + const typeAssertionNumber: ResultSummary<number> = summary |
| 406 | + // @ts-expect-error |
| 407 | + const typeAssertionBigInt: ResultSummary<bigint> = summary |
| 408 | + |
| 409 | + expect(typeAssertionInteger).toEqual( |
| 410 | + new ResultSummary<Integer>(query, params, meta) |
| 411 | + ) |
| 412 | + |
| 413 | + expect(typeAssertionNumber).toEqual( |
| 414 | + new ResultSummary<Integer>(query, params, meta) |
| 415 | + ) |
| 416 | + |
| 417 | + expect(typeAssertionBigInt).toEqual( |
| 418 | + new ResultSummary<Integer>(query, params, meta) |
| 419 | + ) |
| 420 | + }) |
| 421 | + }) |
| 422 | + |
| 423 | + describe('when results fail', () => { |
| 424 | + it('should propagate the exception', async () => { |
| 425 | + const expectedError = newError('expected error') |
| 426 | + const result = new Result(Promise.reject(expectedError), 'query') |
| 427 | + |
| 428 | + await expect(resultTransformers.summary()(result)).rejects.toThrow(expectedError) |
| 429 | + }) |
| 430 | + }) |
| 431 | + }) |
| 432 | + |
271 | 433 | describe('.first', () => {
|
272 | 434 | describe('with a valid result', () => {
|
273 | 435 | it('should return an single Record', async () => {
|
|
0 commit comments