-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsonhandler: allow reading yaml data from disk (#254)
allow reading yaml data from disk with YAMLDiskLoadingJSONHandler This commit aims to extend the jsonhandler to be able to read data from disk if it is a yaml file. Simply replacing the loads call with yaml.safe_load is not enough due to the nature of the NaN checker requiring an unsafe load[1]. closes #253 Tests with the same filename were failing when running the tests under pytest, see #255 To avoid running more than one response handler for the same type, use a set to track the handers we have available.
- Loading branch information
1 parent
ddce7d6
commit bc20dac
Showing
20 changed files
with
283 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
"""JSON-related content handling with YAML data disk loading.""" | ||
|
||
import yaml | ||
|
||
import six | ||
|
||
from gabbi.handlers import jsonhandler | ||
|
||
|
||
class YAMLDiskLoadingJSONHandler(jsonhandler.JSONHandler): | ||
"""A ContentHandler for JSON responses that loads YAML from disk | ||
* Structured test ``data`` is turned into JSON when request | ||
content-type is JSON. | ||
* Response bodies that are JSON strings are made into Python | ||
data on the test ``response_data`` attribute when the response | ||
content-type is JSON. | ||
* A ``response_json_paths`` response handler is added. Data read | ||
from disk during this handle will be loaded with the yaml.safe_load | ||
method to support both JSON and YAML data sources from disk. | ||
* JSONPaths in $RESPONSE substitutions are supported. | ||
""" | ||
|
||
@staticmethod | ||
def load_data_file(test, file_path): | ||
info = test.load_data_file(file_path) | ||
info = six.text_type(info, 'UTF-8') | ||
return yaml.safe_load(info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "cat", | ||
"sound": "meow" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"foo": {"bár": 1}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"type": "cat", | ||
"sound": "meow" | ||
}, | ||
{ | ||
"type": "dog", | ||
"sound": "woof" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo: | ||
bár: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- type: cat | ||
sound: meow | ||
- type: dog | ||
sound: woof |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
values: | ||
- pets: | ||
- type: cat | ||
sound: meow | ||
- type: dog | ||
sound: woof | ||
- people: | ||
- name: chris | ||
id: 1 | ||
- name: justin | ||
id: 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Test loading expected data from file on disk with JSONPath | ||
# | ||
defaults: | ||
method: POST | ||
url: /somewhere | ||
request_headers: | ||
content-type: application/json | ||
verbose: True | ||
|
||
tests: | ||
- name: yaml encoded value from disk | ||
data: <@data.json | ||
response_json_paths: | ||
$.foo['bár']: <@subdir/data.yaml:$.foo['bár'] | ||
|
||
- name: json encoded value from disk | ||
data: <@data.json | ||
response_json_paths: | ||
$.foo['bár']: <@data.json:$.foo['bár'] | ||
|
||
- name: yaml parital from disk | ||
data: <@cat.json | ||
response_json_paths: | ||
$: <@subdir/pets.yaml:$[?type = "cat"] | ||
|
||
- name: yaml partial both sides | ||
data: <@pets.json | ||
response_json_paths: | ||
$[?type = "cat"].sound: <@subdir/values.yaml:$.values[0].pets[?type = "cat"].sound |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
values: | ||
- pets: | ||
- type: cat | ||
sound: meow | ||
- type: dog | ||
sound: woof | ||
- people: | ||
- name: chris | ||
id: 1 | ||
- name: justin | ||
id: 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.