blob: 108ae4efe03c648e3b2f34ab052c9961b8fc5de9 [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
15
16import unittest
17from xosgenx.jinja2_extensions.django import *
18
Zack Williams045b63d2019-01-22 16:30:57 -070019
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080020class Jinja2BaseTests(unittest.TestCase):
21 def test_xproto_optioned_fields_to_list(self):
22
23 fields = [
Zack Williams045b63d2019-01-22 16:30:57 -070024 {"name": "has_feedback_1", "options": {"feedback_state": "True"}},
25 {"name": "has_feedback_2", "options": {"feedback_state": "True"}},
26 {"name": "no_feedback", "options": {"feedback_state": "False"}},
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080027 ]
28
Zack Williams045b63d2019-01-22 16:30:57 -070029 res = xproto_optioned_fields_to_list(fields, "feedback_state", "True")
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080030 self.assertEqual(res, ["has_feedback_1", "has_feedback_2"])
31
Matteo Scandolo61a9f202018-08-01 08:58:13 -040032 def test_xproto_required_to_django(self):
Zack Williams045b63d2019-01-22 16:30:57 -070033 field = {"name": "foo", "options": {"modifier": "required"}}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040034
35 res = map_xproto_to_django(field)
Zack Williams045b63d2019-01-22 16:30:57 -070036 self.assertEqual(res, {"blank": False, "null": False})
Matteo Scandolo61a9f202018-08-01 08:58:13 -040037
38 def test_xproto_optional_to_django(self):
Zack Williams045b63d2019-01-22 16:30:57 -070039 field = {"name": "foo", "options": {"modifier": "optional"}}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040040
41 res = map_xproto_to_django(field)
Zack Williams045b63d2019-01-22 16:30:57 -070042 self.assertEqual(res, {"blank": True, "null": True})
Matteo Scandolo61a9f202018-08-01 08:58:13 -040043
44 def test_map_xproto_to_django(self):
45
46 options = {
Zack Williams045b63d2019-01-22 16:30:57 -070047 "help_text": "bar",
48 "default": "default_value",
49 "null": True,
50 "db_index": False,
51 "blank": False,
52 "min_value": 16,
53 "max_value": 16,
Matteo Scandolo61a9f202018-08-01 08:58:13 -040054 }
55
Zack Williams045b63d2019-01-22 16:30:57 -070056 field = {"name": "foo", "options": options}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040057
58 res = map_xproto_to_django(field)
59 self.assertEqual(res, options)
60
61 def test_format_options_string(self):
62
Zack Williams045b63d2019-01-22 16:30:57 -070063 options = {"null": True, "min_value": 16, "max_value": 16}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040064
65 res = format_options_string(options)
Zack Williams045b63d2019-01-22 16:30:57 -070066 self.assertEqual(
67 res,
68 "null = True, validators=[MaxValueValidator(16), MinValueValidator(16)]",
69 )
Matteo Scandolo61a9f202018-08-01 08:58:13 -040070
Zack Williams045b63d2019-01-22 16:30:57 -070071 options = {"min_value": 16, "max_value": 16}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040072
73 res = format_options_string(options)
Zack Williams045b63d2019-01-22 16:30:57 -070074 self.assertEqual(
75 res, "validators=[MaxValueValidator(16), MinValueValidator(16)]"
76 )
Matteo Scandolo61a9f202018-08-01 08:58:13 -040077
Zack Williams045b63d2019-01-22 16:30:57 -070078 options = {"null": True}
Matteo Scandolo61a9f202018-08-01 08:58:13 -040079
80 res = format_options_string(options)
81 self.assertEqual(res, "null = True")
82
Zack Williams045b63d2019-01-22 16:30:57 -070083
84if __name__ == "__main__":
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080085 unittest.main()