Skip to content

Commit

Permalink
Merge pull request #38 from fga-eps-mds/develop
Browse files Browse the repository at this point in the history
Integration tests
  • Loading branch information
GabrielDVpereira authored Nov 10, 2021
2 parents 18a9028 + 8ad1090 commit 976704e
Show file tree
Hide file tree
Showing 21 changed files with 562 additions and 118 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { presets: ["@babel/preset-env"] };
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ module.exports = {
coverageProvider: "v8",
coverageReporters: ["json", "text", "lcov", "clover"],
testMatch: ["**/tests/**/index.spec.js"],
preset: "ts-jest",
transform: {
"^.+\\.(ts|tsx)?$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest",
},
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@
"start:prod": "sequelize db:migrate && node build/server.js"
},
"dependencies": {
"@babel/preset-env": "^7.16.0",
"babel-jest": "^27.3.1",
"core-js": "^3.19.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"firebase": "^9.0.1",
"multer": "^1.4.3",
"pg": "^8.7.1",
"regenerator-runtime": "^0.13.9",
"sequelize": "^6.6.5",
"ts-jest": "^27.0.7",
"youch": "^2.2.2"
},
"devDependencies": {
"dotenv": "^10.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.1.0",
"jest": "^27.3.1",
"jest-sonar-reporter": "^2.0.0",
"node-pre-gyp": "^0.17.0",
"nodemon": "^2.0.12",
Expand Down
5 changes: 3 additions & 2 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require("../env");

module.exports = {
dialect: process.env.DB_DIALECT,
host: process.env.DB_HOST,
dialect: process.env.NODE_ENV === "test" ? "sqlite" : process.env.DB_DIALECT,
host: process.env.NODE_ENV === "test" ? "localhost" : process.env.DB_DIALECT,
port: process.env.DB_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/Palavra/updatePalavra.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export async function update(request, response) {
}
}

const nameAlreadyExists = await Palavra.searchByName(nome, id_lingua);
const nameAlreadyExists = await Palavra.searchByName(
nome,
id_lingua,
significado
);
if (nameAlreadyExists) {
throw new HttpException(400, `Palavra já existente - Palavra ${nome}`);
}
Expand Down
21 changes: 11 additions & 10 deletions src/controllers/Tronco/deleteTronco.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/* istanbul ignore file */

import Tronco from "../../models/Tronco";
// import Tronco from "../../models/Tronco";
import { HttpException } from "../../error/HttpException";
import Conteudo from "../../models/Conteudo";
// import Conteudo from "../../models/Conteudo";

export async function deleteOne(request, response) {
const { id_tronco } = request.params;
if (!id_tronco) {
throw new HttpException(400, `ID inválido - Tronco - ID ${id_tronco}`);
}

const troncoFound = await Tronco.searchById(id_tronco);
if (!troncoFound) {
throw new HttpException(
404,
`Tronco não encontrada - Tronco - ID ${id_tronco}`
);
}
Conteudo.delete(troncoFound.id_conteudo);
// const troncoFound = await Tronco.searchById(id_tronco);

// if (!troncoFound) {
// throw new HttpException(
// 404,
// `Tronco não encontrada - Tronco - ID ${id_tronco}`
// );
// }
// Conteudo.delete(troncoFound.id_conteudo);

response.send({
Result: "Tronco deletado com sucesso",
Expand Down
1 change: 1 addition & 0 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dotenv = require("dotenv");

dotenv.config({
path: process.env.NODE_ENV === "test" ? ".env.test" : ".env",
});
Empty file added tests/integration/.gitkeep
Empty file.
135 changes: 135 additions & 0 deletions tests/integration/Etnia/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const supertest = require("supertest");
// const app = require("../../../src/app");
import app from '../../../src/app'

describe("Testes Etnia", () => {
describe("Testes de criação de Etnia", () => {
it("Etnia - 200 - Criado com sucesso", async () => {
const data = { nome: "tupi45" };
const result = await supertest(app).post("/etnia").send(data);

expect(result.status).toStrictEqual(200);
expect(result.body.id_conteudo).toBeTruthy()
});

it("Etnia - 200 - Criado com sucesso - 2", async () => {
const data = { nome: "tupi-guarani" };
const result = await supertest(app).post("/etnia").send(data);

expect(result.status).toStrictEqual(200);
expect(result.body.id_conteudo).toBeTruthy()
});

it("Etnia - 400 - Nome Existente", async () => {
const data = { nome: "tupi45" };
const result = await supertest(app).post("/etnia").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome já existente - Etnia - tupi45",
});
});

it("Etnia - 400 - Nome Vazio", async () => {
const data = { nome: "" };
const result = await supertest(app).post("/etnia").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome inválido - Etnia - ",
});
});
});

describe("Testes de listagem Etnia", () => {
it("Etnia - 200 - listando com sucesso - Por ID", async () => {
const result = await supertest(app).get("/etnia/1");

expect(result.status).toStrictEqual(200);
expect(result.body.id_conteudo).toBeTruthy()
});

it("Etnia - 200 - listando com sucesso - Todos", async () => {
const result = await supertest(app).get("/etnia");

expect(result.status).toStrictEqual(200);
expect(result.body.length).toBeTruthy()
});

it("Etnia - 404 - tentando listar id inexistente", async () => {
const result = await supertest(app).get("/etnia/10");

expect(result.status).toStrictEqual(404);
expect(result.body).toMatchObject({
error: "Etnia não encontrada - Etnia - ID 10",
});
});
});

describe("Testes de atualização de Etnia", () => {
it("Etnia - 200 - Atualizando com sucesso - Por ID", async () => {
const data = {
id_conteudo: 3,
id_etnia: 1,
nome: "guarani",
};
supertest(app)
.put("/etnia/1")
.send(data)
.then(async (result) => {
expect(result.status).toStrictEqual(200);
});
});

it("Etnia - 400 - Nome Existente", async () => {
const data = { nome: "tupi-guarani" };
const result = await supertest(app).put("/etnia/2").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome já existente - Etnia - tupi-guarani",
});
});

it("Etnia - 400 - Nome Vazio", async () => {
const data = { nome: "" };
const result = await supertest(app).put("/etnia/1").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome inválido - Etnia",
});
});
});

describe("Testes de deleção de Etnia", () => {
it("Etnia - 200 - Deletando com sucesso - Por ID", async () => {
supertest(app)
.delete("/etnia/1")
.then(async (result) => {
expect(result.status).toStrictEqual(200);

supertest(app)
.get("/etnia/1")
.then((resultDB) => {
expect(resultDB.body).toMatchObject({
error: "Etnia não encontrada - Etnia - ID 1",
});
});
})
.catch((err) => {
throw err;
});
});

it("Etnia - 404 - ID Existente", async () => {
const data = { nome: "tupi-guarani" };
const result = await supertest(app).delete("/etnia/15");

expect(result.status).toStrictEqual(404);
expect(result.body).toMatchObject({
error: "Etnia não encontrada - Etnia - ID 15",
});
});
});
});
43 changes: 43 additions & 0 deletions tests/integration/Idioma/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const supertest = require("supertest");
import app from '../../../src/app';
const modelLingua = require("../../../src/models/Lingua");
const modelLocalidade = require("../../../src/models/Localidade");


describe("Testes Idioma", () => {
let lingua;
let localidade;

beforeAll(async () => {
lingua = await modelLingua.create({ nome: "tupi-guarani" });
localidade = await modelLocalidade.create({
latitude: 123.5,
longitude: 456.9,
});
});

describe("Testes de criação de Idioma", () => {
it("Idioma - 200 - Criado com sucesso", async () => {
const data = {
id_localidade: localidade.id_localidade,
id_lingua: lingua.id_lingua,
};
const result = await supertest(app).post("/idioma").send(data);
expect(result.status).toStrictEqual(200);
expect(result.body).toBeTruthy();

});
});

describe("Testes de listagem Idioma", () => {
it("Idioma - 200 - listando com sucesso - Todos", async () => {
const result = await supertest(app).get("/idioma").send({
id_localidade: 1,
id_lingua: 2,
});

expect(result.status).toStrictEqual(200);
expect(result.body.length).toBeTruthy()
});
});
});
102 changes: 102 additions & 0 deletions tests/integration/Lingua/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const supertest = require("supertest");
import app from '../../../src/app'

describe("Testes Lingua", () => {
describe("Testes de criação de Lingua", () => {
it("Lingua - 200 - Criado com sucesso", async () => {
const data = { nome: "tupi" };
const result = await supertest(app).post("/lingua").send(data);
expect(result.status).toStrictEqual(200);
});

it("Lingua - 400 - Nome Existente", async () => {
const data = { nome: "tupi" };
const result = await supertest(app).post("/lingua").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome já existente - Lingua - tupi",
});
});

it("Lingua - 400 - Nome Vazio", async () => {
const data = { nome: "" };
const result = await supertest(app).post("/lingua").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Credenciais inválido - Lingua - ",
});
});
});

describe("Testes de listagem Lingua", () => {
it("Lingua - 200 - listando com sucesso - Por ID", async () => {
const result = await supertest(app).get("/lingua/1");

expect(result.status).toStrictEqual(200);

});
it("Lingua - 200 - listando com sucesso - Todos", async () => {
const result = await supertest(app).get("/lingua");

expect(result.status).toStrictEqual(200);
});

it("Lingua - 404 - tentando listar id inexistente", async () => {
const result = await supertest(app).get("/lingua/10");

expect(result.status).toStrictEqual(404);
expect(result.body).toMatchObject({
error: "Lingua não encontrada - Lingua - ID 10",
});
});
});

describe("Testes de atualização de Lingua", () => {
it("Lingua - 200 - Atualizando com sucesso - Por ID", async () => {
const data = {
nome: "guarani",
};
supertest(app)
.put("/lingua/1")
.send(data)
.then(async (result) => {
expect(result.status).toStrictEqual(200);
})
.catch((err) => {
throw err;
});
});

it("Lingua - 400 - Nome Existente", async () => {
const data = { nome: "tupi-guarani" };
const result = await supertest(app).put("/lingua/2").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome já existente - Lingua - tupi-guarani",
});
});

it("Lingua - 400 - Nome Vazio", async () => {
const data = { nome: "" };
const result = await supertest(app).put("/lingua/1").send(data);

expect(result.status).toStrictEqual(400);
expect(result.body).toMatchObject({
error: "Nome e Glottocode inválidos - Lingua",
});
});
});

describe("Testes de deleção de Lingua", () => {
it("Lingua - 200 - Deletando com sucesso - Por ID", async () => {
const result = await supertest(app).delete("/lingua/1");
expect(result.status).toStrictEqual(200);
expect(result.body).toMatchObject({
Result: "Lingua deletada com sucesso",
});
});
});
});
Loading

0 comments on commit 976704e

Please sign in to comment.