Esempio n. 1
0
 def test_find_item(self):
     """ Find an Item by ID """
     Item(sku="ID111",
          count=3,
          price=2.00,
          name="test_item",
          link="test.com",
          brand_name="gucci",
          is_available=True).save()
     test_item = Item(sku="ID222",
                      count=5,
                      price=10.00,
                      name="some_item",
                      link="link.com",
                      brand_name="nike",
                      is_available=False)
     test_item.save()
     item = Item.find(test_item.id)
     self.assertIsNot(item, None)
     self.assertEqual(item.sku, test_item.sku)
     self.assertEqual(item.count, 5)
     self.assertEqual(item.price, 10.00)
     self.assertEqual(item.name, "some_item")
     self.assertEqual(item.link, "link.com")
     self.assertEqual(item.is_available, False)
Esempio n. 2
0
 def test_find_item(self):
     """ Find a Item by id """
     Item(0, "fido", "dog").save()
     Item(0, "kitty", "cat").save()
     item = Item.find(2)
     self.assertIsNot(item, None)
     self.assertEqual(item.id, 2)
     self.assertEqual(item.name, "kitty")
Esempio n. 3
0
def delete_items(item_id):
    """
    Delete a Item

    This endpoint will delete a Item based the id specified in the path
    """
    item = Item.find(item_id)
    if item:
        item.delete()
    return make_response('', status.HTTP_204_NO_CONTENT)
Esempio n. 4
0
def get_items(item_id):
    """
    Retrieve a single Item

    This endpoint will return a Item based on it's id
    """
    item = Item.find(item_id)
    if not item:
        raise NotFound("Item with id '{}' was not found.".format(item_id))
    return make_response(jsonify(item.serialize()), status.HTTP_200_OK)
Esempio n. 5
0
def purchase_items(item_id):
    """ Purchasing a Item makes it unavailable """
    item = Item.find(item_id)
    if not item:
        abort(status.HTTP_404_NOT_FOUND,
              "Item with id '{}' was not found.".format(item_id))
    if not item.available:
        abort(status.HTTP_400_BAD_REQUEST,
              "Item with id '{}' is not available.".format(item_id))
    item.available = False
    item.save()
    return make_response(jsonify(item.serialize()), status.HTTP_200_OK)
Esempio n. 6
0
def update_items(item_id):
    """
    Update a Item

    This endpoint will update a Item based the body that is posted
    """
    check_content_type('application/json')
    item = Item.find(item_id)
    if not item:
        raise NotFound("Item with id '{}' was not found.".format(item_id))
    data = request.get_json()
    app.logger.info(data)
    item.deserialize(data)
    item.id = item_id
    item.save()
    mqtt_update_message = "Price of the Item with id '{}' and name '{}' was changed.".format(
        item_id, item.name)
    mqtt.publish(topic, mqtt_update_message)
    return make_response(jsonify(item.serialize()), status.HTTP_200_OK)
Esempio n. 7
0
 def test_item_not_found(self):
     """ Find a Item that doesnt exist """
     Item(0, "fido", "dog").save()
     item = Item.find(2)
     self.assertIs(item, None)
Esempio n. 8
0
 def test_find_with_no_items(self):
     """ Find a Item with empty database """
     item = Item.find(1)
     self.assertIs(item, None)