Exemplo n.º 1
0
 def test_find_by_price(self):
     """ Find Products by Price """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=26.8,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     Product(name="shampos", category="Health Care", stock=48,
             price=12.34).save()
     products = Product.find_by_price(25, 50)
     self.assertEqual(products[0].category, "food")
     self.assertEqual(products[0].name, "Wagyu Tenderloin Steak")
     self.assertEqual(products[0].stock, 11)
Exemplo n.º 2
0
 def test_find_by_name(self):
     """ Find a Product by Name """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=20.56,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     Product(name="shampos", category="Health Care", stock=48,
             price=12.34).save()
     products = Product.find_by_name("shampos")
     self.assertEqual(products[0].category, "Health Care")
     self.assertEqual(products[0].name, "shampos")
     print(products[0].price)
     print(getcontext())
     self.assertAlmostEqual(products[0].price, Decimal(12.34))
Exemplo n.º 3
0
 def test_find_product(self):
     """ Find a Product by ID """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=20.56,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     shampo = Product(name="shampos",
                      category="Health Care",
                      stock=48,
                      price=12.34)
     shampo.save()
     product = Product.find(shampo.id)
     self.assertIsNot(product, None)
     self.assertEqual(product.id, shampo.id)
     self.assertEqual(product.name, "shampos")
     self.assertEqual(product.category, "Health Care")
Exemplo n.º 4
0
 def test_delete_a_product(self):
     """ Delete a Product """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     product.save()
     self.assertEqual(len(Product.all()), 1)
     # delete the product and make sure it isn't in the database
     product.delete()
     self.assertEqual(len(Product.all()), 0)
Exemplo n.º 5
0
 def test_create_a_product(self):
     """ Create a product and assert that it exists """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     self.assertTrue(product != None)
     self.assertEqual(product.id, None)
     self.assertEqual(product.name, "Wagyu Tenderloin Steak")
     self.assertEqual(product.category, "food")
     self.assertAlmostEqual(product.price, Decimal(20.56))
     self.assertEqual(product.stock, 11)
Exemplo n.º 6
0
 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
     }
Exemplo n.º 7
0
 def test_update_a_product(self):
     """ Update a Product """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     product.save()
     self.assertEqual(product.id, 1)
     # Change it an save it
     product.category = "beverage"
     product.save()
     self.assertEqual(product.id, 1)
     # Fetch it back and make sure the id hasn't changed
     # but the data did change
     products = Product.all()
     self.assertEqual(len(products), 1)
     self.assertEqual(products[0].category, "beverage")
Exemplo n.º 8
0
 def test_deserialize_a_product(self):
     """ Test deserialization of a Product """
     data = {
         "id": 1,
         "name": "shampos",
         "category": "Health Care",
         "stock": 48,
         "price": 12.34,
         "description": "Test"
     }
     product = Product()
     product.deserialize(data)
     self.assertNotEqual(product, None)
     self.assertEqual(product.id, None)
     self.assertEqual(product.name, "shampos")
     self.assertEqual(product.category, "Health Care")
     self.assertEqual(product.stock, 48)
     self.assertAlmostEqual(product.price, Decimal(12.34))
     self.assertEqual(product.description, "Test")
Exemplo n.º 9
0
 def test_add_a_product(self):
     """ Create a product and add it to the database """
     products = Product.all()
     self.assertEqual(products, [])
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     self.assertTrue(product != None)
     self.assertEqual(product.id, None)
     self.assertEqual(product.name, "Wagyu Tenderloin Steak")
     self.assertEqual(product.category, "food")
     self.assertAlmostEqual(product.price, Decimal(20.56))
     self.assertEqual(product.stock, 11)
     product.save()
     # Asert that it was assigned an id and shows up in the database
     self.assertEqual(product.id, 1)
     products = Product.all()
     self.assertEqual(len(products), 1)
Exemplo n.º 10
0
 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.")
Exemplo n.º 11
0
 def test_deserialize_a_miss_product(self):
     """ Test deserialization of product missing agrs """
     data = {"id": 1, "name": "shampos", "category": "Health Care"}
     product = Product()
     self.assertRaises(DataValidationError, product.deserialize, data)
Exemplo n.º 12
0
 def test_deserialize_bad_data(self):
     """ Test deserialization of bad data """
     data = "this is not a dictionary"
     product = Product()
     self.assertRaises(DataValidationError, product.deserialize, data)