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]]))
def then_we_get_text_in_response_field(context, field): response = get_json_data(context.response)[field] # Remove blank lines to make testing easier response_text = '\n'.join([ line for line in response.split('\n') if len(line) ]) assert context.text.strip() in response_text, '"{}" not in "{}"'.format( context.text.strip(), response_text )
def then_we_get_formatted_items(context, total_count): assert_200(context.response) data = get_json_data(context.response) int_count = int(total_count.replace('+', '').replace('<', '')) if '+' in total_count: assert int_count <= data['_meta']['total'], '%d items is not enough' % data['_meta']['total'] elif total_count.startswith('<'): assert int_count > data['_meta']['total'], '%d items is too much' % data['_meta']['total'] else: assert int_count == data['_meta']['total'], 'got %d: %s' % (data['_meta']['total'], format_items(data['_items'])) if context.text: test_json(context, ['formatted_item'])
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)
def then_we_get_formatted_items(context, total_count): assert_200(context.response) data = get_json_data(context.response) int_count = int(total_count.replace("+", "").replace("<", "")) if "+" in total_count: assert int_count <= data["_meta"][ "total"], "%d items is not enough" % data["_meta"]["total"] elif total_count.startswith("<"): assert int_count > data["_meta"][ "total"], "%d items is too much" % data["_meta"]["total"] else: assert int_count == data["_meta"]["total"], "got %d: %s" % ( data["_meta"]["total"], format_items(data["_items"]), ) if context.text: test_json(context, ["formatted_item"])
def step_impl_then_get_charts(context, total_count): assert_200(context.response) data = get_json_data(context.response) int_count = int(total_count) report = (data.get('_items') or [{}])[0] num_reports = len(report.get('highcharts')) assert int_count == num_reports, 'Number of charts not equal. {} != {}'.format( int_count, num_reports) 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 report = (response_data.get('_items') or [{}])[0] context_data = json.loads(apply_placeholders(context, context.text)) chart_index = 0 for context_chart in context_data: response_chart = report.get('highcharts')[chart_index] for key, value in context_chart.items(): if isinstance(value, dict): for subkey, subvalue in value.items(): assert response_chart[key][subkey] == subvalue,\ 'chart[{}][{}][{}] {} != {}'.format( chart_index, key, subkey, subvalue, response_chart[key][subkey] ) else: assert response_chart[ key] == value, 'chart[{}][{}] {} != {}'.format( chart_index, key, value, response_chart[key]) chart_index += 1 return response_data
def step_imp_store_item_from_patch(context, tag): data = get_json_data(context.response) setattr(context, tag, data)
def steip_impl_store_indexed_item_to_ctx(context, tag, index): data = get_json_data(context.response) item = data['_items'][int(index) - 1] setattr(context, tag, item)
def steip_impl_store_first_item_to_ctx(context, tag): data = get_json_data(context.response) first_item = data['_items'][0] setattr(context, tag, first_item)
def step_impl_list(context, total_count): step_impl_then_get_existing(context) data = get_json_data(context.response) assert len(data['_items']) == int(total_count), len(data['_items'])
def then_we_get_text_in_response_field(context, field): response = get_json_data(context.response)[field] assert context.text in response, response
def step_impl_then_save_event_id(context): items = get_json_data(context.response)['_items'] set_placeholder(context, 'EVENT_TO_PATCH', str(items[0]['guid']))
def then_item_has_current_date(context): response = get_json_data(context.response) assert "planning_date" in response, 'planning_date field not defined' response_date_time = datetime.strptime(response["planning_date"], DATETIME_FORMAT) assert response_date_time.date() == get_local_end_of_day(context).date(), 'Planning Item has not got current date'
def then_we_store_assignment_id(context, index): index = int(index) response = get_json_data(context.response) assert len(response.get('coverages')), 'Coverage are not defined.' coverage = response.get('coverages')[index] assert not coverage.get('assigned_to', {}).get('assignment_id'), 'Coverage has an assignment'
def step_imp_store_last_duplicate_item(context, tag): data = get_json_data(context.response) new_id = data['duplicate_to'][-1] setattr(context, tag, {'id': new_id})
def step_store_next_page_from_response(context): data = get_json_data(context.response) href = ((data.get('_links') or {}).get('next_page') or {}).get('href') assert href, data set_placeholder(context, 'NEXT_PAGE', href)
def step_impl_then_get_list_without_ntb_id(context): items = get_json_data(context.response)['_items'] for item in items: assert 'ntb_id' not in item