Skip to content

Commit

Permalink
Merge pull request #26 from game-node-app/dev
Browse files Browse the repository at this point in the history
Cache for Statistics Controller
  • Loading branch information
Lamarcke authored Feb 29, 2024
2 parents 5746664 + b6601bc commit 7bf4ca6
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 33 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/achievements/achievements.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { AchievementCategory } from "./achievements.constants";
import Mocked = jest.Mocked;
import sleep from "../utils/sleep";
import { CollectionEntry } from "../collections/collections-entries/entities/collection-entry.entity";
import { UserLevelService } from "../user-level/user-level.service";

describe("AchievementsService", () => {
let service: AchievementsService;
let dataSource: Mocked<DataSource>;
let obtainedAchievementRepository: Mocked<Repository<ObtainedAchievement>>;
let userLevelService: Mocked<UserLevelService>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -28,6 +30,12 @@ describe("AchievementsService", () => {
getRepository: jest.fn(),
},
},
{
provide: UserLevelService,
useValue: {
increaseExp: jest.fn(),
},
},
],
}).compile();

Expand All @@ -36,6 +44,7 @@ describe("AchievementsService", () => {
obtainedAchievementRepository = module.get(
getRepositoryToken(ObtainedAchievement),
);
userLevelService = module.get(UserLevelService);
});

it("should be defined", () => {
Expand All @@ -60,6 +69,7 @@ describe("AchievementsService", () => {
);
// Timeout to wait for internal promises to finish
await sleep(2000);
expect(userLevelService.increaseExp).toHaveBeenCalled();
expect(obtainedAchievementRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
id: "space-station",
Expand Down
26 changes: 13 additions & 13 deletions src/follow/follow.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { FollowService } from './follow.service';
import { Test, TestingModule } from "@nestjs/testing";
import { FollowService } from "./follow.service";

describe('FollowService', () => {
let service: FollowService;
describe("FollowService", () => {
let service: FollowService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [FollowService],
}).compile();
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [FollowService],
}).compile();

service = module.get<FollowService>(FollowService);
});
service = module.get<FollowService>(FollowService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BaseFindDto } from "../../../utils/base-find.dto";
import { Game } from "../entities/game.entity";
import { IsObject, IsOptional } from "class-validator";
import { FindOptionsRelations } from "typeorm";
Expand Down
1 change: 1 addition & 0 deletions src/game/game-repository/game-repository.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class GameRepositoryController {
}

@Post("platforms/icon")
@HttpCode(200)
getIconNamesForPlatformAbbreviations(
@Body() dto: IconNamesForPlatformRequestDto,
) {
Expand Down
13 changes: 12 additions & 1 deletion src/game/game-repository/game-repository.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ConfigModule } from "@nestjs/config";
import { GameRepositoryService } from "./game-repository.service";
import { getMockRepositoryProvider } from "../../../test/mocks/repositoryMocks";
import { Game } from "./entities/game.entity";
import { getDataSourceToken } from "@nestjs/typeorm";
import { dataSourceMock } from "../../../test/mocks/dataSource.mock";

describe("GameSearchService", () => {
let service: GameRepositoryService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot()],
providers: [GameRepositoryService],
providers: [
GameRepositoryService,
getMockRepositoryProvider(Game),
{
provide: getDataSourceToken(),
useValue: dataSourceMock,
},
],
}).compile();

service = module.get<GameRepositoryService>(GameRepositoryService);
Expand Down
44 changes: 31 additions & 13 deletions src/health/health.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthService } from './health.service';
import { Test, TestingModule } from "@nestjs/testing";
import { HealthService } from "./health.service";
import { HealthCheckService, TypeOrmHealthIndicator } from "@nestjs/terminus";
import { RedisHealthIndicator } from "@liaoliaots/nestjs-redis-health";

describe('HealthService', () => {
let service: HealthService;
describe("HealthService", () => {
let service: HealthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [HealthService],
}).compile();
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
HealthService,
{
provide: HealthCheckService,
useValue: {
check: jest.fn(),
},
},
{
provide: RedisHealthIndicator,
useValue: {},
},
{
provide: TypeOrmHealthIndicator,
useValue: {},
},
],
}).compile();

service = module.get<HealthService>(HealthService);
});
service = module.get<HealthService>(HealthService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
});
5 changes: 5 additions & 0 deletions src/statistics/statistics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { StatisticsStatusRequestDto } from "./dto/statistics-status-request.dto"
import { FindStatisticsTrendingGamesDto } from "./dto/find-statistics-trending-games.dto";
import { FindStatisticsTrendingReviewsDto } from "./dto/find-statistics-trending-reviews.dto";
import { FindOneStatisticsDto } from "./dto/find-one-statistics.dto";
import { CacheInterceptor, CacheTTL } from "@nestjs/cache-manager";

@Controller("statistics")
@ApiTags("statistics")
Expand All @@ -41,6 +42,8 @@ export class StatisticsController {
@Post("trending/games")
@Public()
@UseInterceptors(PaginationInterceptor)
@UseInterceptors(CacheInterceptor)
@CacheTTL(600)
@ApiResponse({
status: 200,
type: StatisticsPaginatedResponseDto,
Expand All @@ -55,6 +58,8 @@ export class StatisticsController {

@Post("trending/reviews")
@UseInterceptors(PaginationInterceptor)
@UseInterceptors(CacheInterceptor)
@CacheTTL(600)
@ApiResponse({
status: 200,
type: StatisticsPaginatedResponseDto,
Expand Down
12 changes: 11 additions & 1 deletion src/statistics/statistics.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Test, TestingModule } from "@nestjs/testing";
import { StatisticsService } from "./statistics.service";
import { getMockRepositoriesProviders } from "../../test/mocks/repositoryMocks";
import { Statistics } from "./entity/statistics.entity";
import { UserLike } from "./entity/user-like.entity";
import { UserView } from "./entity/user-view.entity";

describe("StatisticsService", () => {
let service: StatisticsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [StatisticsService, ...getMockRepositoriesProviders([])],
providers: [
StatisticsService,
...getMockRepositoriesProviders([
Statistics,
UserLike,
UserView,
]),
],
}).compile();

service = module.get<StatisticsService>(StatisticsService);
Expand Down
2 changes: 1 addition & 1 deletion src/sync/igdb/utils/build-filter-find-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameRepositoryFilterDto } from "../../../game/game-repository/dto/game-repository-filter.dto";
import { FindManyOptions, FindOptionsWhere, In } from "typeorm";
import { FindOptionsWhere, In } from "typeorm";
import { Game } from "../../../game/game-repository/entities/game.entity";

const singleValueProperties = ["category", "status"];
Expand Down
4 changes: 3 additions & 1 deletion src/user-level/user-level.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Test, TestingModule } from "@nestjs/testing";
import { UserLevelController } from "./user-level.controller";
import { UserLevelService } from "./user-level.service";
import { getMockRepositoryProvider } from "../../test/mocks/repositoryMocks";
import { UserLevel } from "./entities/user-level.entity";

describe("UserLevelController", () => {
let controller: UserLevelController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserLevelController],
providers: [UserLevelService],
providers: [UserLevelService, getMockRepositoryProvider(UserLevel)],
}).compile();

controller = module.get<UserLevelController>(UserLevelController);
Expand Down
3 changes: 2 additions & 1 deletion src/user-level/user-level.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ describe("UserLevelService", () => {
);
expect(repository.save).toHaveBeenCalledWith(
expect.objectContaining({
currentLevel: mockUserLevel.currentLevel + 1,
currentLevel:
structuredClone(getMockUserLevel()).currentLevel + 1,
} satisfies Partial<UserLevel>),
);
});
Expand Down
7 changes: 7 additions & 0 deletions test/mocks/dataSource.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const dataSourceMock = {
getRepository: jest.fn(() => {
console.warn(
"getRepository of dataSourceMock is being called and not being mocked. This is probably going to result in errors.",
);
}),
};

0 comments on commit 7bf4ca6

Please sign in to comment.