def test_find_by_name(self): Pet(0, "fido", "dog").save() Pet(0, "kitty", "cat").save() pets = Pet.find_by_name("kitty") self.assertEqual( len(pets), 1 ) self.assertEqual( pets[0].category, "cat" ) self.assertEqual( pets[0].name, "kitty" )
def test_find_by_name(self): """ Find a Pet by Name """ Pet(0, "fido", "dog").save() Pet(0, "kitty", "cat").save() pets = Pet.find_by_name("fido") self.assertNotEqual(len(pets), 0) self.assertEqual(pets[0].category, "dog") self.assertEqual(pets[0].name, "fido")
def test_find_by_name(self): """ Find a Pet by Name """ Pet(name="fido", category="dog", available=True).save() Pet(name="kitty", category="cat", available=False).save() pets = Pet.find_by_name("kitty") self.assertEqual(pets[0].category, "cat") self.assertEqual(pets[0].name, "kitty") self.assertEqual(pets[0].available, False)
def test_get_pet(self): """ Get a single Pet """ # get the id of a pet pet = Pet.find_by_name('fido')[0] resp = self.app.get('/pets/{}'.format(pet.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(data['name'], pet.name)
def test_for_case_insensitive(self): """ Test for Case Insensitive Search """ Pet(0, "Fido", "DOG").save() Pet(0, "Kitty", "CAT").save() pets = Pet.find_by_name("fido") self.assertNotEqual(len(pets), 0) self.assertEqual(pets[0].name, "Fido") pets = Pet.find_by_category("cat") self.assertNotEqual(len(pets), 0) self.assertEqual(pets[0].category, "CAT")
def test_delete_pet(self): """ Delete a Pet """ pet = Pet.find_by_name('fido')[0] # save the current number of pets for later comparrison pet_count = self.get_pet_count() resp = self.app.delete('/pets/{}'.format(pet.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) new_count = self.get_pet_count() self.assertEqual(new_count, pet_count - 1)
def test_update_pet(self): """ Update an existing Pet """ pet = Pet.find_by_name('kitty')[0] new_kitty = {'name': 'kitty', 'category': 'tabby', 'available': 'True'} data = json.dumps(new_kitty) resp = self.app.put('/pets/{}'.format(pet.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['category'], 'tabby')
def list_pets(): """ Retrieve a list of Pets This endpoint will return all Pets unless a query parameter is specificed --- tags: - Pets description: The Pets endpoint allows you to query Pets parameters: - name: category in: query description: the category of Pet you are looking for required: false type: string - name: name in: query description: the name of Pet you are looking for required: false type: string definitions: Pet: type: object properties: id: type: integer description: unique id assigned internallt by service name: type: string description: the pets's name category: type: string description: the category of pet (e.g., dog, cat, fish, etc.) responses: 200: description: An array of Pets schema: type: array items: schema: $ref: '#/definitions/Pet' """ pets = [] category = request.args.get('category') name = request.args.get('name') if category: pets = Pet.find_by_category(category) elif name: pets = Pet.find_by_name(name) else: pets = Pet.all() results = [pet.serialize() for pet in pets] return make_response(jsonify(results), status.HTTP_200_OK)
def list_pets(): """ Returns all of the Pets """ pets = [] category = request.args.get('category') name = request.args.get('name') if category: pets = Pet.find_by_category(category) elif name: pets = Pet.find_by_name(name) else: pets = Pet.all() results = [pet.serialize() for pet in pets] return make_response(jsonify(results), status.HTTP_200_OK)
def list_pets(): """ Returns all of the Pets """ app.logger.info('Request for List Pets') pets = [] category = request.args.get('category') name = request.args.get('name') available = request.args.get('available') if category: pets = Pet.find_by_category(category) elif name: pets = Pet.find_by_name(name) elif available: pets = Pet.find_by_availability(available) else: pets = Pet.all() results = [pet.serialize() for pet in pets] return make_response(jsonify(results), HTTP_200_OK)