diff options
author | Ian Cordasco <graffatcolmingov@gmail.com> | 2015-10-04 13:47:55 -0500 |
---|---|---|
committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2015-10-04 13:48:41 -0500 |
commit | 2ecd303d0a52895f263cd775a3113b4467062b10 (patch) | |
tree | 3a5ce01beeb3338893ef1b7e7972dd9bbbfd9d47 | |
parent | 224c5f7db47ab55b30632d60ea84e7a69b8a95bd (diff) | |
download | uritemplates-2ecd303d0a52895f263cd775a3113b4467062b10.tar.gz |
Clean up and actually run tests from fixtures
-rw-r--r-- | MANIFEST.in | 6 | ||||
-rw-r--r--[-rwxr-xr-x] | setup.py | 6 | ||||
-rw-r--r-- | test_from_uritemplatepy.py | 191 | ||||
-rw-r--r-- | tests/fixtures/README.md (renamed from testfiles/README.md) | 0 | ||||
-rw-r--r-- | tests/fixtures/extended-tests.json (renamed from testfiles/extended-tests.json) | 0 | ||||
-rw-r--r-- | tests/fixtures/json2xml.xslt (renamed from testfiles/json2xml.xslt) | 0 | ||||
-rw-r--r-- | tests/fixtures/negative-tests.json (renamed from testfiles/negative-tests.json) | 0 | ||||
-rw-r--r-- | tests/fixtures/spec-examples-by-section.json (renamed from testfiles/spec-examples-by-section.json) | 0 | ||||
-rw-r--r-- | tests/fixtures/spec-examples.json (renamed from testfiles/spec-examples.json) | 0 | ||||
-rw-r--r-- | tests/fixtures/transform-json-tests.xslt (renamed from testfiles/transform-json-tests.xslt) | 0 | ||||
-rw-r--r-- | tests/test_from_fixtures.py | 119 | ||||
-rw-r--r-- | tests/test_uritemplate.py (renamed from test_uritemplate.py) | 0 | ||||
-rw-r--r-- | tox.ini | 4 |
13 files changed, 131 insertions, 195 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index c36f0c7..0c06e4c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,3 +2,9 @@ include README.rst include HISTORY.rst include LICENSE include AUTHORS.rst + +recursive-include docs * +recursive-include tests * + +prune docs/_build +global-exclude *.py[cdo] __pycache__ *.so *.pyd @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import sys import os @@ -7,7 +5,7 @@ packages = [ 'uritemplate' ] -from distutils.core import setup +from setuptools import setup if sys.argv[-1] in ("submit", "publish"): os.system("python setup.py sdist upload") @@ -39,6 +37,8 @@ setup( 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: Implementation :: CPython', ], ) diff --git a/test_from_uritemplatepy.py b/test_from_uritemplatepy.py deleted file mode 100644 index c48e46b..0000000 --- a/test_from_uritemplatepy.py +++ /dev/null @@ -1,191 +0,0 @@ -# these tests are derived from the uritemplate-py project -# https://raw.github.com/uri-templates/uritemplate-py/master/test/uritemplate_test.py -# modified to run correctly here -# -# Copyright 2011-2012 The Authors -# -# 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. - -import json -import os -import re -from unittest import TestCase, main - -from uritemplate import URITemplate, expand - - -class TestJSONFromUritemplatePy(TestCase): - # test methods based on JSON data are attached to this class at - # import time - pass - - -nopunc = re.compile(r'[\W_]', re.MULTILINE) - - -def python_safe_name(s): - """ - Return a name safe to use for a python function from string s. - """ - s = s.strip() - s = s.lower() - s = re.sub(nopunc, ' ', s) - s = s.strip() - s = '_'.join(s.split()) - return str(s) - - -def make_test(test_name, template, variables, expected): - def json_test_function(self): - actual = unicode(expand(template, variables)) - msg = ('%(template)r did not expand as expected, got %(actual)r.' % - locals()) - if isinstance(expected, list): - self.assertTrue(actual in expected, msg) - else: - self.assertEqual(expected, actual) - json_test_function.__name__ = test_name - json_test_function.funcname = test_name - return json_test_function - - -def build_tests_from_json(data_set, test_class=TestJSONFromUritemplatePy): - """ - Build test methods from data_set and attach these to test_class. - """ - with open(data_set) as fin: - testdata = json.load(fin) - - # loop through all suite/cases and attach a test method to our test class - for test_name, suite in testdata.items(): - variables = suite['variables'] - testcases = suite['testcases'] - for i, testcase in enumerate(testcases): - # build a nice test method name - fun_name = 'test_from_uritemplatepy %(test_name)s %(i)d' % locals() - safe_name = python_safe_name(fun_name) - template = testcase[0] - expected = testcase[1] - # closure on the test params - test_method = make_test(safe_name, template, variables, expected) - # attach that method to the class - setattr(test_class, safe_name, test_method) - - -for jds in os.listdir('testfiles'): - if not jds.endswith('.json'): - continue - pth = os.path.join('testfiles', jds) - build_tests_from_json(pth) - - -############################# -# these tests are from the uritemplate-py project -# https://raw.github.com/uri-templates/uritemplate-py/master/test/variables_test.py -# modified to run correctly here -# -# Copyright 2011-2012 The Authors -# -# 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. - - -class TestVariablesFromUritemplatePy(TestCase): - - def get_vars(self, template): - for vr in URITemplate(template).variables: - for n in vr.variable_names: - yield n - - def test_simple(self): - template = 'http://example.com/{x,y}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y']), vrs) - - def test_simple2(self): - template = 'http://example.com/{x,y}/{z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_reserved(self): - template = 'http://example.com/{+x,y}/{+z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_fragment(self): - template = 'http://example.com/{#x,y},{#z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_label(self): - template = 'http://{.x,y,z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_path_segment(self): - template = 'http://example.com{/x,y}/w{/z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_parameter(self): - template = 'http://example.com{;x,y}{;z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_query(self): - template = 'http://example.com{?x,y,z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_query_continuation(self): - template = 'http://example.com?a=1&b=2{&x,y}&r=13{&z}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_prefix_modifier(self): - template = 'http://example.com{/x:5,y:7}{/z:2}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_explode_modifier(self): - template = 'http://example.com{/x*,y*}/page{/z*}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['x', 'y', 'z']), vrs) - - def test_mixed_expansion_types(self): - template = 'http://{a,b}.com{;c,d}{/e,f}/page{?g,h}{&i,j}{#k,l}' - vrs = set(self.get_vars(template)) - self.assertEquals(set('abcdefghijkl'), vrs) - - def test_overlapping_expansion(self): - template = 'http://{a,b}.com{;a,b}{/a,b}/page{?a,b}{&a,b}{#a,b}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['a', 'b']), vrs) - - def test_partially_overlapping(self): - template = 'http://{.a,b}{/b,c}/{c,d}' - vrs = set(self.get_vars(template)) - self.assertEquals(set(['a', 'b', 'c', 'd']), vrs) - - -if __name__ == '__main__': - main() diff --git a/testfiles/README.md b/tests/fixtures/README.md index 11dcddb..11dcddb 100644 --- a/testfiles/README.md +++ b/tests/fixtures/README.md diff --git a/testfiles/extended-tests.json b/tests/fixtures/extended-tests.json index fd69744..fd69744 100644 --- a/testfiles/extended-tests.json +++ b/tests/fixtures/extended-tests.json diff --git a/testfiles/json2xml.xslt b/tests/fixtures/json2xml.xslt index 59b3548..59b3548 100644 --- a/testfiles/json2xml.xslt +++ b/tests/fixtures/json2xml.xslt diff --git a/testfiles/negative-tests.json b/tests/fixtures/negative-tests.json index 552a6bf..552a6bf 100644 --- a/testfiles/negative-tests.json +++ b/tests/fixtures/negative-tests.json diff --git a/testfiles/spec-examples-by-section.json b/tests/fixtures/spec-examples-by-section.json index 5aef182..5aef182 100644 --- a/testfiles/spec-examples-by-section.json +++ b/tests/fixtures/spec-examples-by-section.json diff --git a/testfiles/spec-examples.json b/tests/fixtures/spec-examples.json index 2e8e942..2e8e942 100644 --- a/testfiles/spec-examples.json +++ b/tests/fixtures/spec-examples.json diff --git a/testfiles/transform-json-tests.xslt b/tests/fixtures/transform-json-tests.xslt index d956b6b..d956b6b 100644 --- a/testfiles/transform-json-tests.xslt +++ b/tests/fixtures/transform-json-tests.xslt diff --git a/tests/test_from_fixtures.py b/tests/test_from_fixtures.py new file mode 100644 index 0000000..f4ae1b8 --- /dev/null +++ b/tests/test_from_fixtures.py @@ -0,0 +1,119 @@ +import json +import os.path + +import pytest + +import uritemplate + + +def fixture_file_path(filename): + absolute_dir = os.path.abspath(os.path.dirname(__file__)) + filename = filename + '.json' + return os.path.join(absolute_dir, 'fixtures', filename) + + +def load_examples(filename): + path = fixture_file_path(filename) + with open(path, 'r') as examples_file: + examples = json.load(examples_file) + return examples + + +def expected_set(expected): + if isinstance(expected, list): + return set(expected) + return set([expected]) + + +class FixtureMixin(object): + def _get_test(self, section): + test = self.examples.get(section, {}) + return test.get('variables', {}), test.get('testcases', []) + + def _test(self, testname): + variables, testcases = self._get_test(testname) + for template, expected in testcases: + expected = expected_set(expected) + expanded = uritemplate.expand(template, variables) + assert expanded in expected + + +class TestSpecExamples(FixtureMixin): + examples = load_examples('spec-examples') + + def test_level_1(self): + """Check that uritemplate.expand matches Level 1 expectations.""" + self._test('Level 1 Examples') + + def test_level_2(self): + """Check that uritemplate.expand matches Level 2 expectations.""" + self._test('Level 2 Examples') + + def test_level_3(self): + """Check that uritemplate.expand matches Level 3 expectations.""" + self._test('Level 3 Examples') + + def test_level_4(self): + """Check that uritemplate.expand matches Level 4 expectations.""" + self._test('Level 4 Examples') + + +class TestSpecExamplesByRFCSection(FixtureMixin): + examples = load_examples('spec-examples-by-section') + + def test_variable_expansion(self): + """Check variable expansion.""" + self._test('3.2.1 Variable Expansion') + + def test_simple_string_expansion(self): + """Check simple string expansion.""" + self._test('3.2.2 Simple String Expansion') + + def test_reserved_expansion(self): + """Check reserved expansion.""" + self._test('3.2.3 Reserved Expansion') + + def test_fragment_expansion(self): + """Check fragment expansion.""" + self._test('3.2.4 Fragment Expansion') + + def test_dot_prefixed_label_expansion(self): + """Check label expansion with dot-prefix.""" + self._test('3.2.5 Label Expansion with Dot-Prefix') + + def test_path_segment_expansion(self): + """Check path segment expansion.""" + self._test('3.2.6 Path Segment Expansion') + + def test_path_style_parameter_expansion(self): + """Check path-style param expansion.""" + self._test('3.2.7 Path-Style Parameter Expansion') + + def test_form_style_query_expansion(self): + """Check form-style query expansion.""" + self._test('3.2.8 Form-Style Query Expansion') + + def test_form_style_query_cntinuation(self): + """Check form-style query continuation.""" + self._test('3.2.9 Form-Style Query Continuation') + + +class TestExtendedTests(FixtureMixin): + examples = load_examples('extended-tests') + + @pytest.mark.xfail(reason='See bug #17') + def test_additional_examples_1(self): + """Check Additional Examples 1.""" + self._test('Additional Examples 1') + + def test_additional_examples_2(self): + """Check Additional Examples 2.""" + self._test('Additional Examples 2') + + def test_additional_examples_3(self): + """Check Additional Examples 3.""" + self._test('Additional Examples 3: Empty Variables') + + def test_additional_examples_4(self): + """Check Additional Examples 4.""" + self._test('Additional Examples 4: Numeric Keys') diff --git a/test_uritemplate.py b/tests/test_uritemplate.py index 26aeb72..26aeb72 100644 --- a/test_uritemplate.py +++ b/tests/test_uritemplate.py @@ -9,7 +9,9 @@ envlist = pep8, [testenv] -commands = python test_uritemplate.py +deps = + pytest +commands = py.test [testenv:pep8] deps = |