def test_method()

in sample-apps/emr/sample-app/src/app.py [0:0]


def test_method():
    # TODO:
    # * replace hard coded assertions with configurables on response values that may differ from service to service
    # * make function code less ugly and repetitive
    success = True
    response = {
        "cleanup": {"success": False, "data": []},
        "put": {"success": False, "data": ["skipped"]},
        "describe": {"success": False, "data": ["skipped"]},
        "list": {"success": False, "data": ["skipped"]},
        "idempotency": {"success": False, "data": ["skipped"]},
        "delete": {"success": False, "data": ["skipped"]}
    }
    try:
        response['cleanup']["success"] = True
        items = list_method()
        assert items['success'], items['error']
        for item in items['response']:
            response['cleanup']["data"].append("cleaning up item %s" % item)
            delete_method(item_id=item)
        if len(items) == 0:
            response['cleanup']["data"].append("No items to cleanup")
        else:
            response['cleanup']["data"].append("Successfully cleaned up all items")
    except Exception as e:
        tb = traceback.format_exc()
        success = False
        response['cleanup']["success"] = False
        response['cleanup']["data"].append("%s %s\n\n%s" % (str(e.__class__), str(e), tb))
    if success:
        try:
            response['put'] = put_method(
                item_id="test",
                content_type="application/json",
                data='{"content": "test_content"}'
            )
            assert response['put']["success"], response['put']["error"]
            response['put']['data'] = ["Successfully put item"]
        except Exception as e:
            tb = traceback.format_exc()
            success = False
            response['put']["success"] = False
            response['put']["data"] = ["%s %s\n\n%s" % (str(e.__class__), str(e), tb)]
    if success:
        try:
            response['describe'] = describe_method(item_id="test")
            assert response['describe']["success"], response['describe']["error"]
            expected = "test"
            assert response['describe']["response"]['item_id'] == expected, \
                'unexpected response object, expecting:\n\n%s\n\ngot:\n\n%s ' % (
                    str([expected]),
                    str([response['describe']["response"]['item_id']])
                )
            response['describe']['data'] = ["Successfully described item"]
        except Exception as e:
            tb = traceback.format_exc()
            success = False
            response['describe']["success"] = False
            response['describe']["data"] = ["%s %s\n\n%s" % (str(e.__class__), str(e), tb)]
    if success:
        try:
            response['list'] = list_method()
            assert response['list']["success"], response['list']["error"]
            expected = ["test"]
            assert [i[0] for i in response['list']["response"]] == expected, \
                'unexpected response object, expecting: "%s" got: "%s" ' % (
                    str([expected]),
                    str([[i[0] for i in response['list']["response"]]])
                )
            response['list']['data'] = ["Successfully listed items"]
        except Exception as e:
            tb = traceback.format_exc()
            success = False
            response['list']["success"] = False
            response['list']["data"] = ["%s %s\n\n%s" % (str(e.__class__), str(e), tb)]
    if success:
        try:
            response['idempotency']["success"] = True
            response['idempotency']["data"] = []
            put_method(
                item_id="test",
                content_type="application/json",
                data='{"content": "test_content_idempotent"}'
            )
            response['idempotency']['data'].append("Put an additional item with duplicate item_id")
            describe = describe_method(item_id="test")
            assert describe["success"], describe["error"]
            expected = "test"
            assert describe["response"]['item_id'] == expected, \
                'unexpected describe response object, expecting: "%s" got: "%s" ' % (
                    str([expected]),
                    str([describe["response"]['item_id']])
                )
            response['idempotency']['data'].append("Verified describe has updated content")
            list_items = list_method()
            assert list_items["success"], list_items["error"]
            expected = ["test"]
            assert [i[0] for i in list_items["response"]] == expected, \
                'unexpected list response object, expecting: "%s" got: "%s" ' % (
                    str([expected]),
                    str([[i[0] for i in list_items["response"]]])
                )
            response['idempotency']['data'].append("Verified list does not contain duplicates")
            response['idempotency']['data'].append("Successfully tested idempotency")
        except Exception as e:
            tb = traceback.format_exc()
            success = False
            response['idempotency']["success"] = False
            response['idempotency']["data"].append("%s %s\n\n%s" % (str(e.__class__), str(e), tb))
    if success:
        try:
            response['delete'] = delete_method(item_id="test")
            assert response['delete']["success"], response['delete']["error"]
            response['delete']['data'] = ["Successfully deleted item"]
        except Exception as e:
            tb = traceback.format_exc()
            success = False
            response['delete']["success"] = False
            response['delete']["data"] = ["%s %s\n\n%s" % (str(e.__class__), str(e), tb)]
    return {"success": success, "data": response}