diff --git a/package.json b/package.json index b7635ed..9a6ed0e 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prisma:test:deploy": "dotenv -e .test.env -- prisma migrate deploy", "db:test:rm": "docker compose rm test-db -sfv", "db:test:up": "docker compose up test-db -d", - "db:test:restart": "npm run db:test:rm && npm run db:test:up && npm run prisma:test:deploy && npm run db:seed", + "db:test:restart": "npm run db:test:rm && npm run db:test:up && npm run prisma:test:deploy", "prebuild": "rimraf dist", "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", diff --git a/prisma/clear.ts b/prisma/clear.ts index b3a4313..0c47810 100644 --- a/prisma/clear.ts +++ b/prisma/clear.ts @@ -3,10 +3,10 @@ import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); export async function clearStaticData() { + await prisma.civ.deleteMany(); + await prisma.building.deleteMany(); await prisma.unit.deleteMany(); await prisma.tech.deleteMany(); - await prisma.building.deleteMany(); - await prisma.civ.deleteMany(); await prisma.age.deleteMany(); await prisma.version.deleteMany(); } diff --git a/prisma/seed.ts b/prisma/seed.ts index c30a5c6..4afc64b 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -25,18 +25,26 @@ function getAgeId(ageName: string): number { return age.id; } -const prisma = new PrismaClient(); +let prisma = new PrismaClient(); + +export async function main(prismaClient?: PrismaClient) { + if (prismaClient) prisma = prismaClient; -async function main() { await clearStaticData(); - await addAges(); - await addUnits(); - await addTechs(); + const ageTransactions = addAges(); + const unitTransactions = addUnits(); + const techTransactions = addTechs(); + const buildingTransactions = addBuildings(); + const civTransactions = addCivs(); - const buildingTransactions = await addBuildings(); - const civTransactions = await addCivs(); - await prisma.$transaction([...buildingTransactions, ...civTransactions]); + await prisma.$transaction([ + ...ageTransactions, + ...unitTransactions, + ...techTransactions, + ...buildingTransactions, + ...civTransactions, + ]); await addVersions(); } @@ -51,52 +59,88 @@ main() process.exit(1); }); -async function addAges() { - await prisma.age.createMany({ - data: ages, +function addAges() { + const transactions = [] as any; + + ages.forEach((age) => { + transactions.push( + prisma.age.upsert({ + where: { + id: age.id, + }, + update: {}, + create: age, + }), + ); }); + + return transactions; } -async function addUnits() { - const unitsData = units.map((unit) => { - return { - id: getUniqueId(), - unitName: unit.unitName, - ageId: getAgeId(unit.age), - }; - }); +function addUnits() { + const transactions = [] as any; - await prisma.unit.createMany({ - data: unitsData, + units.forEach(async (unit) => { + const id = getUniqueId(); + + transactions.push( + prisma.unit.upsert({ + where: { + id, + }, + update: {}, + create: { + id, + unitName: unit.unitName, + ageId: getAgeId(unit.age), + }, + }), + ); }); + + return transactions; } -async function addTechs() { - const techsData = techs.map((tech) => { - return { - id: getUniqueId(), - techName: tech.techName, - ageId: getAgeId(tech.age), - }; - }); +function addTechs() { + const transactions = [] as any; + + techs.forEach(async (tech) => { + const id = getUniqueId(); - await prisma.tech.createMany({ - data: techsData, + transactions.push( + prisma.tech.upsert({ + where: { + id, + }, + update: {}, + create: { + id, + techName: tech.techName, + ageId: getAgeId(tech.age), + }, + }), + ); }); + + return transactions; } function addBuildings() { const transactions = [] as any; - buildings.forEach(async (building) => { - const ageId = getAgeId(building.age); + buildings.forEach((building) => { + const id = getUniqueId(); transactions.push( - prisma.building.create({ - data: { - id: getUniqueId(), + prisma.building.upsert({ + where: { + id, + }, + update: {}, + create: { + id, buildingName: building.buildingName, - ageId, + ageId: getAgeId(building.age), units: { connect: building.units, }, @@ -114,11 +158,17 @@ function addBuildings() { function addCivs() { const transactions = [] as any; - civs.forEach(async (civ) => { + civs.forEach((civ) => { + const id = getUniqueId(); + transactions.push( - prisma.civ.create({ - data: { - id: getUniqueId(), + prisma.civ.upsert({ + where: { + id, + }, + update: {}, + create: { + id, civName: civ.civName, units: { connect: civ.units, @@ -138,8 +188,12 @@ function addCivs() { } async function addVersions() { - await prisma.version.create({ - data: { + await prisma.version.upsert({ + where: { + id: VERSION_ID, + }, + update: {}, + create: { id: VERSION_ID, gameVersion: GAME_VERSION, apiVersion: API_VERSION, diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index 3f43d72..6ab7e75 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -3,8 +3,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import * as pactum from 'pactum'; import { AppModule } from '../src/app.module'; import { AuthDto } from '../src/auth/dto/auth.dto'; -import { CivEntity } from '../src/civ/entities/civ.entity'; -import { CreateDraftDto } from '../src/draft/dto/create-draft.dto'; import { PrismaService } from '../src/prisma/prisma.service'; const TEST_API_BASE_URL = `http://localhost:${process.env.TEST_PORT || 4001}`; @@ -34,8 +32,6 @@ describe('App e2e', () => { await prisma.deleteUsers(); pactum.request.setBaseUrl(TEST_API_BASE_URL); - const civs = await prisma.civ.findMany(); - console.log({ civs }); }); afterAll(() => { @@ -43,9 +39,6 @@ describe('App e2e', () => { }); it('starts up', async () => { - const civs = await prisma.civ.findMany(); - console.log({ civs }); - expect(app).toBeDefined(); }); @@ -57,8 +50,6 @@ describe('App e2e', () => { describe('Auth', () => { it('should sign up', async () => { - // await pactum.spec().get(`/civs`).expectStatus(HttpStatus.OK).inspect(); - await pactum .spec() .post(`/auth/signup`) @@ -234,39 +225,7 @@ describe('App e2e', () => { }); describe('Draft', () => { - const draftDto: CreateDraftDto = { - name: 'draft name', - desc: 'draft desc', - private: true, - civs: [{ civName: 'Aztecs' }] as CivEntity[], - }; - - it('creates a draft', async () => { - // recreate deleted user - await pactum - .spec() - .post(`/auth/signup`) - .withBody(authDto) - .expectStatus(HttpStatus.CREATED); - - await pactum - .spec() - .post(`/auth/login`) - .withBody(authDto) - .expectStatus(HttpStatus.OK) - .stores('accessToken', 'access_token'); - - // await pactum.spec().get(`/civs`).expectStatus(HttpStatus.OK).inspect(); - - // create draft - // await pactum - // .spec() - // .post(`/drafts`) - // .withBody(draftDto) - // .withHeaders({ Authorization: 'Bearer $S{accessToken}' }) - // .expectStatus(HttpStatus.CREATED); - }); - + it.todo('creates a draft'); it.todo('gets all drafts'); it.todo('gets a draft by id'); it.todo('edits a draft');