Skip to content

Commit

Permalink
refactor: update api packages & refactor tests (#62)
Browse files Browse the repository at this point in the history
* refactor: update api packages & refactor tests

* refactor: folder structure (test-utils)
  • Loading branch information
h4l-yup authored Oct 20, 2023
1 parent bbf09bd commit b016684
Show file tree
Hide file tree
Showing 61 changed files with 4,182 additions and 4,122 deletions.
82 changes: 41 additions & 41 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,71 @@
},
"prettier": "@ufb/prettier-config",
"dependencies": {
"@fastify/static": "^6.10.2",
"@fastify/static": "^6.11.2",
"@nestjs-modules/mailer": "^1.9.1",
"@nestjs/common": "^9.4.2",
"@nestjs/config": "^2.3.2",
"@nestjs/core": "^9.4.2",
"@nestjs/jwt": "^10.0.3",
"@nestjs/passport": "^9.0.3",
"@nestjs/platform-express": "^9.4.2",
"@nestjs/platform-fastify": "^9.4.2",
"@nestjs/swagger": "^6.3.0",
"@nestjs/terminus": "^9.2.2",
"@nestjs/typeorm": "^9.0.1",
"@opensearch-project/opensearch": "^1.2.0",
"@willsoto/nestjs-prometheus": "^5.1.2",
"axios": "^1.4.0",
"bcrypt": "^5.1.0",
"@nestjs/common": "^10.2.7",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.7",
"@nestjs/jwt": "^10.1.1",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.2.7",
"@nestjs/platform-fastify": "^10.2.7",
"@nestjs/swagger": "^7.1.13",
"@nestjs/terminus": "^10.1.1",
"@nestjs/typeorm": "^10.0.0",
"@opensearch-project/opensearch": "^2.4.0",
"@willsoto/nestjs-prometheus": "^6.0.0",
"axios": "^1.5.1",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dayjs": "^1.11.9",
"dayjs": "^1.11.10",
"exceljs": "^4.3.0",
"fast-csv": "^4.3.6",
"mysql2": "^3.3.3",
"nestjs-cls": "^3.5.0",
"nestjs-pino": "^3.2.0",
"mysql2": "^3.6.2",
"nestjs-cls": "^3.5.1",
"nestjs-pino": "^3.5.0",
"nestjs-typeorm-paginate": "^4.0.4",
"nodemailer": "^6.9.3",
"nodemailer": "^6.9.6",
"passport": "^0.6.0",
"passport-custom": "^1.1.1",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pino-http": "^8.3.3",
"pino-pretty": "^10.0.0",
"prom-client": "^14.2.0",
"pino-http": "^8.5.0",
"pino-pretty": "^10.2.3",
"prom-client": "^15.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"source-map-support": "^0.5.21",
"typeorm": "^0.3.17",
"typeorm-naming-strategies": "^4.1.0",
"typeorm-transactional": "^0.4.1",
"yup": "^0.32.11"
"yup": "0.32.11"
},
"devDependencies": {
"@faker-js/faker": "^7.6.0",
"@nestjs/cli": "^9.5.0",
"@nestjs/schematics": "^9.2.0",
"@nestjs/testing": "^9.4.2",
"@swc-node/jest": "^1.6.5",
"@swc/core": "^1.3.62",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "18.11.18",
"@types/nodemailer": "^6.4.8",
"@types/supertest": "^2.0.12",
"@faker-js/faker": "^8.2.0",
"@nestjs/cli": "^10.1.18",
"@nestjs/schematics": "^10.0.2",
"@nestjs/testing": "^10.2.7",
"@swc-node/jest": "^1.6.8",
"@swc/core": "^1.3.93",
"@types/bcrypt": "^5.0.1",
"@types/express": "^4.17.20",
"@types/jest": "^29.5.6",
"@types/node": "20.8.6",
"@types/nodemailer": "^6.4.11",
"@types/supertest": "^2.0.14",
"@ufb/eslint-config": "^0.1.0",
"@ufb/prettier-config": "^0.1.0",
"@ufb/tsconfig": "^0.1.0",
"eslint": "^8.50.0",
"jest": "^29.5.0",
"eslint": "^8.51.0",
"jest": "^29.7.0",
"mockdate": "^3.0.5",
"supertest": "^6.3.3",
"ts-jest": "29.0.3",
"ts-loader": "^9.4.3",
"ts-jest": "^29.1.1",
"ts-loader": "^9.5.0",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "4.9.5"
"typescript": "^5.2.2"
}
}
12 changes: 6 additions & 6 deletions apps/api/src/common/decorators/is-password.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class IsPasswordTest {
describe('IsPassword decorator', () => {
it('', () => {
const instance = new IsPasswordTest();
instance.password = faker.datatype.string(
faker.datatype.number({ min: 8, max: 15 }),
instance.password = faker.string.sample(
faker.number.int({ min: 8, max: 15 }),
);
const validator = new Validator();
validator.validate(instance).then((errors) => {
Expand All @@ -36,8 +36,8 @@ describe('IsPassword decorator', () => {
});
it('minLength', () => {
const instance = new IsPasswordTest();
instance.password = faker.datatype.string(
faker.datatype.number({ min: 0, max: 7 }),
instance.password = faker.string.sample(
faker.number.int({ min: 0, max: 7 }),
);
const validator = new Validator();
validator.validate(instance).then((errors) => {
Expand All @@ -48,7 +48,7 @@ describe('IsPassword decorator', () => {

it('isString', () => {
const instance = new IsPasswordTest();
instance.password = faker.datatype.number({ min: 10000000, max: 99999999 });
instance.password = faker.number.int({ min: 10000000, max: 99999999 });

const validator = new Validator();
validator.validate(instance).then((errors) => {
Expand All @@ -59,7 +59,7 @@ describe('IsPassword decorator', () => {

it('isString, minLength', () => {
const instance = new IsPasswordTest();
instance.password = faker.datatype.number({ min: 0, max: 9999999 });
instance.password = faker.number.int({ min: 0, max: 9999999 });
const validator = new Validator();
validator.validate(instance).then((errors) => {
expect(errors.length).toEqual(1);
Expand Down
43 changes: 27 additions & 16 deletions apps/api/src/common/repositories/opensearch.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { Test } from '@nestjs/testing';
import type { Client } from '@opensearch-project/opensearch';

import { getMockProvider } from '@/utils/test-utils';
import { getMockProvider } from '@/test-utils/util-functions';
import { CreateDataDto, PutMappingsDto } from './dtos';
import { OpensearchRepository } from './opensearch.repository';

Expand All @@ -47,6 +47,17 @@ const OpensearchRepositoryProviders = [
getMockProvider('OPENSEARCH_CLIENT', MockClient),
];

const COMPLICATE_JSON = {
KEY1: 'VALUE1',
KEY2: 'VALUE2',
};

const MAPPING_JSON = {
KEY1: {
type: 'text',
},
};

describe('Opensearch Repository Test suite', () => {
let osRepo: OpensearchRepository;
let osClient: Client;
Expand All @@ -60,7 +71,7 @@ describe('Opensearch Repository Test suite', () => {

describe('create index', () => {
it('positive case', async () => {
const index = faker.datatype.number().toString();
const index = faker.number.int().toString();
const indexName = 'channel_' + index;
jest.spyOn(osClient.indices, 'create');
jest.spyOn(osClient.indices, 'putAlias');
Expand Down Expand Up @@ -103,8 +114,8 @@ describe('Opensearch Repository Test suite', () => {
describe('putMappings', () => {
it('putting mappings succeeds with an existent index', async () => {
const dto = new PutMappingsDto();
dto.index = faker.datatype.number().toString();
dto.mappings = JSON.parse(faker.datatype.json());
dto.index = faker.number.int().toString();
dto.mappings = MAPPING_JSON;
jest
.spyOn(osClient.indices, 'exists')
.mockResolvedValue({ body: true } as never);
Expand All @@ -121,8 +132,8 @@ describe('Opensearch Repository Test suite', () => {
});
it('putting mappings fails with a nonexistent index', async () => {
const dto = new PutMappingsDto();
dto.index = faker.datatype.number().toString();
dto.mappings = JSON.parse(faker.datatype.json());
dto.index = faker.number.int().toString();
dto.mappings = MAPPING_JSON;
jest
.spyOn(osClient.indices, 'exists')
.mockResolvedValue({ body: false } as never);
Expand All @@ -139,12 +150,12 @@ describe('Opensearch Repository Test suite', () => {

describe('createData', () => {
it('creating data succeeds with valid inputs', async () => {
const index = faker.datatype.number().toString();
const id = faker.datatype.number().toString();
const index = faker.number.int().toString();
const id = faker.number.int().toString();
const dto = new CreateDataDto();
dto.id = id;
dto.index = index;
dto.data = JSON.parse(faker.datatype.json());
dto.data = COMPLICATE_JSON;
jest
.spyOn(osClient.indices, 'exists')
.mockResolvedValue({ body: true } as never);
Expand Down Expand Up @@ -176,18 +187,18 @@ describe('Opensearch Repository Test suite', () => {
});
});
it('creating data fails with an invalid index', async () => {
const invalidIndex = faker.datatype.number().toString();
const id = faker.datatype.number().toString();
const invalidIndex = faker.number.int().toString();
const id = faker.number.int().toString();
const dto = new CreateDataDto();
dto.id = id;
dto.index = invalidIndex;
dto.data = JSON.parse(faker.datatype.json());
dto.data = COMPLICATE_JSON;
jest
.spyOn(osClient.indices, 'exists')
.mockResolvedValue({ body: false } as never);
jest.spyOn(osClient.indices, 'getMapping').mockResolvedValue({
body: {
['channel_' + faker.datatype.number().toString()]: {
['channel_' + faker.number.int().toString()]: {
mappings: {
properties: dto.data,
},
Expand All @@ -209,9 +220,9 @@ describe('Opensearch Repository Test suite', () => {
expect(osClient.index).not.toBeCalled();
});
it('creating data fails with invalid data', async () => {
const index = faker.datatype.number().toString();
const id = faker.datatype.number().toString();
const data = JSON.parse(faker.datatype.json());
const index = faker.number.int().toString();
const id = faker.number.int().toString();
const data = COMPLICATE_JSON;
const dto = new CreateDataDto();
dto.id = id;
dto.index = index;
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/domains/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { faker } from '@faker-js/faker';
import { Test } from '@nestjs/testing';
import dayjs from 'dayjs';

import { getMockProvider } from '@/utils/test-utils';
import { getMockProvider } from '@/test-utils/util-functions';
import { TenantService } from '../tenant/tenant.service';
import { UserDto } from '../user/dtos';
import { AuthController } from './auth.controller';
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('AuthController', () => {
});
it('verifyEmailCode', () => {
const dto = new EmailVerificationCodeRequestDto();
dto.code = faker.datatype.string();
dto.code = faker.string.sample();
dto.email = faker.internet.email();
authController.verifyEmailCode(dto);

Expand All @@ -85,7 +85,7 @@ describe('AuthController', () => {
});
it('signUpInvitationUser', () => {
const dto = new InvitationUserSignUpRequestDto();
dto.code = faker.datatype.string();
dto.code = faker.string.sample();
dto.email = faker.internet.email();
dto.password = faker.internet.password();
authController.signUpInvitationUser(dto);
Expand Down
Loading

0 comments on commit b016684

Please sign in to comment.