コード例 #1
0
def test_event_call_resource():
    print(
        "####################   TESTING EVENT CALL RESOURCE   ####################"
    )

    # GETTING ALL EVENT_CALLS
    print("TEST_1 --- GETTING ALL EVENT_CALLS")
    uri = "event_calls"
    expected_result = {"event_calls": []}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE EVENT_CALL
    print("TEST_2 --- POSTING ONE EVENT_CALL")
    body = {
        "name": "name",
        "is_module": "is_module",
    }
    event_call_1 = EventCallModel(str(json.dumps(body)))
    event_call_1_json = event_call_1.to_json()
    event_call_1_json['id'] = 1
    expected_result = event_call_1_json
    expected_status = 201
    uri = "event_calls"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL EVENT_CALLS
    print("TEST_3 --- GETTING ALL EVENT_CALLS")
    uri = "event_calls"
    expected_result = {"event_calls": [event_call_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)
コード例 #2
0
def test_schedule_resource():
    print(
        "####################   TESTING SCHEDULE RESOURCE   ####################"
    )

    # GETTING ALL EVENT_CALLS
    print("TEST_1 --- GETTING ALL SCHEDULES")
    uri = "schedules"
    expected_result = {"schedules": []}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE EVENT_CALL
    print("TEST_2 --- POSTING ONE SCHEDULE")
    body = {
        "time":
        "18:00:00",
        "schedule_days": [1, 2, 3],
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 2,
            "value": 1
        }]
    }
    schedule_1 = ScheduleModel(datetime.strptime("18:00:00", "%H:%M:%S"))
    schedule_1_json = schedule_1.to_json()
    schedule_1_json['id'] = 1
    schedule_1_json['url'] = "127.0.0.1:5000/api/v1/schedules/1"

    schedule_days_1 = ScheduleDayModel(schedule_1_json['id'], 1)
    schedule_days_1_json = schedule_days_1.to_json()
    schedule_days_1_json['id'] = 1
    schedule_days_2 = ScheduleDayModel(schedule_1_json['id'], 2)
    schedule_days_2_json = schedule_days_2.to_json()
    schedule_days_2_json['id'] = 2
    schedule_days_3 = ScheduleDayModel(schedule_1_json['id'], 3)
    schedule_days_3_json = schedule_days_3.to_json()
    schedule_days_3_json['id'] = 3
    schedule_1_json['schedule_days'] = [
        schedule_days_1_json, schedule_days_2_json, schedule_days_3_json
    ]

    scheduled_usage_1 = ScheduledUsageModel(1, 1, 1)
    scheduled_usage_1_json = scheduled_usage_1.to_json()
    scheduled_usage_1_json['id'] = 1
    scheduled_usage_2 = ScheduledUsageModel(1, 2, 1)
    scheduled_usage_2_json = scheduled_usage_2.to_json()
    scheduled_usage_2_json['id'] = 2
    schedule_1_json['scheduled_usages'] = [
        scheduled_usage_1_json, scheduled_usage_2_json
    ]

    expected_result = schedule_1_json
    expected_status = 201
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL SCHEDULES
    print("TEST_3 --- GETTING ALL SCHEDULES")
    uri = "schedules"
    expected_result = {"schedules": [schedule_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_4 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time":
        "100:00:00",
        "schedule_days": [1, 2, 3],
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 2,
            "value": 1
        }]
    }
    expected_result = {
        "errors": [
            Error("Invalid time format given, expected format is: '12:00:00'",
                  "Invalid time format, expected format is: '%H:%M:%S'", 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_5 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time":
        "10:00:00",
        "schedule_days": [],
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 2,
            "value": 1
        }]
    }
    expected_result = {
        "errors": [
            Error("No schedule days given.", "Expecting array of days", 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_6 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time":
        "10:00:00",
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 2,
            "value": 1
        }]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_7 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time": "10:00:00",
        "schedule_days": [1, 2, 3],
        "scheduled_usages": []
    }
    expected_result = {
        "errors": [
            Error("No schedule usages given.", "Expecting array of usages",
                  422, "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_8 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {"time": "10:00:00", "schedule_days": [1, 2, 3]}
    expected_result = {
        "errors": [
            Error("No schedule usages given.", "Expecting array of usages",
                  422, "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_9 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time":
        "10:00:00",
        "schedule_days": [1, 1, 2, 2],
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 2,
            "value": 1
        }]
    }
    expected_result = {
        "errors": [
            Error("Duplicate day entry. {}".format(1),
                  "Duplicate day entry. {}".format(1), 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json(),
            Error("Duplicate day entry. {}".format(2),
                  "Duplicate day entry. {}".format(2), 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_10 --- POSTING ONE SCHEDULE - BAD REQUEST")
    body = {
        "time":
        "10:00:00",
        "schedule_days": [1, 2, 3],
        "scheduled_usages": [{
            "usage_id": 1,
            "value": 1
        }, {
            "usage_id": 1,
            "value": 1
        }]
    }
    expected_result = {
        "errors": [
            Error("Duplicate usage entry. {}".format(1),
                  "Duplicate usage entry. {}".format(1), 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "schedules"
    test_post(uri, body, expected_result, expected_status)
コード例 #3
0
def test_group_resource():
    print(
        "####################   TESTING GROUP RESOURCE   ####################")

    # GETTING ALL GROUPS
    print("TEST_1 --- GETTING ALL GROUPS")
    uri = "groups"
    expected_result = {"groups": []}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE GROUP
    print("TEST_2 --- POSTING ONE GROUP")
    group_1_name = 'Huiskamer'
    group_1_is_module = True
    group_1 = GroupModel(group_1_name, group_1_is_module)
    body = {
        "name": group_1_name,
        "is_module": group_1_is_module,
    }
    group_1_json = group_1.to_json()
    group_1_json['id'] = 1
    group_1_json['url'] = "127.0.0.1:5000/api/v1/groups/1"
    expected_result = group_1_json
    expected_status = 201
    uri = "groups"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL GROUPS
    print("TEST_3 --- GETTING ALL GROUPS")
    uri = "groups"
    expected_result = {"groups": [group_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ONE GROUP
    print("TEST_4 --- GETTING ONE GROUP")
    uri = "groups/1"
    expected_result = group_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # ADDING ONE ITEM TO GROUP
    print("TEST_5 --- ADDING ONE ITEM TO GROUP")
    uri = "groups/1/items"
    item_1_json = send_get('items/1')
    body = {"item_id": item_1_json['id']}
    expected_result = group_1_json
    expected_result['items'] = [{
        "id": item_1_json['id'],
        "name": item_1_json['name'],
        "comment": item_1_json['comment'],
        "url": item_1_json['url']
    }]
    expected_status = 200
    test_post(uri, body, expected_result, expected_status)
    group_1_json['items'] = [{
        "id": item_1_json['id'],
        "name": item_1_json['name'],
        "comment": item_1_json['comment'],
        "url": item_1_json['url']
    }]

    # GETTING ONE GROUP
    print("TEST_6 --- GETTING ONE GROUP")
    uri = "groups/1"
    expected_result = group_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE GROUP
    print("TEST_7 --- POSTING ONE GROUP")
    group_2_name = 'Verlichting'
    group_2_is_module = False
    group_2 = GroupModel(group_2_name, group_2_is_module)
    body = {
        "name": group_2_name,
        "is_module": group_2_is_module,
    }
    group_2_json = group_2.to_json()
    group_2_json['id'] = 2
    group_2_json['url'] = "127.0.0.1:5000/api/v1/groups/2"
    expected_result = group_2_json
    expected_status = 201
    uri = "groups"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE GROUP
    print("TEST_8 --- POSTING ONE GROUP")
    group_3_name = 'Badkamer'
    group_3_is_module = True
    group_3 = GroupModel(group_3_name, group_3_is_module)
    body = {
        "name": group_3_name,
        "is_module": group_3_is_module,
    }
    group_3_json = group_3.to_json()
    group_3_json['id'] = 3
    group_3_json['url'] = "127.0.0.1:5000/api/v1/groups/3"
    expected_result = group_3_json
    expected_status = 201
    uri = "groups"
    test_post(uri, body, expected_result, expected_status)

    # ADDING ONE ITEM TO GROUP
    print("TEST_9 --- ADDING ONE ITEM TO GROUP")
    uri = "groups/2/items"
    item_1_json = send_get('items/1')
    body = {"item_id": item_1_json['id']}
    expected_result = group_2_json
    expected_result['items'] = [{
        "id": item_1_json['id'],
        "name": item_1_json['name'],
        "comment": item_1_json['comment'],
        "url": item_1_json['url']
    }]
    expected_status = 200
    test_post(uri, body, expected_result, expected_status)
    group_2_json['items'] = [{
        "id": item_1_json['id'],
        "name": item_1_json['name'],
        "comment": item_1_json['comment'],
        "url": item_1_json['url']
    }]

    # ADDING ONE ITEM TO GROUP
    print("TEST_10 --- ADDING ONE ITEM TO SECOND MODULE")
    uri = "groups/3/items"
    item_1_json = send_get('items/1')
    body = {"item_id": item_1_json['id']}
    error = Error("Item cannot be in two different modules",
                  "item.is_in_module() returned True", 422,
                  "https://en.wikipedia.org/wiki/HTTP_422")
    expected_result = {"errors": [error.to_json()]}
    expected_status = 422
    test_post(uri, body, expected_result, expected_status)

    # GETTING ONE ITEM
    print("TEST_11 --- GETTING ONE ITEM")
    uri = "items/1"
    expected_result = {
        "id":
        1,
        "name":
        'Z04 Gang lamp (SW)',
        "comment":
        'new_comment',
        "last_use":
        None,
        "usages": [],
        "url":
        "127.0.0.1:5000/api/v1/items/1",
        "groups": [{
            "id": 1,
            "name": 'Huiskamer'
        }, {
            "id": 2,
            "name": 'Verlichting'
        }]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # ADDING ONE ITEM TO GROUP
    print("TEST_12 --- REMOVING ONE ITEM FROM GROUP")
    uri = "groups/1/items/1"
    item_1_json = send_get('items/1')
    body = {"item_id": item_1_json['id']}
    expected_result = group_1_json
    expected_result['items'] = []
    expected_status = 200
    test_delete(uri, body, expected_result, expected_status)
    group_1_json['items'] = []

    # GETTING ONE GROUP
    print("TEST_13 --- GETTING ONE GROUP")
    uri = "groups/1"
    expected_result = group_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # ADDING ONE ITEM TO NON EXISTING GROUP
    print("TEST_14 --- ADDING ITEM TO NON EXISTING GROUP")
    uri = "groups/5/items"
    item_1_json = send_get('items/1')
    body = {"item_id": item_1_json['id']}
    error = Error("Could not find group with id: {}".format(5),
                  "GroupModel.find_by_id({}) returned None".format(5), 404,
                  "https://en.wikipedia.org/wiki/HTTP_404")
    expected_result = {"errors": [error.to_json()]}
    expected_status = 422
    test_post(uri, body, expected_result, expected_status)
    group_1_json['items'] = []

    # POSTING ONE GROUP
    print("TEST_15 --- POSTING ONE GROUP - BAD REQUEST")
    group_3_name = 'Badkamer _____________________________________________________________________' \
                   '______________________________________________________________________________' \
                   '______________________________________________________________________________' \
                   '______________________________________________________________________________'
    group_3_is_module = True
    body = {
        "name": group_3_name,
        "is_module": group_3_is_module,
    }
    error = Error("Name cannot be longer than 255 characters.",
                  "Name parameter cannot be longer than 255 characters.", 400,
                  "https://en.wikipedia.org/wiki/HTTP_400")
    expected_result = {"errors": [error.to_json()]}
    expected_status = 400
    uri = "groups"
    test_post(uri, body, expected_result, expected_status)

    # DELETING ONE GROUP
    print("TEST_16 --- DELETING ONE GROUP")
    uri = "groups/3"
    body = {}
    expected_result = "Group with id: {} was successfully deleted.".format(3)
    expected_status = 200
    test_delete(uri, body, expected_result, expected_status)

    # CHECKING IF GROUP WAS DELETED
    print("TEST_17 --- GETTING ALL GROUPS")
    uri = "groups"
    expected_result = {"groups": [group_1_json, group_2_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # ADDING ONE ITEM TO GROUP
    print("TEST_18 --- ADDING ONE ITEM TO GROUP")
    uri = "groups/1/items"
    item_2_json = send_get('items/2')
    body = {"item_id": 2}
    expected_result = group_1_json
    expected_result['items'] = [{
        "id": item_2_json['id'],
        "name": item_2_json['name'],
        "comment": item_2_json['comment'],
        "url": item_2_json['url']
    }]
    expected_status = 200
    test_post(uri, body, expected_result, expected_status)
    group_1_json['items'] = [{
        "id": item_2_json['id'],
        "name": item_2_json['name'],
        "comment": item_2_json['comment'],
        "url": item_2_json['url']
    }]
コード例 #4
0
def test_presets_resource():
    print(
        "####################   TESTING PRESETS RESOURCE   ####################"
    )

    # GETTING ALL PRESETS
    print("TEST_1 --- GETTING ALL PRESETS")
    uri = "groups/1/presets"
    expected_result = {"presets": []}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE PRESET
    print("TEST_2 --- POSTING ONE PRESET")
    preset_1_group_id = 1
    preset_1_name = "name"
    body = {
        "name": preset_1_name,
    }
    preset_1 = PresetModel(preset_1_group_id, preset_1_name)
    preset_1_json = preset_1.to_json()
    preset_1_json['id'] = 1
    preset_1_json['url'] = "127.0.0.1:5000/api/v1/groups/1/presets/1"
    expected_result = preset_1_json
    expected_status = 201
    uri = "groups/{}/presets".format(preset_1_group_id)
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE PRESET
    print("TEST_3 --- POSTING ONE PRESET - BAD REQUEST")
    body = {
        "name": "na",
    }
    expected_result = {
        "errors": [
            Error("Name must be at least 3 characters long.",
                  "len(preset_name) < 3 returned True", 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "groups/{}/presets".format(preset_1_group_id)
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE PRESET
    print("TEST_4 --- POSTING ONE PRESET - BAD REQUEST")
    body = {
        "name": "namenamenamenamenamenamenamenamenamename",
    }
    expected_result = {
        "errors": [
            Error("Name cannot be longer than 30 characters.",
                  "len(preset_name) > 30 returned True", 422,
                  "https://en.wikipedia.org/wiki/HTTP_422").to_json()
        ]
    }
    expected_status = 422
    uri = "groups/{}/presets".format(preset_1_group_id)
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL PRESETS
    print("TEST_5 --- GETTING ALL PRESETS")
    uri = "groups/{}/presets".format(preset_1_group_id)
    expected_result = {"presets": [preset_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE PRESET
    print("TEST_6 --- POSTING ONE PRESET")
    preset_2_group_id = 1
    preset_2_name = "preset_22"
    body = {
        "name": preset_2_name,
    }
    preset_2 = PresetModel(preset_2_group_id, preset_2_name)
    preset_2_json = preset_2.to_json()
    preset_2_json['id'] = 2
    preset_2_json['url'] = "127.0.0.1:5000/api/v1/groups/1/presets/2"
    expected_result = preset_2_json
    expected_status = 201
    uri = "groups/{}/presets".format(preset_2_group_id)
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL PRESETS
    print("TEST_7 --- GETTING ALL PRESETS")
    uri = "groups/{}/presets".format(preset_1_group_id)
    expected_result = {"presets": [preset_1_json, preset_2_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # DELETING ONE PRESET
    print("TEST_8 --- DELETING ONE PRESET")
    uri = "groups/{}/presets/{}".format(preset_1_group_id, preset_2_json['id'])
    expected_result = "Preset with id: {} was successfully deleted.".format(
        preset_2_json['id'])
    expected_status = 200
    test_delete(uri, {}, expected_result, expected_status)

    # GETTING ALL PRESETS
    print("TEST_9 --- GETTING ALL PRESETS")
    uri = "groups/{}/presets".format(preset_1_group_id)
    expected_result = {"presets": [preset_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # UPDATING ONE PRESET
    print("TEST_10 --- UPDATING ONE PRESET")
    uri = "groups/{}/presets/{}".format(preset_1_group_id, preset_1_json['id'])
    body = {"name": "preset_1"}
    preset_1_json['name'] = "preset_1"
    expected_status = 200
    test_put(uri, body, preset_1_json, expected_status)

    # GETTING ALL PRESETS
    print("TEST_11 --- GETTING ALL PRESETS")
    uri = "groups/{}/presets".format(preset_1_group_id)
    expected_result = {"presets": [preset_1_json]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE PRESET
    print("TEST_12 --- POSTING ONE PRESET")
    preset_3_group_id = 2
    preset_3_name = "preset_3"
    body = {
        "name": preset_3_name,
    }
    preset_3 = PresetModel(preset_3_group_id, preset_3_name)
    preset_3_json = preset_3.to_json()
    preset_3_json['id'] = 3
    preset_3_json['url'] = "127.0.0.1:5000/api/v1/groups/2/presets/3"
    expected_result = preset_3_json
    expected_status = 201
    uri = "groups/{}/presets".format(preset_3_group_id)
    test_post(uri, body, expected_result, expected_status)
コード例 #5
0
def test_preset_actions_resource():
    print("####################   TESTING PRESET_ACTIONS RESOURCE   ####################")
    uri = "groups/2/presets/3"
    preset_3 = send_get(uri)

    # GETTING ALL PRESET_ACTIONS
    print("TEST_1 --- GETTING ALL PRESETS_ACTIONS")
    uri = "groups/{}/presets/{}/preset_actions".format(preset_3['group_id'], preset_3['id'])
    expected_result = {
        "preset_actions": []
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE PRESET_ACTION
    print("TEST_2 --- POSTING ONE PRESET ACTION")
    preset_action_1_usage_id = 1
    preset_action_1_value = 0
    body = {
        "usage_id": preset_action_1_usage_id,
        "value": preset_action_1_value
    }
    preset_action_1 = PresetActionModel(preset_3['id'], preset_action_1_usage_id, preset_action_1_value)
    preset_action_1_json = preset_action_1.to_json()
    preset_action_1_json['id'] = 1
    expected_result = preset_action_1_json
    expected_status = 201
    uri = "groups/{}/presets/{}/preset_actions".format(preset_3['group_id'], preset_3['id'])
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE PRESET_ACTION
    print("TEST_3 --- POSTING ONE PRESET ACTION - BAD REQUEST")
    body = {
        "usage_id": 2,
        "value": preset_action_1_value
    }
    expected_result = {"errors":
        [Error(
            "The item that usage with id {} is attached to does not belong to group with id {}."
                .format(body['usage_id'], preset_3['group_id']),
            "Usage is not in this group",
            422,
            "https://en.wikipedia.org/wiki/HTTP_422"
        ).to_json()]}
    expected_status = 422
    uri = "groups/{}/presets/{}/preset_actions".format(preset_3['group_id'], preset_3['id'])
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE PRESET_ACTION
    print("TEST_4 --- POSTING ONE PRESET ACTION - BAD REQUEST")
    body = {
        "usage_id": 1,
        "value": 2
    }
    expected_result = {"errors": [
        Error(
            "Given value is not in range of Usage values. (0 - 1) (2 given)",
            "value is not within range. (0 - 1) (2 given)",
            422,
            "https://en.wikipedia.org/wiki/HTTP_404"
        ).to_json()
    ]}
    expected_status = 422
    uri = "groups/{}/presets/{}/preset_actions".format(preset_3['group_id'], preset_3['id'])
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL PRESET_ACTIONS
    print("TEST_5 --- GETTING ALL PRESETS_ACTIONS")
    uri = "groups/{}/presets/{}/preset_actions".format(preset_3['group_id'], preset_3['id'])
    expected_result = {
        "preset_actions": [preset_action_1_json]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ONE PRESET_ACTION
    print("TEST_6 --- GETTING ONE PRESETS_ACTION")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], preset_action_1_json['id'])
    expected_result = preset_action_1_json

    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ONE PRESET_ACTION
    print("TEST_7 --- GETTING ONE PRESETS_ACTION - BAD REQUEST")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], 2)
    expected_result = {"errors": [Error(
                "Could not find preset action with id: {}".format(2),
                "PresetActionModel.find_by_id({}) returned None".format(2),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}

    expected_status = 422
    test_get(uri, expected_result, expected_status)

    # PUTTING ONE PRESET_ACTION
    print("TEST_8 --- PUTTING ONE PRESET_ACTION - BAD REQUEST")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], 2)
    expected_result = {"errors": [Error(
                "Could not find preset action with id: {}".format(2),
                "PresetActionModel.find_by_id({}) returned None".format(2),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}
    expected_status = 422
    body = {
        "usage_id": 1,
        "value": 0
    }
    test_put(uri, body, expected_result, expected_status)

    # PUTTING ONE PRESET_ACTION
    print("TEST_9 --- PUTTING ONE PRESET_ACTION - BAD REQUEST")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], 2, 1)
    expected_result = {"errors": [Error(
                "Could not find preset with id: {}".format(2),
                "PresetModel.find_by_id({}) returned None".format(2),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}
    expected_status = 422
    body = {
        "usage_id": 1,
        "value": 0
    }
    test_put(uri, body, expected_result, expected_status)

    # PUTTING ONE PRESET_ACTION
    print("TEST_10 --- PUTTING ONE PRESET_ACTION - BAD REQUEST")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], 1)
    expected_result = {"errors": [Error(
                "Could not find usage with id: {}".format(15),
                "UsageModel.find_by_id({}) returned None".format(15),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}
    expected_status = 422
    body = {
        "usage_id": 15,
        "value": 0
    }
    test_put(uri, body, expected_result, expected_status)

    # PUTTING ONE PRESET_ACTION
    print("TEST_11 --- PUTTING ONE PRESET_ACTION - BAD REQUEST")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], 1)
    expected_result = {"errors": [Error(
                "Given value is not in range of Usage values. ({} - {}) ({} given)".format(
                    0, 1, 200),
                "value is not within range. ({} - {}) ({} given)".format(
                    0, 1, 200),
                422,
                "https://en.wikipedia.org/wiki/HTTP_404"
            ).to_json()]}
    expected_status = 422
    body = {
        "usage_id": 1,
        "value": 200
    }
    test_put(uri, body, expected_result, expected_status)

    # PUTTING ONE PRESET_ACTION
    print("TEST_12 --- PUTTING ONE PRESET_ACTION")
    uri = "groups/{}/presets/{}/preset_actions/{}"\
        .format(preset_3['group_id'], preset_3['id'], 1)
    preset_action_1_json['value'] = 0
    expected_result = preset_action_1_json
    expected_status = 200
    body = {
        "usage_id": 1,
        "value": 0
    }
    test_put(uri, body, expected_result, expected_status)

    # GETTING ALL PRESET_ACTIONS
    print("TEST_13 --- GETTING ALL PRESET_ACTIONS")
    uri = "groups/{}/presets/{}/preset_actions"\
        .format(preset_3['group_id'], preset_3['id'])
    expected_result = {"preset_actions": [
        preset_action_1_json
    ]}
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    print("TEST_14 --- POSTING ONE PRESET ACTION")
    preset_action_2_usage_id = 2
    preset_action_2_value = 1
    body = {
        "usage_id": preset_action_2_usage_id,
        "value": preset_action_2_value
    }
    preset_action_2 = PresetActionModel(1, preset_action_2_usage_id, preset_action_2_value)
    preset_action_2_json = preset_action_2.to_json()
    preset_action_2_json['id'] = 2
    expected_result = preset_action_2_json
    expected_status = 201
    uri = "groups/1/presets/1/preset_actions"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL PRESET_ACTIONS
    print("TEST_15 --- GETTING ALL PRESET_ACTIONS")
    uri = "groups/{}/presets/{}/preset_actions"\
        .format(preset_3['group_id'], preset_3['id'])
    expected_result = {
        "preset_actions": [
            preset_action_1_json
        ]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ALL PRESET_ACTIONS
    print("TEST_16 --- GETTING ALL PRESET_ACTIONS")
    uri = "groups/1/presets/1/preset_actions"
    expected_result = {
        "preset_actions": [
            preset_action_2_json
        ]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # DELETING ONE PRESET_ACTION
    print("TEST_17 --- GETTING ALL PRESET_ACTIONS - BAD REQUEST")
    uri = "groups/1/presets/1/preset_actions/12"
    expected_result = {"errors": [Error(
                "Could not find preset action with id: {}".format(12),
                "PresetActionModel.find_by_id({}) returned None".format(12),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}
    expected_status = 422
    test_delete(uri, {}, expected_result, expected_status)

    # DELETING ONE PRESET_ACTION
    print("TEST_18 --- GETTING ALL PRESET_ACTIONS - BAD REQUEST")
    uri = "groups/1/presets/2/preset_actions/2"
    expected_result = {"errors": [Error(
                "Could not find preset with id: {}".format(2),
                "PresetModel.find_by_id({}) returned None".format(2),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404").to_json()]}
    expected_status = 422
    test_delete(uri, {}, expected_result, expected_status)

    # DELETING ONE PRESET_ACTION
    print("TEST_19 --- GETTING ALL PRESET_ACTIONS - BAD REQUEST")
    uri = "groups/1/presets/1/preset_actions/2"
    expected_result = "Preset action with id: 2 was successfully deleted."
    expected_status = 200
    test_delete(uri, {}, expected_result, expected_status)
コード例 #6
0
def test_usage_resource():
    print("####################   TESTING USAGE RESOURCE   ####################")

    # GETTING ALL USAGES
    print("TEST_1 --- GETTING ALL USAGES")
    uri = "usages"
    expected_result = {
        "usages": []
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE USAGE
    print("TEST_2 --- POSTING ONE USAGE")
    item_1 = send_get('items/1')
    usage_1_item_id = item_1['id']
    usage_1_external_item_id = 1
    usage_1_consumption_type = UsageTypeEnum.KILOWATT
    usage_1_consumption_amount = 5
    usage_1_address = "http://127.0.0.1:5000/usages/1"
    usage_1_unit = UnitEnum.TOGGLE
    usage_1_min_value = 0
    usage_1_max_value = 1

    usage_1 = UsageModel(usage_1_item_id,
                         usage_1_external_item_id,
                         usage_1_consumption_type,
                         usage_1_consumption_amount,
                         usage_1_address,
                         usage_1_unit,
                         usage_1_min_value,
                         usage_1_max_value)
    body = {
        "item_id": usage_1_item_id,
        "external_item_id": usage_1_external_item_id,
        "consumption_type": usage_1_consumption_type.value,
        "consumption_amount": usage_1_consumption_amount,
        "address": usage_1_address,
        "unit": "TOGGLE",
        "min_value": usage_1_min_value,
        "max_value": usage_1_max_value
    }
    usage_1_json = usage_1.to_json()
    usage_1_json['id'] = 1
    usage_1_json['url'] = "127.0.0.1:5000/api/v1/usages/1"
    expected_result = usage_1_json
    expected_status = 201
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL USAGES
    print("TEST_3 --- GETTING ALL USAGES")
    uri = "usages"
    expected_result = {
        "usages": [usage_1_json]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ONE USAGE
    print("TEST_4 --- GETTING ONE USAGE")
    uri = "usages/1"
    expected_result = usage_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # UPDATING ONE USAGE
    print("TEST_5 --- UPDATING ONE USAGE")
    uri = 'usages/1'
    expected_result = usage_1_json
    expected_result['address'] = '127.0.0.1:5000/api/usages/7'
    body = expected_result
    expected_status = 200
    test_put(uri, body, expected_result, expected_status)
    usage_1_json['address'] = '127.0.0.1:5000/api/usages/7'

    # GETTING ONE USAGE
    print("TEST_6 --- GETTING ONE USAGE")
    uri = "usages/1"
    expected_result = usage_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_7 --- POSTING ONE USAGE - BAD REQUEST")
    item_1 = send_get('items/1')
    usage_1_item_id = item_1['id']
    usage_1_external_item_id = item_1['id']
    usage_1_consumption_type = UsageTypeEnum.KILOWATT
    usage_1_consumption_amount = 5
    usage_1_address = "http://127.0.0.1:5000/usage/1"
    usage_1_min_value = 0
    usage_1_max_value = 1

    body = {
        "item_id": usage_1_item_id,
        "external_item_id": usage_1_external_item_id,
        "consumption_type": usage_1_consumption_type.value,
        "consumption_amount": usage_1_consumption_amount,
        "address": usage_1_address,
        "unit": "FOGGLE",
        "min_value": usage_1_min_value,
        "max_value": usage_1_max_value
    }
    expected_result = {"errors": [Error(
                "{} is not a valid unit option.".format("FOGGLE"),
                "UnitEnum.has_value({}) returned False".format("FOGGLE"),
                422,
                "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_8 --- POSTING ONE USAGE - BAD REQUEST")
    body['unit'] = usage_1_unit.value
    body['consumption_type'] = 'KILO WHAT?'
    expected_result = {"errors": [Error(
                "{} is not a valid consumption type.".format("KILO WHAT?"),
                "UsageTypeEnum.has_value({}) returned False".format("KILO WHAT?"),
                422,
                "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_9 --- POSTING ONE USAGE - BAD REQUEST")
    body['consumption_type'] = usage_1_consumption_type.value
    body['address'] = '12'
    expected_result = {"errors": [Error(
                "Address must be at least 4 characters long.",
                "address was {} characters long".format(len(body['address'])),
                422,
                "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_10 --- POSTING ONE USAGE - BAD REQUEST")
    body['address'] = "________________________________________________________________________________________" \
                      "________________________________________________________________________________________" \
                      "________________________________________________________________________________________" \
                      "________________________________________________________________________________________" \
                      "________________________________________________________________________________________" \
                      "________________________________________________________________________________________" \
                      "________________________________________________________________________________________"
    expected_result = {"errors": [Error(
                "Address cannot be longer than 255 characters.",
                "address was {} characters long".format(len(body['address'])),
                422,
                "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_11 --- POSTING ONE USAGE - BAD REQUEST")
    body['address'] = usage_1_address
    body['consumption_amount'] = -1
    expected_result = {"errors": [Error(
                "Consumption amount cannot be below 0.",
                "{} is below 0".format(-1),
                422,
                "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_12 --- POSTING ONE USAGE - BAD REQUEST")
    body['consumption_amount'] = usage_1_consumption_amount
    body['min_value'] = None
    expected_result = {"errors": [Error(
            "If either min or max value is given, both should be given.",
            "Either min or max was None while the other was not.",
            422,
            "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE - BAD REQUEST
    print("TEST_13 --- POSTING ONE USAGE - BAD REQUEST")
    body['min_value'] = usage_1_min_value
    body['max_value'] = None
    expected_result = {"errors": [Error(
            "If either min or max value is given, both should be given.",
            "Either min or max was None while the other was not.",
            422,
            "https://en.wikipedia.org/wiki/HTTP_422").to_json()]}
    expected_status = 422
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)

    # EXECUTING COMMAND - BAD REQUEST
    print("TEST 14 --- POSTING COMMAND - BAD REQUEST")
    body = {}
    uri = "usages/1/command/2"
    expected_result = {"errors": Error(
                "New value does not fall within the expected range. ({} - {})"
                    .format(usage_1_json["min_value"], usage_1_json['max_value']),
                "{} is outside of ({} - {})".format(2, usage_1_json["min_value"], usage_1_json['max_value']),
                422,
                ""
            ).to_json()}
    expected_status = 422
    test_patch(uri, body, expected_result, expected_status)

    # POSTING ONE USAGE
    print("TEST_15 --- POSTING ONE USAGE")
    item_2 = send_get('items/2')
    usage_2_item_id = item_2['id']
    usage_2_external_item_id = 1
    usage_2_consumption_type = UsageTypeEnum.KILOWATT
    usage_2_consumption_amount = 5
    usage_2_address = "http://127.0.0.1:5000/usages/2"
    usage_2_unit = UnitEnum.TOGGLE
    usage_2_min_value = 0
    usage_2_max_value = 1

    usage_2 = UsageModel(usage_2_item_id,
                         usage_2_external_item_id,
                         usage_2_consumption_type,
                         usage_2_consumption_amount,
                         usage_2_address,
                         usage_2_unit,
                         usage_2_min_value,
                         usage_2_max_value)
    body = {
        "item_id": usage_2_item_id,
        "external_item_id": usage_2_external_item_id,
        "consumption_type": usage_2_consumption_type.value,
        "consumption_amount": usage_2_consumption_amount,
        "address": usage_2_address,
        "unit": "TOGGLE",
        "min_value": usage_2_min_value,
        "max_value": usage_2_max_value
    }
    usage_2_json = usage_2.to_json()
    usage_2_json['id'] = 2
    usage_2_json['url'] = "127.0.0.1:5000/api/v1/usages/2"
    expected_result = usage_2_json
    expected_status = 201
    uri = "usages"
    test_post(uri, body, expected_result, expected_status)
コード例 #7
0
def test_item_resource():
    print("####################   TESTING ITEM RESOURCE   ####################")

    # GETTING ALL ITEMS
    print("TEST_1 --- GETTING ALL ITEMS")
    uri = "items"
    expected_result = {
        "items": []
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_2 --- POSTING ONE ITEM")
    item_1_name = 'Z04 Gang lamp (SW)'
    item_1_comment = 'ETS import'
    item_1 = ItemModel(item_1_name, item_1_comment)
    body = {
        "name": item_1_name,
        "comment": item_1_comment
    }
    item_1_json = item_1.to_json()
    item_1_json['id'] = 1
    item_1_json['url'] = "127.0.0.1:5000/api/v1/items/1"
    expected_result = item_1_json
    expected_status = 201
    uri = "items"
    test_post(uri, body, expected_result, expected_status)

    # GETTING ALL ITEMS
    print("TEST_3 --- GETTING ALL ITEMS")
    uri = "items"
    expected_result = {
        "items": [item_1_json]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ONE ITEM
    print("TEST_4 --- GETTING ONE ITEM")
    uri = "items/1"
    expected_result = item_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # UPDATING ONE ITEM
    print("TEST_5 --- UPDATING ONE ITEM")
    uri = 'items/1'
    expected_result = item_1_json
    expected_result['comment'] = 'new_comment'
    body = {
        'name': item_1.name,
        'comment': 'new_comment'
    }
    expected_status = 200
    test_put(uri, body, expected_result, expected_status)
    item_1_json['comment'] = 'new_comment'

    # GETTING ONE ITEM
    print("TEST_6 --- GETTING UPDATED ITEM")
    uri = 'items/1'
    expected_result = item_1_json
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_7 --- POSTING ONE ITEM - BAD REQUEST")
    item_1_name = 'Z04 Gang lamp (SW)_______________________________________________________________' \
                  '_________________________________________________________________________________' \
                  '_________________________________________________________________________________' \
                  '_________________________________________________________________________________'
    item_1_comment = 'ETS import'
    item_1 = ItemModel(item_1_name, item_1_comment)
    body = {
        "name": item_1_name,
        "comment": item_1_comment
    }
    item_1_json = item_1.to_json()
    item_1_json['id'] = 2
    expected_result = {"errors": [
        Error(
            "Name cannot be longer than 255 characters.",
            "Name parameter cannot be longer than 255 characters.",
            400,
            "https://en.wikipedia.org/wiki/HTTP_400").to_json()
    ]}
    expected_status = 422
    uri = "items"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_8 --- POSTING ONE ITEM - BAD REQUEST")
    item_1_name = 'Z04 Gang lamp (SW)'
    item_1_comment = 'ETS import_______________________________________________________________' \
                     '_________________________________________________________________________________' \
                     '_________________________________________________________________________________' \
                     '_________________________________________________________________________________'
    item_1 = ItemModel(item_1_name, item_1_comment)
    body = {
        "name": item_1_name,
        "comment": item_1_comment
    }
    item_1_json = item_1.to_json()
    item_1_json['id'] = 2
    expected_result = {"errors": [
        Error(
            "Comment cannot be longer than 255 characters.",
            "Name parameter cannot be longer than 255 characters.",
            400,
            "https://en.wikipedia.org/wiki/HTTP_400").to_json()
    ]}
    expected_status = 422
    uri = "items"
    test_post(uri, body, expected_result, expected_status)

    # POSTING ONE ITEM
    print("TEST_9 --- POSTING ONE ITEM")
    item_2_name = 'Z04 Eetkamer lamp (SW)'
    item_2_comment = 'ETS import'
    item_2 = ItemModel(item_2_name, item_2_comment)
    body = {
        "name": item_2_name,
        "comment": item_2_comment
    }
    item_2_json = item_2.to_json()
    item_2_json['id'] = 2
    item_2_json['url'] = "127.0.0.1:5000/api/v1/items/2"
    expected_result = item_2_json
    expected_status = 201
    uri = "items"
    test_post(uri, body, expected_result, expected_status)
コード例 #8
0
def test_event_resource():
    print("####################   TESTING EVENT RESOURCE   ####################")

    # GETTING ALL USAGES
    print("TEST_1 --- GETTING ALL EVENTS")
    uri = "events"
    expected_result = {
        "events": []
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POST ONE EVENT
    print("TEST_2 --- POSTING ONE EVENT")
    uri = "events"
    usage_1 = send_get('usages/1')
    event_1 = EventModel(usage_1['id'], 'True', round(datetime.now().timestamp()))
    event_1_json = event_1.to_json()
    event_1_json['id'] = 1
    body = {
        "usage_id": usage_1['id'],
        "data_type": UnitEnum.TOGGLE.value,
        "data": 'True'
    }

    expected_result = event_1_json
    expected_status = 201

    test_post(uri, body, expected_result, expected_status)

    # GETTING ONE EVENT
    print("TEST_3 --- GETTING ONE EVENT")
    uri = "events/1"
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # GETTING ALL EVENTS
    print("TEST_4 --- GETTING ALL EVENTS")
    uri = "events"
    expected_result = {
        "events": [event_1_json]
    }
    expected_status = 200
    test_get(uri, expected_result, expected_status)

    # POST ONE EVENT
    print("TEST_5 --- POSTING ONE EVENT - BAD REQUEST")
    uri = "events"
    body = {
        "usage_id": 17,
        "data_type": UnitEnum.TOGGLE.value,
        "data": 'True'
    }
    error = Error(
                "Cannot find usage with id: {}".format(17),
                "UsageModel.find_by_id({}) returns None".format(17),
                404,
                "https://en.wikipedia.org/wiki/HTTP_404")
    expected_result = {"errors": [error.to_json()]}
    expected_status = 422
    test_post(uri, body, expected_result, expected_status)

    # POST ONE EVENT
    print("TEST_6 --- POSTING ONE EVENT - BAD REQUEST")
    uri = "events"
    body = {
        "usage_id": 1,
        "data_type": "KILO WHAT?",
        "data": 'True'
    }
    error = Error(
                '"{}" is not a valid unit type.'.format("KILO WHAT?"),
                "UnitEnum.has_value({}) returned False".format("KILO WHAT?"), 400,
                "https://en.wikipedia.org/wiki/HTTP_400"
            )
    expected_result = {"errors": [error.to_json()]}
    expected_status = 422
    test_post(uri, body, expected_result, expected_status)