Пример #1
0
    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)
Пример #2
0
    def test_create_shopcart(self):
        """ Create a new 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)
        # 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 = resp.get_json()
        self.assertEqual(new_shopcart["user_id"], test_shopcart.user_id,
                         "User ids do not match")
        # times set by db
        self.assertIsNotNone(new_shopcart["create_time"],
                             "Creation time not set")
        self.assertIsNotNone(new_shopcart["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 = resp.get_json()
        self.assertEqual(new_shopcart["user_id"], test_shopcart.user_id,
                         "User ids do not match")
        # times set by db
        self.assertIsNotNone(new_shopcart["create_time"],
                             "Creation time not set")
        self.assertIsNotNone(new_shopcart["update_time"],
                             "Update time not set")
Пример #3
0
    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)
Пример #4
0
    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")
Пример #5
0
    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")
Пример #6
0
 def _create_shopcarts(self, count):
     """ Factory method to create shopcarts in bulk """
     shopcarts = []
     for _ in range(count):
         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,
                          "Could not create test shopcart")
         new_shopcart = resp.get_json()
         test_shopcart.id = new_shopcart["id"]
         shopcarts.append(test_shopcart)
     return shopcarts
Пример #7
0
    def test_place_order(self):
        """ Test sending shopcart order """
        test_shopcart = ShopcartFactory()
        resp = self.app.post("/api/shopcarts",
                             json={"user_id": test_shopcart.user_id},
                             content_type="application/json")
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        shopcart_id = resp.json["id"]
        shopcart_items = self._create_shopcart_items(10, shopcart_id)
        test_sku = shopcart_items[0].sku
        sku_shopcart_items = [
            shopcart_item for shopcart_item in shopcart_items
            if shopcart_item.sku == test_sku
        ]
        resp = self.app.get("/api/shopcarts/items",
                            query_string="sku={}".format(test_sku))
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        data = resp.get_json()
        self.assertEqual(len(data), len(sku_shopcart_items))

        resp = self.app.put(
            "/api/shopcarts/{}/place-order".format(shopcart_id),
            content_type="application/json",
        )
        self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(len(resp.data), 0)
        self.assertEqual(len(Shopcart.all()), 0)
        self.assertEqual(len(ShopcartItem.all()), 0)
Пример #8
0
    def test_place_order_empty_shopcart(self):
        test_shopcart = ShopcartFactory()
        resp = self.app.post("/shopcarts",
                             json={"user_id": test_shopcart.user_id},
                             content_type="application/json")
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        shopcart_id = resp.json["id"]

        resp = self.app.put(
            "/shopcarts/{}/place-order".format(shopcart_id),
            content_type="application/json",
        )
        self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
Пример #9
0
 def test_query_shopcart_item(self):
     """ Query shopcart item list without any query string """
     test_shopcart = ShopcartFactory()
     resp = self.app.post("/api/shopcarts",
                          json={"user_id": test_shopcart.user_id},
                          content_type="application/json")
     self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
     shopcart_id = resp.json["id"]
     count = 10
     shopcart_items = self._create_shopcart_items(count, shopcart_id)
     resp = self.app.get("/api/shopcarts/items")
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     data = resp.get_json()
     self.assertEqual(len(data), count)
Пример #10
0
    def test_create_duplicated_shopcart(self):
        """ Create Duplicated Shopcart """
        test_shopcart = ShopcartFactory()

        # 1st shopcart
        resp = self.app.post("/api/shopcarts",
                             json={'user_id': test_shopcart.user_id},
                             content_type="application/json")
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        new_shopcart = resp.get_json()

        # duplicated shopcart
        resp = self.app.post("/api/shopcarts",
                             json={'user_id': test_shopcart.user_id},
                             content_type="application/json")
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        duplicated_shopcart = resp.get_json()
        self.assertEqual(new_shopcart["id"], duplicated_shopcart["id"])
Пример #11
0
 def test_get_shopcart_with_zero_items(self):
     """ Find a Shopcart by id that has no items """
     shopcart_id = 1
     test_shopcart = ShopcartFactory()
     create_resp = self.app.post("/shopcarts",
                                 json={
                                     "id": shopcart_id,
                                     "user_id": test_shopcart.user_id
                                 },
                                 content_type="application/json")
     self.assertEqual(create_resp.status_code, status.HTTP_201_CREATED)
     shopcart_resp = self.app.get("/shopcarts/" + str(shopcart_id))
     resp = shopcart_resp.get_json()
     self.assertEqual(shopcart_resp.status_code, status.HTTP_200_OK)
     self.assertEqual(resp["id"], shopcart_id)
     self.assertEqual(resp["user_id"], test_shopcart.user_id)
     self.assertEqual(len(resp["items"]), 0)
     self.assertIsNotNone(resp["create_time"], "Creation time not set")
     self.assertIsNotNone(resp["update_time"], "Update time not set")
Пример #12
0
 def test_query_shopcart_item_list_by_amount(self):
     """ Query shopcart item list by amount """
     test_shopcart = ShopcartFactory()
     resp = self.app.post("/api/shopcarts",
                          json={"user_id": test_shopcart.user_id},
                          content_type="application/json")
     self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
     shopcart_id = resp.json["id"]
     shopcart_items = self._create_shopcart_items(10, shopcart_id)
     test_amount = shopcart_items[0].amount
     amount_shopcart_items = [
         shopcart_item for shopcart_item in shopcart_items
         if shopcart_item.amount == test_amount
     ]
     resp = self.app.get("/api/shopcarts/items",
                         query_string="amount={}".format(test_amount))
     self.assertEqual(resp.status_code, status.HTTP_200_OK)
     data = resp.get_json()
     self.assertEqual(len(data), len(amount_shopcart_items))
     # check the data
     for shopcart_item in data:
         self.assertEqual(shopcart_item["amount"], test_amount)
Пример #13
0
 def test_get_shopcart_with_items(self):
     """ Find a Shopcart by id that has multiple items"""
     test_shopcart = ShopcartFactory()
     resp = self.app.post("/shopcarts",
                          json={"user_id": test_shopcart.user_id},
                          content_type="application/json")
     self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
     count = 5
     shopcart_id = resp.json["id"]
     shopcart_items = self._create_shopcart_items(count, shopcart_id)
     get_items_response = self.app.get("/shopcarts/" + str(shopcart_id))
     self.assertEqual(get_items_response.status_code, status.HTTP_200_OK)
     response = get_items_response.get_json()
     self.assertEqual(response["id"], shopcart_id)
     self.assertEqual(response["user_id"], test_shopcart.user_id)
     self.assertEqual(len(response["items"]), len(shopcart_items))
     self.assertIsNotNone(response["create_time"], "Creation time not set")
     self.assertIsNotNone(response["update_time"], "Update time not set")
     for itr in range(len(shopcart_items)):
         self.assertTrue(
             self.is_shopcart_item_same(shopcart_items[itr].serialize(),
                                        response["items"][itr]))