Ver código fonte

add tests for select template

Daniel Sheffield 1 ano atrás
pai
commit
a3d4fb127e

+ 23 - 32
app/rest/templates/select.tpl

@@ -1,35 +1,26 @@
+% multiple = (get("multiple", False) and "multiple") or ""
+% setdefault("option_groups", [{ "name": None, "options": (defined("options") and options) or [] }])
 <div class="pure-u-1">
-<%
-  if defined("label"):
-    include('app/rest/templates/label', id=id, label=label)
-  end
-%>
-<select
-  id="{{id}}"
-  name="{{name}}"
-  size=10
-  {{"multiple" if defined("multiple") and multiple else ""}}
-  style="width: 98%; margin: 1em"
-  >
-  <%
-    if defined("hint"):
-      include('app/rest/templates/option', value=hint, disabled=True)
-    end
-    if not defined("option_groups"):
-      option_groups = [{
-        "name": None,
-        "options": options
-      }]
-    end
-    for group in option_groups:
-      if group["name"] is None:
-        for opt in group["options"]:
-          include('app/rest/templates/option', **opt)
-        end
-      else:
-        include('app/rest/templates/optgroup', **group)
-      end
-    end
-  %>
+%  if defined("label"):
+%    include('app/rest/templates/label', id=id, label=label)
+%  end
+
+<select id="{{id}}" name="{{name}}" size=10 {{multiple}} style="width: 98%; margin: 1em">
+%  if defined("hint"):
+%    include('app/rest/templates/option', value=hint, disabled=True)
+%  end
+
+%  for group in option_groups:
+%    if group["name"] is None:
+%      for opt in group["options"]:
+
+%        include('app/rest/templates/option', **opt)
+%      end
+%    else:
+%      include('app/rest/templates/optgroup', **group)
+%    end
+%  end
+%
+
 </select>
 </div>

+ 5 - 5
test/rest/templates/test_optgroup.py

@@ -20,15 +20,15 @@ from bottle import template
         "disabled": True,
     }, ] }),
     ("""<optgroup label="Group">
-  <option value="val-to-backend"  >val-displayed</option>
-  <option value="val-to-backend" disabled >val-to-backend</option>
+  <option value="val1-to-backend"  >val1</option>
+  <option value="val2-to-backend" disabled >val2-to-backend</option>
 </optgroup>""", { "name": "Group", "options": [
     {
-        "value": "val-to-backend",
-        "display": "val-displayed",
+        "value": "val1-to-backend",
+        "display": "val1",
     },
     {
-        "value": "val-to-backend",
+        "value": "val2-to-backend",
         "disabled": True,
     }, ] }),
 ])

+ 45 - 0
test/rest/templates/test_select.py

@@ -0,0 +1,45 @@
+import pytest
+from pytest import mark, raises
+from bottle import template
+
+@mark.parametrize('expected, params', [
+    ("""<div class="pure-u-1">
+<label for="select-id">Choose: </label>
+<select id="select-id" name="select-name" size=10 multiple style="width: 98%; margin: 1em">
+  <option value="hint" disabled >hint</option>
+<optgroup label="Group">
+  <option value="val1-to-backend"  >val1</option>
+  <option value="val2" disabled >val2</option>
+</optgroup>
+</select>
+</div>""", {
+    "id": "select-id", "label": "Choose: ", "name": "select-name",
+    "hint": "hint", "multiple": True, "option_groups": [{ "name": "Group", "options": [
+    {
+        "value": "val1-to-backend",
+        "display": "val1",
+    },
+    {
+        "value": "val2",
+        "disabled": True,
+    }, ] }, ]}),
+    ("""<div class="pure-u-1">
+
+<select id="select-unit-id" name="unit" size=10  style="width: 98%; margin: 1em">
+
+
+  <option value="val1"  >val1</option>
+  <option value="val2"  selected>val2</option>
+</select>
+</div>""",{
+        "id": "select-unit-id", "name": "unit", "options": [
+    {
+        "value": "val1",
+    },
+    {
+        "value": "val2",
+        "selected": True,
+    }, ] }),
+])
+def test_select_render_exact(expected, params):
+    assert template('app/rest/templates/select', **params) == expected