Skip to content

Commit

Permalink
Add test (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboulton authored Jun 16, 2024
1 parent 512eed6 commit 3113eab
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tavern/_core/dict_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
logger: logging.Logger = logging.getLogger(__name__)


def _check_and_format_values(to_format: str, box_vars: Mapping[str, Any]) -> str:
def _check_and_format_values(to_format: str, box_vars: Box) -> str:
formatter = string.Formatter()
would_format = formatter.parse(to_format)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.10-alpine

RUN pip3 install 'pyjwt>=2.4.0,<3' 'flask>=2.2.3'
RUN pip3 install 'pyjwt>=2.4.0,<3' 'flask>=2.2.3' "python-box>=6,<7"

ENV FLASK_DEBUG=1
ENV PYTHONUNBUFFERED=0
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from urllib.parse import unquote_plus, urlencode

import jwt
from box import Box
from flask import Flask, Response, jsonify, make_response, redirect, request, session
from itsdangerous import URLSafeTimedSerializer

Expand Down Expand Up @@ -471,6 +472,14 @@ def get_606_dict():
return jsonify({})


@app.route("/sub-path-query", methods=["POST"])
def sub_path_query():
r = request.get_json(force=True)
sub_path = r["sub_path"]

return jsonify({"result": Box(r, box_dots=True)[sub_path]})


@app.route("/magic-multi-method", methods=["GET", "POST", "DELETE"])
def get_any_method():
return jsonify({"method": request.method})
Expand Down
15 changes: 0 additions & 15 deletions tests/integration/test_env_var_format.tavern.yaml

This file was deleted.

31 changes: 31 additions & 0 deletions tests/integration/test_format.tavern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
test_name: Test getting format vars from environment variables

includes:
- !include common.yaml

stages:
- name: Make requests using environment variables
request:
url: "{tavern.env_vars.TEST_HOST}/{first_part}/{second_part}"
method: GET
response:
status_code: 200
json:
status: OK

---
test_name: Test slicing request vars

stages:
- name: Make request and expect part of list in it to be returned
request:
url: "{global_host}/sub-path-query"
method: POST
json:
sub_path: "a.b[0].c"
"a": { "b": [{ "c": 3 }] }
response:
status_code: 200
json:
result: !int "{tavern.request_vars.json.a.b[0].c}"
41 changes: 36 additions & 5 deletions tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import _pytest
import pytest
import yaml
from box import Box

from tavern._core import exceptions
from tavern._core.dict_util import _check_and_format_values, format_keys
Expand Down Expand Up @@ -291,26 +292,56 @@ def test_validate_schema_incorrect(self, nested_response):


class TestCheckParseValues:
@pytest.mark.parametrize(
"item", [[134], {"a": 2}, yaml, yaml.load, yaml.SafeLoader]
)
@pytest.mark.parametrize("item", [yaml, yaml.load, yaml.SafeLoader])
def test_warns_bad_type(self, item):
with patch("tavern._core.dict_util.logger.warning") as wmock:
_check_and_format_values("{fd}", {"fd": item})
_check_and_format_values("{fd}", Box({"fd": item}))

wmock.assert_called_with(
"Formatting '%s' will result in it being coerced to a string (it is a %s)",
"fd",
type(item),
)

@pytest.mark.parametrize(
"item",
[
[134],
{"a": 2},
],
)
def test_warns_bad_type_box(self, item):
box = Box({"fd": item})
with patch("tavern._core.dict_util.logger.warning") as wmock:
_check_and_format_values("{fd}", box)

wmock.assert_called_with(
"Formatting '%s' will result in it being coerced to a string (it is a %s)",
"fd",
type(box["fd"]),
)

@pytest.mark.parametrize("item", [1, "a", 1.3, format_keys("{s}", {"s": 2})])
def test_no_warn_good_type(self, item):
with patch("tavern._core.dict_util.logger.warning") as wmock:
_check_and_format_values("{fd}", {"fd": item})
_check_and_format_values("{fd}", Box({"fd": item}))

assert not wmock.called

def test_format_with_array_access(self):
"""Test accessing using array indexing format"""

assert "hello" == _check_and_format_values(
"{a.b.c[0].d}", Box({"a": {"b": {"c": [{"d": "hello"}]}}})
)

def test_format_with_dict_access(self):
"""Test accessing using dict indexing format"""

assert "hello" == _check_and_format_values(
"{a[b].c[0].d}", Box({"a": {"b": {"c": [{"d": "hello"}]}}})
)


class TestFormatWithJson:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 3113eab

Please sign in to comment.