blob: d5da2d39670fe6160e106bd7825de8fccfe83731 [file] [log] [blame]
Matteo Scandolo23cf15f2018-03-06 18:12:36 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain 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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080015import unittest
16from xosgenx.jinja2_extensions.django import *
17
Zack Williams045b63d2019-01-22 16:30:57 -070018
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080019class Jinja2BaseTests(unittest.TestCase):
20 def test_xproto_optioned_fields_to_list(self):
21
22 fields = [
Zack Williams045b63d2019-01-22 16:30:57 -070023 {"name": "has_feedback_1", "options": {"feedback_state": "True"}},
24 {"name": "has_feedback_2", "options": {"feedback_state": "True"}},
25 {"name": "no_feedback", "options": {"feedback_state": "False"}},
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080026 ]
27
Zack Williams045b63d2019-01-22 16:30:57 -070028 res = xproto_optioned_fields_to_list(fields, "feedback_state", "True")
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080029 self.assertEqual(res, ["has_feedback_1", "has_feedback_2"])
30
Matteo Scandolo61a9f202018-08-01 08:58:13 -040031 def test_xproto_required_to_django(self):
Zack Williams045b63d2019-01-22 16:30:57 -070032 field = {"name": "foo", "options": {"modifier": "required"}}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040033
34 res = map_xproto_to_django(field)
Zack Williams00e22d62019-03-01 22:32:13 -070035 self.assertEqual(res, {"blank": "False", "null": "False"})
Matteo Scandolo61a9f202018-08-01 08:58:13 -040036
37 def test_xproto_optional_to_django(self):
Zack Williams045b63d2019-01-22 16:30:57 -070038 field = {"name": "foo", "options": {"modifier": "optional"}}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040039
40 res = map_xproto_to_django(field)
Zack Williams00e22d62019-03-01 22:32:13 -070041 self.assertEqual(res, {"blank": "True", "null": "True"})
Matteo Scandolo61a9f202018-08-01 08:58:13 -040042
43 def test_map_xproto_to_django(self):
44
45 options = {
Zack Williams045b63d2019-01-22 16:30:57 -070046 "help_text": "bar",
47 "default": "default_value",
48 "null": True,
49 "db_index": False,
50 "blank": False,
51 "min_value": 16,
52 "max_value": 16,
Matteo Scandolo61a9f202018-08-01 08:58:13 -040053 }
54
Zack Williams045b63d2019-01-22 16:30:57 -070055 field = {"name": "foo", "options": options}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040056
57 res = map_xproto_to_django(field)
58 self.assertEqual(res, options)
59
60 def test_format_options_string(self):
61
Zack Williams045b63d2019-01-22 16:30:57 -070062 options = {"null": True, "min_value": 16, "max_value": 16}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040063
64 res = format_options_string(options)
Zack Williams045b63d2019-01-22 16:30:57 -070065 self.assertEqual(
66 res,
67 "null = True, validators=[MaxValueValidator(16), MinValueValidator(16)]",
68 )
Matteo Scandolo61a9f202018-08-01 08:58:13 -040069
Zack Williams045b63d2019-01-22 16:30:57 -070070 options = {"min_value": 16, "max_value": 16}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040071
72 res = format_options_string(options)
Zack Williams045b63d2019-01-22 16:30:57 -070073 self.assertEqual(
74 res, "validators=[MaxValueValidator(16), MinValueValidator(16)]"
75 )
Matteo Scandolo61a9f202018-08-01 08:58:13 -040076
Zack Williams045b63d2019-01-22 16:30:57 -070077 options = {"null": True}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040078
79 res = format_options_string(options)
80 self.assertEqual(res, "null = True")
81
Zack Williams045b63d2019-01-22 16:30:57 -070082
83if __name__ == "__main__":
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080084 unittest.main()