def test_delete_removes_expected_item(self, service_url, db): """ DELETE request for existing item Setup: Existing item is in the DB Expected: Success response Item is removed from DB """ db.add(Item(item_id='item_42', item_name='test_item', item_description='test_item_desc')) db.add(Item(item_id='item_24', item_name='test_item', item_description='test_item_desc')) db.commit() response = requests.delete(urljoin(service_url, api_path_tpl.format('item_42'))) assert response.status_code == 200 validate_json_response(response.json(), item_id='item_24', item_name='test_item', item_description='test_item_desc') items = db.query(Item).all() assert len(items) == 1 validate_db_item(items[0], item_id='item_24', item_name='test_item', item_description='test_item_desc')
def test_put_update_name_and_desc_performs_expected_action(self, service_url, db): """ PUT request with new name and description updates both Setup: Existing item in the DB Expected: Success response Specified item fields are updated Updated item is returned """ db.add(Item(item_id='item_42', item_name='test_item', item_description='test_item_desc')) db.commit() update = {'item_name': 'new_name', 'item_description': 'new_desc'} response = requests.put(urljoin(service_url, api_path_tpl.format('item_42')), data=update) assert response.status_code == 200 validate_json_response(response.json(), item_id='item_42', **update) items = db.query(Item).all() assert len(items) == 1 validate_db_item(items[0], item_id='item_42', **update)
def test_post_with_existing_item_creates_expected(self, service_url, db): """ POST request with new item Setup: Existing item is in the DB Expected: Success response New item is created Created item is returned """ db.add(Item(item_id='item_42', item_name='test_item', item_description='test_item_desc')) db.commit() item = { 'item_id': 'item_24', 'item_name': 'test_item', 'item_description': 'test_item_desc' } response = requests.post(urljoin(service_url, api_path), json=item) assert response.status_code == 200 validate_json_response(response.json(), **item) items = db.query(Item).all() assert len(items) == 2 validate_db_item(items[1], **item)
def post(self): """ Creates an item. """ # Retrieve the payload item_id = request.json.get('item_id') item_name = request.json.get('item_name') item_description = request.json.get('item_description') # Add the new Item model object and commit. db.session.add(Item(item_id=item_id, item_name=item_name, item_description=item_description)) db.session.commit() # Return the response with status and result. return jsonify({"status": "sucesss"})
def test_get_by_invalid_id_returns_expected_error(self, service_url, db): """ GET request with item ID specified Setup: Specified item is not in the DB Expected: Error response for an invalid item """ db.add(Item(item_id='item_42', item_name="test_item", item_description="test_item_desc")) db.commit() response = requests.get(urljoin(service_url, api_path_tpl.format('item_24'))) assert response.status_code == 500
def test_delete_invalid_item_id_returns_expected_error(self, service_url, db): """ DELETE request for invalid item Setup: Existing item is in the DB Expected: Error response No item is removed from DB """ db.add(Item(item_id='item_42', item_name='test_item', item_description='test_item_desc')) db.commit() response = requests.delete(urljoin(service_url, api_path_tpl.format('invalid_item'))) assert response.status_code == 500
def test_get_by_id_with_items_returns_expected_item(self, service_url, db): """ GET request with item ID specified Setup: Specified item is in the DB Expected: Success response Specified item is returned """ db.add(Item(item_id='item_42', item_name="test_item", item_description="test_item_desc")) db.commit() response = requests.get(urljoin(service_url, api_path_tpl.format('item_42'))) assert response.status_code == 200 validate_json_response(response.json(), 'item_42', 'test_item', 'test_item_desc')
def test_put_invalid_item_id_returns_expected_error(self, service_url, db): """ PUT request with invalid ID returns an error Setup: Existing item in the DB Expected: Error response Existing item is not updated """ db.add(Item(item_id='item_42', item_name='test_item', item_description='test_item_desc')) db.commit() update = {'item_name': 'test_item', 'item_description': 'x' * 256} response = requests.put(urljoin(service_url, api_path_tpl.format('invalid_id')), data=update) assert response.status_code == 500 items = db.query(Item).all() assert len(items) == 1 validate_db_item(items[0], item_id='item_42', item_name='test_item', item_description='test_item_desc')