-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from neet/safe-test
fix: Create test DB
- Loading branch information
Showing
10 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface TestDb { | ||
readonly uri: string; | ||
readonly name: string; | ||
readonly shouldCreate: boolean; | ||
} | ||
|
||
export const getTestDbConfig = (originalUri: string): TestDb => { | ||
const uriObj = new URL(originalUri as string); | ||
const shouldCreate = !uriObj.pathname.includes('test'); | ||
|
||
if (shouldCreate) { | ||
uriObj.pathname = uriObj.pathname + '_test'; | ||
} | ||
|
||
const uri = uriObj.toString(); | ||
const name = uriObj.pathname.replace(/^\//, ''); | ||
|
||
return { uri, name, shouldCreate }; | ||
}; |
29 changes: 27 additions & 2 deletions
29
packages/@neet/vschedule-api/test-utils/jest-global-setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { PrismaClient } from '@prisma/client'; | ||
import assert from 'assert'; | ||
import execa from 'execa'; | ||
import path from 'path'; | ||
|
||
import { createSeed } from './seed'; | ||
import { IConfig } from '../src/app'; | ||
import { TYPES } from '../src/types'; | ||
import { createSeed } from './db-seed'; | ||
import { getTestDbConfig } from './db-uri'; | ||
import { container } from './inversify-config'; | ||
|
||
export default async (): Promise<void> => { | ||
assert(process.env.DATABASE_URL != null); | ||
const testDb = getTestDbConfig(process.env.DATABASE_URL); | ||
|
||
// CREATE DATABASE; | ||
if (testDb.shouldCreate) { | ||
const client = new PrismaClient(); | ||
try { | ||
await client.$executeRawUnsafe(`CREATE DATABASE ${testDb.name}`); | ||
} finally { | ||
await client.$disconnect(); | ||
} | ||
} | ||
|
||
// Mock DATABASE_URL | ||
process.env.DATABASE_URL = testDb.uri; | ||
|
||
// Run migration | ||
await execa('prisma', ['migrate', 'reset', '--force', '--skip-seed'], { | ||
preferLocal: true, | ||
stdio: 'pipe', | ||
cwd: path.join(__dirname, '..'), | ||
}); | ||
|
||
await createSeed(); | ||
// Seed | ||
const config = container.get<IConfig>(TYPES.Config); | ||
await createSeed(config); | ||
}; |
19 changes: 19 additions & 0 deletions
19
packages/@neet/vschedule-api/test-utils/jest-global-teardown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import assert from 'assert'; | ||
|
||
import { getTestDbConfig } from './db-uri'; | ||
|
||
export default async (): Promise<void> => { | ||
assert(process.env.DATABASE_URL != null); | ||
const testDb = getTestDbConfig(process.env.DATABASE_URL); | ||
|
||
// Mock DATABASE_URL | ||
process.env.DATABASE_URL = testDb.uri; | ||
|
||
const client = new PrismaClient(); | ||
try { | ||
await client.$executeRawUnsafe(`DROP DATABASE ${testDb.name}`); | ||
} finally { | ||
await client.$disconnect(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1f1c73d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
vschedule – ./
refined-itsukara-link.neet.love
vschedule-git-main-neet.vercel.app
refined-itsukara-link.vercel.app
vschedule-neet.vercel.app