def test_update_shopcart_item_not_found(self): """ Test update an existing shopcart item with a bad request """ test_shopcart = ShopcartFactory() resp = self.app.post("/api/shopcarts", json=test_shopcart.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # create a shopcart item to update test_shopcart_item = ShopcartItemFactory() test_shopcart_item.sid = resp.json["id"] resp = self.app.post("/api/shopcarts/{}/items".format( test_shopcart_item.sid), json=test_shopcart_item.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # update the shopcart item new_shopcart_item = resp.get_json() new_shopcart_item["price"] = 50.00 new_shopcart_item["sku"] = 1001 new_shopcart_item["name"] = "item_1" new_shopcart_item["amount"] = 4 resp = self.app.put( "/api/shopcarts/{}/items/{}".format(new_shopcart_item["sid"] + 1, new_shopcart_item["id"]), json=new_shopcart_item, content_type="application/json", ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
def test_update_shopcart_item(self): """ Update an existing shopcart item """ test_shopcart = ShopcartFactory() resp = self.app.post("/shopcarts", json=test_shopcart.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # create a shopcart item to update test_shopcart_item = ShopcartItemFactory() test_shopcart_item.sid = resp.json["id"] resp = self.app.post("/shopcarts/{}/items".format( test_shopcart_item.sid), json=test_shopcart_item.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # update the shopcart item new_shopcart_item = resp.get_json() new_shopcart_item["price"] = 50.00 new_shopcart_item["sku"] = 1001 new_shopcart_item["name"] = "item_1" new_shopcart_item["amount"] = 4 resp = self.app.put( "/shopcarts/{}/items/{}".format(new_shopcart_item["sid"], new_shopcart_item["id"]), json=new_shopcart_item, content_type="application/json", ) self.assertEqual(resp.status_code, status.HTTP_200_OK) updated_shopcart_item = resp.get_json() self.assertEqual(updated_shopcart_item["price"], 50.00) self.assertEqual(updated_shopcart_item["sku"], 1001) self.assertEqual(updated_shopcart_item["name"], "item_1") self.assertEqual(updated_shopcart_item["amount"], 4)
def test_create_shopcart_item_with_existing_item(self): """ Add to shopcart item when it already exists in the shopcart """ test_shopcart = ShopcartFactory() resp = self.app.post("/api/shopcarts", json=test_shopcart.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) test_shopcart_item = ShopcartItemFactory() test_shopcart_item.sid = resp.json["id"] original_amount = test_shopcart_item.amount resp = self.app.post("/api/shopcarts/{}/items".format( test_shopcart_item.sid), json=test_shopcart_item.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set updated_shopcart_item = ShopcartItemFactory() updated_shopcart_item.sku = test_shopcart_item.sku updated_shopcart_item.sid = resp.json["id"] updated_amount = original_amount + updated_shopcart_item.amount resp = self.app.post("/api/shopcarts/{}/items".format( updated_shopcart_item.sid), json=updated_shopcart_item.serialize(), content_type="application/json") # check for status code self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # check for updated amount new_shopcart_item = resp.get_json() self.assertEqual(new_shopcart_item["amount"], updated_amount, "Amounts do not match")
def test_create_shopcart_item(self): """ Create a new ShopcartItem """ test_shopcart = ShopcartFactory() resp = self.app.post("/api/shopcarts", json=test_shopcart.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) test_shopcart_item = ShopcartItemFactory() test_shopcart_item.sid = resp.json["id"] resp = self.app.post("/api/shopcarts/{}/items".format( test_shopcart_item.sid), json=test_shopcart_item.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = resp.headers.get("Location", None) self.assertTrue(location is not None) # Check the data is correct new_shopcart_item = resp.get_json() self.assertEqual(new_shopcart_item["sid"], test_shopcart_item.sid, "Shopcart ids do not match") self.assertEqual(new_shopcart_item["sku"], test_shopcart_item.sku, "SKUs do not match") self.assertEqual(new_shopcart_item["name"], test_shopcart_item.name, "Product names do not match") self.assertEqual(new_shopcart_item["price"], test_shopcart_item.price, "Prices do not match") self.assertEqual(new_shopcart_item["amount"], test_shopcart_item.amount, "Amounts do not match") # times set by db self.assertIsNotNone(new_shopcart_item["create_time"], "Creation time not set") self.assertIsNotNone(new_shopcart_item["update_time"], "Update time not set") # Check that the location header was correct resp = self.app.get(location, content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_200_OK) new_shopcart_item = resp.get_json() self.assertEqual(new_shopcart_item["sid"], test_shopcart_item.sid, "Shopcart ids do not match") self.assertEqual(new_shopcart_item["sku"], test_shopcart_item.sku, "SKUs do not match") self.assertEqual(new_shopcart_item["name"], test_shopcart_item.name, "Product names do not match") self.assertEqual(new_shopcart_item["price"], test_shopcart_item.price, "Prices do not match") self.assertEqual(new_shopcart_item["amount"], test_shopcart_item.amount, "Amounts do not match") # times set by db self.assertIsNotNone(new_shopcart_item["create_time"], "Creation time not set") self.assertIsNotNone(new_shopcart_item["update_time"], "Update time not set")
def _create_shopcart_items(self, count, sid=None): """ Factory method to create shopcart_items in bulk """ shopcart_items = [] for _ in range(count): test_shopcart_item = ShopcartItemFactory() if sid is not None: test_shopcart_item.sid = sid resp = self.app.post("/shopcarts/{}/items".format(sid), json=test_shopcart_item.serialize(), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED, "Could not create test shopcart item") new_shopcart_item = resp.get_json() test_shopcart_item.id = new_shopcart_item["id"] shopcart_items.append(test_shopcart_item) return shopcart_items