Skip to content

Commit

Permalink
feat: increase test coverage in weather service (#490)
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree authored Nov 3, 2023
1 parent 7cdf976 commit a236b92
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 69 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<h1 align="center">Cats Cradle :yarn:</h1>

Get ready to embark on an adventure with **Cats Cradle** - the upcoming co-op
RPG that will transport you to a nostalgic world filled with wonder. Join us on
this exciting journey as we continue to develop and bring this game to life.
Embark on an nostalgic adventure with **Cats Cradle** - the upcoming co-op RPG
that will transport you to a world filled with wonder. Join us on this journey
as we flesh in the game and bring it to life.

This repo houses the game's code. This enterprise architecture monorepo includes
numerous microservices built on a PaaS with IaC for streamlined DevOps CI/CD.
This repo houses the game's code in a enterprise architecture monorepo. It
includes numerous microservices built on a PaaS with IaC for streamlined DevOps
CI/CD.

Although each package is maintained primarily for the game, most stable packages
are individually published to
Although each package is maintained primarily for the game, most of the stable
packages are individually published to
[NPM](https://www.npmjs.com/search?q=%40cats-cradle) and available under
open-source software license.

Expand Down
33 changes: 29 additions & 4 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "374cc425860ef0a63a23f3ed83e8b649005cedfb",
"pnpmShrinkwrapHash": "6ce700965739812a8afa2c8ceead36735bcbcef5",
"preferredVersionsHash": "8ae0ba5bd02ec9c5763773a15e27aee08a6567f6"
}
16 changes: 8 additions & 8 deletions services/character-sheet/openapi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"SNOW_LEOPARD",
"CHIMERA",
"SLOW_MOVER",
"SNAKE_OF_WATER",
"SNAKE_OF_THE_WATER",
"BANDIT",
"ROWAN",
"CHUBBS"
Expand Down Expand Up @@ -547,7 +547,7 @@
"required": true,
"in": "path",
"schema": {
"default": "1f0fb038-21f8-47ff-9de1-010502a010d3",
"default": "9201a8eb-e105-4760-938f-0afc6166834c",
"type": "string"
}
}
Expand Down Expand Up @@ -586,11 +586,11 @@
"properties": {
"_id": {
"type": "string",
"default": "de2e5e17-70ee-4c27-b01a-c38bc6c3e4e2"
"default": "afdd27df-e56f-42b5-a76b-668bb791375b"
},
"instanceId": {
"type": "string",
"default": "26be8bd5-5d6f-4bbd-8878-b14675fc8dd5"
"default": "f1641d0f-9595-4677-9949-b29dbceaf3e7"
},
"archetypeId": {
"type": "string",
Expand Down Expand Up @@ -630,7 +630,7 @@
"SNOW_LEOPARD",
"CHIMERA",
"SLOW_MOVER",
"SNAKE_OF_WATER",
"SNAKE_OF_THE_WATER",
"BANDIT",
"ROWAN",
"CHUBBS"
Expand All @@ -647,11 +647,11 @@
"properties": {
"id": {
"type": "string",
"default": "be51385e-9c08-40ba-b92c-d6b3790db555"
"default": "1077d648-a6aa-468b-86f7-788e42421f90"
},
"instanceId": {
"type": "string",
"default": "2e308a56-a26d-4513-ba79-2a412f5c113b"
"default": "7b6744b0-f364-4833-9c5d-2273361f32b1"
},
"place": {
"type": "string",
Expand All @@ -674,7 +674,7 @@
"properties": {
"characterSheetId": {
"type": "string",
"default": "8eddefd6-612c-421b-a033-ac5234a330e4"
"default": "88623540-0638-4e3d-8e15-a86792290835"
},
"affiliationId": {
"type": "string",
Expand Down
1 change: 0 additions & 1 deletion services/character-sheet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"aws-lambda": "1.0.7",
"aws-serverless-express": "~3.4.0",
"class-transformer": "0.5.1",
"class-validator": "0.13.2",
"express": "4.18.2",
"node-cache": "5.1.2",
"reflect-metadata": "0.1.13",
Expand Down
5 changes: 2 additions & 3 deletions services/character-sheet/src/models/character-sheet.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Type,
IsUuidV4,
IsString,
IsUUID,
IsEnum,
IsOptional,
IsInstance,
Expand Down Expand Up @@ -35,7 +34,7 @@ export class CharacterSheet {
// @Transform(({ value }) => value.toString())
// _id: ObjectId;

@IsUUID()
@IsUuidV4()
@Prop({
type: String,
default: function genUUID() {
Expand All @@ -44,7 +43,7 @@ export class CharacterSheet {
})
public _id!: string;

@IsUUID()
@IsUuidV4()
@Prop()
public instanceId!: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { v4 } from 'uuid';
import { IsEnum, IsInt, IsPositive } from 'class-validator';
import { IsEnum, IsInt, IsPositive } from '@cats-cradle/validation-schemas';
import { AffiliationId, AffiliationIds } from '../../data/affiliations';

export enum Operation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('/character-sheets', () => {
it('should find result if exists', async () => {
const characterSheet = await FakerFactory.create<CharacterSheet>(
CharacterSheet,
{ archetypeId: 'MEEKU_ONI' },
{ archetypeId: 'MEEKU_ONI', name: 'Meeku', surname: 'Oni' },
{ optionals: false, pojo: true },
);
await characterSheetRepository.create(characterSheet);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsEnum, IsOptional, IsString, IsUUID,
} from 'class-validator';
IsEnum,
IsOptional,
IsString,
IsUuidV4,
} from '@cats-cradle/validation-schemas';
import { v4 } from 'uuid';
import { ArchetypeId, ArchetypeIds } from '../../data/archetype';

export class CreateCharacterSheetDto {
@ApiProperty({
default: v4(),
})
@IsUUID()
@IsUuidV4()
@IsOptional()
public _id?: string;

@ApiProperty({
default: v4(),
})
@IsUUID()
@IsUuidV4()
public instanceId!: string;

@ApiProperty({ enum: ArchetypeIds, default: 'DARUMA_NAKAMURA' })
Expand Down
6 changes: 3 additions & 3 deletions services/character-sheet/src/modules/npc/create-spawn-dto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID, IsEnum } from '@cats-cradle/validation-schemas';
import { IsUuidV4, IsEnum } from '@cats-cradle/validation-schemas';
import { v4 } from 'uuid';
import { PlaceIds, PlaceId } from '../../data/place';

export class CreateSpawnDto {
@IsUUID()
@IsUuidV4()
@ApiProperty({
default: v4(),
})
public id?: string;

@IsUUID()
@IsUuidV4()
@ApiProperty({
default: v4(),
})
Expand Down
14 changes: 13 additions & 1 deletion services/weather-control/src/module/weather/query.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { IsLongitude, IsLatitude } from '@cats-cradle/validation-schemas';
import {
IsLongitude,
IsLatitude,
IsDateString,
} from '@cats-cradle/validation-schemas';
import { ApiProperty } from '@nestjs/swagger';

export class QueryDto {
@IsDateString()
@ApiProperty({
description: 'Date',
default: new Date().toISOString(),
type: String,
})
date: string;

@IsLatitude()
@ApiProperty({
description: 'Latitude',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import {
Query,
} from '@nestjs/common';
import { WeatherService } from './weather.service';
import { QueryDto } from './query.dto';

@Controller({ path: 'weather', version: ['1', VERSION_NEUTRAL] })
export class WeatherController {
constructor(private readonly weatherService: WeatherService) {}

@Post()
async fetch(
@Query('latitude') latitude: number,
@Query('longitude') longitude: number,
) {
return this.weatherService.get(latitude, longitude);
async fetch(@Body() body: QueryDto) {
return this.weatherService.get(
new Date(body.date),
body.latitude,
body.longitude,
);
}
}
77 changes: 60 additions & 17 deletions services/weather-control/src/module/weather/weather.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import supertest from 'supertest';
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, Injectable } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { FakerFactory } from '@cats-cradle/faker-factory';
import { WeatherService } from './weather.service';
import { WeatherController } from './weather.controller';
import { ClimateType } from './climates.type';
import { QueryDto } from './query.dto';
import { TimeOfDayType } from './time-of-day.type';

describe('/weather', () => {
let app: INestApplication;
Expand All @@ -30,21 +31,63 @@ describe('/weather', () => {
});

describe('POST /weather', () => {
it('should determine climate', async () => {
const body = await FakerFactory.create<QueryDto>(QueryDto, {
// longitude: '0',
});
console.log(body);
console.log(body);

const response = await supertest(app.getHttpServer())
.post('/weather')
.send(body)
.expect(201);

expect(response.body).toMatchObject({
climate: ClimateType.POLAR,
});
});
it.each([
[0, TimeOfDayType.MIDNIGHT],
[2, TimeOfDayType.MIDNIGHT],
[5, TimeOfDayType.DAWN],
[8, TimeOfDayType.MORNING],
[12, TimeOfDayType.NOON],
[17, TimeOfDayType.EVENING],
[20, TimeOfDayType.DUSK],
[23, TimeOfDayType.MIDNIGHT],
])(
'should determine hour %i as %s',
async (hour: number, timeOfDay: TimeOfDayType) => {
const now = new Date();
now.setHours(hour);

const body = await FakerFactory.create<QueryDto>(QueryDto, {
date: now.toISOString(),
});

const response = await supertest(app.getHttpServer())
.post('/weather')
.send(body)
.expect(201);

expect(response.body).toMatchObject({
timeOfDay,
});
},
);

it.each([
[60, ClimateType.POLAR],
[40, ClimateType.CONTINENTAL],
[25, ClimateType.TEMPERATE],
[20, ClimateType.DRY],
[-15, ClimateType.TROPICAL],
[-20, ClimateType.DRY],
[-25, ClimateType.TEMPERATE],
[-40, ClimateType.CONTINENTAL],
[-60, ClimateType.POLAR],
[-90, ClimateType.POLAR],
])(
'should determine climate %i as %s',
async (latitude: number, climate: ClimateType) => {
const body = await FakerFactory.create<QueryDto>(QueryDto, {
latitude: latitude.toString(),
});

const response = await supertest(app.getHttpServer())
.post('/weather')
.send(body)
.expect(201);

expect(response.body).toMatchObject({
climate,
});
},
);
});
});
Loading

0 comments on commit a236b92

Please sign in to comment.