From db94179c2679136ac27cf8af09782663ed074a58 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Tue, 24 Mar 2020 20:27:37 +0100
Subject: [PATCH] forms: unpack all values from scema paths list (#40990)

---
 tests/test_zoo.py     | 34 ++++++++++++++++++++++++++++++----
 zoo/zoo_demo/forms.py |  2 +-
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/tests/test_zoo.py b/tests/test_zoo.py
index 2034b31..b727532 100644
--- a/tests/test_zoo.py
+++ b/tests/test_zoo.py
@@ -1,18 +1,20 @@
 # -*- coding: utf-8 -*-
 
 import datetime
+import pytest
+
 from django.utils.timezone import now
 
 from zoo.models import EntitySchema, Entity, Job
 
 
-def test_new_schema(db):
-
+@pytest.fixture
+def person_schema(db):
     schema = EntitySchema.objects.create(
         name='person',
         schema={
             'type': 'object',
             'properties': {
                 'first_name': {
                     'type': 'string',
                 },
@@ -21,30 +23,38 @@ def test_new_schema(db):
                 },
                 'address': {
                     'type': 'object',
                     'properties': {
                         'street': {
                             'type': 'string',
                         },
                     }
+                },
+                'bad_type_property': {
+                    'type': 'not string type'
                 }
             }
         })
+    return schema
+
+
+def test_new_schema(db, person_schema):
+
     Entity.objects.create(
-        schema=schema,
+        schema=person_schema,
         meta={},
         content={
             'first_name': 'Leon',
             'last_name': 'Blum',
             'address': {
                 'street': 'Rue du Château',
             }
         })
-    qs = Entity.objects.content_search(schema, address__street='chateau')
+    qs = Entity.objects.content_search(person_schema, address__street='chateau')
     assert qs.count() == 1
 
 
 class ActionExample(object):
     def __init__(self, tries=2, content=None):
         self.content = content or {
             'tries': tries,
             'done': False,
@@ -84,8 +94,24 @@ def test_jobs(db):
     assert Job.objects.get().content['tries'] == 0
     assert Job.objects.get().state == Job.STATE_ERROR
     # second retry
     Job.redo(timestamp=now() + datetime.timedelta(seconds=1))
     assert Job.objects.count() == 1
     assert Job.objects.get().content['done'] is True
     assert Job.objects.get().content['tries'] == 0
     assert Job.objects.get().state == Job.STATE_SUCCESS
+
+
+def test_demo_schemas_view(app, person_schema):
+    resp = app.get('/demo/schemas/', status=200)
+    assert resp.html.find('a').text == 'person'
+    assert resp.html.find('a')['href'] == '/demo/schemas/person/'
+
+
+def test_demo_schema_view(app, person_schema):
+    resp = app.get('/demo/schemas/person/', {'page': 'not a number'}, status=200)
+    assert resp.html.find('h1').text == 'person'
+    assert resp.html.find('label', {'for': 'id_first_name'}).name
+    assert resp.html.find('label', {'for': 'id_last_name'}).name
+    assert resp.html.find('label', {'for': 'id_address__street'}).name
+    assert not resp.html.find('label', {'for': 'bad_type_property'})
+    assert resp.html.find('input', {'type': 'submit'})['value'] == 'Recherche'
diff --git a/zoo/zoo_demo/forms.py b/zoo/zoo_demo/forms.py
index 1322d7c..f1add5c 100644
--- a/zoo/zoo_demo/forms.py
+++ b/zoo/zoo_demo/forms.py
@@ -26,17 +26,17 @@ class EntitySearchForm(Form):
         self.fields['limit'] = DecimalField(
             widget=NumberInput(attrs={
                 'type': 'range',
                 'min': '0',
                 'max': '1',
                 'step': '0.02',
             }),
             required=False)
-        for path, _type in self.schema.paths():
+        for path, _type, _format in self.schema.paths():
             if _type != 'string':
                 continue
             key = '__'.join(path)
             self.fields[key] = CharField(
                 max_length=32,
                 required=False,
                 widget=TextInput(attrs={'autocomplete': 'off'}),
             )
-- 
2.25.1

