forked from b00tc4mp/isdi-bootcamp-202409
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
364 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
staff/carlos-tomas/project/api/logic/users/getUserPets.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'dotenv/config' | ||
|
||
import * as chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import bcrypt from 'bcryptjs' | ||
|
||
chai.use(chaiAsPromised) | ||
const { expect } = chai | ||
|
||
import db, { User } from 'dat' | ||
import { errors } from 'com' | ||
|
||
const { NotFoundError } = errors | ||
|
||
import getUserPets from './getUserPets.js' | ||
|
||
debugger | ||
|
||
describe('getUserPets', () => { | ||
before(() => db.connect(process.env.MONGO_URL_TEST)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
it('get user pets', () => { | ||
User.create({ name: 'Carlos Tomas', username: 'ctcarlos25', password: bcrypt.hashSync('123123123', 10), phone: '+34682519205', email: 'ctcarlos25@gmail.com' }) | ||
.then((user) => | ||
getUserPets(user.id) | ||
.then(user => { | ||
expect(user[0].id).to.equal(user.id) | ||
expect(user[0].name).to.equal(user.name) | ||
expect(user[0].username).to.equal(user.username) | ||
expect(user[0].phone).to.equal(user.phone) | ||
expect(user[0].email).to.equal(user.email) | ||
}) | ||
) | ||
|
||
}) | ||
|
||
it('fails on non-existing user', () => | ||
expect( | ||
getUserPets('012345678901234567890123') | ||
).to.be.rejectedWith(NotFoundError, 'user not found') | ||
) | ||
|
||
|
||
after(() => db.disconnect()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
staff/carlos-tomas/project/api/logic/veterinary/updateVaccinesDewornsPet.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
import 'dotenv/config'; | ||
import * as chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import db, { User, Pet } from 'dat'; | ||
import { errors } from 'com'; | ||
import updateVaccinesDewornsPet from './updateVaccinesDewornsPet.js'; | ||
|
||
chai.use(chaiAsPromised); | ||
const { expect } = chai; | ||
|
||
const { DuplicityError, NotFoundError } = errors; | ||
|
||
describe('updateVaccinesDewornsPet', () => { | ||
before(() => db.connect(process.env.MONGO_URL_TEST)); | ||
|
||
beforeEach(() => Promise.all([User.deleteMany(), Pet.deleteMany()])); | ||
|
||
it('adds a new vaccine to a pet', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [], | ||
deworns: [], | ||
}).save(); | ||
|
||
await updateVaccinesDewornsPet(user.id, pet.id, 'Rabies', null); | ||
|
||
const updatedPet = await Pet.findById(pet.id); | ||
expect(updatedPet.vaccines).to.have.lengthOf(1); | ||
expect(updatedPet.vaccines[0].name).to.equal('Rabies'); | ||
}); | ||
|
||
it('throws DuplicityError if vaccine already exists', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [{ name: 'Rabies' }], | ||
deworns: [], | ||
}).save(); | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, pet.id, 'Rabies', null)).to.be.rejectedWith( | ||
DuplicityError, | ||
'La vacuna "Rabies" ya ha sido administrada al Animal.' | ||
); | ||
}); | ||
|
||
it('adds a new deworn treatment', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [], | ||
deworns: [], | ||
}).save(); | ||
|
||
await updateVaccinesDewornsPet(user.id, pet.id, null, 'internal'); | ||
|
||
const updatedPet = await Pet.findById(pet.id); | ||
expect(updatedPet.deworns).to.have.lengthOf(1); | ||
expect(updatedPet.deworns[0].type).to.equal('internal'); | ||
}); | ||
|
||
it('throws DuplicityError if deworn treatment already exists', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [], | ||
deworns: [{ type: 'internal' }], | ||
}).save(); | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'internal')).to.be.rejectedWith( | ||
DuplicityError, | ||
"La desparacitación de tipo 'internal' ya está registrada." | ||
); | ||
}); | ||
|
||
it('throws DuplicityError when adding "both" if "external" or "internal" already exists', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [], | ||
deworns: [{ type: 'internal' }], | ||
}).save(); | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'both')).to.be.rejectedWith( | ||
DuplicityError, | ||
"No se puede agregar 'Ambas' si ya se han administrado 'external' o 'internal'." | ||
); | ||
}); | ||
|
||
it('throws DuplicityError when adding "external" or "internal" if "both" already exists', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
vaccines: [], | ||
deworns: [{ type: 'both' }], | ||
}).save(); | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'external')).to.be.rejectedWith( | ||
DuplicityError, | ||
"No se puede agregar 'external' porque ya está registrado 'both'." | ||
); | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, pet.id, null, 'internal')).to.be.rejectedWith( | ||
DuplicityError, | ||
"No se puede agregar 'internal' porque ya está registrado 'both'." | ||
); | ||
}); | ||
|
||
it('fails when user does not exist', async () => { | ||
const fakeUserId = '012345678901234567890123'; | ||
const pet = await new Pet({ | ||
chip: '012345678912345', | ||
name: 'Peke', | ||
race: 'Meztizo', | ||
sex: true, | ||
weight: 35, | ||
sterilized: true, | ||
dateOfBirth: new Date('2018-09-09'), | ||
}).save(); | ||
|
||
await expect(updateVaccinesDewornsPet(fakeUserId, pet.id, 'Rabies', null)).to.be.rejectedWith( | ||
NotFoundError, | ||
'user not found' | ||
); | ||
}); | ||
|
||
it('fails when pet does not exist', async () => { | ||
const user = await new User({ | ||
name: 'Carlos Tomas', | ||
username: 'ctcarlos25', | ||
password: '123123123', | ||
phone: '+34682519205', | ||
email: 'ctcarlos25@gmail.com', | ||
}).save(); | ||
|
||
const fakePetId = '012345678901234567890123'; | ||
|
||
await expect(updateVaccinesDewornsPet(user.id, fakePetId, 'Rabies', null)).to.be.rejectedWith( | ||
NotFoundError, | ||
'pet not found' | ||
); | ||
}); | ||
|
||
after(() => db.disconnect()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.