Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add e2e tests #135

Merged
merged 22 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/daily.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: "Daily"
on:
schedule:
- cron: "0 6,12,18 * * *"
- cron: "0 10,16,22 * * *"
workflow_dispatch:

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# frontend env
NUXT_PUBLIC_API_BASE_URL: http://localhost:3001/open-api
NUXT_PUBLIC_API_BASE_URL: http://localhost:4000/open-api
NITRO_PORT: 3001

# backend env
Expand Down
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"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",
"seed:college": "node ./dist/seeder.js college"
"seed:college": "node ./dist/seeder.js college",
"seed:course": "node ./dist/seeder.js course",
"seed:department": "node ./dist/seeder.js department"
},
"dependencies": {
"@casl/ability": "^5.4.4",
Expand Down
4 changes: 2 additions & 2 deletions backend/src/course/course.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export class Course {
passwordCard: string;

@Column('json')
teachers: string[];
teachers: string;

@Column('json')
classTimes: string[];
classTimes: string;

@Column()
limitCnt: number;
Expand Down
6 changes: 5 additions & 1 deletion backend/src/seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function execSeed(type: string, seederService: SeederService) {
switch (type) {
case 'college':
return seederService.seedCollege();
case 'course':
return seederService.seedCourse();
case 'department':
return seederService.seedDepartment();
default:
console.error(`No seed found for ${type}`);
}
Expand All @@ -17,7 +21,7 @@ async function bootstrap() {
const type = process.argv[2];
if (!type) console.error('Please provide a type of seed to run.');
console.log(`Running seed for ${type}`);
execSeed(type, seederService);
await execSeed(type, seederService);
console.log(`Finished running seed for ${type}`);
await app.close();
}
Expand Down
11 changes: 11 additions & 0 deletions backend/src/seeder/course-seeder/course-seeder.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Course } from '../../course/course.entity';
import { CourseSeederService } from './course-seeder.service';

@Module({
imports: [TypeOrmModule.forFeature([Course])],
providers: [CourseSeederService],
exports: [CourseSeederService],
})
export class CourseSeederModule {}
34 changes: 34 additions & 0 deletions backend/src/seeder/course-seeder/course-seeder.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Course } from '../../course/course.entity';
import { Repository } from 'typeorm';
import { CourseSeederService } from './course-seeder.service';

describe('CourseSeederService', () => {
let service: CourseSeederService;
let courseRepository: Repository<Course>;

const COURSE_REPOSITORY_TOKEN = getRepositoryToken(Course);
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CourseSeederService,
{
provide: COURSE_REPOSITORY_TOKEN,
useValue: {},
},
],
}).compile();

service = module.get<CourseSeederService>(CourseSeederService);
courseRepository = module.get<Repository<Course>>(COURSE_REPOSITORY_TOKEN);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should be defined', () => {
expect(courseRepository).toBeDefined();
});
});
129 changes: 129 additions & 0 deletions backend/src/seeder/course-seeder/course-seeder.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { Injectable } from '@nestjs/common';
import { Course } from '../../course/course.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class CourseSeederService {
constructor(
@InjectRepository(Course)
private readonly courseRepository: Repository<Course>,
) {}

async seedCourse() {
await this.courseRepository.clear();
Copy link
Member

Choose a reason for hiding this comment

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

Could you extract it into the npm script?

const course1 = this.courseRepository.create({
year: 110,
semester: 1,
serialNo: 1001,
classNo: 'PE1022A',
title: '大一體育',
credit: 0,
passwordCard: 'OPTIONAL',
teachers: '["沈淑鳳"]',
classTimes: '["1-3", "1-4"]',
limitCnt: 0,
admitCnt: 0,
waitCnt: 47,
collegeId: 'collegeI0',
departmentId: 'deptI0I1I0',
courseType: 'REQUIRED',
});
await this.courseRepository.save(course1);

const course2 = this.courseRepository.create({
year: 110,
semester: 1,
serialNo: 1002,
classNo: 'CL0126*',
title: '國文(A)經典閱讀:傳統節氣作品選讀',
credit: 3,
passwordCard: 'OPTIONAL',
teachers: '["李宜學"]',
classTimes: '["3-3, 3-4, 1-5"]',
limitCnt: 58,
admitCnt: 0,
waitCnt: 155,
collegeId: 'collegeI1',
departmentId: 'deptI1I1001I0',
courseType: 'REQUIRED',
});
await this.courseRepository.save(course2);

const course3 = this.courseRepository.create({
year: 110,
semester: 1,
serialNo: 1003,
classNo: 'CE2004A',
title: '程式語言',
credit: 3,
passwordCard: 'OPTIONAL',
teachers: '["許富皓"]',
classTimes: '["2-6", "2-7", "2-8"]',
limitCnt: 60,
admitCnt: 0,
waitCnt: 140,
collegeId: 'collegeI5',
departmentId: 'deptI1I5002I0',
courseType: 'REQUIRED',
});
await this.courseRepository.save(course3);

const course4 = this.courseRepository.create({
year: 111,
semester: 0,
serialNo: 1004,
classNo: 'PE1011B',
title: '大一體育',
credit: 0,
passwordCard: 'OPTIONAL',
teachers: '["姚承義"]',
classTimes: '["1-5", "1-6"]',
limitCnt: 0,
admitCnt: 0,
waitCnt: 0,
collegeId: 'collegeI0',
departmentId: 'deptI0I1I0',
courseType: 'REQUIRED',
});
await this.courseRepository.save(course4);

const course = new Course();
course.year = 111;
course.semester = 0;
course.serialNo = 1001;
course.classNo = 'SE6030*';
course.title = '軟體工程實務';
course.credit = 1;
course.passwordCard = 'passwordCard';
course.teachers = '["梁德容", "王尉任", "鄭永斌"]';
course.classTimes = '["1-2", "1-3", "1-4"]';
course.limitCnt = 456;
course.admitCnt = 789;
course.waitCnt = 1011;
course.collegeId = 'collegeId';
course.departmentId = 'csie';
await this.courseRepository.save(course);

for (let i = 0; i < 50; i++) {
const course5 = this.courseRepository.create({
year: 111,
semester: 0,
serialNo: 1005,
classNo: 'EG1007*',
title: '普通化學',
credit: 3,
passwordCard: 'OPTIONAL',
teachers: '["劉奕宏"]',
classTimes: '["2-6", "2-7", "4-2"]',
limitCnt: 0,
admitCnt: 0,
waitCnt: 63,
collegeId: 'collegeI3',
departmentId: 'deptI1I3000I0',
courseType: 'REQUIRED',
});
await this.courseRepository.save(course5);
}
}
}
11 changes: 11 additions & 0 deletions backend/src/seeder/department-seeder/department-seeder.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Department } from '../../department/department.entity';
import { DepartmentSeederService } from './department-seeder.service';

@Module({
imports: [TypeOrmModule.forFeature([Department])],
providers: [DepartmentSeederService],
exports: [DepartmentSeederService],
})
export class DepartmentSeederModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Department } from '../../department/department.entity';
import { Repository } from 'typeorm';
import { DepartmentSeederService } from './department-seeder.service';

describe('DepartmentSeederService', () => {
let service: DepartmentSeederService;
let departmentRepository: Repository<Department>;

const DEPARTMENT_REPOSITORY_TOKEN = getRepositoryToken(Department);
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DepartmentSeederService,
{
provide: DEPARTMENT_REPOSITORY_TOKEN,
useValue: {},
},
],
}).compile();

service = module.get<DepartmentSeederService>(DepartmentSeederService);
departmentRepository = module.get<Repository<Department>>(
DEPARTMENT_REPOSITORY_TOKEN,
);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should be defined', () => {
expect(departmentRepository).toBeDefined();
});
});
50 changes: 50 additions & 0 deletions backend/src/seeder/department-seeder/department-seeder.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Injectable } from '@nestjs/common';
import { Department } from '../../department/department.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class DepartmentSeederService {
constructor(
@InjectRepository(Department)
private readonly departmentRepository: Repository<Department>,
) {}

async seedDepartment() {
await this.departmentRepository.clear();
const department1 = this.departmentRepository.create({
departmentId: 'deptI0I1I0',
departmentName: '體育室',
collegeId: 'collegeI0',
});
await this.departmentRepository.save(department1);

const department2 = this.departmentRepository.create({
departmentId: 'deptI1I1001I0',
departmentName: '中國文學系',
collegeId: 'collegeI1',
});
await this.departmentRepository.save(department2);

const department3 = this.departmentRepository.create({
departmentId: 'deptI1I3000I0',
departmentName: '工學院',
collegeId: 'collegeI3',
});
await this.departmentRepository.save(department3);

const department4 = this.departmentRepository.create({
departmentId: 'deptI1I5002I0',
departmentName: '資電學院',
collegeId: 'collegeI5',
});
await this.departmentRepository.save(department4);

const department = new Department();
department.departmentId = 'csie';
department.departmentName = '資訊工程學系';
department.collegeId = 'ncu';

await this.departmentRepository.save(department);
}
}
4 changes: 4 additions & 0 deletions backend/src/seeder/seeder.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { configuration } from '../config';
import { CollegeSeederModule } from './college-seeder/college-seeder.module';
import { CourseSeederModule } from './course-seeder/course-seeder.module';
import { DepartmentSeederModule } from './department-seeder/department-seeder.module';
import { SeederService } from './seeder.service';

@Module({
Expand All @@ -20,6 +22,8 @@ import { SeederService } from './seeder.service';
inject: [ConfigService],
}),
CollegeSeederModule,
CourseSeederModule,
DepartmentSeederModule,
],
providers: [SeederService],
})
Expand Down
10 changes: 10 additions & 0 deletions backend/src/seeder/seeder.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CollegeSeederService } from './college-seeder/college-seeder.service';
import { CourseSeederService } from './course-seeder/course-seeder.service';
import { DepartmentSeederService } from './department-seeder/department-seeder.service';
import { SeederService } from './seeder.service';

describe('SeederService', () => {
Expand All @@ -13,6 +15,14 @@ describe('SeederService', () => {
provide: CollegeSeederService,
useValue: {},
},
{
provide: CourseSeederService,
useValue: {},
},
{
provide: DepartmentSeederService,
useValue: {},
},
],
}).compile();

Expand Down
Loading