Skip to content
Snippets Groups Projects
Commit a7bc0e86 authored by HENRIQUES Valentin's avatar HENRIQUES Valentin
Browse files

implémentation test unitaire

parent 486d0aab
No related branches found
No related tags found
No related merge requests found
import { expect } from 'chai';
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import MonkeyDbDAO from '../src/DAO/MonkeyDbDAO.js';
import SpeciesDbDAO from '../src/DAO/SpeciesDbDAO.js';
describe('MonkeyDbDAO', () => {
let db;
let monkeyDAO;
before(async () => {
db = await open({
filename: './db/database.db',
driver: sqlite3.Database
});
monkeyDAO = new MonkeyDbDAO(db);
});
var monkeyId;
describe('insert()', () => {
it('should insert a monkey into the database', async () => {
const monkey = {
name: 'Test Monkey',
speciesId: 1,
sex: 'M',
age: 5,
birthLoc: 'Test Location',
liveLoc: 'Test Location'
};
await monkeyDAO.insert(monkey);
const result = await db.get('SELECT * FROM monkey WHERE name = ?', ['Test Monkey']);
monkeyId = result.id;
expect(result.name).to.equal(monkey.name);
});
});
describe('update()', () => {
it('should update a monkey in the database', async () => {
const monkey = {
id: monkeyId,
name: 'Test Monkey Updated'
}
await monkeyDAO.update(monkey);
const result = await db.get('SELECT * FROM monkey WHERE id = ?', [monkeyId]);
expect(result.name).to.equal(monkey.name);
});
});
describe('findAll()', () => {
it('should find all monkeys in the database', async () => {
const result = await monkeyDAO.findAll();
expect(result).to.be.an('array');
});
});
describe('findById()', () => {
it('should find a monkey by id in the database', async () => {
const result = await monkeyDAO.findById(monkeyId);
expect(result).to.be.an('object');
expect(result.id).to.equal(monkeyId);
});
});
describe('delete()', () => {
it('should delete a monkey by id in the database', async () => {
await monkeyDAO.delete(monkeyId);
const result = await db.get('SELECT * FROM monkey WHERE id = ?', [monkeyId]);
expect(result).to.equal(undefined);
});
});
describe('deleteAll()', () => {
it('should delete all monkeys in the database', async () => {
await monkeyDAO.deleteAll();
const result = await db.get('SELECT * FROM monkey');
expect(result).to.equal(undefined);
});
});
});
describe('SpeciesDbDAO', () => {
let db;
let speciesDAO;
before(async () => {
db = await open({
filename: './db/database.db',
driver: sqlite3.Database
});
speciesDAO = new SpeciesDbDAO(db);
});
var speciesId;
describe('insert()', () => {
it('should insert a species into the database', async () => {
const species = {
name: 'Test Species',
habitat: 'Test Habitat',
height: 1.2,
weight: 190,
diet: 'Test Diet'
};
await speciesDAO.insert(species);
const result = await db.get('SELECT * FROM species WHERE name = ?', ['Test Species']);
speciesId = result.id;
expect(result.name).to.equal(species.name);
});
});
describe('update()', () => {
it('should update a species in the database', async () => {
const species = {
id: speciesId,
name: 'Test Species Updated'
}
await speciesDAO.update(species);
const result = await db.get('SELECT * FROM species WHERE id = ?', [speciesId]);
expect(result.name).to.equal(species.name);
});
});
describe('findAll()', () => {
it('should find all species in the database', async () => {
const result = await speciesDAO.findAll();
expect(result).to.be.an('array');
});
});
describe('findById()', () => {
it('should find a species by id in the database', async () => {
const result = await speciesDAO.findById(speciesId);
expect(result).to.be.an('object');
expect(result.id).to.equal(speciesId);
});
});
describe('delete()', () => {
it('should delete a species by id in the database', async () => {
await speciesDAO.delete(speciesId);
const result = await db.get('SELECT * FROM species WHERE id = ?', [speciesId]);
expect(result).to.equal(undefined);
});
});
describe('deleteAll()', () => {
it('should delete all species in the database', async () => {
await speciesDAO.deleteAll();
const result = await db.get('SELECT * FROM species');
expect(result).to.equal(undefined);
});
});
});
import assert from 'assert';
import Monkey from '../src/models/Monkey.js';
import Species from '../src/models/Species.js';
describe('Monkey', function() {
describe('#getId()', function() {
it('should return the correct id', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getId(), 1);
});
});
// Répétez pour chaque méthode
describe('#getName()', function() {
it('should return the correct name', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getName(), 'George');
});
});
describe('#getSpeciesId()', function() {
it('should return the correct speciesId', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getSpeciesId(), 2);
});
});
describe('#getSex()', function() {
it("should return the correct sex", function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getSex(), 'M');
});
});
describe('#getAge()', function() {
it('should return the correct age', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getAge(), 3);
});
});
describe('#getBirthLoc()', function() {
it('should return the correct birthLoc', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getBirthLoc(), 'Jungle');
});
});
describe('#getLiveLoc()', function() {
it('should return the correct liveLoc', function() {
let monkey = new Monkey(1, 'George', 2, 'M', 3, 'Jungle', 'Zoo');
assert.equal(monkey.getLiveLoc(), 'Zoo');
});
});
});
describe('Species', function() {
let species = new Species('Drill', 'Savannah', 1.2, 190, 'Omnivore');
describe('#getId()', function() {
it('should return the correct id', function() {
assert.equal(species.getId(), null);
});
});
describe('#getName()', function() {
it('should return the correct name', function() {
assert.equal(species.getName(), 'Drill');
});
});
describe('#getHabitat()', function() {
it('should return the correct habitat', function() {
assert.equal(species.getHabitat(), 'Savannah');
});
});
describe('#getHeight()', function() {
it('should return the correct height', function() {
assert.equal(species.getHeight(), 1.2);
});
});
describe('#getWeight()', function() {
it('should return the correct weight', function() {
assert.equal(species.getWeight(), 190);
});
});
describe('#getDiet()', function() {
it('should return the correct diet', function() {
assert.equal(species.getDiet(), 'Omnivore');
});
});
describe('#getMonkeys()', function() {
it('should return the correct monkeys', function() {
assert.equal(species.getMonkeys(), species.monkeys);
});
});
});
No preview for this file type
This diff is collapsed.
......@@ -6,13 +6,14 @@
"type": "module",
"scripts": {
"start": "nodemon index",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha"
},
"author": "V. Henriques",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"better-sqlite3": "^9.2.2",
"chai": "^4.3.10",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jose": "^5.1.3",
......@@ -21,6 +22,7 @@
"sqlite3": "^5.1.6"
},
"devDependencies": {
"mocha": "^10.2.0",
"nodemon": "^2.0.22",
"standard": "^17.0.0"
}
......
......@@ -36,3 +36,4 @@ export default class Species {
return this.monkeys;
}
}
......@@ -7,7 +7,7 @@ dotenv.config();
const router = express.Router();
router.post('/refresh', (req, res) => {
router.post('/', (req, res) => {
const refreshToken = req.body.token;
// Vérifiez si le refresh token est valide
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment