Skip to content

Commit

Permalink
refactor(armies-lists): Fix tests after relations updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rateau-violent committed Jul 28, 2023
1 parent 47492f2 commit 6a205da
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 343 deletions.
17 changes: 10 additions & 7 deletions src/army-list/army-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import { ArmyService } from "@army/army.service";
import { Army } from "@army/army.entity";
import { UnitService } from "@army/unit/unit.service";

type Id = {
id: string;
}

@Controller("armies-lists")
export class ArmyListController {
constructor(
Expand All @@ -68,7 +72,7 @@ export class ArmyListController {
@Body("valuePoints") valuePoints: number,
@Body("units") units: ArmyListUnitCredentialsDTO[],
@Body("isShared") isShared: boolean,
@Body("isFavorite") isFavorite: boolean) {
@Body("isFavorite") isFavorite: boolean): Promise<Id> {
if (!ParamHelper.isValid(name) || !ParamHelper.isValid(armyId) || !ParamHelper.isValid(valuePoints) ||
!ParamHelper.isValid(units) || !ParamHelper.isValid(isFavorite)) {
throw new BadRequestException();
Expand All @@ -83,20 +87,19 @@ export class ArmyListController {
try {
await this.armyListService.save(list);
list.units = await this.saveUnits(list.id, units, list);
return { id: list.id };
} catch (error) {
console.error(error);
if (error instanceof QueryFailedError) {
throw new NotFoundException(`The army ${list.armyId} was not found`);
}

}
// await this.saveUnits(list.id, units);
}

@UseGuards(JwtAuthGuard)
@Get("")
@HttpCode(HttpStatus.OK)
async lookup(@Request() req) {
async lookup(@Request() req): Promise<ArmyListCredentialsDTO[]> {
const lists: ArmyList[] = await this.armyListService.findByOwner(req.user.username);
let credentials: ArmyListCredentialsDTO[] = [];

Expand All @@ -109,7 +112,7 @@ export class ArmyListController {
@UseGuards(JwtAuthGuard)
@Get(":id")
@HttpCode(HttpStatus.OK)
async get(@Request() req, @Param("id") id: string) {
async get(@Request() req, @Param("id") id: string): Promise<ArmyListDTO> {
let list: ArmyList = await this.armyListService.findOneById(id, { loadAll: true });

if (list === null) {
Expand All @@ -124,7 +127,7 @@ export class ArmyListController {
@UseGuards(JwtAuthGuard)
@Delete("/:id")
@HttpCode(HttpStatus.OK)
async delete(@Request() req, @Param("id") id: string) {
async delete(@Request() req, @Param("id") id: string): Promise<void> {
let list: ArmyList = await this.armyListService.findOneById(id);

if (list === null) {
Expand All @@ -146,7 +149,7 @@ export class ArmyListController {
@Body("valuePoints") valuePoints: number,
@Body("units") units: ArmyListUnitCredentialsDTO[],
@Body("isShared") isShared: boolean,
@Body("isFavorite") isFavorite) {
@Body("isFavorite") isFavorite): Promise<void> {
let list: ArmyList = await this.armyListService.findOneById(id, { loadUnits: true});

if (list === null) {
Expand Down
48 changes: 25 additions & 23 deletions tests/account/profile/profile.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ import { Test } from "@nestjs/testing";
import { TypeOrmModule } from "@nestjs/typeorm";
import * as dotenv from "dotenv";
import * as bcrypt from "bcrypt";
import { faker } from "@faker-js/faker";

import { AccountType, Profile } from "@account/profile/profile.entity";
import { ProfileService } from "@account/profile/profile.service";
import { ProfileModule } from "@account/profile/profile.module";
import { ArmyList } from "@army-list/army-list.entity";

dotenv.config();

dotenv.config()
jest.setTimeout(30000);

const DB = process.env.POSTGRES_DB;
const DB_HOST = process.env.DATABASE_IP;
const DB: string = process.env.POSTGRES_DB;
const DB_HOST: string = process.env.DATABASE_IP;
const DB_PORT: number = +process.env.DATABASE_PORT;
const DB_USERNAME = process.env.POSTGRES_USER;
const DB_PASSWORD = process.env.POSTGRES_PASSWORD;
const DB_DIALECT = "postgres"
const DB_USERNAME: string = process.env.POSTGRES_USER;
const DB_PASSWORD: string = process.env.POSTGRES_PASSWORD;
const DB_DIALECT = "postgres";

const USERNAME = "username";
const EMAIL = "email@prophecy.com"
const PASSWORD = "password";
const USERNAME: string = faker.internet.userName();
const EMAIL: string = faker.internet.email();
const PASSWORD: string = faker.internet.password();

const USERNAME1 = "username1";
const EMAIL1 = "email1@prophecy.com";
const PASSWORD1 = "password1";
const USERNAME1: string = faker.internet.userName();
const EMAIL1: string = faker.internet.email();
const PASSWORD1: string = faker.internet.password();

function initDefaultProfile(username: string, email: string, password: string): Profile {
const profile = new Profile();
Expand All @@ -44,16 +45,17 @@ describe("ProfileService", () => {
beforeAll(async () => {
const moduleRef = await Test.createTestingModule(({
imports: [TypeOrmModule.forRoot({
type: DB_DIALECT,
host: DB_HOST,
port: DB_PORT,
username: DB_USERNAME,
password: DB_PASSWORD,
database: DB,
entities: [Profile, ArmyList],
synchronize: true,
}),
ProfileModule],
type: DB_DIALECT,
host: DB_HOST,
port: DB_PORT,
username: DB_USERNAME,
password: DB_PASSWORD,
database: DB,
entities: [Profile],
synchronize: true,
}),
ProfileModule,
],
})).compile();

service = moduleRef.get<ProfileService>(ProfileService);
Expand Down
Loading

0 comments on commit 6a205da

Please sign in to comment.