From 15ff99bc9e068660d30eb7ca4c2108061517cd12 Mon Sep 17 00:00:00 2001 From: Ryan Laughlin Date: Thu, 1 Apr 2021 16:32:19 -0400 Subject: [PATCH] Start to fix tests + add more factories (#334) * Stop using npm run migrate:reset in tests * Begin moving other existing fixtures to factories * Update cypress/support/index.ts Co-authored-by: Kalle <38327916+Sendouc@users.noreply.github.com> * Clean up usage of _.sample * Rename files * Remove incomplete building of associations * Add missing references to calendarEvent * Remove inaccurate seeds * Add explicit lodash devDependency Co-authored-by: Kalle <38327916+Sendouc@users.noreply.github.com> --- cypress/support/index.ts | 2 +- package-lock.json | 13 ++-- package.json | 1 + prisma/factories/plusStatus.ts | 21 ++++++ prisma/factories/plusSuggestion.ts | 18 +++++ prisma/factories/plusVotingSummary.ts | 20 ++++++ prisma/mocks/plus.ts | 97 ++++++--------------------- prisma/seed.ts | 26 +++++-- 8 files changed, 109 insertions(+), 89 deletions(-) create mode 100644 prisma/factories/plusStatus.ts create mode 100644 prisma/factories/plusSuggestion.ts create mode 100644 prisma/factories/plusVotingSummary.ts diff --git a/cypress/support/index.ts b/cypress/support/index.ts index 065ebc68a..b9ee6cd9a 100644 --- a/cypress/support/index.ts +++ b/cypress/support/index.ts @@ -14,5 +14,5 @@ Cypress.Commands.add("login", (user: "sendou" | "nzap") => { beforeEach(() => { // TODO: use database transactions, instead of dropping and recreating the database with each individual test - cy.exec("npm run migrate:reset -- --force"); + cy.exec("npm run seed"); }); diff --git a/package-lock.json b/package-lock.json index 0b4210fdb..8fdf5f1f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -63,6 +63,7 @@ "cross-env": "^7.0.3", "cypress": "^6.8.0", "fishery": "^1.2.0", + "lodash": "^4.17.21", "prettier": "^2.2.1", "prisma": "^2.20.1", "ts-node": "^9.1.1", @@ -5967,9 +5968,9 @@ } }, "node_modules/lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "node_modules/lodash.camelcase": { "version": "4.3.0", @@ -14351,9 +14352,9 @@ } }, "lodash": { - "version": "4.17.20", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, "lodash.camelcase": { "version": "4.3.0", diff --git a/package.json b/package.json index f681770a3..96f605d00 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "cross-env": "^7.0.3", "cypress": "^6.8.0", "fishery": "^1.2.0", + "lodash": "^4.17.21", "prettier": "^2.2.1", "prisma": "^2.20.1", "ts-node": "^9.1.1", diff --git a/prisma/factories/plusStatus.ts b/prisma/factories/plusStatus.ts new file mode 100644 index 000000000..5e99c822d --- /dev/null +++ b/prisma/factories/plusStatus.ts @@ -0,0 +1,21 @@ +import { Factory } from "fishery"; +import { PlusStatus, PlusRegion } from "@prisma/client"; +import prisma from "../client"; +import _ from "lodash"; + +export default Factory.define(({ params, onCreate }) => { + onCreate(plusStatus => { + return prisma.plusStatus.create({ data: plusStatus }); + }); + + return { + userId: 1, // TODO: automatically build a User object, if necessary + membershipTier: 1, + region: _.sample(Object.values(PlusRegion))!, + voucherId: null, + vouchTier: null, + canVouchFor: null, + canVouchAgainAfter: null, + nameForVoting: null + }; +}); diff --git a/prisma/factories/plusSuggestion.ts b/prisma/factories/plusSuggestion.ts new file mode 100644 index 000000000..607a85a22 --- /dev/null +++ b/prisma/factories/plusSuggestion.ts @@ -0,0 +1,18 @@ +import { Factory } from "fishery"; +import { PlusSuggestion } from "@prisma/client"; +import prisma from "../client"; + +export default Factory.define(({ params, onCreate }) => { + onCreate(plusSuggestion => { + return prisma.plusSuggestion.create({ data: plusSuggestion }); + }); + + return { + suggestedId: 1, // TODO: automatically build a User object, if necessary + suggesterId: 2, // TODO: automatically build a User object, if necessary + tier: 1, + description: "yooo so cracked", + isResuggestion: false, + createdAt: new Date() + }; +}); diff --git a/prisma/factories/plusVotingSummary.ts b/prisma/factories/plusVotingSummary.ts new file mode 100644 index 000000000..f58663764 --- /dev/null +++ b/prisma/factories/plusVotingSummary.ts @@ -0,0 +1,20 @@ +import { Factory } from "fishery"; +import { PlusVotingSummary } from "@prisma/client"; +import prisma from "../client"; + +export default Factory.define(({ params, onCreate }) => { + onCreate(plusVotingSummary => { + return prisma.plusVotingSummary.create({ data: plusVotingSummary }); + }); + + return { + userId: 1, // TODO: automatically build a User object, if necessary + month: 1, + tier: 1, + year: 2020, + wasVouched: false, + wasSuggested: false, + countsEU: [0, 0, 0, 3], + countsNA: [0, 0, 2, 0] + }; +}); diff --git a/prisma/mocks/plus.ts b/prisma/mocks/plus.ts index 1015a7f70..225e40a3c 100644 --- a/prisma/mocks/plus.ts +++ b/prisma/mocks/plus.ts @@ -1,82 +1,75 @@ -import { Prisma } from "@prisma/client"; +import { PlusRegion, Prisma } from "@prisma/client"; +import plusStatusFactory from "../factories/plusStatus"; +import plusSuggestionFactory from "../factories/plusSuggestion"; +import plusVotingSummaryFactory from "../factories/plusVotingSummary"; + +// TODO: All this data is currently hard-coded so that Cypress tests will pass. +// In the future, we could allow the data generated by these factories be more random, and +// update the tests so that they are responsible for creating the specific data that they want to check. export const getPlusStatusesData = (): Prisma.PlusStatusCreateManyInput[] => { return [ { userId: 1, - region: "EU", - membershipTier: 1, canVouchAgainAfter: new Date(Date.UTC(2020, 1, 1)), }, { userId: 2, - region: "NA", - membershipTier: 1, }, { userId: 3, - region: "EU", - membershipTier: 1, }, { userId: 4, - region: "NA", - membershipTier: 1, }, { userId: 5, - region: "EU", - membershipTier: 1, }, { userId: 6, - region: "NA", membershipTier: 2, vouchTier: 1, }, { userId: 7, - region: "EU", membershipTier: 2, }, { userId: 8, - region: "NA", membershipTier: 2, }, { userId: 9, - region: "EU", membershipTier: 2, }, { userId: 10, - region: "NA", }, { userId: 333, - region: "EU", + region: PlusRegion.EU, membershipTier: 2, canVouchAgainAfter: new Date(Date.UTC(2030, 1, 1)), canVouchFor: 2, }, { userId: 999, - region: "EU", - membershipTier: 1, + region: PlusRegion.EU, canVouchFor: 1, - }, - ]; + } + ].map((params) => { + return plusStatusFactory.build(params); + }); }; export const getPlusSuggestionsData = (): Prisma.PlusSuggestionCreateManyInput[] => { return [ - { + plusSuggestionFactory.build({ description: "yooo so cracked", tier: 2, suggestedId: 10, suggesterId: 1, - }, + }) ]; }; @@ -84,125 +77,73 @@ export const getPlusVotingSummaryData = (): Prisma.PlusVotingSummaryCreateManyIn return [ { userId: 1, - month: 1, - tier: 1, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 0, 0, 3], countsNA: [0, 0, 2, 0], }, { userId: 2, - month: 1, - tier: 1, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 3, 0, 0], countsNA: [2, 0, 0, 0], }, { userId: 3, - month: 1, - tier: 1, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [1, 1, 1, 0], countsNA: [0, 1, 1, 0], }, { userId: 4, - month: 1, - tier: 1, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 1, 2, 0], countsNA: [2, 0, 0, 0], }, { userId: 5, - month: 1, - tier: 1, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [1, 0, 1, 1], countsNA: [0, 2, 0, 0], }, { userId: 6, - month: 1, - tier: 1, - wasSuggested: false, wasVouched: true, - year: 2020, countsEU: [0, 3, 0, 0], countsNA: [2, 0, 0, 0], }, { userId: 7, - month: 1, - tier: 1, - wasSuggested: false, wasVouched: true, - year: 2020, countsEU: [0, 0, 0, 3], countsNA: [0, 0, 2, 0], }, // +2 { userId: 6, - month: 1, tier: 2, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 0, 2, 0], countsNA: [0, 0, 0, 2], }, - { userId: 7, - month: 1, tier: 2, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 1, 0, 1], countsNA: [0, 1, 1, 0], }, { userId: 8, - month: 1, tier: 2, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [0, 2, 0, 0], countsNA: [0, 0, 2, 0], }, { userId: 9, - month: 1, tier: 2, - wasSuggested: false, - wasVouched: false, - year: 2020, countsEU: [1, 1, 0, 0], countsNA: [0, 2, 0, 0], }, { userId: 10, - month: 1, tier: 2, - wasSuggested: true, - wasVouched: false, - year: 2020, countsEU: [0, 0, 2, 0], countsNA: [0, 0, 0, 2], }, - ]; + ].map((params) => { + return plusVotingSummaryFactory.build(params); + }); }; diff --git a/prisma/seed.ts b/prisma/seed.ts index 2189e72df..27a51ef60 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,16 +1,17 @@ import fs from "fs"; import path from "path"; import prisma from "./client"; -import calendarEventFactory from "./factories/calendarEvent"; -import userFactory from "./factories/user"; import { - getPlusStatusesData, - getPlusSuggestionsData, getPlusVotingSummaryData, + getPlusSuggestionsData, + getPlusStatusesData, } from "./mocks/plus"; +import userFactory from "./factories/user" +import calendarEventFactory from "./factories/calendarEvent" async function main() { throwIfNotLocalhost(); + await dropAllData(); await seedNewData(); } @@ -37,6 +38,23 @@ function throwIfNotLocalhost() { ); } +async function dropAllData() { + // TODO: Programatically clear/truncate all tables, rather than listing each model individually + // That way, we won't need to update this method each time we add a new model + 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.calendarEvent.deleteMany({}); + await prisma.user.deleteMany({}); +} + async function seedNewData() { await seedUsers(); await seedEvents();