-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(misc): function scan examples (#78)
- Loading branch information
Showing
18 changed files
with
1,176 additions
and
145 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MiscModule } from './misc.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { MiscController } from './misc.controller'; | ||
import { DateService, FileService, XmlService } from './services'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RawBodyRequest } from '@nestjs/common'; | ||
import { IncomingMessage } from 'http'; | ||
|
||
describe('MiscController', () => { | ||
let miscController: MiscController; | ||
let dateService: jest.Mocked<DateService>; | ||
let fileService: jest.Mocked<FileService>; | ||
let xmlService: jest.Mocked<XmlService>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [MiscController], | ||
providers: [ | ||
{ | ||
provide: DateService, | ||
useValue: { | ||
calculateWeekdays: jest.fn() | ||
} | ||
}, | ||
{ | ||
provide: FileService, | ||
useValue: { | ||
fetch: jest.fn() | ||
} | ||
}, | ||
{ | ||
provide: XmlService, | ||
useValue: { | ||
parse: jest.fn() | ||
} | ||
} | ||
] | ||
}).compile(); | ||
|
||
miscController = module.get<MiscController>(MiscController); | ||
dateService = module.get(DateService); | ||
fileService = module.get(FileService); | ||
xmlService = module.get(XmlService); | ||
}); | ||
|
||
it('should be defined', () => expect(miscController).toBeDefined()); | ||
|
||
describe('fetch', () => { | ||
it('should call fileService fetch() with the provided URL', async () => { | ||
const url = 'https://example.com'; | ||
const expectedResult = 'fetched content'; | ||
fileService.fetch.mockResolvedValue(expectedResult); | ||
|
||
const result = await miscController.fetch({ url }); | ||
|
||
expect(fileService.fetch).toHaveBeenCalledWith(url); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('parse', () => { | ||
it('should call xmlService parse() with the request body', async () => { | ||
const xmlBody = '<root><child>content</child></root>'; | ||
const expectedResult = 'parsed content'; | ||
xmlService.parse.mockResolvedValue(expectedResult); | ||
const mockRequest = { | ||
rawBody: Buffer.from(xmlBody) | ||
} as RawBodyRequest<IncomingMessage>; | ||
|
||
const result = await miscController.parse(mockRequest); | ||
|
||
expect(xmlService.parse).toHaveBeenCalledWith(xmlBody); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('weekdays', () => { | ||
it('should call dateService calculateWeekdays() and return JSON result', async () => { | ||
const from = '2023-01-01'; | ||
const to = '2023-12-31'; | ||
const weekday = 1; | ||
const count = 52; | ||
dateService.calculateWeekdays.mockResolvedValue(count); | ||
|
||
const result = await miscController.weekdays( | ||
from, | ||
to, | ||
weekday.toString() | ||
); | ||
|
||
expect(dateService.calculateWeekdays).toHaveBeenCalledWith( | ||
from, | ||
to, | ||
weekday | ||
); | ||
expect(result).toBe(JSON.stringify({ count }, null, 2)); | ||
}); | ||
|
||
it('should use default weekday 1 if not provided', async () => { | ||
const from = '2023-01-01'; | ||
const to = '2023-12-31'; | ||
const count = 52; | ||
dateService.calculateWeekdays.mockResolvedValue(count); | ||
|
||
await miscController.weekdays(from, to); | ||
|
||
expect(dateService.calculateWeekdays).toHaveBeenCalledWith(from, to, 1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-disable max-classes-per-file */ | ||
import { DateService, FileService, XmlService } from './services'; | ||
import { | ||
BadRequestException, | ||
Body, | ||
Controller, | ||
Get, | ||
Post, | ||
Query, | ||
RawBodyRequest, | ||
Req | ||
} from '@nestjs/common'; | ||
import { | ||
ApiResponse, | ||
ApiTags, | ||
ApiBody, | ||
ApiQuery, | ||
ApiProperty | ||
} from '@nestjs/swagger'; | ||
import { IncomingMessage } from 'http'; | ||
|
||
class FetchDto { | ||
@ApiProperty({ description: 'URL to fetch content from' }) | ||
public url!: string; | ||
} | ||
|
||
class WeekdaysResponseDto { | ||
@ApiProperty({ description: 'Number of weekdays in the given range' }) | ||
public count!: number; | ||
} | ||
|
||
@Controller('misc') | ||
@ApiTags('misc') | ||
export class MiscController { | ||
constructor( | ||
private readonly dateService: DateService, | ||
private readonly fileService: FileService, | ||
private readonly xmlService: XmlService | ||
) {} | ||
|
||
@Post('/fetch') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Successfully fetched the content from the URL', | ||
type: String | ||
}) | ||
@ApiBody({ type: FetchDto }) | ||
public fetch(@Body() body: FetchDto): Promise<string> { | ||
return this.fileService.fetch(body.url); | ||
} | ||
|
||
@Post('/xml') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Successfully parsed XML', | ||
type: Object | ||
}) | ||
@ApiBody({ type: String, description: 'Raw XML string' }) | ||
public parse(@Req() req: RawBodyRequest<IncomingMessage>): Promise<string> { | ||
if (!req.rawBody) { | ||
throw new BadRequestException('Request body is required'); | ||
} | ||
|
||
return this.xmlService.parse(req.rawBody.toString()); | ||
} | ||
|
||
@Get('/weekdays') | ||
@ApiResponse({ | ||
status: 200, | ||
description: | ||
'Successfully calculated number of given weekday in date range', | ||
type: WeekdaysResponseDto | ||
}) | ||
@ApiQuery({ | ||
name: 'from', | ||
required: true, | ||
type: String, | ||
description: 'Start date (YYYY-MM-DD)' | ||
}) | ||
@ApiQuery({ | ||
name: 'to', | ||
required: true, | ||
type: String, | ||
description: 'End date (YYYY-MM-DD)' | ||
}) | ||
@ApiQuery({ | ||
name: 'weekday', | ||
required: false, | ||
type: Number, | ||
description: 'Weekday number (0-6, where 0 is Sunday)' | ||
}) | ||
public async weekdays( | ||
@Query('from') from: string, | ||
@Query('to') to: string, | ||
@Query('weekday') weekday?: string | ||
): Promise<string> { | ||
const count = await this.dateService.calculateWeekdays( | ||
from, | ||
to, | ||
weekday ? +weekday : 1 | ||
); | ||
|
||
return JSON.stringify({ count }, null, 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MiscController } from './misc.controller'; | ||
import { DateService, FileService, XmlService } from './services'; | ||
import { Module } from '@nestjs/common'; | ||
|
||
@Module({ | ||
imports: [], | ||
providers: [DateService, FileService, XmlService], | ||
controllers: [MiscController] | ||
}) | ||
export class MiscModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { DateService } from './date.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
describe('DateService', () => { | ||
let service: DateService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [DateService] | ||
}).compile(); | ||
|
||
service = module.get<DateService>(DateService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('calculateWeekdays', () => { | ||
it('should calculate weekdays correctly', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2024-07-15', | ||
'2024-07-15', | ||
1 | ||
); | ||
|
||
expect(result).toBe(1); | ||
}); | ||
|
||
it('should handle different weekdays', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2024-07-14', | ||
'2024-07-15', | ||
0 | ||
); | ||
|
||
expect(result).toBe(1); | ||
}); | ||
|
||
it('should use default weekday if not provided', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2024-07-01', | ||
'2024-07-31' | ||
); | ||
|
||
expect(result).toBe(5); | ||
}); | ||
|
||
it('should handle common years correctly', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2023-01-01', | ||
'2023-12-31', | ||
3 | ||
); | ||
|
||
expect(result).toBe(52); | ||
}); | ||
|
||
it('should handle leap years correctly', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2024-01-01', | ||
'2024-12-31', | ||
2 | ||
); | ||
|
||
expect(result).toBe(53); | ||
}); | ||
|
||
it('should return 0 if end date is before start date', async () => { | ||
const result = await service.calculateWeekdays( | ||
'2023-07-31', | ||
'2023-07-01', | ||
1 | ||
); | ||
|
||
expect(result).toBe(0); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.