Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import unittest |
| 18 | from xosgenx.jinja2_extensions.django import * |
| 19 | |
| 20 | class Jinja2BaseTests(unittest.TestCase): |
| 21 | def test_xproto_optioned_fields_to_list(self): |
| 22 | |
| 23 | fields = [ |
| 24 | { |
| 25 | 'name': 'has_feedback_1', |
| 26 | 'options': { |
| 27 | 'feedback_state': 'True', |
| 28 | } |
| 29 | }, |
| 30 | { |
| 31 | 'name': 'has_feedback_2', |
| 32 | 'options': { |
| 33 | 'feedback_state': 'True', |
| 34 | } |
| 35 | }, |
| 36 | { |
| 37 | 'name': 'no_feedback', |
| 38 | 'options': { |
| 39 | 'feedback_state': 'False', |
| 40 | } |
| 41 | } |
| 42 | ] |
| 43 | |
| 44 | res = xproto_optioned_fields_to_list(fields, 'feedback_state', 'True') |
| 45 | self.assertEqual(res, ["has_feedback_1", "has_feedback_2"]) |
| 46 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 47 | def test_xproto_required_to_django(self): |
| 48 | field = { |
| 49 | 'name': 'foo', |
| 50 | 'options': { |
| 51 | 'modifier': 'required' |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | res = map_xproto_to_django(field) |
| 56 | self.assertEqual(res, {'blank': False, 'null': False}) |
| 57 | |
| 58 | def test_xproto_optional_to_django(self): |
| 59 | field = { |
| 60 | 'name': 'foo', |
| 61 | 'options': { |
| 62 | 'modifier': 'optional' |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | res = map_xproto_to_django(field) |
| 67 | self.assertEqual(res, {'blank': True, 'null': True}) |
| 68 | |
| 69 | |
| 70 | def test_map_xproto_to_django(self): |
| 71 | |
| 72 | options = { |
| 73 | 'help_text': 'bar', |
| 74 | 'default': 'default_value', |
| 75 | 'null': True, |
| 76 | 'db_index': False, |
| 77 | 'blank': False, |
| 78 | 'min_value': 16, |
| 79 | 'max_value': 16 |
| 80 | } |
| 81 | |
| 82 | field = { |
| 83 | 'name': 'foo', |
| 84 | 'options': options |
| 85 | } |
| 86 | |
| 87 | res = map_xproto_to_django(field) |
| 88 | self.assertEqual(res, options) |
| 89 | |
| 90 | def test_format_options_string(self): |
| 91 | |
| 92 | options = { |
| 93 | 'null': True, |
| 94 | 'min_value': 16, |
| 95 | 'max_value': 16 |
| 96 | } |
| 97 | |
| 98 | res = format_options_string(options) |
| 99 | self.assertEqual(res, "null = True, validators=[MaxValueValidator(16), MinValueValidator(16)]") |
| 100 | |
| 101 | options = { |
| 102 | 'min_value': 16, |
| 103 | 'max_value': 16 |
| 104 | } |
| 105 | |
| 106 | res = format_options_string(options) |
| 107 | self.assertEqual(res, "validators=[MaxValueValidator(16), MinValueValidator(16)]") |
| 108 | |
| 109 | options = { |
| 110 | 'null': True, |
| 111 | } |
| 112 | |
| 113 | res = format_options_string(options) |
| 114 | self.assertEqual(res, "null = True") |
| 115 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 116 | if __name__ == '__main__': |
| 117 | unittest.main() |
| 118 | |
| 119 | |