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