def test_delete_a_pet(self): pet = Pet(0, "fido", "dog") pet.save() self.assertEqual( len(Pet.all()), 1) # delete the pet and make sure it isn't in the database pet.delete() self.assertEqual( len(Pet.all()), 0)
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted or data that is sent via an html form post. """ app.logger.info('Request to create a Pet') data = {} # Check for form submission data if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded': app.logger.info('Processing FORM data') data = { 'name': request.form['name'], 'category': request.form['category'], 'available': request.form['available'].lower() in ['true', '1', 't'] } else: app.logger.info('Processing JSON data') data = request.get_json() pet = Pet() pet.deserialize(data) pet.save() message = pet.serialize() return make_response(jsonify(message), HTTP_201_CREATED, {'Location': url_for('get_pets', pet_id=pet.id, _external=True)})
def setUp(self): self.app = create_test_client(app) self.assertEqual(app.debug, False) self.app.NOT_FOUND_ID = 100 person1_unmarried = Person(first_name='A', last_name='First') person1_unmarried.save() person2 = Person(first_name='B', last_name='Second') person2.save() person3_married = Person(first_name='Y', last_name='Zeta') person3_married.save() person4_married = Person(first_name='Z', last_name='Zeta', partner=person3_married) person4_married.save() person3_married.partner = person4_married person3_married.save() self.app.persons = [person1_unmarried, person2, person3_married, person4_married] self.app.persons_desc = self.app.persons[::-1] pet1 = Pet(name='PetA', owner=person1_unmarried) pet1.save() pet2 = Pet(name='PetB', owner=person3_married) pet2.save() pet3 = Pet(name='PetC', owner=person4_married) pet3.save() pet4 = Pet(name='PetD', owner=person4_married) pet4.save() pet5 = Pet(name='PetE') pet5.save() self.app.pets = [pet1, pet2, pet3, pet4, pet5] self.app.pets_desc = self.app.pets[::-1] self.app.persons_with_pets = [person1_unmarried, person3_married, person4_married]
def test_delete_a_pet(self): """ Delete a Pet """ pet = Pet(name="fido", category="dog", available=True) pet.save() self.assertEqual(len(Pet.all()), 1) # delete the pet and make sure it isn't in the database pet.delete() self.assertEqual(len(Pet.all()), 0)
def create_pets(): """ Creates a Pet in the datbase from the posted database """ payload = request.get_json() pet = Pet() pet.deserialize(payload) pet.save() message = pet.serialize() response = make_response(jsonify(message), HTTP_201_CREATED) response.headers['Location'] = url_for('get_pets', id=pet.id, _external=True) return response
def test_find_pet(self): """ Find a Pet by ID """ Pet(name="fido", category="dog", available=True).save() kitty = Pet(name="kitty", category="cat", available=False) kitty.save() pet = Pet.find(kitty.id) self.assertIsNot(pet, None) self.assertEqual(pet.id, kitty.id) self.assertEqual(pet.name, "kitty") self.assertEqual(pet.available, False)
def test_add_a_pet(self): # Create a pet and add it to the database pets = Pet.all() self.assertEqual( pets, []) pet = Pet(0, "fido", "dog") self.assertTrue( pet != None ) self.assertEqual( pet.id, 0 ) pet.save() # Asert that it was assigned an id and shows up in the database self.assertEqual( pet.id, 1 ) pets = Pet.all() self.assertEqual( len(pets), 1)
def create_pets(): data = {} # Check for form submission data if request.headers.get('Content-Type') == 'application/x-www-form-urlencoded': data = {'name': request.form['name'], 'category': request.form['category']} else: data = request.get_json() pet = Pet() pet.deserialize(data) pet.save() message = pet.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': pet.self_url() })
def test_add_a_pet(self): """ Create a pet and add it to the database """ pets = Pet.all() self.assertEqual(pets, []) pet = Pet(name="fido", category="dog", available=True) self.assertTrue(pet != None) self.assertEqual(pet.id, None) pet.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(pet.id, 1) pets = Pet.all() self.assertEqual(len(pets), 1)
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ check_content_type('application/json') pet = Pet() pet.deserialize(request.get_json()) pet.save() message = pet.serialize() location_url = url_for('get_pets', pet_id=pet.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def test_update_a_pet(self): pet = Pet(0, "fido", "dog") pet.save() self.assertEqual( pet.id, 1 ) # Change it an save it pet.category = "k9" pet.save() self.assertEqual( pet.id, 1 ) # Fetch it back and make sure the id hasn't changed # but the data did change pets = Pet.all() self.assertEqual( len(pets), 1) self.assertEqual( pets[0].category, "k9")
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted --- tags: - Pets consumes: - application/json produces: - application/json parameters: - in: body name: body required: true schema: id: data required: - name - category properties: name: type: string description: name for the Pet category: type: string description: the category of pet (dog, cat, etc.) responses: 201: description: Pet created schema: id: Pet properties: id: type: integer description: unique id assigned internally by service name: type: string description: the pets's name category: type: string description: the category of pet (e.g., dog, cat, fish, etc.) 400: description: Bad Request (the posted data was not valid) """ pet = Pet() pet.deserialize(request.get_json()) pet.save() message = pet.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': pet.self_url() })
def test_update_a_pet(self): """ Update a Pet """ pet = Pet(name="fido", category="dog", available=True) pet.save() self.assertEqual(pet.id, 1) # Change it an save it pet.category = "k9" pet.save() self.assertEqual(pet.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change pets = Pet.all() self.assertEqual(len(pets), 1) self.assertEqual(pets[0].category, "k9")
def test_add_a_pet(self): """ Create a pet and add it to the database """ pets = Pet.all() self.assertEqual(pets, []) pet = Pet(0, "fido", "dog", True) self.assertTrue(pet != None) self.assertEqual(pet.id, 0) pet.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(pet.id, 1) pets = Pet.all() self.assertEqual(len(pets), 1) self.assertEqual(pets[0].id, 1) self.assertEqual(pets[0].name, "fido") self.assertEqual(pets[0].category, "dog") self.assertEqual(pets[0].available, True)
def create_pet(person_id): """ Create a pet for an existing owner. """ try: owner = Person.get_or_none(Person.id == person_id) if owner is None: raise NotFoundException('Owner not found') data = request.get_json(force=True) name = data.get('name') if name is None: raise InvalidRequestException('Name is required') result = Pet(name=name, owner=owner) result.save() response = generate_response(model_to_dict(result), 201) except Exception as e: app.logger.error(e) response = generate_error_response(e) return response
def data_load(payload): """ Loads a Pet into the database """ pet = Pet(0, payload['name'], payload['category']) pet.save()
def data_load(payload): pet = Pet(0, payload['name'], payload['category']) pet.save()