Skip to content

Commit 1eba32f

Browse files
committed
♻️ utilize a testing helper to mysql tests
1 parent 23df328 commit 1eba32f

18 files changed

+67
-272
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
2+
3+
export function setupMySQL(module: 'stations' | 'cards') {
4+
const _connection = MySQLConnection.getInstance();
5+
6+
const truncateTables = async () => {
7+
const database = process.env.DB_DATABASE_TEST;
8+
9+
if (module === 'stations') {
10+
await _connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
11+
} else if (module === 'cards') {
12+
await _connection.query('SET FOREIGN_KEY_CHECKS = 0');
13+
await _connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
14+
await _connection.query(
15+
`TRUNCATE TABLE \`${database}\`.\`transactions\``,
16+
);
17+
await _connection.query('SET FOREIGN_KEY_CHECKS = 1');
18+
}
19+
};
20+
21+
beforeEach(async () => {
22+
await truncateTables();
23+
});
24+
25+
afterEach(async () => {
26+
await truncateTables();
27+
});
28+
29+
return {
30+
get connection() {
31+
return _connection;
32+
},
33+
};
34+
}

src/@core/card/app/facade/card.facade.test.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { NotFoundException } from '#shared/exception/not-found.exception';
23
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
3-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
44
import { RepositoryVendor } from '#shared/utils/repository-vendor';
55

66
import { Card } from '../../domain/card';
@@ -105,24 +105,7 @@ describe('CardFacade', () => {
105105
});
106106

107107
describe('MYSQL', () => {
108-
const connection = MySQLConnection.getInstance();
109-
110-
const truncateTables = async () => {
111-
const database = process.env.DB_DATABASE_TEST;
112-
113-
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
114-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
115-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`transactions\``);
116-
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
117-
};
118-
119-
beforeEach(async () => {
120-
await truncateTables();
121-
});
122-
123-
afterEach(async () => {
124-
await truncateTables();
125-
});
108+
setupMySQL('cards');
126109

127110
it('should add a card', async () => {
128111
const { facade } = makeSut('MYSQL');

src/@core/card/app/use-case/add/add-card.use-case.test.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CardInMemoryRepository } from '#card/infra/repository/in-memory/card.in-memory.repository';
22
import { CardMongoRepository } from '#card/infra/repository/mongo/card.mongo.repository';
33
import { CardMySQLRepository } from '#card/infra/repository/mysql/card.mysql.repository';
4+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
45
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
5-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
66
import { RepositoryVendor } from '#shared/utils/repository-vendor';
77

88
import { AddCardUseCase } from './add-card.use-case';
@@ -40,24 +40,7 @@ describe('AddCardUseCase', () => {
4040
});
4141

4242
describe('MYSQL', () => {
43-
const connection = MySQLConnection.getInstance();
44-
45-
const truncateTables = async () => {
46-
const database = process.env.DB_DATABASE_TEST;
47-
48-
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
49-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
50-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`transactions\``);
51-
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
52-
};
53-
54-
beforeEach(async () => {
55-
await truncateTables();
56-
});
57-
58-
afterEach(async () => {
59-
await truncateTables();
60-
});
43+
setupMySQL('cards');
6144

6245
it('should add a card', async () => {
6346
const useCase = makeSut('MYSQL');

src/@core/card/app/use-case/find-transactions-by-card-id/find-transactions-by-card-id.use-case.test.ts

+2-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Card } from '#card/domain/card';
22
import { CardInMemoryRepository } from '#card/infra/repository/in-memory/card.in-memory.repository';
33
import { CardMongoRepository } from '#card/infra/repository/mongo/card.mongo.repository';
44
import { CardMySQLRepository } from '#card/infra/repository/mysql/card.mysql.repository';
5+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
56
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
6-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
77
import { RepositoryVendor } from '#shared/utils/repository-vendor';
88

99
import { FindTransactionsByCardIdUseCase } from './find-transactions-by-card-id.use-case';
@@ -70,23 +70,7 @@ describe('FindTransactionsByCardIdUseCase', () => {
7070
});
7171

7272
describe('MYSQL', () => {
73-
const connection = MySQLConnection.getInstance();
74-
const truncateTables = async () => {
75-
const database = process.env.DB_DATABASE_TEST;
76-
77-
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
78-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
79-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`transactions\``);
80-
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
81-
};
82-
83-
beforeEach(async () => {
84-
await truncateTables();
85-
});
86-
87-
afterEach(async () => {
88-
await truncateTables();
89-
});
73+
setupMySQL('cards');
9074

9175
it('should find transactions by card id', async () => {
9276
const { findTransactionsByCardIdUseCase, repository } = makeSut('MYSQL');

src/@core/card/app/use-case/update/update-card.use-case.test.ts

+2-18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Card } from '#card/domain/card';
22
import { CardInMemoryRepository } from '#card/infra/repository/in-memory/card.in-memory.repository';
33
import { CardMongoRepository } from '#card/infra/repository/mongo/card.mongo.repository';
44
import { CardMySQLRepository } from '#card/infra/repository/mysql/card.mysql.repository';
5+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
56
import { NotFoundException } from '#shared/exception/not-found.exception';
67
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
7-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
88
import { RepositoryVendor } from '#shared/utils/repository-vendor';
99

1010
import { UpdateCardUseCase } from './update-card.use-case';
@@ -65,23 +65,7 @@ describe('UpdateCardUseCase', () => {
6565
});
6666

6767
describe('MYSQL', () => {
68-
const connection = MySQLConnection.getInstance();
69-
const truncateTables = async () => {
70-
const database = process.env.DB_DATABASE_TEST;
71-
72-
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
73-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
74-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`transactions\``);
75-
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
76-
};
77-
78-
beforeEach(async () => {
79-
await truncateTables();
80-
});
81-
82-
afterEach(async () => {
83-
await truncateTables();
84-
});
68+
setupMySQL('cards');
8569

8670
it('should update a card', async () => {
8771
const { updateUseCase, repository } = makeSut('MYSQL');

src/@core/card/infra/repository/mysql/card.mysql.repository.spec.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
import { Card, CreateCardInput } from '#card/domain/card';
2+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
23
import { NotFoundException } from '#shared/exception/not-found.exception';
3-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
44

55
import { CardMySQLRepository } from './card.mysql.repository';
66

77
const makeSut = () => new CardMySQLRepository();
88

99
describe('CardMySQLRepository', () => {
10-
const connection = MySQLConnection.getInstance();
11-
12-
const truncateTables = async () => {
13-
const database = process.env.DB_DATABASE_TEST;
14-
15-
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
16-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`cards\``);
17-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`transactions\``);
18-
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
19-
};
20-
21-
beforeEach(async () => {
22-
await truncateTables();
23-
});
24-
25-
afterEach(async () => {
26-
await truncateTables();
27-
});
10+
setupMySQL('cards');
2811

2912
it('should insert a card', async () => {
3013
const repository = makeSut();

src/@core/station/app/facade/station.facade.test.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { NotFoundException } from '#shared/exception/not-found.exception';
23
import { UniqueFieldException } from '#shared/exception/unique-field.exception';
34
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
4-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
55
import { RepositoryVendor } from '#shared/utils/repository-vendor';
66

77
import { StationInMemoryRepository } from '../../infra/repository/in-memory/station.in-memory.repository';
@@ -176,20 +176,7 @@ describe('StationFacade', () => {
176176
});
177177

178178
describe('MYSQL', () => {
179-
const connection = MySQLConnection.getInstance();
180-
const truncateTable = async () => {
181-
const database = process.env.DB_DATABASE_TEST;
182-
183-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
184-
};
185-
186-
beforeEach(async () => {
187-
await truncateTable();
188-
});
189-
190-
afterEach(async () => {
191-
await truncateTable();
192-
});
179+
setupMySQL('stations');
193180

194181
it('should add a station', async () => {
195182
const facade = makeSut('MYSQL');

src/@core/station/app/use-case/add/add-station.use-case.test.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { InvalidFieldException } from '#shared/exception/invalid-field.exception';
23
import { UniqueFieldException } from '#shared/exception/unique-field.exception';
34
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
4-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
55
import { RepositoryVendor } from '#shared/utils/repository-vendor';
66

77
import {
@@ -137,21 +137,7 @@ describe('AddStationUseCase', () => {
137137
});
138138

139139
describe('MYSQL', () => {
140-
const connection = MySQLConnection.getInstance();
141-
142-
const truncateTable = async () => {
143-
const database = process.env.DB_DATABASE_TEST;
144-
145-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
146-
};
147-
148-
beforeEach(async () => {
149-
await truncateTable();
150-
});
151-
152-
afterEach(async () => {
153-
await truncateTable();
154-
});
140+
setupMySQL('stations');
155141

156142
it('should add a station', async () => {
157143
const { addUseCase: useCase } = makeSut('MYSQL');

src/@core/station/app/use-case/find-all/find-all-stations.use-case.test.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
2-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
33
import { RepositoryVendor } from '#shared/utils/repository-vendor';
44

55
import { Station } from '../../../domain/station';
@@ -71,21 +71,7 @@ describe('FindAllStationsUseCase', () => {
7171
});
7272

7373
describe('MYSQL', () => {
74-
const connection = MySQLConnection.getInstance();
75-
76-
const truncateTable = async () => {
77-
const database = process.env.DB_DATABASE_TEST;
78-
79-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
80-
};
81-
82-
beforeEach(async () => {
83-
await truncateTable();
84-
});
85-
86-
afterEach(async () => {
87-
await truncateTable();
88-
});
74+
setupMySQL('stations');
8975

9076
it('should find all stations', async () => {
9177
const { findAllUseCase, repository } = makeSut('MYSQL');

src/@core/station/app/use-case/find-by-id/find-station-by-id.use-case.test.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { NotFoundException } from '#shared/exception/not-found.exception';
23
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
3-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
44
import { RepositoryVendor } from '#shared/utils/repository-vendor';
55

66
import { Station } from '../../../domain/station';
@@ -67,21 +67,7 @@ describe('FindStationByIdUseCase', () => {
6767
});
6868

6969
describe('MYSQL', () => {
70-
const connection = MySQLConnection.getInstance();
71-
72-
const truncateTable = async () => {
73-
const database = process.env.DB_DATABASE_TEST;
74-
75-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
76-
};
77-
78-
beforeEach(async () => {
79-
await truncateTable();
80-
});
81-
82-
afterEach(async () => {
83-
await truncateTable();
84-
});
70+
setupMySQL('stations');
8571

8672
it('should find all stations', async () => {
8773
const { findByIdUseCase, repository } = makeSut('MYSQL');

src/@core/station/app/use-case/remove-all/remove-all-stations.use-case.test.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
2-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
33
import { RepositoryVendor } from '#shared/utils/repository-vendor';
44

55
import { Station } from '../../../domain/station';
@@ -52,21 +52,7 @@ describe('RemoveAllStationsUseCase', () => {
5252
});
5353

5454
describe('MYSQL', () => {
55-
const connection = MySQLConnection.getInstance();
56-
57-
const truncateTable = async () => {
58-
const database = process.env.DB_DATABASE_TEST;
59-
60-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
61-
};
62-
63-
beforeEach(async () => {
64-
await truncateTable();
65-
});
66-
67-
afterEach(async () => {
68-
await truncateTable();
69-
});
55+
setupMySQL('stations');
7056

7157
it('should remove all stations', async () => {
7258
const { removeAllUseCase, repository } = makeSut('MYSQL');

src/@core/station/app/use-case/remove/remove-station.use-case.test.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { setupMySQL } from '#core/@seedwork/infra/testing/helpers/db';
12
import { NotFoundException } from '#shared/exception/not-found.exception';
23
import { MongoHelper } from '#shared/infra/db/mongo/mongo-helper';
3-
import { MySQLConnection } from '#shared/infra/db/mysql/mysql-connection';
44
import { RepositoryVendor } from '#shared/utils/repository-vendor';
55

66
import { Station } from '../../../domain/station';
@@ -68,21 +68,7 @@ describe('RemoveStationUseCase', () => {
6868
});
6969

7070
describe('MYSQL', () => {
71-
const connection = MySQLConnection.getInstance();
72-
73-
const truncateTable = async () => {
74-
const database = process.env.DB_DATABASE_TEST;
75-
76-
await connection.query(`TRUNCATE TABLE \`${database}\`.\`stations\``);
77-
};
78-
79-
beforeEach(async () => {
80-
await truncateTable();
81-
});
82-
83-
afterEach(async () => {
84-
await truncateTable();
85-
});
71+
setupMySQL('stations');
8672

8773
it('should remove a station', async () => {
8874
const { removeUseCase, repository } = makeSut('MYSQL');

0 commit comments

Comments
 (0)