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'
|