[SEBA-126] Adding xproto support for min and max validators
Change-Id: I6141c678d88a894db2a86132bdbad4e9c6b31b2f
diff --git a/lib/xos-genx/xos-genx-tests/test_django_generator.py b/lib/xos-genx/xos-genx-tests/test_django_generator.py
index c3ffef5..d2b3c8a 100644
--- a/lib/xos-genx/xos-genx-tests/test_django_generator.py
+++ b/lib/xos-genx/xos-genx-tests/test_django_generator.py
@@ -95,10 +95,30 @@
args.target = 'django.xtarget'
output = XOSProcessor.process(args)
- print output
-
self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
+ def test_min_max_validators(self):
+ """
+ [XOS-GenX] Use django validors for min and max values
+ """
+ xproto = \
+ """
+ option app_label = "test";
+
+ message Foo (ParentFoo) {
+ required int32 val = 1 [min_value = 1, max_value = 10];
+ }
+ """
+
+ args = FakeArgs()
+ args.inputs = xproto
+ args.target = 'django.xtarget'
+ output = XOSProcessor.process(args)
+
+ self.assertIn("validators=[", output)
+ self.assertIn("MinValueValidator(1)", output)
+ self.assertIn("MaxValueValidator(10)", output)
+
if __name__ == '__main__':
unittest.main()
diff --git a/lib/xos-genx/xos-genx-tests/test_jinja2_django.py b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
index ea8738e..ab47443 100644
--- a/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
+++ b/lib/xos-genx/xos-genx-tests/test_jinja2_django.py
@@ -44,6 +44,75 @@
res = xproto_optioned_fields_to_list(fields, 'feedback_state', 'True')
self.assertEqual(res, ["has_feedback_1", "has_feedback_2"])
+ def test_xproto_required_to_django(self):
+ field = {
+ 'name': 'foo',
+ 'options': {
+ 'modifier': 'required'
+ }
+ }
+
+ res = map_xproto_to_django(field)
+ self.assertEqual(res, {'blank': False, 'null': False})
+
+ def test_xproto_optional_to_django(self):
+ field = {
+ 'name': 'foo',
+ 'options': {
+ 'modifier': 'optional'
+ }
+ }
+
+ res = map_xproto_to_django(field)
+ self.assertEqual(res, {'blank': True, 'null': True})
+
+
+ def test_map_xproto_to_django(self):
+
+ options = {
+ 'help_text': 'bar',
+ 'default': 'default_value',
+ 'null': True,
+ 'db_index': False,
+ 'blank': False,
+ 'min_value': 16,
+ 'max_value': 16
+ }
+
+ field = {
+ 'name': 'foo',
+ 'options': options
+ }
+
+ res = map_xproto_to_django(field)
+ self.assertEqual(res, options)
+
+ def test_format_options_string(self):
+
+ options = {
+ 'null': True,
+ 'min_value': 16,
+ 'max_value': 16
+ }
+
+ res = format_options_string(options)
+ self.assertEqual(res, "null = True, validators=[MaxValueValidator(16), MinValueValidator(16)]")
+
+ options = {
+ 'min_value': 16,
+ 'max_value': 16
+ }
+
+ res = format_options_string(options)
+ self.assertEqual(res, "validators=[MaxValueValidator(16), MinValueValidator(16)]")
+
+ options = {
+ 'null': True,
+ }
+
+ res = format_options_string(options)
+ self.assertEqual(res, "null = True")
+
if __name__ == '__main__':
unittest.main()