def _test_expression(given, expression, expected, filename): parsed = jmespath.compile(expression) actual = parsed.search(given) expected_repr = json.dumps(expected, indent=4) actual_repr = json.dumps(actual, indent=4) error_msg = ("\n(%s) The expression '%s' was suppose to give: %s.\n" "Instead it matched: %s" % (filename, expression, expected_repr, actual_repr)) assert_equal(actual, expected, error_msg)
def _test_expression(given, expression, expected, filename): try: parsed = jmespath.compile(expression) except ValueError as e: raise AssertionError( 'jmespath expression failed to compile: "%s", error: %s"' % (expression, e)) actual = parsed.search(given) expected_repr = json.dumps(expected, indent=4) actual_repr = json.dumps(actual, indent=4) error_msg = ("\n\n (%s) The expression '%s' was suppose to give: %s.\n" "Instead it matched: %s\nparsed as:\n%s" % ( filename, expression, expected_repr, actual_repr, parsed)) error_msg = error_msg.replace(r'\n', '\n') assert_equal(actual, expected, error_msg)
def _test_expression(given, expression, expected, filename): import jmespath.parser try: parsed = jmespath.compile(expression) except ValueError as e: raise AssertionError( 'jmespath expression failed to compile: "%s", error: %s"' % (expression, e)) actual = parsed.search(given) expected_repr = json.dumps(expected, indent=4) actual_repr = json.dumps(actual, indent=4) error_msg = ("\n\n (%s) The expression '%s' was suppose to give:\n%s\n" "Instead it matched:\n%s\nparsed as:\n%s\ngiven:\n%s" % (filename, expression, expected_repr, actual_repr, parsed, json.dumps(given, indent=4))) error_msg = error_msg.replace(r'\n', '\n') assert_equal(actual, expected, error_msg)
def setUp(self): data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": TEST_INSTANCE_NUMBER } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data))
def load_cases(full_path): all_test_data = json.load(open(full_path), object_pairs_hook=OrderedDict) for test_data in all_test_data: given = test_data['given'] for case in test_data['cases']: if 'result' in case: test_type = 'result' elif 'error' in case: test_type = 'error' elif 'bench' in case: test_type = 'bench' else: raise RuntimeError("Unknown test type: %s" % json.dumps(case)) yield (given, test_type, case)
def setUp(self): # Ensures an already existing valid input means 403 error data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": 1 } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data))
def test_config_response(self): # Ensures valid input means 201 data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": TEST_INSTANCE_NUMBER } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) res = response.json() self.assertEqual(response.status_code, 201) self.assertEqual(res['message'], 'Create Success')
def test_config_existing_response(self): # Ensures an already existing valid input means 403 error data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": 1 } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) res = response.json() self.assertEqual(response.status_code, 403) self.assertEqual(res['message'], 'Config already exists! Please update instead')
def test_Update_config_non_existing_response(self): # Ensures an non existing valid input means 404 error data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": 333333 } response = requests.patch(BASE_URL + '/config/update', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) res = response.json() self.assertEqual(response.status_code, 404) self.assertEqual(res['message'], "Config for {} not found".format(data["api_name"]))