Example #1
0
def then_get_transmitted_item(context, path):
    path = apply_placeholders(context, path)
    assert os.path.isfile(path), '{} is not a file'.format(path)
    with open(path, 'r') as json_file:
        data = json.load(json_file)
        json_file.close()
    context_data = json.loads(apply_placeholders(context, context.text))
    json_match(context_data, data)
Example #2
0
def step_assert_response_header(context):
    test_headers = json.loads(apply_placeholders(context, context.text))
    response_headers = context.response.headers
    headers_dict = {}

    for h in response_headers:
        headers_dict[h[0]] = h[1]

    for t_h in test_headers:
        json_match(t_h, headers_dict)
Example #3
0
def then_we_pushed_x_items(context, count):
    history = context.http_mock.request_history
    assert count == len(history), 'there were %d calls' % (len(history), )
    if context.text:
        context_data = json.loads(apply_placeholders(context, context.text))
        for i, _ in enumerate(context_data):
            assert_equal(json_match(context_data[i], history[i].json()), True,
                         msg='item[%d]: %s' % (i, history[i]))
Example #4
0
def then_we_get_formatted_item(context):
    assert_200(context.response)
    try:
        response_data = json.loads(context.response.get_data())
        formatted_item = json.loads(response_data.get('formatted_item', ''))
    except Exception:
        fail_and_print_body(context.response, 'response does not contain a valid formatted_item field')
    context_data = json.loads(apply_placeholders(context, context.text))
    assert_equal(json_match(context_data, formatted_item), True,
                 msg=str(context_data) + '\n != \n' + str(formatted_item))
Example #5
0
def then_we_get_array_of_by(context, field, fid):
    response = get_json_data(context.response)
    assert field in response, '{} field not defined'.format(field)
    assert len(response.get(field)), '{} field not defined'.format(field)
    context_data = json.loads(apply_placeholders(context, context.text))

    for row in response[field]:
        if row[fid] not in context_data.keys():
            continue

        assert_equal(json_match(context_data[row[fid]], row),
                     True,
                     msg=str(row) + '\n != \n' + str(context_data[row[fid]]))
Example #6
0
def step_impl_then_we_get_config(context, report_id):
    assert_200(context.response)

    if not context.text:
        return

    data = get_json_data(context.response)

    config = next(
        (c for c in (data.get('_items') or []) if c.get('_id') == report_id),
        None)

    expected_config = json.loads(apply_placeholders(context, context.text))
    assert_equal(json_match(expected_config, config), True)
Example #7
0
def step_impl_then_get_stats_for_item(context):
    assert_200(context.response)

    if context.text:
        try:
            response_data = json.loads(context.response.get_data())
        except Exception:
            fail_and_print_body(context.response, 'response is not valid json')
            return

        stats = response_data.get('stats') or {}
        context_stats = json.loads(apply_placeholders(context, context.text))

        # parent stat entries (i.e. timeline, desk_transitions, featuremedia_updates)
        for stat_type, stat_entries in context_stats.items():
            assert stat_type in stats.keys(), 'stats.{} does not exist'.format(
                stat_type)

            if stat_entries is None:
                assert stats[stat_type] is None, 'stats.{} is not empty'.format(
                    stat_type)
                continue
            elif stats[stat_type] is None:
                assert stat_entries == stats[
                    stat_type], 'stats.{} {} != {}'.format(
                        stat_type, stats[stat_type], stat_entries)

            assert len(stat_entries) == len(stats[stat_type]),\
                'stats.{}. len {} != {}.\nStats={}'.format(
                stat_type,
                len(stat_entries),
                len(stats[stat_type]),
                stats[stat_type]
            )

            stat_index = 0
            for stat_entry in stat_entries:
                expected_stats = stats[stat_type][stat_index]

                for key, value in stat_entry.items():
                    assert key in expected_stats.keys(
                    ), 'stats.{}[{}] key "{}" not found'.format(
                        stat_type, stat_index, key)

                    if isinstance(value, dict):
                        for subkey, subvalue in value.items():
                            assert subkey in expected_stats[key].keys(),\
                                'stats.{}[{}][{}] key "{}" not found.\nEntry={}'.format(
                                stat_type,
                                stat_index,
                                key,
                                subkey,
                                expected_stats[key]
                            )
                            assert json_match(subvalue, expected_stats[key][subkey]),\
                                'stats.{}[{}].{}.{} {} != {}\nEntry={}'.format(
                                stat_type,
                                stat_index,
                                key,
                                subkey,
                                expected_stats[key][subkey],
                                subvalue,
                                expected_stats[key]
                            )
                    else:
                        assert expected_stats[
                            key] == value, 'stats.{}[{}].{} {} != {}'.format(
                                stat_type, stat_index, key,
                                expected_stats[key], value)

                stat_index += 1

        return response_data