Skip to content

feat: make typeorm migration no auto load and create end to end tests #7

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

Merged
merged 2 commits into from
Jul 25, 2024
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
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
Dockerfile
.gitignore
README.md
.vscodew
dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lerna-debug.log*
.env.test.local
.env.production.local
.env.local
.env.docker

# temp directory
.temp
Expand Down
15 changes: 14 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
{
"a-file-icon-vscode.activeIconPacks": ["nest", "rails"]
"a-file-icon-vscode.activeIconPacks": [
"angular",
"ngrx",
"nest",
"react",
"redux",
"recoil",
"vue",
"vuex",
"rails",
"phalcon",
"volt",
"none"
]
}
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.9'

services:
db:
image: mysql:latest
container_name: mostaqem_db
environment:
MYSQL_ROOT_PASSWORD: 'db_password'
MYSQL_DATABASE: 'mostaqem_db'
api_v1:
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
expose:
- '3000'
env_file:
- .env.docker
volumes:
- ./src:/app/src
links:
- db
13 changes: 13 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:22-alpine

WORKDIR /app

COPY . .

RUN npm install -g pnpm

RUN pnpm install

EXPOSE 3000

CMD [ "pnpm" , "run", "start:prod"]
12 changes: 12 additions & 0 deletions jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "./",
"testRegex": ".*\\.e2e-spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/src/$1"
},
"testEnvironment": "node"
}
29 changes: 29 additions & 0 deletions migrations/1721758921061-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Init1721758921061 implements MigrationInterface {
name = 'Init1721758921061'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`reciter\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name_english\` varchar(100) NOT NULL, \`name_arabic\` varchar(100) NOT NULL, \`image\` varchar(250) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`reciter_surah\` (\`reciter_id\` int NOT NULL, \`surah_id\` int NOT NULL, \`url\` varchar(255) NOT NULL, PRIMARY KEY (\`reciter_id\`, \`surah_id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`surah\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name_arabic\` varchar(100) NOT NULL, \`name_complex\` varchar(100) NOT NULL, \`verses_count\` int NOT NULL, \`revelation_place\` varchar(30) NOT NULL, \`image\` varchar(250) NULL, INDEX \`IDX_SURAH\` (\`id\`, \`name_arabic\`, \`name_complex\`, \`verses_count\`, \`revelation_place\`, \`image\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`verse\` (\`id\` int NOT NULL AUTO_INCREMENT, \`vers\` text NOT NULL, \`verse_number\` int NOT NULL, \`vers_lang\` varchar(3) NOT NULL, \`surah_id\` int NOT NULL, INDEX \`idx_surah_id\` (\`surah_id\`), INDEX \`SURAH_VERSE_UNIQUE\` (\`surah_id\`, \`verse_number\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` ADD CONSTRAINT \`FK_c083c2e64bff5563b0940b6ae9a\` FOREIGN KEY (\`reciter_id\`) REFERENCES \`reciter\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` ADD CONSTRAINT \`FK_faeb6bf0be06de7f316921c0068\` FOREIGN KEY (\`surah_id\`) REFERENCES \`surah\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`verse\` ADD CONSTRAINT \`FK_7da0f9ffbbe4ea8116f2f36610a\` FOREIGN KEY (\`surah_id\`) REFERENCES \`surah\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`verse\` DROP FOREIGN KEY \`FK_7da0f9ffbbe4ea8116f2f36610a\``);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` DROP FOREIGN KEY \`FK_faeb6bf0be06de7f316921c0068\``);
await queryRunner.query(`ALTER TABLE \`reciter_surah\` DROP FOREIGN KEY \`FK_c083c2e64bff5563b0940b6ae9a\``);
await queryRunner.query(`DROP INDEX \`SURAH_VERSE_UNIQUE\` ON \`verse\``);
await queryRunner.query(`DROP INDEX \`idx_surah_id\` ON \`verse\``);
await queryRunner.query(`DROP TABLE \`verse\``);
await queryRunner.query(`DROP INDEX \`IDX_SURAH\` ON \`surah\``);
await queryRunner.query(`DROP TABLE \`surah\``);
await queryRunner.query(`DROP TABLE \`reciter_surah\``);
await queryRunner.query(`DROP TABLE \`reciter\``);
}

}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"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": "jest --config ./jest-e2e.json --detectOpenHandles",
"typeorm": "ts-node --require tsconfig-paths/register ./node_modules/typeorm/cli",
"typeorm:generate-migration": "npm run typeorm -- -d ./typeorm.config.ts migration:generate ./migrations/$npm_config_name",
"typeorm:migrate": "npm run typeorm -- -d ./typeorm.config.ts migration:run"
},
"dependencies": {
"@nestjs/cache-manager": "^2.2.2",
Expand All @@ -33,6 +36,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"dotenv": "^16.4.5",
"mysql2": "^3.10.3",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { CacheModule } from '@nestjs/cache-manager';
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
autoLoadEntities: true,
synchronize: true,
synchronize: process.env.NODE_ENV == 'development',
connectTimeout: 60000,
retryDelay: 6000,
}),
Expand Down
16 changes: 12 additions & 4 deletions test/app.e2e-spec.ts → test/audio.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { isURL } from 'class-validator';

describe('AppController (e2e)', () => {
describe('Audio E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
Expand All @@ -15,10 +16,17 @@ describe('AppController (e2e)', () => {
await app.init();
});

it('/ (GET)', () => {
it('should get reciter surah audio', () => {
const surah_id = 1;
const reciter_id = 1;

return request(app.getHttpServer())
.get('/')
.get(`/audio?surah_id=${surah_id}&reciter_id=${reciter_id}`)
.expect(200)
.expect('Hello World!');
.expect((res) => {
expect(res.body.surah_id).toBe(surah_id);
expect(res.body.reciter_id).toBe(reciter_id);
expect(isURL(res.body.url)).toBe(true);
});
});
});
9 changes: 0 additions & 9 deletions test/jest-e2e.json

This file was deleted.

45 changes: 45 additions & 0 deletions test/reciter.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Reciter E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get all reciters', () => {
return request(app.getHttpServer()).get('/reciter').expect(200);
});

it('should get reciter by id', () => {
return request(app.getHttpServer()).get('/reciter/1').expect(200);
});

it('should return 404 if reciter not found by id', () => {
return request(app.getHttpServer()).get('/reciter/99999').expect(404);
});

describe('adding image section', () => {
it('should add an image to a reciter', () => {
return request(app.getHttpServer())
.patch('/reciter/1')
.send({ image: 'image-url' })
.expect(200);
});

it('should return 404 if reciter not found by id', () => {
return request(app.getHttpServer())
.patch('/reciter/99999')
.send({ image: 'image-url' })
.expect(404);
});
});
});
70 changes: 70 additions & 0 deletions test/surah.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Surah E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get list of all surah', () => {
return request(app.getHttpServer())
.get('/surah')
.expect(200)
.expect((res) => {
expect(res.body).toHaveLength(114);
expect(res.body[0]).toMatchObject({
id: 1,
name_arabic: 'الفاتحة',
name_complex: 'Al-Fatihah',
revelation_place: 'meccan',
verses_count: 7,
image: 'image-url',
});
});
});

it('should get a surah by id', () => {
return request(app.getHttpServer())
.get('/surah/1')
.expect(200)
.expect((res) => {
expect(res.body).toMatchObject({
id: 1,
name_arabic: 'الفاتحة',
name_complex: 'Al-Fatihah',
revelation_place: 'meccan',
verses_count: 7,
image: 'image-url',
});
});
});

describe('adding image section', () => {
it('should add an image to a surah', () => {
return request(app.getHttpServer())
.patch('/surah/1')
.send({ image: 'image-url' })
.expect(200);
});

it('should return 404 if surah not found by id', () => {
return request(app.getHttpServer())
.patch('/surah/150')
.send({ image: 'image-url' })
.expect(404);
});
});

it('should return 404 if surah not found by id', () => {
return request(app.getHttpServer()).get('/surah/150').expect(404);
});
});
33 changes: 33 additions & 0 deletions test/verse.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';

describe('Verse E2E TESTS', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('should get surah verses', () => {
return request(app.getHttpServer())
.get('/verse/surah?surah_id=1')
.expect(200)
.expect((res) => {
expect(res.body.verses).toHaveLength(7);
expect(res.body.verses[0]).toMatchObject({
id: 1,
vers: 'بِسۡمِ ٱللَّهِ ٱلرَّحۡمَٰنِ ٱلرَّحِيمِ',
verse_number: 1,
vers_lang: 'ar',
surah_id: 1,
});
});
});
});
15 changes: 15 additions & 0 deletions typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataSource } from 'typeorm';
import { config } from 'dotenv';
config({ path: '.env.docker' });

export default new DataSource({
type: 'mysql',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: ['dist/**/*.entity{.ts,.js}', 'src/**/*.entity{.ts,.js}'],
migrations: ['migrations/**'],
ssl: process.env.NODE_ENV == 'production' ? true : false,
});
Loading