Skip to content

Commit 958c3af

Browse files
committed
feat: basic test setup with db
1 parent 9e9ce2b commit 958c3af

12 files changed

+3524
-24
lines changed

.env.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
MYSQL_ROOT_PASSWORD=root
2+
MYSQL_DATABASE=polymorphic
3+
MYSQL_USER=root
4+
MYSQL_PASSWORD=root
5+
6+
TYPEORM_CONNECTION=mysql
7+
TYPEORM_HOST=localhost
8+
TYPEORM_PORT=3306
9+
TYPEORM_DATABASE=polymorphic
10+
TYPEORM_USERNAME=root
11+
TYPEORM_PASSWORD=root
12+
TYPEORM_ENTITIES=src/__tests__/**/*.entity.ts
13+
TYPEORM_SYNCHRONIZE=true
14+
TYPEORM_LOGGING=true
15+
TYPEORM_SUBSCRIBERS=src/__tests__/**/*.subscriber.ts

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules/
22
dist/
33
coverage/
4+
.env

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
src
22
.idea
33
.travis.yml
4+
docker-compose.yml

docker-compose.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "3"
2+
3+
services:
4+
db:
5+
image: mysql:5.7
6+
env_file: .env
7+
ports:
8+
- 3306:3306

package.json

+21-1
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,36 @@
2525
"format": "prettier --write \"**/*.ts\"",
2626
"build": "rm -rf ./dist && tsc && npm run build:index",
2727
"build:index": "rm -rf ./index.js ./index.d.ts && tsc -d --skipLibCheck ./index.ts",
28-
"prepublish": "npm run format && npm run build"
28+
"prepublish": "npm run format && npm run build",
29+
"setup:test": "node_modules/.bin/ts-node node_modules/.bin/typeorm schema:sync",
30+
"test": "jest"
2931
},
3032
"devDependencies": {
33+
"@types/jest": "^26.0.19",
3134
"@types/node": "^14.14.14",
35+
"dotenv": "^8.2.0",
36+
"jest": "^26.6.3",
37+
"mysql": "^2.18.1",
3238
"prettier": "^2.2.1",
3339
"reflect-metadata": "^0.1.13",
40+
"ts-jest": "^26.4.4",
3441
"typeorm": "^0.2.29",
3542
"typescript": "^4.1.3"
3643
},
3744
"peerDependencies": {
3845
"typeorm": "^0.2.29"
46+
},
47+
"jest": {
48+
"moduleFileExtensions": [
49+
"js",
50+
"json",
51+
"ts"
52+
],
53+
"rootDir": "src",
54+
"testRegex": ".spec.ts$",
55+
"transform": {
56+
"^.+\\.(t|j)s$": "ts-jest"
57+
},
58+
"coverageDirectory": "../coverage"
3959
}
4060
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
2+
import { PolymorphicParent } from '../../../dist';
3+
import { MerchantEntity } from './merchant.entity';
4+
import { UserEntity } from './user.entity';
5+
6+
@Entity('adverts')
7+
export class AdvertEntity {
8+
@PrimaryGeneratedColumn()
9+
id: number;
10+
11+
@PolymorphicParent(() => [UserEntity, MerchantEntity], {
12+
eager: true,
13+
})
14+
owner: UserEntity | MerchantEntity;
15+
16+
@Column()
17+
entityId: number;
18+
19+
@Column()
20+
entityType: string;
21+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Entity, PrimaryGeneratedColumn } from 'typeorm';
2+
import { PolymorphicChildren } from '../../../dist';
3+
import { AdvertEntity } from './advert.entity';
4+
5+
@Entity('merchants')
6+
export class MerchantEntity {
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@PolymorphicChildren(() => AdvertEntity, {
11+
eager: false,
12+
})
13+
adverts: AdvertEntity[];
14+
}

src/__tests__/entities/user.entity.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Entity, PrimaryGeneratedColumn } from 'typeorm';
2+
import { PolymorphicChildren } from '../../../dist';
3+
import { AdvertEntity } from './advert.entity';
4+
5+
@Entity('users')
6+
export class UserEntity {
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@PolymorphicChildren(() => AdvertEntity, {
11+
eager: false,
12+
})
13+
adverts: AdvertEntity[];
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Connection, createConnection } from 'typeorm';
2+
import { AdvertEntity } from './entities/advert.entity';
3+
import { UserEntity } from './entities/user.entity';
4+
import { config } from 'dotenv';
5+
import { resolve } from 'path';
6+
import { AdvertRepository } from './repository/advert.repository';
7+
8+
describe('AbstractPolymorphicRepository', () => {
9+
let connection: Connection;
10+
beforeAll(async () => {
11+
config({
12+
path: resolve(__dirname, '.', '..', '..', '.env'),
13+
});
14+
15+
connection = await createConnection({
16+
type: 'mysql',
17+
host: process.env.TYPEORM_HOST,
18+
port: parseInt(process.env.TYPEORM_PORT, 10),
19+
username: process.env.TYPEORM_USERNAME,
20+
password: process.env.TYPEORM_PASSWORD,
21+
entities: ['./*/**/*.entity.ts'],
22+
synchronize: process.env.TYPEORM_SYNCHRONIZE === 'true',
23+
database: process.env.TYPEORM_DATABASE,
24+
});
25+
});
26+
27+
afterAll(async () => {
28+
await connection.close();
29+
});
30+
31+
it('Can create', async () => {
32+
const repository = connection.getCustomRepository(AdvertRepository);
33+
34+
const user = new UserEntity();
35+
36+
const result = repository.create({
37+
owner: user,
38+
});
39+
40+
expect(result).toBeInstanceOf(AdvertEntity);
41+
expect(result.owner).toBeInstanceOf(UserEntity);
42+
});
43+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { EntityRepository } from 'typeorm';
2+
import { AbstractPolymorphicRepository } from '../../polymorphic.repository';
3+
import { AdvertEntity } from '../entities/advert.entity';
4+
5+
@EntityRepository(AdvertEntity)
6+
export class AdvertRepository extends AbstractPolymorphicRepository<AdvertEntity> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { AbstractPolymorphicRepository } from '../../../dist';
2+
import { UserEntity } from '../entities/user.entity';
3+
4+
export class UserRepository extends AbstractPolymorphicRepository<UserEntity> {}

0 commit comments

Comments
 (0)