Skip to content

Commit

Permalink
Add backend e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
version-1 committed Feb 26, 2024
1 parent ed141f9 commit b480b97
Show file tree
Hide file tree
Showing 26 changed files with 1,702 additions and 202 deletions.
95 changes: 55 additions & 40 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,52 @@ name: Frontend/Backend pipeline
run-name: Frontend/Backend pipeline
on: [push]
jobs:
fe-pipeline:
name: Frontend Pipeline
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./frontend/core
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20.x'
- name: Cache node_modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-fe-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
name: List the state of node modules
continue-on-error: true
run: npm list
- name: Install dependencies
run: npm install
- name: Lint
run: npm run lint
- name: Compile
run: npm run tsc
- name: Test
run: npm run test
- name: Build
run: npm run build
# fe-pipeline:
# name: Frontend Pipeline
# runs-on: ubuntu-latest
# defaults:
# run:
# working-directory: ./frontend/core
# steps:
# - uses: actions/checkout@v4
# - name: Use Node.js
# uses: actions/setup-node@v3
# with:
# node-version: '20.x'
# - name: Cache node_modules
# id: cache-npm
# uses: actions/cache@v3
# env:
# cache-name: cache-fe-node-modules
# with:
# # npm cache files are stored in `~/.npm` on Linux/macOS
# path: ~/.npm
# key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
# restore-keys: |
# ${{ runner.os }}-build-${{ env.cache-name }}-
# ${{ runner.os }}-build-
# ${{ runner.os }}-
# - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
# name: List the state of node modules
# continue-on-error: true
# run: npm list
# - name: Install dependencies
# run: npm install
# - name: Lint
# run: npm run lint
# - name: Compile
# run: npm run tsc
# - name: Test
# run: npm run test
# - name: Build
# run: npm run build
be-pipeline:
name: Backend Pipeline
runs-on: ubuntu-latest
env:
AUTH_SECRET: testsecret
AUTH_SUGAR: testsugar
DATABASE_PASSWORD: root
defaults:
run:
working-directory: ./api
Expand All @@ -70,6 +74,8 @@ jobs:
name: List the state of node modules
continue-on-error: true
run: npm list
- name: MySQL
run: which mysql
- name: Install dependencies
run: npm install
- name: Lint
Expand All @@ -80,4 +86,13 @@ jobs:
run: npm run build
- name: Test
run: npm run test

- name: E2E - Start Database Service
run: sudo systemctl start mysql.service
- name: E2E - Create Database
run: sudo mysql -u root -proot -e 'create database if not exists todo_test;'
- name: E2E Test - Setup Database
run: npm run db:test:setup
- name: E2E Test - Check DB
run: sudo mysql -u root -proot todo_test -e 'show tables'
- name: E2E Test
run: sudo npm run test:e2e
10 changes: 7 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"tsc": "tsc --project tsconfig.json",
"test": "jest --passWithNoTests",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:e2e": "NODE_ENV=test jest --config ./test/jest-e2e.json",
"typeorm": "typeorm-ts-node-commonjs",
"migration:create": "typeorm-ts-node-commonjs migration:create",
"migration:run": "typeorm-ts-node-commonjs migration:run -d src/db/config.ts",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d src/db/config.ts",
"db:migrate": "ts-node src/db/cli/index.ts migrate",
"db:seed": "ts-node src/db/cli/index.ts seed",
"db:reset": "ts-node src/db/cli/index.ts reset",
"db:setup": "npm run db:reset && npm run db:migrate && npm run db:seed"
"db:setup": "npm run db:reset && npm run db:seed",
"db:test:migrate": "NODE_ENV=test ts-node src/db/cli/index.ts migrate",
"db:test:seed": "NODE_ENV=test ts-node src/db/cli/index.ts seed",
"db:test:reset": "NODE_ENV=test ts-node src/db/cli/index.ts reset",
"db:test:setup": "npm run db:test:reset && npm run db:test:seed"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down
1 change: 0 additions & 1 deletion api/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class AppController {
@Get()
index(): { version: string } {
const res = this.appService.index();
console.log(res);
return res;
}
}
4 changes: 3 additions & 1 deletion api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import { LoggerModule } from './lib/modules/logger/logger.module';

const config = appConfig();

const envFilePath = [`.env.${process.env.NODE_ENV || 'development'}`];

@Module({
imports: [
ConfigModule.forRoot({
load: [appConfig],
envFilePath: ['.env.development.local', '.env.development'],
envFilePath,
validationSchema: Joi.object({
NODE_ENV: Joi.string()
.valid('development', 'production', 'test', 'provision')
Expand Down
123 changes: 103 additions & 20 deletions api/src/db/cli/seeds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import { EntityManager, DeepPartial } from 'typeorm';
import dayjs from 'dayjs';
import { UsersService } from '../../../domains/users/users.service';
import { User } from '../../../domains/users/user.entity';
import { Project } from '../../../domains/projects/project.entity';
import { Project, StatusType } from '../../../domains/projects/project.entity';
import { Task } from '../../../domains/tasks/task.entity';
import { Tag } from '../../../domains/tags/tag.entity';

export const seed = async ({ app, dataSource, logger }) => {
const usersService = app.get(UsersService);
const now = dayjs();
console.log('connection is establised');
logger.info('connection is establised');

console.log('users ========');
const users = [];
await dataSource.transaction(async (manager: EntityManager) => {
logger.info('[START] users ========');
for (let i = 1; i <= 10; i++) {
logger.info(`seeding for a user. index: ${i}`);
const timestamp = new Date();
const user = manager.create(User, {
username: `user ${i}`,
Expand All @@ -30,8 +29,9 @@ export const seed = async ({ app, dataSource, logger }) => {
await manager.save(user);
users.push(user);
}
logger.info('[END]', 'count:', users.length);

console.log('tags ========');
logger.info('[START] tags ========');
const user = users[0];
const tags = [];
await Promise.all(
Expand Down Expand Up @@ -62,14 +62,16 @@ export const seed = async ({ app, dataSource, logger }) => {
tags.push(tag);
}),
);
logger.info('[END]', 'count:', tags.length);

console.log('projects ========');
logger.info('[START] projects ========');
const projects = [];
await Promise.all(
[
{
userId: user.id,
name: 'プログラミング',
uuid: '9a1b53d8-4afc-4630-a26e-3634a10bf619',
slug: 'programming',
goal: '期限日までにフロントエンドエンジニアとして就職する。',
shouldbe: 'エンジニアとしての学習習慣を身につけて生活する。',
Expand All @@ -78,6 +80,7 @@ export const seed = async ({ app, dataSource, logger }) => {
{
userId: user.id,
name: '英語',
uuid: 'ee9f5f2e-fc8c-4830-985a-a44e96e96ffe',
slug: 'english',
goal: 'IELTS Over All 7.0',
shouldbe: '英語に浸る',
Expand All @@ -86,6 +89,7 @@ export const seed = async ({ app, dataSource, logger }) => {
{
userId: user.id,
name: 'プライベート',
uuid: '75cda72f-9883-4570-b3b5-66d389d5b1a9',
slug: 'private',
goal: '長期休みに海外旅行する',
status: 'active' as const,
Expand All @@ -104,12 +108,17 @@ export const seed = async ({ app, dataSource, logger }) => {
);
Promise.all(
new Array(20 - projects.length).fill('').map(async (_, index) => {
let status: StatusType = 'active' as const;
if (index >= 14) {
status = 'archived' as const;
}

const it: DeepPartial<Project> = {
userId: user.id,
name: `ダミープロジェクト ${index + 1}`,
slug: `dummy-projec-${index + 1}`,
goal: `ダミープロジェクト ${index + 1}`,
status: 'active' as const,
status,
};

it.deadline = dayjs()
Expand All @@ -127,38 +136,105 @@ export const seed = async ({ app, dataSource, logger }) => {
projects.push(project);
}),
);
console.log('tasks ========');
logger.info('[END]', 'count:', projects.length);

logger.info('[START] tasks ========');
const args = [
{
project: projects[0],
premadeTasks: [
{
userId: user.id,
projectId: projects[0].id,
uuid: '067176b2-baaf-4936-b7d4-6d202ab72639',
kind: 'task',
status: 'scheduled',
},
],
premadeMilestones: [
{
userId: user.id,
uuid: '67331996-7671-4cce-87a4-18b46adbd230',
projectId: projects[0].id,
title: `milestone 0`,
kind: 'milestone' as const,
status: 'scheduled' as const,
},
],
tasks: [],
count: 100,
baseDate: dayjs().add(1, 'year'),
},
{
project: projects[1],
premadeTasks: [
{
userId: user.id,
projectId: projects[1].id,
uuid: 'c185e0c2-842e-46f0-a1e8-41d598569431',
kind: 'task',
status: 'scheduled',
},
],
premadeMilestones: [
{
userId: user.id,
uuid: 'be36a559-ec88-4817-b98e-c084a0d51616',
projectId: projects[1].id,
title: `milestone 0`,
kind: 'milestone' as const,
status: 'scheduled' as const,
},
],
tasks: [],
count: 30,
baseDate: dayjs().add(1, 'year'),
},
{
project: projects[2],
premadeTasks: [
{
userId: user.id,
uuid: '1afdd678-1690-4567-9b9f-053cbbec286d',
projectId: projects[2].id,
kind: 'task',
status: 'scheduled',
},
],
premadeMilestones: [
{
userId: user.id,
projectId: projects[2].id,
title: `milestone 0`,
kind: 'milestone' as const,
status: 'scheduled' as const,
},
],
tasks: [],
count: 10,
baseDate: dayjs().add(1, 'year'),
},
];

await Promise.all(
args.map(async (arg, index) => {
console.log('tasks ========', index);
const { project, tasks, count, baseDate } = arg;
for (let i = 0; i < count; i++) {
args.map(async (arg) => {
const { project, premadeTasks, tasks, count, baseDate } = arg;
premadeTasks.forEach((it) => tasks.push(it));

const _count = count - premadeTasks.length;
for (let i = 0; i < _count; i++) {
let status = 'scheduled';
if (i >= arg.count - 3) {
status = 'archived';
} else if (i >= arg.count - 6) {
status = 'completed';
}

tasks.push({
userId: user.id,
projectId: project.id,
kind: 'task' as const,
status: 'scheduled' as const,
status,
});
}

Expand All @@ -178,13 +254,16 @@ export const seed = async ({ app, dataSource, logger }) => {

let head = 0;
for (let i = 0; i < 5; i++) {
const milestone = {
userId: user.id,
projectId: project.id,
title: `milestone ${i}`,
kind: 'milestone' as const,
status: 'scheduled' as const,
};
const milestone =
i === 0
? arg.premadeMilestones[0]
: {
userId: user.id,
projectId: project.id,
title: `milestone ${i}`,
kind: 'milestone' as const,
status: 'scheduled' as const,
};

const m = manager.create(Task, milestone);
const tail = head + Math.ceil((tasks.length - 1 - head) / 2);
Expand All @@ -201,5 +280,9 @@ export const seed = async ({ app, dataSource, logger }) => {
}
}),
);
const allTaskCount = args.reduce((acc, it) => {
return acc + it.tasks.length;
}, 0);
logger.info('[END]', 'count:', allTaskCount);
});
};

0 comments on commit b480b97

Please sign in to comment.