Skip to content

Commit e7102e8

Browse files
Merge pull request #812 from hugapi/feature/improve-test-coverage
Feature/improve test coverage
2 parents ddcc792 + b39787c commit e7102e8

File tree

8 files changed

+41
-21
lines changed

8 files changed

+41
-21
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ exclude_lines = def hug
77
sys.stdout.buffer.write
88
class Socket
99
pragma: no cover
10+
except ImportError:
11+
if MARSHMALLOW_MAJOR_VERSION is None or MARSHMALLOW_MAJOR_VERSION == 2:

hug/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ def version_router(
348348
self, request, response, api_version=None, versions=None, not_found=None, **kwargs
349349
):
350350
"""Intelligently routes a request to the correct handler based on the version being requested"""
351-
if versions is None:
352-
versions = {}
351+
versions = {} if versions is None else versions
353352
request_version = self.determine_version(request, api_version)
354353
if request_version:
355354
request_version = int(request_version)

hug/interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ def render_content(self, content, context, request, response, **kwargs):
854854
if size:
855855
response.set_stream(content, size)
856856
else:
857-
response.stream = content
857+
response.stream = content # pragma: no cover
858858
else:
859859
response.data = content
860860

hug/routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def allow_origins(
317317
response_headers = {}
318318
if origins:
319319

320-
@hug.response_middleware()
320+
@hug.response_middleware(api=self.route.get("api", None))
321321
def process_data(request, response, resource):
322322
if "ORIGIN" in request.headers:
323323
origin = request.headers["ORIGIN"]

tests/test_decorators.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,17 +1042,14 @@ def extend_with():
10421042
def extend_with():
10431043
return (tests.module_fake_http_and_cli,)
10441044

1045-
assert hug.test.cli("sub_api", "made_up_go", api=api)
1046-
10471045
# But not both
10481046
with pytest.raises(ValueError):
1049-
10501047
@hug.extend_api(sub_command="sub_api", command_prefix="api_", http=False)
10511048
def extend_with():
10521049
return (tests.module_fake_http_and_cli,)
10531050

10541051

1055-
def test_extending_api_with_http_and_cli():
1052+
def test_extending_api_with_http_and_cli_sub_module():
10561053
"""Test to ensure it's possible to extend the current API so both HTTP and CLI APIs are extended"""
10571054
import tests.module_fake_http_and_cli
10581055

@@ -1579,20 +1576,25 @@ def test_multiple_cli(ints: hug.types.Multiple[int]() = []):
15791576
assert hug.test.cli(test_multiple_cli, ints=["1", "2", "3"]) == [1, 2, 3]
15801577

15811578

1582-
def test_startup():
1579+
def test_startup(hug_api):
15831580
"""Test to ensure hug startup decorators work as expected"""
1581+
happened_on_startup = []
15841582

1585-
@hug.startup()
1583+
@hug.startup(api=hug_api)
15861584
def happens_on_startup(api):
1587-
pass
1585+
happened_on_startup.append("non-async")
15881586

1589-
@hug.startup()
1587+
@hug.startup(api=hug_api)
15901588
@asyncio.coroutine
15911589
def async_happens_on_startup(api):
1592-
pass
1590+
happened_on_startup.append("async")
1591+
1592+
assert happens_on_startup in hug_api.startup_handlers
1593+
assert async_happens_on_startup in hug_api.startup_handlers
15931594

1594-
assert happens_on_startup in api.startup_handlers
1595-
assert async_happens_on_startup in api.startup_handlers
1595+
hug_api._ensure_started()
1596+
assert "async" in happened_on_startup
1597+
assert "non-async" in happened_on_startup
15961598

15971599

15981600
@pytest.mark.skipif(sys.platform == "win32", reason="Currently failing on Windows build")

tests/test_routing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,16 @@ def test_prefixes(self):
406406
def test_suffixes(self):
407407
"""Test to ensure setting suffixes works as expected"""
408408
assert self.route.suffixes(".js", ".xml").route["suffixes"] == (".js", ".xml")
409+
410+
def test_allow_origins_request_handling(self, hug_api):
411+
"""Test to ensure a route with allowed origins works as expected"""
412+
route = URLRouter(api=hug_api)
413+
test_headers = route.allow_origins(
414+
"google.com", methods=("GET", "POST"), credentials=True, headers="OPTIONS", max_age=10
415+
)
416+
417+
@test_headers.get()
418+
def my_endpoint():
419+
return "Success"
420+
421+
assert hug.test.get(hug_api, "/my_endpoint", headers={'ORIGIN': 'google.com'}).data == "Success"

tests/test_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ def test_json():
320320
with pytest.raises(ValueError):
321321
hug.types.json("Invalid JSON")
322322

323+
assert hug.types.json(json.dumps(["a", "b"]).split(",")) == ["a", "b"]
324+
with pytest.raises(ValueError):
325+
assert hug.types.json(["Invalid JSON", "Invalid JSON"])
326+
323327

324328
def test_multi():
325329
"""Test to ensure that the multi type correctly handles a variety of value types"""

tox.ini

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ envlist=py{35,36,37,py3}-marshmallow{2,3}, cython-marshmallow{2,3}
55
deps=
66
-rrequirements/build_common.txt
77
marshmallow2: marshmallow <3.0
8-
marshmallow3: marshmallow >=3.0.0rc5
8+
marshmallow3: marshmallow==3.0.0rc6
99

1010
whitelist_externals=flake8
1111
commands=py.test --cov-report html --cov hug -n auto tests
1212

1313
[testenv:py37-black]
1414
deps=
1515
-rrequirements/build_style_tools.txt
16-
marshmallow >=3.0.0rc5
16+
marshmallow==3.0.0rc6
1717

1818
whitelist_externals=flake8
1919
commands=black --check --verbose -l 100 hug
2020

2121
[testenv:py37-vulture]
2222
deps=
2323
-rrequirements/build_style_tools.txt
24-
marshmallow >=3.0.0rc5
24+
marshmallow==3.0.0rc6
2525

2626
whitelist_externals=flake8
2727
commands=vulture hug --min-confidence 100 --ignore-names req_succeeded
@@ -30,23 +30,23 @@ commands=vulture hug --min-confidence 100 --ignore-names req_succeeded
3030
[testenv:py37-flake8]
3131
deps=
3232
-rrequirements/build_style_tools.txt
33-
marshmallow >=3.0.0rc5
33+
marshmallow==3.0.0rc6
3434

3535
whitelist_externals=flake8
3636
commands=flake8 hug
3737

3838
[testenv:py37-bandit]
3939
deps=
4040
-rrequirements/build_style_tools.txt
41-
marshmallow >=3.0.0rc5
41+
marshmallow==3.0.0rc6
4242

4343
whitelist_externals=flake8
4444
commands=bandit -r hug/ -ll
4545

4646
[testenv:py37-isort]
4747
deps=
4848
-rrequirements/build_style_tools.txt
49-
marshmallow >=3.0.0rc5
49+
marshmallow==3.0.0rc6
5050

5151
whitelist_externals=flake8
5252
commands=isort -c --diff --recursive hug

0 commit comments

Comments
 (0)