Completed
Push — master ( b7e6d1...4d8241 )
by
unknown
01:17
created

test_check_erfgoedgemeente_partial_overlap()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 16
loc 16
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import os
3
import unittest
4
import json
5
6
from paste.deploy import appconfig
7
from webtest import TestApp
8
from pyramid import testing
9
import responses
10
11
try:
12
    from testdata import test_json_outside_flanders, test_json_intersects_flanders
13
except:
14
    from tests.testdata import test_json_outside_flanders, test_json_intersects_flanders
15
16
try:
17
    from __init__ import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results
18
except:
19
    from tests import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results
20
21
from oe_geoutils import main
22
23
from oe_geoutils.views.exceptions import internal_server_error
24
25
here = os.path.dirname(__file__)
26
27
test_geom = json.dumps({
28
            "type": "MultiPolygon",
29
            "coordinates": [[[[172933.6922879719058983, 174851.14960918109863997],
30
                              [172930.21180502674542367, 174832.7836931711062789],
31
                              [172920.64762709615752101, 174848.13247794657945633],
32
                              [172933.6922879719058983, 174851.14960918109863997]]]],
33
            "crs": {
34
                "type": "name",
35
                "properties": {
36
                    "name": "urn:ogc:def:crs:EPSG::31370"
37
                }
38
            }
39
        })
40
41
42
class FunctionalTests(unittest.TestCase):
43
    def _get_default_headers(self):
44
        return {'Accept': 'application/json'}
45
46
    @classmethod
47
    def setUpClass(cls):
48
        cls.settings = appconfig('config:' + os.path.join(here, 'test.ini'))
49
50
    def setUp(self):
51
        self.app = main({}, **self.settings)
52
        self.testapp = TestApp(self.app)
53
        responses.add(responses.POST, "https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen")
54
55
    def tearDown(self):
56
        self.testapp.reset()
57
58
    def test_get_nearest_address(self):
59
        res = self.testapp.post('/nearest_address', test_geom)
60
        self.assertEqual('200 OK', res.status)
61
62
    def test_get_nearest_address_outside_Flanders(self):
63
        res = self.testapp.post('/nearest_address', json.dumps(test_json_outside_flanders), expect_errors=True)
64
        self.assertEqual('400 Bad Request', res.status)
65
66
    def test_get_nearest_address_not_found(self):
67
        res = self.testapp.post('/nearest_address', json.dumps(test_json_intersects_flanders), expect_errors=True)
68
        self.assertEqual('200 OK', res.status)
69
70
    def test_check_in_flanders(self):
71
        res = self.testapp.post('/check_in_flanders', test_geom)
72
        self.assertEqual('200 OK', res.status)
73
74
    def test_check_within_flanders(self):
75
        res = self.testapp.post('/check_within_flanders', test_geom)
76
        self.assertEqual('200 OK', res.status)
77
78
    def test_check_in_flanders_no_json_body(self):
79
        res = self.testapp.post('/check_in_flanders', expect_errors=True)
80
        self.assertEqual('400 Bad Request', res.status)
81
82
    def test_check_in_flanders_validation_failure(self):
83
        res = self.testapp.post('/check_in_flanders', '{}', expect_errors=True)
84
        self.assertEqual('400 Bad Request', res.status)
85
86
    def test_check_in_flanders_invalid_url(self):
87
        res = self.testapp.post('/test', '{}', expect_errors=True)
88
        self.assertEqual('404 Not Found', res.status)
89
90
    def test_internal_server_error(self):
91
        a = Exception()
92
        internal_server_error(a, testing.DummyRequest())
93
94
    @responses.activate
95
    def test_gemeente(self):
96
        res = self.testapp.post('/gemeente', test_geom)
97
        self.assertEqual('200 OK', res.status)
98
        print res.text
99
100
    @responses.activate
101
    def test_gemeente(self):
102
        responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_gemeente_results))
103
        res = self.testapp.post('/gemeente', test_geom)
104
        self.assertEqual('200 OK', res.status)
105
        print res.text
106
107
    @responses.activate
108
    def test_provincie(self):
109
        responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_provincie_results))
110
        res = self.testapp.post('/provincie', test_geom)
111
        self.assertEqual('200 OK', res.status)
112
        print res.text
113
114 View Code Duplication
    @responses.activate
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
115
    def test_check_erfgoedgemeente(self):
116
        contour = {
117
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
118
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
119
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
120
        }
121
        responses.add(
122
            responses.POST,
123
            'http://geozoekdienst.en',
124
            body='[{"naam": "Leuven", "type": "gemeente", "id": "24062"}]', status=200)
125
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
126
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
127
        self.assertIn('ok', res.json["status"])
128
129 View Code Duplication
    @responses.activate
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
    def test_check_erfgoedgemeente_full_overlap(self):
131
        responses.add(
132
            responses.POST,
133
            'http://geozoekdienst.en',
134
            body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}]', status=200)
135
        contour = {
136
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
137
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
138
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
139
        }
140
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
141
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
142
        self.assertIn('error', res.json["status"])
143
        self.assertIn('Gelieve de melding in te dienen bij deze gemeente', res.json["message"])
144
145 View Code Duplication
    @responses.activate
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
146
    def test_check_erfgoedgemeente_partial_overlap(self):
147
        responses.add(
148
            responses.POST,
149
            'http://geozoekdienst.en',
150
            body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}, '
151
                 '{"naam": "Nieuwpoort", "type": "gemeente", "id": "38016"}]', status=200)
152
        contour = {
153
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
154
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
155
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
156
        }
157
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
158
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
159
        self.assertIn('warn', res.json["status"])
160
        self.assertIn('Gelieve de melding vooronderzoek eveneens in te dienen bij deze gemeente', res.json['message'])
161
162
163
164