Revert "Removing deprecated/unused/outdated code"
[osm/RO.git] / RO / osm_ro / tests / test_utils.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 ##
15
16 # pylint: disable=E1101
17
18 import unittest
19
20 from ..utils import (
21 get_arg,
22 inject_args,
23 remove_extra_items,
24 )
25
26
27 class TestUtils(unittest.TestCase):
28 def test_inject_args_curries_arguments(self):
29 fn = inject_args(lambda a=None, b=None: a + b, a=3, b=5)
30 self.assertEqual(fn(), 8)
31
32 def test_inject_args_doesnt_add_arg_if_not_needed(self):
33 fn = inject_args(lambda: 7, a=1, b=2)
34 self.assertEqual(fn(), 7)
35 fn = inject_args(lambda a=None: a, b=2)
36 self.assertEqual(fn(1), 1)
37
38 def test_inject_args_knows_how_to_handle_arg_order(self):
39 fn = inject_args(lambda a=None, b=None: b - a, a=3)
40 self.assertEqual(fn(b=4), 1)
41 fn = inject_args(lambda b=None, a=None: b - a, a=3)
42 self.assertEqual(fn(b=4), 1)
43
44 def test_inject_args_works_as_decorator(self):
45 fn = inject_args(x=1)(lambda x=None: x)
46 self.assertEqual(fn(), 1)
47
48 def test_get_arg__positional(self):
49 def _fn(x, y, z):
50 return x + y + z
51
52 x = get_arg("x", _fn, (1, 3, 4), {})
53 self.assertEqual(x, 1)
54 y = get_arg("y", _fn, (1, 3, 4), {})
55 self.assertEqual(y, 3)
56 z = get_arg("z", _fn, (1, 3, 4), {})
57 self.assertEqual(z, 4)
58
59 def test_get_arg__keyword(self):
60 def _fn(x, y, z=5):
61 return x + y + z
62
63 z = get_arg("z", _fn, (1, 2), {"z": 3})
64 self.assertEqual(z, 3)
65
66
67
68 def test_remove_extra_items__keep_aditional_properties(self):
69 schema = {
70 "type": "object",
71 "properties": {
72 "a": {
73 "type": "object",
74 "properties": {
75 "type": "object",
76 "properties": {"b": "string"},
77 },
78 "additionalProperties": True,
79 }
80 },
81 }
82
83 example = {"a": {"b": 1, "c": 2}, "d": 3}
84 deleted = remove_extra_items(example, schema)
85 self.assertIn("d", deleted)
86 self.assertIs(example.get("d"), None)
87 self.assertEqual(example["a"]["c"], 2)
88
89
90 if __name__ == "__main__":
91 unittest.main()