From b7822cf88f57efe236425df438df6ea77407d991 Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 12 Apr 2023 11:47:02 +0200 Subject: [PATCH] improve tests --- package.json | 2 + .../redis-configuration.repository.spec.ts | 47 ++++++++ .../matcher/tests/unit/match.query.spec.ts | 111 ++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts create mode 100644 src/modules/matcher/tests/unit/match.query.spec.ts diff --git a/package.json b/package.json index a4896ab..d9801d5 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ ".module.ts", ".request.ts", ".presenter.ts", + ".profile.ts", ".exception.ts", "main.ts" ], @@ -107,6 +108,7 @@ ".module.ts", ".request.ts", ".presenter.ts", + ".profile.ts", ".exception.ts", "main.ts" ], diff --git a/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts b/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts new file mode 100644 index 0000000..8efa436 --- /dev/null +++ b/src/modules/configuration/tests/unit/redis-configuration.repository.spec.ts @@ -0,0 +1,47 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { RedisConfigurationRepository } from '../../adapters/secondaries/redis-configuration.repository'; +import { getRedisToken } from '@liaoliaots/nestjs-redis'; + +const mockRedis = { + get: jest.fn().mockResolvedValue('myValue'), + set: jest.fn().mockImplementation(), + del: jest.fn().mockImplementation(), +}; + +describe('RedisConfigurationRepository', () => { + let redisConfigurationRepository: RedisConfigurationRepository; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: getRedisToken('default'), + useValue: mockRedis, + }, + RedisConfigurationRepository, + ], + }).compile(); + + redisConfigurationRepository = module.get( + RedisConfigurationRepository, + ); + }); + + it('should be defined', () => { + expect(redisConfigurationRepository).toBeDefined(); + }); + + describe('interact', () => { + it('should get a value', async () => { + expect(await redisConfigurationRepository.get('myKey')).toBe('myValue'); + }); + it('should set a value', async () => { + expect( + await redisConfigurationRepository.set('myKey', 'myValue'), + ).toBeUndefined(); + }); + it('should delete a value', async () => { + expect(await redisConfigurationRepository.del('myKey')).toBeUndefined(); + }); + }); +}); diff --git a/src/modules/matcher/tests/unit/match.query.spec.ts b/src/modules/matcher/tests/unit/match.query.spec.ts new file mode 100644 index 0000000..3ad22d3 --- /dev/null +++ b/src/modules/matcher/tests/unit/match.query.spec.ts @@ -0,0 +1,111 @@ +import { MatchRequest } from '../../domain/dtos/match.request'; +import { Role } from '../../domain/entities/role.enum'; +import { IDefaultParams } from '../../domain/interfaces/default-params.type'; +import { MatchQuery } from '../../queries/match.query'; + +const defaultParams: IDefaultParams = { + DEFAULT_IDENTIFIER: 0, + MARGIN_DURATION: 900, + VALIDITY_DURATION: 365, + DEFAULT_TIMEZONE: 'Europe/Paris', +}; + +describe('Match query', () => { + it('should be defined', () => { + const matchRequest: MatchRequest = new MatchRequest(); + matchRequest.departure = '2023-04-01 12:00'; + matchRequest.waypoints = [ + { + lat: 49.440041, + lon: 1.093912, + }, + { + lat: 50.630992, + lon: 3.045432, + }, + ]; + const matchQuery: MatchQuery = new MatchQuery(matchRequest, defaultParams); + expect(matchQuery).toBeDefined(); + }); + + describe('init', () => { + it('should create a query with excluded identifiers', () => { + const matchRequest: MatchRequest = new MatchRequest(); + matchRequest.departure = '2023-04-01 12:00'; + matchRequest.waypoints = [ + { + lat: 49.440041, + lon: 1.093912, + }, + { + lat: 50.630992, + lon: 3.045432, + }, + ]; + matchRequest.identifier = 125; + matchRequest.exclusions = [126, 127, 128]; + const matchQuery: MatchQuery = new MatchQuery( + matchRequest, + defaultParams, + ); + expect(matchQuery.exclusions.length).toBe(4); + }); + }); + + it('should create a query with driver role only', () => { + const matchRequest: MatchRequest = new MatchRequest(); + matchRequest.departure = '2023-04-01 12:00'; + matchRequest.waypoints = [ + { + lat: 49.440041, + lon: 1.093912, + }, + { + lat: 50.630992, + lon: 3.045432, + }, + ]; + matchRequest.driver = true; + const matchQuery: MatchQuery = new MatchQuery(matchRequest, defaultParams); + expect(matchQuery.roles).toEqual([Role.DRIVER]); + }); + + it('should create a query with passenger role only', () => { + const matchRequest: MatchRequest = new MatchRequest(); + matchRequest.departure = '2023-04-01 12:00'; + matchRequest.waypoints = [ + { + lat: 49.440041, + lon: 1.093912, + }, + { + lat: 50.630992, + lon: 3.045432, + }, + ]; + matchRequest.passenger = true; + const matchQuery: MatchQuery = new MatchQuery(matchRequest, defaultParams); + expect(matchQuery.roles).toEqual([Role.PASSENGER]); + }); + + it('should create a query with driver and passenger roles', () => { + const matchRequest: MatchRequest = new MatchRequest(); + matchRequest.departure = '2023-04-01 12:00'; + matchRequest.waypoints = [ + { + lat: 49.440041, + lon: 1.093912, + }, + { + lat: 50.630992, + lon: 3.045432, + }, + ]; + matchRequest.passenger = true; + matchRequest.driver = true; + const matchQuery: MatchQuery = new MatchQuery(matchRequest, defaultParams); + expect(matchQuery.roles.length).toBe(2); + expect(matchQuery.roles).toContain(Role.PASSENGER); + expect(matchQuery.roles).toContain(Role.DRIVER); + }); +});