Projet

Général

Profil

0001-forms-unpack-all-values-from-scema-paths-list-40990.patch

Nicolas Roche, 24 mars 2020 20:35

Télécharger (4,1 ko)

Voir les différences:

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(-)
tests/test_zoo.py
1 1
# -*- coding: utf-8 -*-
2 2

  
3 3
import datetime
4
import pytest
5

  
4 6
from django.utils.timezone import now
5 7

  
6 8
from zoo.models import EntitySchema, Entity, Job
7 9

  
8 10

  
9
def test_new_schema(db):
10

  
11
@pytest.fixture
12
def person_schema(db):
11 13
    schema = EntitySchema.objects.create(
12 14
        name='person',
13 15
        schema={
14 16
            'type': 'object',
15 17
            'properties': {
16 18
                'first_name': {
17 19
                    'type': 'string',
18 20
                },
......
21 23
                },
22 24
                'address': {
23 25
                    'type': 'object',
24 26
                    'properties': {
25 27
                        'street': {
26 28
                            'type': 'string',
27 29
                        },
28 30
                    }
31
                },
32
                'bad_type_property': {
33
                    'type': 'not string type'
29 34
                }
30 35
            }
31 36
        })
37
    return schema
38

  
39

  
40
def test_new_schema(db, person_schema):
41

  
32 42
    Entity.objects.create(
33
        schema=schema,
43
        schema=person_schema,
34 44
        meta={},
35 45
        content={
36 46
            'first_name': 'Leon',
37 47
            'last_name': 'Blum',
38 48
            'address': {
39 49
                'street': 'Rue du Château',
40 50
            }
41 51
        })
42
    qs = Entity.objects.content_search(schema, address__street='chateau')
52
    qs = Entity.objects.content_search(person_schema, address__street='chateau')
43 53
    assert qs.count() == 1
44 54

  
45 55

  
46 56
class ActionExample(object):
47 57
    def __init__(self, tries=2, content=None):
48 58
        self.content = content or {
49 59
            'tries': tries,
50 60
            'done': False,
......
84 94
    assert Job.objects.get().content['tries'] == 0
85 95
    assert Job.objects.get().state == Job.STATE_ERROR
86 96
    # second retry
87 97
    Job.redo(timestamp=now() + datetime.timedelta(seconds=1))
88 98
    assert Job.objects.count() == 1
89 99
    assert Job.objects.get().content['done'] is True
90 100
    assert Job.objects.get().content['tries'] == 0
91 101
    assert Job.objects.get().state == Job.STATE_SUCCESS
102

  
103

  
104
def test_demo_schemas_view(app, person_schema):
105
    resp = app.get('/demo/schemas/', status=200)
106
    assert resp.html.find('a').text == 'person'
107
    assert resp.html.find('a')['href'] == '/demo/schemas/person/'
108

  
109

  
110
def test_demo_schema_view(app, person_schema):
111
    resp = app.get('/demo/schemas/person/', {'page': 'not a number'}, status=200)
112
    assert resp.html.find('h1').text == 'person'
113
    assert resp.html.find('label', {'for': 'id_first_name'}).name
114
    assert resp.html.find('label', {'for': 'id_last_name'}).name
115
    assert resp.html.find('label', {'for': 'id_address__street'}).name
116
    assert not resp.html.find('label', {'for': 'bad_type_property'})
117
    assert resp.html.find('input', {'type': 'submit'})['value'] == 'Recherche'
zoo/zoo_demo/forms.py
26 26
        self.fields['limit'] = DecimalField(
27 27
            widget=NumberInput(attrs={
28 28
                'type': 'range',
29 29
                'min': '0',
30 30
                'max': '1',
31 31
                'step': '0.02',
32 32
            }),
33 33
            required=False)
34
        for path, _type in self.schema.paths():
34
        for path, _type, _format in self.schema.paths():
35 35
            if _type != 'string':
36 36
                continue
37 37
            key = '__'.join(path)
38 38
            self.fields[key] = CharField(
39 39
                max_length=32,
40 40
                required=False,
41 41
                widget=TextInput(attrs={'autocomplete': 'off'}),
42 42
            )
43
-