Home | History | Annotate | Download | only in tests
      1 import json
      2 import os.path
      3 
      4 import uritemplate
      5 
      6 
      7 def fixture_file_path(filename):
      8     absolute_dir = os.path.abspath(os.path.dirname(__file__))
      9     filename = filename + '.json'
     10     return os.path.join(absolute_dir, 'fixtures', filename)
     11 
     12 
     13 def load_examples(filename):
     14     path = fixture_file_path(filename)
     15     with open(path, 'r') as examples_file:
     16         examples = json.load(examples_file)
     17     return examples
     18 
     19 
     20 def expected_set(expected):
     21     if isinstance(expected, list):
     22         return set(expected)
     23     return set([expected])
     24 
     25 
     26 class FixtureMixin(object):
     27     def _get_test(self, section):
     28         test = self.examples.get(section, {})
     29         return test.get('variables', {}), test.get('testcases', [])
     30 
     31     def _test(self, testname):
     32         variables, testcases = self._get_test(testname)
     33         for template, expected in testcases:
     34             expected = expected_set(expected)
     35             expanded = uritemplate.expand(template, variables)
     36             assert expanded in expected
     37 
     38 
     39 class TestSpecExamples(FixtureMixin):
     40     examples = load_examples('spec-examples')
     41 
     42     def test_level_1(self):
     43         """Check that uritemplate.expand matches Level 1 expectations."""
     44         self._test('Level 1 Examples')
     45 
     46     def test_level_2(self):
     47         """Check that uritemplate.expand matches Level 2 expectations."""
     48         self._test('Level 2 Examples')
     49 
     50     def test_level_3(self):
     51         """Check that uritemplate.expand matches Level 3 expectations."""
     52         self._test('Level 3 Examples')
     53 
     54     def test_level_4(self):
     55         """Check that uritemplate.expand matches Level 4 expectations."""
     56         self._test('Level 4 Examples')
     57 
     58 
     59 class TestSpecExamplesByRFCSection(FixtureMixin):
     60     examples = load_examples('spec-examples-by-section')
     61 
     62     def test_variable_expansion(self):
     63         """Check variable expansion."""
     64         self._test('3.2.1 Variable Expansion')
     65 
     66     def test_simple_string_expansion(self):
     67         """Check simple string expansion."""
     68         self._test('3.2.2 Simple String Expansion')
     69 
     70     def test_reserved_expansion(self):
     71         """Check reserved expansion."""
     72         self._test('3.2.3 Reserved Expansion')
     73 
     74     def test_fragment_expansion(self):
     75         """Check fragment expansion."""
     76         self._test('3.2.4 Fragment Expansion')
     77 
     78     def test_dot_prefixed_label_expansion(self):
     79         """Check label expansion with dot-prefix."""
     80         self._test('3.2.5 Label Expansion with Dot-Prefix')
     81 
     82     def test_path_segment_expansion(self):
     83         """Check path segment expansion."""
     84         self._test('3.2.6 Path Segment Expansion')
     85 
     86     def test_path_style_parameter_expansion(self):
     87         """Check path-style param expansion."""
     88         self._test('3.2.7 Path-Style Parameter Expansion')
     89 
     90     def test_form_style_query_expansion(self):
     91         """Check form-style query expansion."""
     92         self._test('3.2.8 Form-Style Query Expansion')
     93 
     94     def test_form_style_query_cntinuation(self):
     95         """Check form-style query continuation."""
     96         self._test('3.2.9 Form-Style Query Continuation')
     97 
     98 
     99 class TestExtendedTests(FixtureMixin):
    100     examples = load_examples('extended-tests')
    101 
    102     def test_additional_examples_1(self):
    103         """Check Additional Examples 1."""
    104         self._test('Additional Examples 1')
    105 
    106     def test_additional_examples_2(self):
    107         """Check Additional Examples 2."""
    108         self._test('Additional Examples 2')
    109 
    110     def test_additional_examples_3(self):
    111         """Check Additional Examples 3."""
    112         self._test('Additional Examples 3: Empty Variables')
    113 
    114     def test_additional_examples_4(self):
    115         """Check Additional Examples 4."""
    116         self._test('Additional Examples 4: Numeric Keys')
    117