Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Commit

Permalink
add TestNormalizeBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
oliland committed May 5, 2018
1 parent d491f1d commit b88aa63
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions test/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,58 @@

import pytest

from urllib3.backends import Backend as UserSpecifiedBackend
from urllib3._backends._loader import load_backend
import urllib3
from urllib3.backends import Backend
from urllib3._backends._loader import normalize_backend, load_backend


class TestLoadBackend(object):
requires_async_pool_manager = pytest.mark.skipif(
not hasattr(urllib3, "AsyncPoolManager"),
reason="async backends require AsyncPoolManager",
)


requires_sync_pool_manager = pytest.mark.skipif(
hasattr(urllib3, "AsyncPoolManager"),
reason="sync backends cannot be used with AsyncPoolManager",
)


class TestNormalizeBackend(object):
"""
We assert that we are able to import compatible backends,
and that we fail correctly if we attempt to use an unavailable or unknown backend.
Assert that we fail correctly if we attempt to use an unknown or incompatible backend.
"""
def test_dummy(self):
with pytest.raises(ImportError):
load_backend("dummy")
def test_unknown(self):
with pytest.raises(ValueError):
normalize_backend("_unknown")

@requires_sync_pool_manager
def test_sync(self):
load_backend("sync")
load_backend(UserSpecifiedBackend("sync"))
assert normalize_backend(Backend("sync")) == Backend("sync")
assert normalize_backend("sync") == Backend("sync")
assert normalize_backend(None) == Backend("sync")

@pytest.mark.skipif(
sys.version_info < (3, 5),
reason="async backends require Python 3.5 or greater",
)
@requires_async_pool_manager
def test_async(self):
load_backend("trio")
load_backend("twisted")
assert normalize_backend(Backend("trio")) == Backend("trio")
assert normalize_backend("twisted") == Backend("twisted")

with pytest.raises(ValueError):
normalize_backend(Backend("sync"))

from twisted.internet import reactor
assert normalize_backend(Backend("twisted", reactor=reactor)) == Backend("twisted", reactor=reactor)


class TestLoadBackend(object):
"""
Assert that we can load a normalized backend
"""
@requires_sync_pool_manager()
def test_sync(self):
load_backend(normalize_backend("sync"))

@requires_async_pool_manager()
def test_async(self):
from twisted.internet import reactor
load_backend(UserSpecifiedBackend("twisted", reactor=reactor))
load_backend(Backend("twisted", reactor=reactor))

0 comments on commit b88aa63

Please sign in to comment.