-
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
7 changed files
with
174 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { app } from "@/app"; | ||
import { prisma } from "@/lib/prisma"; | ||
import { AddressResponse, getAddressByCep } from "@/services/cep/cep-api-service"; | ||
import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
import request from 'supertest' | ||
|
||
describe('Get pet by id', () => { | ||
|
||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should get pet by id', async () => { | ||
const address = await getAddressByCep('08560200') as AddressResponse | ||
|
||
const org = await prisma.org.create({ | ||
data: { | ||
cep: '08560200', | ||
email: '[email protected]', | ||
name: 'OEG TEST', | ||
password_hash: '123456', | ||
whatsapp: '119827464', | ||
bairro: address.bairro, | ||
estado: address.estado, | ||
localidade: address.localidade, | ||
logradouro: address.logradouro, | ||
uf: address.uf | ||
} | ||
}) | ||
|
||
const petAddress = await getAddressByCep('13423790') as AddressResponse | ||
|
||
const pet = await prisma.pet.create({ | ||
data: { | ||
age: 'Small', | ||
cep: '13423790', | ||
description: 'A really nice cat', | ||
energy: 'Active', | ||
environment: 'All', | ||
name: 'Harry', | ||
org_id: org.id, | ||
size: 'Big', | ||
localidade: petAddress.localidade, | ||
bairro: petAddress.bairro, | ||
estado: petAddress.estado, | ||
logradouro: petAddress.logradouro, | ||
uf: petAddress.uf, | ||
} | ||
}) | ||
|
||
const response = await request(app.server).get(`/pets/single/${pet.id}`) | ||
|
||
expect(response.body.pet).toEqual(expect.objectContaining({ | ||
localidade: expect.any(String) | ||
})) | ||
expect(response.body.pet.localidade).toEqual(petAddress.localidade) | ||
expect(response.statusCode).toEqual(200) | ||
}) | ||
}) |
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,16 @@ | ||
import { GetPetFacotry } from "@/services/pets/factories/get-pet-factory"; | ||
import { FastifyReply, FastifyRequest } from "fastify"; | ||
import { z } from "zod"; | ||
|
||
export async function getPetById(request: FastifyRequest, reply: FastifyReply){ | ||
|
||
const getPetBodySchema = z.object({ | ||
id: z.string(), | ||
}) | ||
|
||
const { id } = getPetBodySchema.parse(request.params) | ||
const getPetService = GetPetFacotry() | ||
|
||
const pet = await getPetService.execute({ id }) | ||
reply.send(pet).status(200) | ||
} |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { FastifyInstance } from "fastify"; | ||
import { registerPet } from "./register-pet-controller"; | ||
import { listPet } from "./list-pet-controller"; | ||
import { getPetById } from "./get-pet-controllet"; | ||
|
||
export async function petRoutes(app: FastifyInstance) { | ||
app.post('/pets', registerPet) | ||
app.get('/pets/:city', listPet) | ||
app.get('/pets/single/:id', getPetById) | ||
} |
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,9 @@ | ||
import { PrismaPetsRepository } from "@/repositories/pets/pets-repository" | ||
import { GetPetService } from "../get-pet-service" | ||
|
||
export function GetPetFacotry(){ | ||
const petsRepository = new PrismaPetsRepository() | ||
const getPetService = new GetPetService(petsRepository) | ||
|
||
return getPetService | ||
} |
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,20 @@ | ||
import { Pet } from "@prisma/client"; | ||
import { PetsRepository } from '../../repositories/pets/prisma-pets-repository'; | ||
|
||
interface GetPetRequest { | ||
id: string; | ||
} | ||
|
||
interface GetPetResponse { | ||
pet: Pet | null | ||
} | ||
|
||
export class GetPetService { | ||
|
||
constructor(private petsRepository: PetsRepository) { } | ||
|
||
async execute({ id }: GetPetRequest): Promise<GetPetResponse> { | ||
const pet = await this.petsRepository.findById(id) | ||
return { pet } | ||
} | ||
} |
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,62 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
import { GetPetService } from "../get-pet-service"; | ||
import { PetsRepository } from "@/repositories/pets/prisma-pets-repository"; | ||
import { InMemoryPetsRepository } from "@/repositories/pets/in-memory-pets-repository"; | ||
import { AddressResponse, getAddressByCep } from "@/services/cep/cep-api-service"; | ||
|
||
let sut: GetPetService | ||
let petsRepository: PetsRepository | ||
|
||
describe('Get pet by id', () => { | ||
|
||
beforeEach(() => { | ||
petsRepository = new InMemoryPetsRepository() | ||
sut = new GetPetService(petsRepository) | ||
}) | ||
|
||
it('should get pet by id', async () => { | ||
const address = await getAddressByCep('08560200') as AddressResponse | ||
|
||
const createdPet = await petsRepository.create({ | ||
age: 'Small', | ||
cep: '08560250', | ||
description: 'A really nice cat', | ||
energy: 'Active', | ||
environment: 'All', | ||
name: 'Harry', | ||
org_id: '1', | ||
size: 'Big', | ||
bairro: address.bairro, | ||
estado: address.estado, | ||
localidade: address.localidade, | ||
logradouro: address.logradouro, | ||
uf: address.uf, | ||
}) | ||
|
||
const { pet } = await sut.execute({ id: createdPet.id }) | ||
expect(pet?.age).toEqual(createdPet.age) | ||
}) | ||
|
||
it('should not get pet by id', async () => { | ||
const address = await getAddressByCep('08560200') as AddressResponse | ||
|
||
await petsRepository.create({ | ||
age: 'Small', | ||
cep: '08560250', | ||
description: 'A really nice cat', | ||
energy: 'Active', | ||
environment: 'All', | ||
name: 'Harry', | ||
org_id: '1', | ||
size: 'Big', | ||
bairro: address.bairro, | ||
estado: address.estado, | ||
localidade: address.localidade, | ||
logradouro: address.logradouro, | ||
uf: address.uf, | ||
}) | ||
|
||
const { pet } = await sut.execute({ id: 'NON_EXISTING ID' }) | ||
expect(pet?.age).toBeUndefined() | ||
}) | ||
}) |