|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Tests for the jq-subset engine internals (jq_compile/_traverse/jq_step/ |
| 4 | +jq_full/jq_condition/jq_unwrap) in plugins/jsonread/__init__.py. |
| 5 | +
|
| 6 | +These pin down the *current* hand-rolled implementation's specific |
| 7 | +mechanics (how a path token is split, how brackets are parsed, ...). They |
| 8 | +are expected to be thrown away or substantially rewritten if/when the |
| 9 | +engine is swapped for a real library (jmespath, pyjq, ...) — that's fine, |
| 10 | +that's what they're for. The behavior that needs to survive a swap lives |
| 11 | +in test_filter_contract.py instead; see that file's module docstring. |
| 12 | +""" |
| 13 | + |
| 14 | +import unittest |
| 15 | + |
| 16 | +from .base import JsonreadTestBase |
| 17 | + |
| 18 | + |
| 19 | +class _EngineBase(JsonreadTestBase, unittest.TestCase): |
| 20 | + def setUp(self): |
| 21 | + self.plg = self.plugin() |
| 22 | + |
| 23 | + |
| 24 | +class TestJqCompile(_EngineBase): |
| 25 | + def test_single_path_no_pipe(self): |
| 26 | + self.assertEqual(self.plg.jq_compile('.a.b'), ('.a.b',)) |
| 27 | + |
| 28 | + def test_splits_on_pipe(self): |
| 29 | + self.assertEqual(self.plg.jq_compile('.a | .b'), ('.a', '.b')) |
| 30 | + |
| 31 | + def test_respects_parens_around_pipe(self): |
| 32 | + self.assertEqual(self.plg.jq_compile('(.a | .b)'), ('.a', '.b')) |
| 33 | + |
| 34 | + def test_splits_select_from_following_path(self): |
| 35 | + compiled = self.plg.jq_compile('.items[] | select(.id==1).name') |
| 36 | + self.assertEqual(compiled, ('.items[]', 'select(.id==1)', '.name')) |
| 37 | + |
| 38 | + |
| 39 | +class TestTraversePlainKeys(_EngineBase): |
| 40 | + def test_resolves_nested_dict_path(self): |
| 41 | + data = {'a': {'b': 5}} |
| 42 | + self.assertEqual(self.plg._traverse(data, '.a.b'), [5]) |
| 43 | + |
| 44 | + def test_missing_key_returns_empty(self): |
| 45 | + data = {'a': {}} |
| 46 | + self.assertEqual(self.plg._traverse(data, '.a.b'), []) |
| 47 | + |
| 48 | + def test_broadcasts_over_list_obj(self): |
| 49 | + data = [{'a': 1}, {'a': 2}] |
| 50 | + self.assertEqual(self.plg._traverse(data, '.a'), [1, 2]) |
| 51 | + |
| 52 | + |
| 53 | +class TestTraverseBracketIndices(_EngineBase): |
| 54 | + """ |
| 55 | + Regression coverage for the bug fixed in this branch: .Data["0"] and |
| 56 | + .Data[0] were being treated as one literal (and always-missing) key |
| 57 | + name, so every filter using array indexing silently resolved to |
| 58 | + nothing. |
| 59 | + """ |
| 60 | + |
| 61 | + def setUp(self): |
| 62 | + super().setUp() |
| 63 | + self.data = {'Data': [{'x': 'first'}, {'x': 'second'}]} |
| 64 | + |
| 65 | + def test_quoted_string_index(self): |
| 66 | + self.assertEqual(self.plg._traverse(self.data, '.Data["0"].x'), ['first']) |
| 67 | + |
| 68 | + def test_unquoted_numeric_index(self): |
| 69 | + self.assertEqual(self.plg._traverse(self.data, '.Data[0].x'), ['first']) |
| 70 | + |
| 71 | + def test_second_element_index(self): |
| 72 | + self.assertEqual(self.plg._traverse(self.data, '.Data["1"].x'), ['second']) |
| 73 | + |
| 74 | + def test_out_of_range_index_returns_empty(self): |
| 75 | + self.assertEqual(self.plg._traverse(self.data, '.Data["5"].x'), []) |
| 76 | + |
| 77 | + def test_empty_brackets_still_flattens(self): |
| 78 | + self.assertEqual(sorted(self.plg._traverse(self.data, '.Data[].x')), ['first', 'second']) |
| 79 | + |
| 80 | + |
| 81 | +class TestJqCondition(_EngineBase): |
| 82 | + def test_equals_numeric(self): |
| 83 | + self.assertTrue(self.plg.jq_condition('.id==1', {'id': 1})) |
| 84 | + |
| 85 | + def test_equals_string(self): |
| 86 | + self.assertTrue(self.plg.jq_condition('.name=="a"', {'name': 'a'})) |
| 87 | + |
| 88 | + def test_not_equal(self): |
| 89 | + self.assertTrue(self.plg.jq_condition('.id!=2', {'id': 1})) |
| 90 | + |
| 91 | + def test_greater_than(self): |
| 92 | + self.assertTrue(self.plg.jq_condition('.id>1', {'id': 2})) |
| 93 | + self.assertFalse(self.plg.jq_condition('.id>1', {'id': 1})) |
| 94 | + |
| 95 | + |
| 96 | +class TestJqFullWithSelect(_EngineBase): |
| 97 | + def test_select_filters_list_then_extracts_field(self): |
| 98 | + data = {'items': [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}]} |
| 99 | + compiled = self.plg.jq_compile('.items[] | select(.id==2).name') |
| 100 | + self.assertEqual(self.plg.jq_full(compiled, data), ['b']) |
| 101 | + |
| 102 | + |
| 103 | +class TestJqUnwrap(_EngineBase): |
| 104 | + def test_empty_list_becomes_none(self): |
| 105 | + self.assertIsNone(self.plg.jq_unwrap([])) |
| 106 | + |
| 107 | + def test_single_item_list_unwraps(self): |
| 108 | + self.assertEqual(self.plg.jq_unwrap([42]), 42) |
| 109 | + |
| 110 | + def test_multi_item_list_stays_a_list(self): |
| 111 | + self.assertEqual(self.plg.jq_unwrap([1, 2]), [1, 2]) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + unittest.main(verbosity=2) |
0 commit comments