Projet

Général

Profil

0001-tests-add-tests-on-views-and-commands-41025.patch

Nicolas Roche, 25 mars 2020 20:25

Télécharger (7,89 ko)

Voir les différences:

Subject: [PATCH] tests: add tests on views and commands (#41025)

 tests/test_commands.py                        | 51 +++++++++++++++++++
 tests/test_nanterre_views.py                  | 36 +++++++++++++
 tests/test_zoo.py                             | 38 ++++++++++++--
 .../management/commands/rsu-duplicates.py     |  2 +-
 4 files changed, 123 insertions(+), 4 deletions(-)
 create mode 100644 tests/test_commands.py
 create mode 100644 tests/test_nanterre_views.py
tests/test_commands.py
1
# -*- coding: utf-8 -*-
2

  
3
import mock
4
import pytest
5

  
6
from django.core.management import call_command
7

  
8

  
9
def test_runjobs_command(db):
10
    call_command('runjobs')
11

  
12
    with mock.patch('zoo.zoo_data.management.commands.runjobs.Job.redo',
13
                    side_effect=Exception):
14
        with pytest.raises(Exception):
15
            call_command('runjobs')
16

  
17

  
18
def test_runscript_command(tmpdir):
19
    script_path = '%s/script.py' % str(tmpdir)
20
    open(script_path, 'w').write('# N/A')
21
    call_command('runscript', script_path)
22

  
23

  
24
def test_rsu_cron_command(settings, rsu):
25
    settings.ZOO_NANTERRE_PASSAGE_A_LA_MAJORITE = True
26
    call_command('rsu-cron', '-v2')
27

  
28

  
29
def test_rsu_dupplicate_command(rsu):
30
    call_command('rsu-duplicates', '-v2', 'list', '--dedup')
31
    call_command('rsu-duplicates', '-v2', 'list')
32

  
33
    with pytest.raises(AssertionError, match='Cannot filter a query once a slice has been taken'):
34
        call_command('rsu-duplicates', 'list', '--count', '2')
35
    with pytest.raises(TypeError, match="'datetime.datetime' object is not callable"):
36
        call_command('rsu-duplicates', '-v2', 'list', '--days', '2')
37

  
38
    with pytest.raises(KeyError, match='nom_d_usage'):
39
        call_command('rsu-duplicates', '-v2', 'find', '--limit', '0.1')
40

  
41
    call_command('rsu-duplicates', '-v2', 'delete', '--limit', '0.1')
42

  
43

  
44
def test_rsu_integrity_check_command():
45
    with mock.patch('zoo.zoo_nanterre.utils.integrity_check',
46
                    return_value=(x for x in ('an error',))):
47
        call_command('rsu-integrity-check')
48

  
49
@mock.patch('zoo.zoo_nanterre.utils.psycopg2.connect')
50
def test_rsu_load_dump_command(mocked_connect, rsu):
51
    call_command('rsu-load-dump', 'pg_dsn=nanterre_rsu','authentic_fixture_path')
tests/test_nanterre_views.py
1
# -*- coding: utf-8 -*-
2

  
3

  
4
def test_rsu_demo_view(app, rsu):
5
    resp = app.get('/rsu/demo/', status=200)
6
    assert resp.html.find_all('h1')[1].text == u'Démo pour le RSU'
7
    assert resp.html.find('a', {'href': 'search/'}).text == 'Recherche'
8

  
9

  
10
def test_rsu_demo_search_view(app, rsu):
11
    resp = app.get('/rsu/demo/search/', status=200)
12
    assert u'Aucune fiche trouvée' in resp.text
13

  
14
    resp = app.get('/rsu/demo/search/', {'query': 'Léon', 'limit': 0.1}, status=200)
15
    nb_res_1 = len(resp.html.find_all('div', {'class': 'individu'}))
16

  
17
    resp = app.get('/rsu/demo/search/', {'query': 'Léon', 'limit': 0.2}, status=200)
18
    nb_res_2 = len(resp.html.find_all('div', {'class': 'individu'}))
19
    assert nb_res_1 > nb_res_2 > 0
20

  
21

  
22
def test_rsu_import_control_view(app, rsu):
23
    resp = app.get('/rsu/import_control/', status=200)
24
    assert resp.html.find_all('h1')[1].text == u"Contrôle de l'import"
25
    ints = [
26
        "Nombre d'individus",
27
        "Nombre d'individus majeurs",
28
        "Nombre d'individus mineurs",
29
        u'Fédérations technocarte',
30
    ]
31
    number = []
32
    for i, value in enumerate(ints):
33
        assert resp.html.find_all('dt')[i].text == value
34
        number.append(int(resp.html.find_all('dd')[i].text))
35
    assert number[0] == number[3] == 1000
36
    assert number[1] + number[2] == 1000
tests/test_zoo.py
32 32
                'bad_type_property': {
33 33
                    'type': 'not string type'
34 34
                }
35 35
            }
36 36
        })
37 37
    return schema
38 38

  
39 39

  
40
def test_new_schema(db, person_schema):
41

  
42
    Entity.objects.create(
40
@pytest.fixture
41
def person_entity(person_schema):
42
    entity = Entity.objects.create(
43 43
        schema=person_schema,
44 44
        meta={},
45 45
        content={
46 46
            'first_name': 'Leon',
47 47
            'last_name': 'Blum',
48 48
            'address': {
49 49
                'street': 'Rue du Château',
50 50
            }
51 51
        })
52
    return entity
53

  
54

  
55
def test_new_schema(person_schema, person_entity):
52 56
    qs = Entity.objects.content_search(person_schema, address__street='chateau')
53 57
    assert qs.count() == 1
54 58

  
55 59

  
56 60
class ActionExample(object):
57 61
    def __init__(self, tries=2, content=None):
58 62
        self.content = content or {
59 63
            'tries': tries,
......
110 114
def test_demo_schema_view(app, person_schema):
111 115
    resp = app.get('/demo/schemas/person/', {'page': 'not a number'}, status=200)
112 116
    assert resp.html.find('h1').text == 'person'
113 117
    assert resp.html.find('label', {'for': 'id_first_name'}).name
114 118
    assert resp.html.find('label', {'for': 'id_last_name'}).name
115 119
    assert resp.html.find('label', {'for': 'id_address__street'}).name
116 120
    assert not resp.html.find('label', {'for': 'bad_type_property'})
117 121
    assert resp.html.find('input', {'type': 'submit'})['value'] == 'Recherche'
122
    assert u'Aucune entité trouvée' in resp.text
123

  
124

  
125
def test_demo_search_on_schema_view(app, person_entity):
126
    resp = app.get('/demo/schemas/person/', {'first_name': 'Leo'}, status=200)
127
    assert u'Aucune entité trouvée' not in resp.text
128
    assert 'Blum' in resp.html.pre.text
129

  
130
    resp = app.get('/demo/schemas/person/', {'limit': 'not a float'}, status=200)
131
    assert u'Aucune entité trouvée' in resp.text
132

  
133

  
134
def test_admin_zoo_data_entity_inactive_view(app, admin, rsu):
135
    url = '/admin/zoo_data/entity/inactive/'
136
    app.set_user(admin.username)
137
    resp = app.get(url, status=200)
138
    assert [x.text for x in resp.html.find_all('h1')] == [
139
        'Administration technique du RSU', u'Fiches inactives à supprimer']
140

  
141
    resp = app.get(url+'?recompute', status=302)
142
    assert resp.location == url
143

  
144
    # unavailable post query
145
    resp = app.post(url+'?delete', status=403)
146

  
147
    resp = app.get(url+'?csv', status=200)
148
    assert resp.content_type == 'text/csv'
149
    assert resp.text == 'id,prenoms,nom_d_usage,nom_de_naissance,date_de_naissance,statut_legal,age\r\n'
zoo/zoo_nanterre/management/commands/rsu-duplicates.py
121 121
                qs = qs[:count]
122 122
            if days:
123 123
                since = now() - datetime.timedelta(days=days)
124 124
                self.stdout.write('Duplicates created after', since)
125 125
                qs = qs.filter(created__gte=since)
126 126
            if false:
127 127
                qs = qs.filter(state=Duplicate.STATE_FALSE_POSITIVE)
128 128
            elif dedup:
129
                qs = qs.filter(state=Duplicate.SATE_DEDUP)
129
                qs = qs.filter(state=Duplicate.STATE_DEDUP)
130 130
            else:
131 131
                qs = qs.filter(state=Duplicate.STATE_NEW)
132 132
            column_size = 0
133 133
            for duplicate in qs:
134 134
                column_size = max(column_size, len(individu_caption(duplicate.first)),
135 135
                                  len(individu_caption(duplicate.second)))
136 136

  
137 137
            self.stdout.write('%d duplicates\n' % qs.count())
138
-