Use a factory to create seed data (#315)

* Add fishery for factory creation

* Refactor existing seed file

* Create a UserFactory to seed user data

* Make fishery a devDependency

* Prefer const over let

* Eliminate the dropAllData method

* Move factory files to prisma/factories

* Update UserFactory to return valid discordAvatar value
This commit is contained in:
Ryan Laughlin 2021-03-31 05:01:16 -04:00 committed by GitHub
parent 8c4a0dc5d4
commit bf30c025f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 58 deletions

View File

@ -59,7 +59,7 @@ DATABASE_URL=postgresql://sendou@localhost:5432
_You can see [Prisma's guide on how to set up a PostgreSQL database running locally](https://www.prisma.io/dataguide/postgresql/setting-up-a-local-postgresql-database) for more info._
6. Use `npm run migrate` to get the database formatted with the right tables.
7. There should be a seeding script but this doesn't exist yet. If anyone is interested in contributing this is probably a good starting point (see issue #197).
7. Seed some example data in the database by running `npm run seed`. (This seed data is incomplete see issue #197 if you would like to improve the seed data!)
### Enable logging in

View File

@ -13,5 +13,6 @@ Cypress.Commands.add("login", (user: "sendou" | "nzap") => {
});
beforeEach(() => {
cy.exec("npm run seed");
// TODO: use database transactions, instead of dropping and recreating the database with each individual test
cy.exec("npm run migrate:reset -- --force");
});

19
package-lock.json generated
View File

@ -62,6 +62,7 @@
"@types/uuid": "^8.3.0",
"cross-env": "^7.0.3",
"cypress": "^6.8.0",
"fishery": "^1.2.0",
"prettier": "^2.2.1",
"prisma": "^2.19.0",
"ts-node": "^9.1.1",
@ -4024,6 +4025,15 @@
"node": ">=8"
}
},
"node_modules/fishery": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/fishery/-/fishery-1.2.0.tgz",
"integrity": "sha512-0GG029KHF3p8Q0NiAl/ZOK1fvyAprOiHdtRWUNS46x9QXuQhMwzcGLNDbZ7XIEEBowwBmMsw7StkaU0ek9dSbg==",
"dev": true,
"dependencies": {
"lodash.mergewith": "^4.6.2"
}
},
"node_modules/focus-lock": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/focus-lock/-/focus-lock-0.8.1.tgz",
@ -12164,6 +12174,15 @@
"path-exists": "^4.0.0"
}
},
"fishery": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/fishery/-/fishery-1.2.0.tgz",
"integrity": "sha512-0GG029KHF3p8Q0NiAl/ZOK1fvyAprOiHdtRWUNS46x9QXuQhMwzcGLNDbZ7XIEEBowwBmMsw7StkaU0ek9dSbg==",
"dev": true,
"requires": {
"lodash.mergewith": "^4.6.2"
}
},
"focus-lock": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/focus-lock/-/focus-lock-0.8.1.tgz",

View File

@ -9,6 +9,7 @@
"start": "next start",
"migrate": "prisma migrate deploy --preview-feature",
"migrate:save": "prisma migrate dev --create-only --preview-feature",
"migrate:reset": "prisma migrate reset",
"gen": "npx prisma generate",
"prebuild": "ts-node prisma/scripts/preBuild.ts",
"mongo": "ts-node prisma/scripts/dataFromMongo.ts",
@ -77,6 +78,7 @@
"@types/uuid": "^8.3.0",
"cross-env": "^7.0.3",
"cypress": "^6.8.0",
"fishery": "^1.2.0",
"prettier": "^2.2.1",
"prisma": "^2.19.0",
"ts-node": "^9.1.1",

21
prisma/factories/user.ts Normal file
View File

@ -0,0 +1,21 @@
import { Factory } from "fishery";
import { User } from "@prisma/client";
import prisma from "../client";
export default Factory.define<User>(({ sequence, onCreate }) => {
onCreate(user => {
return prisma.user.create({ data: user });
});
return {
id: sequence,
discordId: sequence.toString().padStart(17, '0'),
discordAvatar: null,
discriminator: sequence.toString().padStart(4, '0'),
username: `User${sequence}`,
patreonTier: 0,
canPostEvents: false,
teamId: null,
ladderTeamId: null
};
});

View File

@ -1,36 +0,0 @@
import { Prisma } from "@prisma/client";
export const getUsersData = (): Prisma.UserCreateManyInput[] => {
return [
...new Array(10).fill(null).map((_, i) => ({
id: i + 1,
discordId: padWithZero(i, 17),
discriminator: padWithZero(i, 4),
username: `User${i + 1}`,
})),
{
id: 11,
discordId: "79237403620945920",
username: "Sendou",
discriminator: "4059",
patreonTier: 1,
discordAvatar: "1e0968214a6ea74aebce4bbd699d6aae",
},
{
id: 12,
discordId: "455039198672453645",
username: "NZAP",
discriminator: "6227",
discordAvatar: "f809176af93132c3db5f0a5019e96339",
},
];
};
function padWithZero(root: number, totalLength: number) {
let result = "" + root;
while (result.length < totalLength) {
result += "0";
}
return result;
}

View File

@ -6,29 +6,11 @@ import {
getPlusSuggestionsData,
getPlusStatusesData,
} from "./mocks/plus";
import { getUsersData } from "./mocks/user";
import userFactory from "./factories/user"
async function main() {
throwIfNotLocalhost();
await prisma.profile.deleteMany({});
await prisma.build.deleteMany({});
await prisma.salmonRunRecord.deleteMany({});
await prisma.freeAgentPost.deleteMany({});
await prisma.team.deleteMany({});
await prisma.ladderPlayerTrueSkill.deleteMany({});
await prisma.ladderMatchPlayer.deleteMany({});
await prisma.plusVotingSummary.deleteMany({});
await prisma.plusSuggestion.deleteMany({});
await prisma.plusStatus.deleteMany({});
await prisma.user.deleteMany({});
await prisma.user.createMany({ data: getUsersData() });
await prisma.plusStatus.createMany({ data: getPlusStatusesData() });
await prisma.plusSuggestion.createMany({ data: getPlusSuggestionsData() });
await prisma.plusVotingSummary.createMany({
data: getPlusVotingSummaryData(),
});
await seedNewData();
}
function throwIfNotLocalhost() {
@ -54,6 +36,27 @@ function throwIfNotLocalhost() {
);
}
async function seedNewData() {
await seedUsers();
await prisma.plusStatus.createMany({ data: getPlusStatusesData() });
await prisma.plusSuggestion.createMany({ data: getPlusSuggestionsData() });
await prisma.plusVotingSummary.createMany({
data: getPlusVotingSummaryData(),
});
}
async function seedUsers() {
const randomUsers = [...Array(10)].map((_, _i) => {
return userFactory.build();
})
await prisma.user.createMany({data: [
...randomUsers,
userFactory.build({username: "Sendou", patreonTier: 1}),
userFactory.build({username: "NZAP"})
]})
}
main()
.catch((e) => {
console.error(e);