1 # -*- coding: utf-8 -*-
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # pylint: disable=E1101
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)
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)
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)
44 def test_inject_args_works_as_decorator(self
):
45 fn
= inject_args(x
=1)(lambda x
=None: x
)
46 self
.assertEqual(fn(), 1)
48 def test_get_arg__positional(self
):
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)
59 def test_get_arg__keyword(self
):
63 z
= get_arg("z", _fn
, (1, 2), {"z": 3})
64 self
.assertEqual(z
, 3)
68 def test_remove_extra_items__keep_aditional_properties(self
):
76 "properties": {"b": "string"},
78 "additionalProperties": True,
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)
90 if __name__
== "__main__":