Skip to content

Commit

Permalink
Merge pull request #528 from neet/safe-test
Browse files Browse the repository at this point in the history
fix: Create test DB
  • Loading branch information
neet authored Nov 19, 2022
2 parents e30d94b + 5affd3c commit 1f1c73d
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"compat",
"Consmiconfig",
"darkmodejs",
"datasources",
"docgen",
"execa",
"firestore",
Expand Down Expand Up @@ -60,6 +61,7 @@
"resave",
"scaffdog",
"semibold",
"sqltag",
"storyshots",
"tailwindcss",
"unfetch",
Expand Down
4 changes: 1 addition & 3 deletions packages/@neet/vschedule-api/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
globalSetup: '<rootDir>/test-utils/jest-global-setup.ts',
globalTeardown: '<rootDir>/test-utils/jest-global-teardown.ts',
projects: [
{
displayName: 'Unit',
Expand All @@ -20,9 +21,6 @@ module.exports = {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
testMatch: ['<rootDir>/test/**/*.spec.{ts,tsx}'],
// testEnvironmentOptions: {
// verboseQuery: true,
// },
setupFilesAfterEnv: [
'<rootDir>/test-utils/jest-setup-after-env-shared.ts',
'<rootDir>/test-utils/jest-setup-after-env-e2e.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import { hashSync } from 'bcryptjs';
import { nanoid } from 'nanoid';

import { IConfig } from '../src/app';
import { TYPES } from '../src/types';
import { container } from './inversify-config';

export const SEED_ORGANIZATION_ID = 'x8WCU1lK79smcYy_mk_1V';
export const SEED_PERFORMER_ID = '1ebCtG0ZwzyfEuMCE3iyE';
export const SEED_STREAM_ID = '7HQVgnsDEOorzKgjNKJ6n';
export const SEED_USER_ID = 'rrCoMB5uaoqjDNU79_oFT';

export const createSeed = async () => {
export const createSeed = async (config: IConfig): Promise<void> => {
const client = new PrismaClient();
const config = container.get<IConfig>(TYPES.Config);

await client.user.create({
data: {
Expand Down Expand Up @@ -85,4 +82,6 @@ export const createSeed = async () => {
},
},
});

await client.$disconnect();
};
19 changes: 19 additions & 0 deletions packages/@neet/vschedule-api/test-utils/db-uri.ts
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 packages/@neet/vschedule-api/test-utils/jest-global-setup.ts
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 packages/@neet/vschedule-api/test-utils/jest-global-teardown.ts
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();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@
import 'reflect-metadata';
import '@quramy/jest-prisma-node';

import assert from 'assert';

import { TYPES } from '../src/types';
import { getTestDbConfig } from './db-uri';
import { container } from './inversify-config';

assert(process.env.DATABASE_URL != null);
const testDb = getTestDbConfig(process.env.DATABASE_URL);

beforeEach(() => {
// Mock DATABASE_URL
process.env.DATABASE_URL = testDb.uri;

const prisma = jestPrisma.client;
container.rebind(TYPES.PrismaClient).toConstantValue(prisma);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { PerformerId } from '../../../src/domain';
import { TaskService } from '../../../src/domain/services/task-service';
import { mockYoutubeWebsubService } from '../../../src/infra/services/youtube-websub-service-mock';
import { createAPI } from '../../../test-utils/api';
import { SEED_PERFORMER_ID } from '../../../test-utils/db-seed';
import { container } from '../../../test-utils/inversify-config';
import { SEED_PERFORMER_ID } from '../../../test-utils/seed';

describe('Performer', () => {
it('cannot subscribe to a performer without token', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@neet/vschedule-api/test/upsert-performer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ShowPerformer, UpsertPerformer } from '../src/app';
import { SEED_ORGANIZATION_ID } from '../test-utils/db-seed';
import { container } from '../test-utils/inversify-config';
import { SEED_ORGANIZATION_ID } from '../test-utils/seed';

describe('UpsertPerformer', () => {
it('creates performer', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@neet/vschedule-api/test/websub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ResubscriptionTaskRepositoryInMemory } from '../src/adapters/repositori
import { IConfig } from '../src/app';
import { TYPES } from '../src/types';
import { createAPI } from '../test-utils/api';
import { SEED_STREAM_ID } from '../test-utils/db-seed';
import { container } from '../test-utils/inversify-config';
import { SEED_STREAM_ID } from '../test-utils/seed';
import {
ytWebsubStreamDeleted,
ytWebsubStreamScheduled,
Expand Down

1 comment on commit 1f1c73d

@vercel
Copy link

@vercel vercel bot commented on 1f1c73d Nov 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.