Ejemplo n.º 1
0
    def test_add_species(self):
        test_case = {'name': 'Max', 'description': 'brave dog', 'price': 44}
        new_species = Species.add_species(
            _name=test_case['name'],
            _description=test_case['description'],
            _price=test_case['price'])
        self.assertEqual(new_species.name, test_case['name'])
        self.assertEqual(new_species.description, test_case['description'])
        self.assertEqual(new_species.price, test_case['price'])

        s = Species.query.filter_by(id=new_species.id).first()
        self.assertIsNot(s, None)
        self.assertEqual(s.name, test_case['name'])
        self.assertEqual(s.description, test_case['description'])
        self.assertEqual(s.price, test_case['price'])
Ejemplo n.º 2
0
def add_species(caller_id):
    request_data = request.get_json()

    # Check if request_data is valid Species object
    if not Species.is_valid_object(request_data):
        invalid_obj = {
            'error': "Valid Species must contain: name, description and price"
        }
        return Response(json.dumps(invalid_obj), 400)

    # If all ok, try to create new species
    try:
        new_species = Species.add_species(request_data['name'],
                                          request_data['description'],
                                          request_data['price'])
        write_to_log_file(request.method, request.path, caller_id, 'Animal',
                          "create", new_species.id)
        return Response('', status=201, mimetype='application/json')
    except TypeError as te:
        return Response(json.dumps({"error": "{}".format(te)}),
                        400,
                        mimetype='application/json')