def test_create_a_product(self): products = Product.all() self.assertEqual(products, []) product = Product(id=123) self.assertTrue(product is not None) product.create() self.assertEqual(product.id, 123) products = Product.all() self.assertEqual(len(products), 1)
def test_delete_a_product(self): """ Delete a Product """ product = Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99) product.create() 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)
def test_update_a_product_empty_id(self): """ Update a Product with empty id """ product = Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99) product.create() self.assertEqual(product.id, 1) # Change it and update it product.id = None self.assertRaises(DataValidationError, product.update)
def test_delete_a_product_commit_error(self): """ Delete a Product """ product = Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99) product.create() self.assertEqual(len(Product.all()), 1) # delete the product and make sure it isn't in the database with patch('service.models.db.session.commit') as commit: commit.side_effect = InvalidRequestError product.delete() self.assertEqual(len(Product.all()), 1)
def test_add_a_product_commit_error(self, commit): """ Create a product and raises an InvalidRequestError """ commit.side_effect = InvalidRequestError products = Product.all() self.assertEqual(products, []) product = Product(name="Cake", description="Chocolate Cake", category="Food", price=10.50) self.assertTrue(product is not None) self.assertEqual(product.id, None) product.create() self.assertEqual(products, [])
def test_add_a_product(self): """ Create a product and add it to the database """ products = Product.all() self.assertEqual(products, []) product = Product(name="Cake", description="Chocolate Cake", category="Food", price=10.50) self.assertTrue(product is not None) self.assertEqual(product.id, None) product.create() # Assert 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)
def create_product(): """ Creates a Product This endpoint will create a Product based on the data in the body that is posted """ app.logger.info("Request to create a product") check_content_type("application/json") product = Product() product.deserialize(request.get_json()) product.create() message = product.serialize() location_url = url_for("get_products", product_id=product.id, _external=True) #location_url = "not yet implemented - story #7" return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def test_find_product(self): """ Find a Product by ID """ Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99).create() t_v = Product(name="TV", description="Black Sony TV", category="Technology", price=1999.99) t_v.create() product = Product.find(t_v.id) self.assertIsNot(product, None) self.assertEqual(product.id, t_v.id) self.assertEqual(product.name, "TV") self.assertEqual(product.description, "Black Sony TV") self.assertEqual(product.category, "Technology") self.assertEqual(product.price, 1999.99)
def test_update_a_product_commit_error(self): """ Update a product and raises an InvalidRequestError """ product = Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99) product.create() self.assertEqual(product.id, 1) # Change it and update it product.price = 9999.99 product.description = "White iPhone" with patch('service.models.db.session.commit') as commit: commit.side_effect = InvalidRequestError product.update() products = Product.all() self.assertEqual(len(products), 1) self.assertEqual(products[0].price, 999.99) self.assertEqual(products[0].description, "Black iPhone")
def test_update_a_product(self): """ Update a Product """ product = Product() logging.debug(product) product.create() logging.debug(product) self.assertEqual(product.id, 1) # Change it and save it product.category = "Shoes" original_id = product.id product.save() self.assertEqual(product.id, original_id) self.assertEqual(product.category, "Shoes") # 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].id, 1) self.assertEqual(products[0].category, "Shoes")
def test_update_a_product(self): """ Update a Product """ product = Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99) product.create() self.assertEqual(product.id, 1) # Change it and update it product.price = 9999.99 product.description = "White iPhone" product.update() 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].price, 9999.99) self.assertEqual(products[0].description, "White iPhone")
def test_add_a_product(self): """ Create a product and add it to the database """ products = Product.all() self.assertEqual(products, []) product = Product(name="Test Product", sku="00000000", price=1.01, stock=10, size="N/A", color="N/A", category="Misc", description="This is a test product", available=True) self.assertTrue(product != None) self.assertEqual(product.id, None) product.create() # Assert 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)
def post(self): """ Creates a Product This endpoint will create a Product based on the data in the body that is posted """ app.logger.info("Request to create a product") check_content_type("application/json") product = Product() product.deserialize(api.payload) if product.id == "" or product.name == "" or product.description == "" or product.price == "" or product.category == "": app.logger.info("Fields cannot be empty.") return api.abort(status.HTTP_400_BAD_REQUEST, "Fields cannot be empty.") product.create() app.logger.info("Product with id [%s] created.", product.id) location_url = api.url_for(ProductResource, product_id=product.id, _external=True) return product.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }