Project

General

Profile

0005-base_adresse-PEP8ness-code-style-30458.patch

Benjamin Dauvergne, 08 Feb 2019 08:43 AM

Download (10.2 KB)

View differences:

Subject: [PATCH 05/13] base_adresse: PEP8ness, code style (#30458)

 passerelle/apps/base_adresse/models.py | 16 +++++---
 tests/test_base_adresse.py             | 51 +++++++++++++++++++++++---
 2 files changed, 56 insertions(+), 11 deletions(-)
passerelle/apps/base_adresse/models.py
1 1
import bz2
2 2
import json
3 3
import os
4
import urllib
5 4
import urlparse
6 5
import unicodedata
7 6

  
7
import six
8

  
8 9
from django.db import connection, models
9 10
from django.db.models import Q
10 11
from django.utils.http import urlencode
......
24 25

  
25 26
    category = _('Geographic information system')
26 27

  
27
    zipcode = models.CharField(max_length=5, blank=True,
28
    zipcode = models.CharField(
29
        max_length=5,
30
        blank=True,
28 31
        verbose_name=_('Postal codes to get streets, separated with commas'))
29 32

  
30 33
    class Meta:
......
158 161
                departments.add(zipcode[:2])
159 162

  
160 163
        for department in departments:
161
            ban_file = self.requests.get('http://bano.openstreetmap.fr/BAN_odbl/BAN_odbl_{}-json.bz2'.format(department))
164
            ban_file = self.requests.get(
165
                'http://bano.openstreetmap.fr/BAN_odbl/BAN_odbl_{}-json.bz2'.format(department))
162 166
            if ban_file.status_code != 200:
163 167
                continue
164 168

  
......
166 170
                street_info = json.loads(line)
167 171
                if street_info['type'] == 'street' and street_info['postcode'].startswith(zipcodes):
168 172
                    if type(street_info['citycode']) is list:
169
                        street_info['citycode'] = unicode(street_info['citycode'][0])
173
                        street_info['citycode'] = six.text_type(street_info['citycode'][0])
170 174
                    if type(street_info['name']) is list:
171
                        street_info['name'] = unicode(street_info['name'][0])
175
                        street_info['name'] = six.text_type(street_info['name'][0])
172 176
                    street = StreetModel.objects.get_or_create(citycode=street_info['citycode'],
173 177
                                                               name=street_info['name'][:150])
174 178
                    street[0].city = street_info['city']
......
205 209
        ordering = ['unaccent_name', 'name']
206 210

  
207 211
    def __unicode__(self):
208
        return unicode(self.name)
212
        return self.name
209 213

  
210 214
    def save(self, *args, **kwargs):
211 215
        self.unaccent_name = unicodedata.normalize('NFKD', self.name).encode('ascii', 'ignore')
tests/test_base_adresse.py
10 10

  
11 11
from passerelle.apps.base_adresse.models import BaseAdresse, StreetModel
12 12

  
13
from passerelle.views import WrongParameter
14

  
15

  
16
FAKED_CONTENT = '{"limit": 1, "attribution": "BAN", "version": "draft", "licence": "ODbL 1.0", "query": "plop", "type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [-0.593775, 47.474633]}, "properties": {"citycode": "49007", "name": "Rue Roger Halope", "id": "49007_6950_be54bd", "city": "Angers", "context": "49, Maine-et-Loire, Pays de la Loire", "score": 0.14097272727272728, "label": "Rue Roger Halope 49000 Angers", "postcode": "49000", "type": "street"}, "type": "Feature"}]}'
13
FAKED_CONTENT = json.dumps({
14
    "limit": 1,
15
    "attribution": "BAN",
16
    "version": "draft",
17
    "licence": "ODbL 1.0",
18
    "query": "plop",
19
    "type": "FeatureCollection",
20
    "features": [
21
        {
22
            "geometry": {
23
                "type": "Point",
24
                "coordinates": [-0.593775, 47.474633]
25
            },
26
            "properties": {
27
                "citycode": "49007",
28
                "name": "Rue Roger Halope",
29
                "id": "49007_6950_be54bd",
30
                "city": "Angers",
31
                "context": "49, Maine-et-Loire, Pays de la Loire",
32
                "score": 0.14097272727272728,
33
                "label": "Rue Roger Halope 49000 Angers",
34
                "postcode": "49000",
35
                "type": "street"
36
            },
37
            "type": "Feature"
38
        }
39
    ]
40
})
17 41

  
18 42
FAKE_DATA = ''
19 43

  
44

  
20 45
@pytest.fixture
21 46
def base_adresse(db):
22 47
    return utils.setup_access_rights(BaseAdresse.objects.create(slug='base-adresse', zipcode='73'))
23 48

  
49

  
24 50
@pytest.fixture
25 51
def base_adresse_97x(db):
26 52
    return utils.setup_access_rights(BaseAdresse.objects.create(slug='base-adresse',
27 53
                                                                zipcode='97425'))
28 54

  
55

  
29 56
@pytest.fixture
30 57
def base_adresse_corsica(db):
31 58
    return utils.setup_access_rights(BaseAdresse.objects.create(slug='base-adresse',
32 59
                                                                zipcode='20000, 20100 '))
33 60

  
61

  
34 62
@pytest.fixture
35 63
def base_adresse_multiple(db):
36 64
    return utils.setup_access_rights(BaseAdresse.objects.create(slug='base-adresse',
37 65
                                                                zipcode='73, 73100, 97425,20000 '))
38 66

  
67

  
39 68
@pytest.fixture
40 69
def street(db):
41 70
    return StreetModel.objects.create(city=u'Chambéry',
......
44 73
                                      type=u'street',
45 74
                                      citycode=u'73001')
46 75

  
76

  
47 77
@mock.patch('passerelle.utils.Request.get')
48 78
def test_base_adresse_search(mocked_get, app, base_adresse):
49 79
    endpoint = utils.generic_endpoint_url('base-adresse', 'search', slug=base_adresse.slug)
......
55 85
    assert data['lon'] == '-0.593775'
56 86
    assert data['display_name'] == 'Rue Roger Halope 49000 Angers'
57 87

  
88

  
58 89
@mock.patch('passerelle.utils.Request.get')
59 90
def test_base_adresse_search_path(mocked_get, app, base_adresse):
60 91
    base_adresse.service_url = 'http://example.net/path/'
......
68 99
    assert data['lon'] == '-0.593775'
69 100
    assert data['display_name'] == 'Rue Roger Halope 49000 Angers'
70 101

  
102

  
71 103
def test_base_adresse_search_qs(app, base_adresse, mock_api_adresse_data_gouv_fr_search):
72 104
    resp = app.get('/base-adresse/%s/search?q=plop' % base_adresse.slug)
73 105
    assert 'display_name' in resp.json[0]
......
77 109
    resp = app.get('/base-adresse/%s/search?q=plop&zipcode=49000' % base_adresse.slug)
78 110
    assert 'display_name' in resp.json[0]
79 111

  
112

  
80 113
def test_base_adresse_search_qs_empty(app, base_adresse, mock_api_adresse_data_gouv_fr_search):
81 114
    resp = app.get('/base-adresse/%s/search?q=' % base_adresse.slug)
82 115
    assert len(resp.json) == 0
83 116

  
117

  
84 118
def test_base_adresse_search_qs_parameters_error(app, base_adresse,
85 119
                                                 mock_api_adresse_data_gouv_fr_search):
86 120
    resp = app.get('/base-adresse/%s/search' % base_adresse.slug, status=400)
......
105 139
    assert data['address']['postcode'] == '49000'
106 140
    assert data['address']['citycode'] == '49007'
107 141

  
142

  
108 143
@mock.patch('passerelle.utils.Request.get')
109 144
def test_base_adresse_reverse_path(mocked_get, app, base_adresse):
110 145
    mocked_get.return_value = utils.FakedResponse(content=json.dumps({'features': []}), status_code=200)
111 146
    base_adresse.service_url = 'http://example.net/path/'
112 147
    base_adresse.save()
113
    resp = app.get('/base-adresse/%s/reverse?lon=-0.593775&lat=47.474633' % base_adresse.slug)
148
    app.get('/base-adresse/%s/reverse?lon=-0.593775&lat=47.474633' % base_adresse.slug)
114 149
    assert mocked_get.call_args[0][0].startswith('http://example.net/path/reverse/?')
115 150

  
151

  
116 152
def test_base_adresse_streets_unaccent(app, base_adresse, street):
117 153
    resp = app.get('/base-adresse/%s/streets?q=une rue tres acc' % base_adresse.slug)
118 154
    data = json.loads(resp.body)
......
124 160
    assert result['zipcode'] == street.zipcode
125 161
    assert result['id'] == street.id
126 162

  
163

  
127 164
@mock.patch('passerelle.utils.Request.get')
128 165
def test_base_adresse_command_update(mocked_get, db, base_adresse):
129 166
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'update_streets_test.bz2')
......
142 179
    call_command('cron', 'daily')
143 180
    mocked_get.call_count == 2
144 181

  
182

  
145 183
@mock.patch('passerelle.utils.Request.get')
146 184
def test_base_adresse_command_hourly_update(mocked_get, db, base_adresse):
147 185
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'update_streets_test.bz2')
......
154 192
    call_command('cron', 'hourly')
155 193
    mocked_get.call_count == 1
156 194

  
195

  
157 196
@mock.patch('passerelle.utils.Request.get')
158 197
def test_base_adresse_command_update_97x(mocked_get, db, base_adresse_97x):
159 198
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'update_streets_test.bz2')
......
162 201
    mocked_get.assert_called_once_with('http://bano.openstreetmap.fr/BAN_odbl/BAN_odbl_974-json.bz2')
163 202
    assert StreetModel.objects.count() == 2
164 203

  
204

  
165 205
@mock.patch('passerelle.utils.Request.get')
166 206
def test_base_adresse_command_update_corsica(mocked_get, db, base_adresse_corsica):
167 207
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'update_streets_test.bz2')
......
172 212
    mocked_get.assert_any_call('http://bano.openstreetmap.fr/BAN_odbl/BAN_odbl_2B-json.bz2')
173 213
    assert StreetModel.objects.count() == 0
174 214

  
215

  
175 216
@mock.patch('passerelle.utils.Request.get')
176 217
def test_base_adresse_command_update_multiple(mocked_get, db, base_adresse_multiple):
177 218
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'update_streets_test.bz2')
178
-