def post(self): """ Creates a Product This endpoint will create a Product based the data in the body that is posted """ app.logger.info('Request to create a product') check_content_type('application/json') product = Product() app.logger.debug('Payload = %s', api.payload) product.deserialize(api.payload) product.save() location_url = api.url_for(ProductResource, product_id=product.id, _external=True) return product.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }
def test_serialize_a_product(self): """ Test serialization of a Product """ product = Product( name="Wagyu Tenderloin Steak", category="food", stock=11, price=20.56, description="The most decadent, succulent cut of beef, ever.") data = product.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('name', data) self.assertEqual(data['name'], "Wagyu Tenderloin Steak") self.assertIn('category', data) self.assertEqual(data['category'], "food") self.assertIn('stock', data) self.assertEqual(data['stock'], 11) self.assertIn('price', data) self.assertAlmostEqual(data['price'], 20.56) self.assertIn('description', data) self.assertEqual(data['description'], "The most decadent, succulent cut of beef, ever.")