Skip to content

Commit

Permalink
chore: removed unsed imports and added tests for countries service
Browse files Browse the repository at this point in the history
  • Loading branch information
MikunsHub committed Aug 7, 2024
1 parent 489caa6 commit 90d2229
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as argon2 from 'argon2';
import { User } from 'src/core/entities/user.entity';
import { createApiResponse } from 'src/utility/utility.core';
import { User } from '../core/entities/user.entity';
import { createApiResponse } from '../utility/utility.core';

@Injectable()
export class AuthService {
Expand Down
6 changes: 3 additions & 3 deletions src/countries/countries.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { CountryService } from './countries.service';
import { ApiResponsePaginated } from 'src/core/interfaces/api-response.interface';
import { createApiResponsePaginated } from 'src/utility/utility.core';
import { Country } from 'src/core/entities/countries.entity';
import { ApiResponsePaginated } from '../core/interfaces/api-response.interface';
import { createApiResponsePaginated } from '../utility/utility.core';
import { Country } from '../core/entities/countries.entity';
import { FetchCountriesDto } from './dto/fetch-countries.dto';
import { FetchCountryDto } from './dto/fetch-country.dto';

Expand Down
2 changes: 1 addition & 1 deletion src/countries/countries.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { CountryService } from './countries.service';
import { CountriesController } from './countries.controller';
import { RegionsModule } from '../regions/regions.module';
import { Country } from 'src/core/entities/countries.entity';
import { Country } from '../core/entities/countries.entity';

@Module({
imports: [TypeOrmModule.forFeature([Country]), RegionsModule],
Expand Down
7 changes: 1 addition & 6 deletions src/countries/countries.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {
Injectable,
Logger,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { CreateCountryDto } from './dto/create-country.dto';
import { RegionsService } from '../regions/regions.service';
import axios from 'axios';
import { Country } from 'src/core/entities/countries.entity';
import { Country } from '../core/entities/countries.entity';
import { FetchCountriesDto } from './dto/fetch-countries.dto';
import { FetchCountryDto } from './dto/fetch-country.dto';

Expand All @@ -20,7 +16,6 @@ export class CountryService {
constructor(
@InjectRepository(Country)
private readonly countriesRepository: Repository<Country>,
private readonly regionsService: RegionsService,
) {}

async getCountries(
Expand Down
62 changes: 62 additions & 0 deletions src/countries/test/countries.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Country } from '../../core/entities/countries.entity';
import { NotFoundException } from '@nestjs/common';
import { CountryService } from '../countries.service';
import { RegionsService } from '../../regions/regions.service';

describe('CountryService', () => {
let service: CountryService;
let countryRepository: Repository<Country>;

const mockCountryRepository = {
createQueryBuilder: jest.fn(() => ({
innerJoinAndSelect: jest.fn().mockReturnThis(),
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
take: jest.fn().mockReturnThis(),
getMany: jest.fn(),
getCount: jest.fn(),
getOne: jest.fn(),
})),
find: jest.fn(),
};

const mockRegionsService = {};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CountryService,
{
provide: getRepositoryToken(Country),
useValue: mockCountryRepository,
},
{
provide: RegionsService,
useValue: mockRegionsService,
},
],
}).compile();

service = module.get<CountryService>(CountryService);
countryRepository = module.get<Repository<Country>>(getRepositoryToken(Country));
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('getCountryDetail', () => {

it('should throw NotFoundException when country is not found', async () => {
mockCountryRepository.createQueryBuilder().getOne.mockResolvedValue(null);

await expect(service.getCountryDetail({ commonName: 'Non-existent Country' }))
.rejects.toThrow(NotFoundException);
});
});
});
2 changes: 1 addition & 1 deletion src/languages/languages.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LanguagesController } from './languages.controller';
import { LanguagesService } from './languages.service';
import { Country } from 'src/core/entities/countries.entity';
import { Country } from '../core/entities/countries.entity';

@Module({
imports: [TypeOrmModule.forFeature([Country])],
Expand Down
2 changes: 1 addition & 1 deletion src/languages/languages.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Country } from 'src/core/entities/countries.entity';
import { Country } from '../core/entities/countries.entity';
import { FetchLanguagesDto } from './dto/fetch-languages.dto';

@Injectable()
Expand Down
4 changes: 2 additions & 2 deletions src/regions/regions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { RegionsController } from './regions.controller';
import { RegionsService } from './regions.service';
import { Region } from '../core/entities/regions.entity';
import { Country } from 'src/core/entities/countries.entity';
import { UtilityModule } from 'src/utility/utility.module';
import { Country } from '../core/entities/countries.entity';
import { UtilityModule } from '../utility/utility.module';

@Module({
imports: [TypeOrmModule.forFeature([Region, Country]),UtilityModule],
Expand Down
5 changes: 2 additions & 3 deletions src/regions/regions.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Country } from 'src/core/entities/countries.entity';
import { Region } from 'src/core/entities/regions.entity';
import { Region } from '../core/entities/regions.entity';
import { FetchRegionsDto } from './dto/fetch-regions.dto';
import { UtilityService } from 'src/utility/utility.service';
import { UtilityService } from '../utility/utility.service';

@Injectable()
export class RegionsService {
Expand Down
2 changes: 1 addition & 1 deletion src/statistics/statistics.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { StatisticsService } from './statistics.service';
import { StatisticsController } from './statistics.controller';
import { Country } from 'src/core/entities/countries.entity';
import { Country } from '../core/entities/countries.entity';

@Module({
imports: [TypeOrmModule.forFeature([Country])],
Expand Down
4 changes: 2 additions & 2 deletions src/statistics/statistics.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Country } from 'src/core/entities/countries.entity';
import { createApiResponsePaginated } from 'src/utility/utility.core';
import { Country } from '../core/entities/countries.entity';
import { createApiResponsePaginated } from '../utility/utility.core';

// Define CountryData type to reflect the correct structure
interface CountryData {
Expand Down
2 changes: 1 addition & 1 deletion src/utility/utility.core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from 'crypto';
import { ApiBaseResponse, ApiResponsePaginated } from 'src/core/interfaces/api-response.interface';
import { ApiBaseResponse, ApiResponsePaginated } from '../core/interfaces/api-response.interface';

const generateRandomString = (length: number): string => {
return randomBytes(length).toString('hex');
Expand Down

0 comments on commit 90d2229

Please sign in to comment.