Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 1 | # 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 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 16 | import unittest |
| 17 | from xosgenx.jinja2_extensions.django import * |
| 18 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 19 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 20 | class Jinja2BaseTests(unittest.TestCase): |
| 21 | def test_xproto_optioned_fields_to_list(self): |
| 22 | |
| 23 | fields = [ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | {"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 Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 27 | ] |
| 28 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | res = xproto_optioned_fields_to_list(fields, "feedback_state", "True") |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 30 | self.assertEqual(res, ["has_feedback_1", "has_feedback_2"]) |
| 31 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 32 | def test_xproto_required_to_django(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 33 | field = {"name": "foo", "options": {"modifier": "required"}} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 34 | |
| 35 | res = map_xproto_to_django(field) |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 36 | self.assertEqual(res, {"blank": "False", "null": "False"}) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 37 | |
| 38 | def test_xproto_optional_to_django(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 39 | field = {"name": "foo", "options": {"modifier": "optional"}} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 40 | |
| 41 | res = map_xproto_to_django(field) |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 42 | self.assertEqual(res, {"blank": "True", "null": "True"}) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 43 | |
| 44 | def test_map_xproto_to_django(self): |
| 45 | |
| 46 | options = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | "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 Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | field = {"name": "foo", "options": options} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 57 | |
| 58 | res = map_xproto_to_django(field) |
| 59 | self.assertEqual(res, options) |
| 60 | |
| 61 | def test_format_options_string(self): |
| 62 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 63 | options = {"null": True, "min_value": 16, "max_value": 16} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 64 | |
| 65 | res = format_options_string(options) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | self.assertEqual( |
| 67 | res, |
| 68 | "null = True, validators=[MaxValueValidator(16), MinValueValidator(16)]", |
| 69 | ) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 70 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 71 | options = {"min_value": 16, "max_value": 16} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 72 | |
| 73 | res = format_options_string(options) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 74 | self.assertEqual( |
| 75 | res, "validators=[MaxValueValidator(16), MinValueValidator(16)]" |
| 76 | ) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 77 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 78 | options = {"null": True} |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 79 | |
| 80 | res = format_options_string(options) |
| 81 | self.assertEqual(res, "null = True") |
| 82 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 83 | |
| 84 | if __name__ == "__main__": |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 85 | unittest.main() |