Skip to content

Commit

Permalink
update seed script to use transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
amrtgaber committed Jun 22, 2024
1 parent 80c50af commit 6618e04
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 88 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
4 changes: 2 additions & 2 deletions prisma/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
140 changes: 97 additions & 43 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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,
},
Expand All @@ -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,
Expand All @@ -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,
Expand Down
43 changes: 1 addition & 42 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -34,18 +32,13 @@ describe('App e2e', () => {
await prisma.deleteUsers();

pactum.request.setBaseUrl(TEST_API_BASE_URL);
const civs = await prisma.civ.findMany();
console.log({ civs });
});

afterAll(() => {
app.close();
});

it('starts up', async () => {
const civs = await prisma.civ.findMany();
console.log({ civs });

expect(app).toBeDefined();
});

Expand All @@ -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`)
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 6618e04

Please sign in to comment.