Skip to content

Commit e685332

Browse files
author
fo057145
committed
refatora e reorganiza testes de integração, adiciona novos dados de exemplo e implementa funções de leitura de arquivos JSON e CSV
1 parent 708294d commit e685332

File tree

14 files changed

+105
-86
lines changed

14 files changed

+105
-86
lines changed

test/data/author-data.ts renamed to integration/data/author-data.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
1-
export const getListExpectData =
1+
export const dadoEsperadoGetList =
22
{
33
id: 1,
44
idBook: 1,
55
firstName: "First Name 1",
66
lastName: "Last Name 1"
77
}
88

9-
export const getByIdExpectData =
9+
export const dadoEsperadoGetById =
1010
{
1111
id: 2,
1212
idBook: 1,
1313
firstName: "First Name 2",
1414
lastName: "Last Name 2"
1515
}
1616

17-
export const getByIdNotFounded =
17+
export const getByIdNaoEncontrado =
1818
{
1919
type: 'https://tools.ietf.org/html/rfc7231#section-6.5.4',
2020
title: 'Not Found',
2121
status: 404,
2222
}
2323

24-
export const createAuthor = {
25-
24+
export const criaAutor = {
2625
id: 650,
2726
idBook: 1,
2827
firstName: "Luke",
2928
lastName: "Skywalker"
30-
3129
}
3230

33-
export const tryToCreateAnAuthorWithInvalidData = {
34-
31+
export const tentaCriarUmAutorComDadosInvalidos = {
3532
id: 650,
3633
idBook: 1,
3734
firstName: 1,
3835
lastName: 1
39-
4036
}
4137

42-
export const createInvalidAuthorReturnBody = {
38+
export const validaRetornoAutorInvalido = {
4339
type: 'https://tools.ietf.org/html/rfc7231#section-6.5.1',
4440
title: 'One or more validation errors occurred.',
4541
status: 400,
File renamed without changes.
File renamed without changes.

test/entity/entities.ts renamed to integration/entity/entities.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export class EntityService {
1010

1111
expect(response.statusCode).toBe(statusCode)
1212

13+
// Validações pré definidas, podem ser ajustada conforme o contexto de validação
1314
if (content) {
1415
expect(response.body[0]).toBeDefined();
1516
expect(response.body[0]).toEqual(expect.objectContaining(content));
1617
}
17-
1818
if (checkResponseMessage) {
1919
expect(response.body.message).toBe(checkResponseMessage);
2020
}
@@ -32,7 +32,6 @@ export class EntityService {
3232
expect(response.body).toBeDefined();
3333
expect(response.body).toEqual(expect.objectContaining(content));
3434
}
35-
3635
if (checkResponseMessage) {
3736
expect(response.body.message).toBe(checkResponseMessage);
3837
}
@@ -51,7 +50,6 @@ export class EntityService {
5150
expect(response.body).toBeDefined();
5251
expect(response.body).toEqual(expect.objectContaining(content));
5352
}
54-
5553
if (checkResponseMessage) {
5654
expect(response.body.message).toBe(checkResponseMessage);
5755
}

test/routes/author-route.ts renamed to integration/routes/author-route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const authorsRoute = {
22
getListAuthors: 'api/v1/Authors',
3-
createAuthors: 'api/v1/Authors',
3+
criaAutors: 'api/v1/Authors',
44
getAuthorsById: 'api/v1/Authors/',
55
updateAuthors: 'api/v1/Authors/',
66
deleteAuthors: 'api/v1/Authors/'
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe } from '@jest/globals';
2+
import {
3+
criaAutor,
4+
validaRetornoAutorInvalido,
5+
dadoEsperadoGetById,
6+
getByIdNaoEncontrado,
7+
dadoEsperadoGetList,
8+
tentaCriarUmAutorComDadosInvalidos
9+
} from '../data/author-data';
10+
import { EntityService } from '../entity/entities';
11+
import { authorsRoute } from '../routes/author-route';
12+
13+
14+
const getList = authorsRoute.getListAuthors
15+
const getById = authorsRoute.getAuthorsById
16+
const create = authorsRoute.criaAutors
17+
18+
const entity = new EntityService();
19+
20+
describe('Exemplos de teste usando o Get list', () => {
21+
22+
it('Deve encontrar o registro e retornar o código de status 200', async () => {
23+
await entity.getList(
24+
getList, // Rota para listar autores
25+
200, // Código de status esperado
26+
dadoEsperadoGetList // Dados esperados na resposta
27+
)
28+
29+
})
30+
});
31+
32+
describe('Exemplos de teste usando o Get by id', () => {
33+
34+
it('Deve encontrar o registro pelo id e retornar o código de status 200', async () => {
35+
await entity.getById(
36+
getById,
37+
2, // ID do autor a ser buscado
38+
200,
39+
dadoEsperadoGetById // Dados esperados na resposta
40+
)
41+
})
42+
43+
it('Deve tentar encontrar o registro pelo id, mas não encontrado e retornar o código de status 404', async () => {
44+
await entity.getById(
45+
getById,
46+
1000,
47+
404,
48+
getByIdNaoEncontrado
49+
)
50+
})
51+
});
52+
53+
describe('Exemplos de teste usando o Create', () => {
54+
55+
it('Deve criar um autor e verificar os dados da resposta e o código de status 200', async () => {
56+
await entity.create(
57+
create, // Rota para criar autor
58+
criaAutor, // Dados do autor a ser criado
59+
200,
60+
criaAutor
61+
)
62+
})
63+
64+
it('Deve tentar criar um autor com dados inválidos e verificar a resposta e o código de status 400', async () => {
65+
await entity.create(
66+
create, // Rota para criar autor
67+
tentaCriarUmAutorComDadosInvalidos, // Dados do autor a ser criado
68+
400, // Código de status esperado
69+
validaRetornoAutorInvalido
70+
)
71+
})
72+
});

test/integration/exemplo-uso-padrao.test.ts renamed to integration/tests/exemplo-uso-padrao.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { expect } from '@jest/globals';
22
import * as request from 'supertest';
33
import { BASE_URL } from '../../constants/constants';
44

5-
describe('Get list request example test', () => {
5+
describe('Exemplos de teste usando o Get list', () => {
66

7-
it('Should find the register and return status code 200', async () => {
8-
const response = await request(BASE_URL).get('/api/v1/Authors');
7+
it('Deve encontrar o registro e retornar o código de status 200', async () => {
8+
const response = await request(BASE_URL).get('api/v1/Authors');
99

1010
expect(response.status).toBe(200);
1111
expect(response.body[0]).toEqual(expect.objectContaining(
@@ -18,10 +18,10 @@ describe('Get list request example test', () => {
1818
));
1919
});
2020

21-
describe('Get by id request example test', () => {
21+
describe('Exemplos de teste usando o Get by id', () => {
2222

23-
it('Should find the register by id and return status code 200', async () => {
24-
const response = await request(BASE_URL).get('/api/v1/Authors/2');
23+
it('Deve encontrar o registro pelo id e retornar o código de status 200', async () => {
24+
const response = await request(BASE_URL).get('api/v1/Authors/2');
2525

2626
expect(response.status).toBe(200);
2727
expect(response.body).toEqual(
@@ -34,8 +34,8 @@ describe('Get list request example test', () => {
3434
);
3535
});
3636

37-
it('Should try to find the register by id but not found and return 404 status code', async () => {
38-
const response = await request(BASE_URL).get('/api/v1/Authors/1000');
37+
it('Deve tentar encontrar o registro pelo id, mas não encontrado e retornar o código de status 404', async () => {
38+
const response = await request(BASE_URL).get('api/v1/Authors/1000');
3939

4040
expect(response.status).toBe(404);
4141
expect(response.body).toEqual(expect.objectContaining(
@@ -49,9 +49,9 @@ describe('Get list request example test', () => {
4949
});
5050
});
5151

52-
describe('Post request example test', () => {
52+
describe('Exemplos de teste usando o Create', () => {
5353

54-
it('Should create an author then check response data and check the return status code 200', async () => {
54+
it('Deve criar um autor e verificar os dados da resposta e o código de status 200', async () => {
5555
const novoAutor = {
5656
id: 650,
5757
idBook: 1,
@@ -76,7 +76,7 @@ describe('Get list request example test', () => {
7676

7777
});
7878

79-
it('Should try to create an author with invalid data then check response and check the return status code 400', async () => {
79+
it('Deve tentar criar um autor com dados inválidos e verificar a resposta e o código de status 400', async () => {
8080
const invalidAuthor = {
8181
id: 650,
8282
idBook: 1,

test/integration/exemplos-test-usando-db/mssql-db.test.ts renamed to integration/tests/exemplos-test-usando-db/mssql-db.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ afterAll(async () => {
66
await pool.close();
77
});
88

9-
describe('MSSQL Connection test', () => {
9+
describe('Teste de conexão MSSQL DB', () => {
1010

11-
it('should connect to MSSQL and run a basic query', async () => {
11+
it('Deve conectar ao MSSQL e executar uma consulta básica', async () => {
1212
const pool = await getMSSQLConnection();
1313
const result = await pool.query`SELECT 1 AS value`;
14+
1415
expect(result.recordset[0].value).toBe(1);
1516
});
1617

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { mysqlPool } from '../../../db-connection/mysql-connection';
22

3-
describe('MySQL connection', () => {
3+
describe('Teste de conexão MYSQL DB', () => {
44

55
afterAll(async () => {
66
await mysqlPool.end(); // fecha a conexão ao fim dos testes
77
});
88

9-
it('should fetch user with id = 1', async () => {
9+
it('Deve buscar o usuário com id = 1', async () => {
1010
const [rows] = await mysqlPool.query('SELECT name FROM users WHERE id = ?', [1]);
1111
const user = (rows as any[])[0];
12+
1213
expect(user.name).toBe('Ana Paula'); // Busca na base de dados teste criada via docker
1314
});
1415

15-
1616
});

test/integration/exemplos-test-usando-db/postgres-db.test.ts renamed to integration/tests/exemplos-test-usando-db/postgres-db.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ afterAll(async () => {
44
await pool.end(); // fecha a conexão ao fim dos testes
55
});
66

7-
describe('Database connection test', () => {
7+
describe('Teste de conexão postgres DB', () => {
88

9-
it('should connect successfully', async () => {
9+
it('Deve conectar com sucesso', async () => {
1010
await expect(testConnection()).resolves.not.toThrow();
1111
});
1212

13-
it('should return user with id = 1 and name = "Ana Souza"', async () => {
13+
it('Deve retornar o usuário com id = 1 e nome = "Ana Souza"', async () => {
1414
const result = await pool.query('SELECT name FROM users WHERE id = $1', [1]);
15+
1516
expect(result.rows.length).toBe(1);
1617
expect(result.rows[0].name).toBe('Ana Souza');
1718
});

0 commit comments

Comments
 (0)