def test_item_json(self): item = ItemModel('test', 19.99) expected = {'name': 'test', 'price': 19.99} self.assertEqual( item.json(), expected, "The JSON export of the item is incorrect. Received {}, expected {}." .format(item.json(), expected))
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db() return item.json()
def post(self, name): if ItemModel.find_by_name(name): return {'message': "An item with name '{}' already exists.".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return item.json(), 201
def test_item_json(self): item = ItemModel('test', 19.99) expected = jsonify({'name': 'test', 'price': 19.99}) self.assertEqual(item.json(), expected)
def test_create_json(self): test_item = ItemModel("salt", 3.20) expected_json = {"name": "salt", "price": 3.20} self.assertEqual(expected_json, test_item.json())