Skip to content

Commit

Permalink
start pytest migration to modern pytest with conftest and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Psotta committed Feb 6, 2021
1 parent 9504c36 commit 07265e5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
48 changes: 48 additions & 0 deletions test/conftest.py
@@ -0,0 +1,48 @@
import unittest
import codecs
import openrouteservice

from urllib.parse import urlparse, parse_qsl

# For relative imports to work in Python 3.6
import os
import sys
import pytest

sys.path.append(os.path.dirname(os.path.realpath(__file__)))


class TestCase(unittest.TestCase):
def setUp(self):
self.key = "sample_key"
self.client = openrouteservice.Client(self.key)

def assertURLEqual(self, first, second, msg=None):
"""Check that two arguments are equivalent URLs. Ignores the order of
query arguments.
"""
first_parsed = urlparse(first)
second_parsed = urlparse(second)
self.assertEqual(first_parsed[:3], second_parsed[:3], msg)

first_qsl = sorted(parse_qsl(first_parsed.query))
second_qsl = sorted(parse_qsl(second_parsed.query))
self.assertEqual(first_qsl, second_qsl, msg)

@staticmethod
def u(string):
"""Create a unicode string, compatible across all versions of Python."""
# NOTE(cbro): Python 3-3.2 does not have the u'' syntax.
return codecs.unicode_escape_decode(string)[0]

def assertDictContainsSubset(self, a, b, **kwargs):
"""Replaces deprecated unittest.TestCase.assertDictContainsSubset"""
c = dict([(k, b[k]) for k in a.keys() if k in b.keys()])
self.assertEqual(a, c)


@pytest.fixture(scope="function")
def simpletestcase():
test_case = TestCase()
test_case.setUp()
yield test_case
12 changes: 6 additions & 6 deletions test/test_client.py
Expand Up @@ -26,12 +26,13 @@
import test as _test


class ClientTest(_test.TestCase):
def test_no_api_key(self):
with self.assertRaises(ValueError):
client = openrouteservice.Client()
client.directions(PARAM_LINE)
def test_no_api_key(simpletestcase):
with simpletestcase.assertRaises(ValueError):
client = openrouteservice.Client()
client.directions(PARAM_LINE)


class ClientTest(_test.TestCase):
def test_invalid_api_key(self):
with self.assertRaises(openrouteservice.exceptions.ApiError):
client = openrouteservice.Client(key="Invalid key.")
Expand Down Expand Up @@ -136,7 +137,6 @@ def test_dry_run(self):

@responses.activate
def test_no_get_parameter(self):

responses.add(
responses.POST,
"https://api.openrouteservice.org/directions",
Expand Down
42 changes: 24 additions & 18 deletions test/test_isochrones.py
Expand Up @@ -16,26 +16,32 @@
#
"""Tests for the distance matrix module."""
import responses
import test as _test
from test.test_helper import PARAM_POINT, ENDPOINT_DICT
from test.test_helper import ENDPOINT_DICT
import pytest


class IsochronesTest(_test.TestCase):
@responses.activate
def test_isochrones(self):
query = ENDPOINT_DICT["isochrones"]
@pytest.mark.parametrize(
"parameter, value",
[
("smoothing", True),
("smoothing", False),
],
)
@responses.activate
def test_isochrones(simpletestcase, parameter, value):
query = ENDPOINT_DICT["isochrones"]

responses.add(
responses.POST,
"https://api.openrouteservice.org/v2/isochrones/{}/geojson".format(
query["profile"]
),
json=query,
status=200,
content_type="application/json",
)
responses.add(
responses.POST,
"https://api.openrouteservice.org/v2/isochrones/{}/geojson".format(
query["profile"]
),
json=query,
status=200,
content_type="application/json",
)

resp = self.client.isochrones(**query)
resp = simpletestcase.client.isochrones(**query)

self.assertEqual(1, len(responses.calls))
self.assertEqual(resp, query)
simpletestcase.assertEqual(1, len(responses.calls))
simpletestcase.assertEqual(resp, query)

0 comments on commit 07265e5

Please sign in to comment.