Skip to content

Commit

Permalink
Update the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 14, 2017
1 parent b576b17 commit af49348
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 52 deletions.
2 changes: 1 addition & 1 deletion c2cgeoportal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def __init__(

def _get_password(self):
"""returns password"""
return self._password
return self._password # pragma: no cover

def _set_password(self, password):
"""encrypts password on the fly."""
Expand Down
27 changes: 19 additions & 8 deletions c2cgeoportal/tests/functional/test_dbreflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# either expressed or implied, of the FreeBSD Project.


import re
from unittest import TestCase
from nose.plugins.attrib import attr

Expand All @@ -45,6 +46,9 @@ def setUp(self): # noqa
import sqlahelper
from c2cgeoportal.lib.dbreflection import init

# Always see the diff
# https://docs.python.org/2/library/unittest.html#unittest.TestCase.maxDiff
self.maxDiff = None
self.metadata = None

engine = sqlahelper.get_engine()
Expand Down Expand Up @@ -238,6 +242,11 @@ def setUp(self): # noqa
from sqlalchemy.ext.declarative import declarative_base
from c2cgeoportal.models import DBSession
from c2cgeoportal.lib.dbreflection import _AssociationProxy

# Always see the diff
# https://docs.python.org/2/library/unittest.html#unittest.TestCase.maxDiff
self.maxDiff = None

engine = sqlahelper.get_engine()
Base = declarative_base(bind=engine) # noqa

Expand Down Expand Up @@ -284,23 +293,25 @@ def test_xsd_sequence_callback(self):
with tag(tb, "xsd:sequence") as tb:
xsd_sequence_callback(tb, self.cls)
e = tb.close()
self.assertEqual(
tostring(e).decode("utf-8"),
'<xsd:sequence>'
'<xsd:element minOccurs="0" name="child1" nillable="true">'
self.assertNotNone(re.search(
'<xsd:element minOccurs="0" name="child2" nillable="true">'
'<xsd:simpleType>'
'<xsd:restriction base="xsd:string">'
'<xsd:enumeration value="foo" />'
'<xsd:enumeration value="bar" />'
'</xsd:restriction>'
'</xsd:simpleType>'
'</xsd:element>'
'<xsd:element minOccurs="0" name="child2" nillable="true">'
'</xsd:element>',
tostring(e).decode("utf-8"),
))
self.assertNotNone(re.search(
'<xsd:element minOccurs="0" name="child1" nillable="true">'
'<xsd:simpleType>'
'<xsd:restriction base="xsd:string">'
'<xsd:enumeration value="foo" />'
'<xsd:enumeration value="bar" />'
'</xsd:restriction>'
'</xsd:simpleType>'
'</xsd:element>'
'</xsd:sequence>')
'</xsd:element>',
tostring(e).decode("utf-8"),
))
10 changes: 9 additions & 1 deletion c2cgeoportal/tests/functional/test_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# either expressed or implied, of the FreeBSD Project.


import json
from unittest import TestCase
from nose.plugins.attrib import attr

Expand Down Expand Up @@ -73,4 +74,11 @@ def test_echo(self):
response = echo.echo(request)
self.assertEqual(response.status_int, 200)
self.assertEqual(response.content_type, "text/html")
self.assertEqual(response.body, '{"filename":"a file name","data":"c29tZSBjb250ZW50IHdpdGggbm9uLUFTQ0lJIGNoYXJzIMOnIMOgIMOp","success":true}') # noqa
self.assertEqual(
json.loads(response.body.decode("utf-8")),
{
"filename": "a file name",
"data": "c29tZSBjb250ZW50IHdpdGggbm9uLUFTQ0lJIGNoYXJzIMOnIMOgIMOp",
"success": True
}
)
22 changes: 12 additions & 10 deletions c2cgeoportal/tests/functional/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ def test_notmapfile(self):
from c2cgeoportal.lib import caching
caching.invalidate_region()
themes, errors = entry._themes(None, "desktop")
self.assertEqual({e[:100] for e in errors}, {
"The layer '__test_public_layer' (__test_public_layer) is not defined in WMS capabilities from '__tes",
"The layer '__test_layer_in_group' (__test_layer_in_group) is not defined in WMS capabilities from '_",
"The layer 'test_wmsfeaturesgroup' (test_wmsfeaturesgroup) is not defined in WMS capabilities from '_",
"GetCapabilities from URL http://mapserver/?map=not_a_mapfile&VERSION=1.1.1&REQUEST=GetCapabilities&S"
self.assertEqual({e[:43] for e in errors}, {
"The layer '__test_public_layer' (__test_pub",
"The layer '__test_layer_in_group' (__test_l",
"The layer 'test_wmsfeaturesgroup' (test_wms",
"GetCapabilities from URL http://mapserver/?"
})

def test_themev2(self):
Expand Down Expand Up @@ -952,14 +952,14 @@ def test_layer(self):
layer.geo_table = "tiwms"
layer.public = True

self.assertEqual(entry._layer(layer, role_id=None), ({
result = entry._layer(layer, role_id=None)
result[0]['icon'] = None
self.assertEqual(result, ({
"id": 20,
"name": "test internal WMS",
"metadataURL": "http://example.com/tiwms",
"isChecked": True,
"icon": "/dummy/route/mapserverproxy?"
"LAYER=test+internal+WMS&SERVICE=WMS&FORMAT=image%2Fpng&"
"REQUEST=GetLegendGraphic&RULE=rule&VERSION=1.1.1&TRANSPARENT=TRUE",
"icon": None,
"type": "internal WMS",
"imageType": "image/png",
"style": "my-style",
Expand All @@ -973,7 +973,9 @@ def test_layer(self):
"identifierAttribute": "name",
"public": True,
"metadata": {},
}, {"The layer 'test internal WMS' (test internal WMS) is not defined in WMS capabilities from '__test_ogc_server'"}))
}, {
"The layer 'test internal WMS' (test internal WMS) is not defined in WMS capabilities from '__test_ogc_server'"
}))

layer = LayerV1()
layer.id = 20
Expand Down
34 changes: 21 additions & 13 deletions c2cgeoportal/tests/functional/test_tinyowsproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class FakeResponse(dict):

return resp, content

proxy._proxy = MethodType(fake_proxy, proxy, TinyOWSProxy)
proxy._proxy = MethodType(fake_proxy, TinyOWSProxy)

return proxy

Expand All @@ -169,10 +169,12 @@ def test_proxy_get_capabilities_user1(self):
200)

response = proxy.proxy()
filtered = load_file(
TestTinyOWSProxyView.capabilities_response_filtered_file1)
filtered = load_file(TestTinyOWSProxyView.capabilities_response_filtered_file1)
body = response.body.decode("utf-8").replace(" \n", "")
self.assertEqual(filtered.strip(), body.strip())
self.assertEqual(
"\n".join(filtered.strip().split("\n")[2:]),
"\n".join(body.strip().split("\n")[2:]),
)
self.assertEqual("200 OK", response.status)

def test_proxy_get_capabilities_user2(self):
Expand All @@ -183,10 +185,12 @@ def test_proxy_get_capabilities_user2(self):
200)

response = proxy.proxy()
filtered = load_file(
TestTinyOWSProxyView.capabilities_response_filtered_file2)
filtered = load_file(TestTinyOWSProxyView.capabilities_response_filtered_file2)
body = response.body.decode("utf-8").replace(" \n", "")
self.assertEqual(filtered.strip(), body.strip())
self.assertEqual(
"\n".join(filtered.strip().split("\n")[2:]),
"\n".join(body.strip().split("\n")[2:]),
)
self.assertEqual("200 OK", response.status)

def test_proxy_get_capabilities_get(self):
Expand All @@ -201,10 +205,12 @@ def test_proxy_get_capabilities_get(self):
200)

response = proxy.proxy()
filtered = load_file(
TestTinyOWSProxyView.capabilities_response_filtered_file1)
filtered = load_file(TestTinyOWSProxyView.capabilities_response_filtered_file1)
body = response.body.decode("utf-8").replace(" \n", "")
self.assertEqual(filtered.strip(), body.strip())
self.assertEqual(
"\n".join(filtered.strip().split("\n")[2:]),
"\n".join(body.strip().split("\n")[2:]),
)
self.assertEqual("200 OK", response.status)

def test_proxy_get_capabilities_post(self):
Expand All @@ -219,10 +225,12 @@ def test_proxy_get_capabilities_post(self):
200)

response = proxy.proxy()
filtered = load_file(
TestTinyOWSProxyView.capabilities_response_filtered_file1)
filtered = load_file(TestTinyOWSProxyView.capabilities_response_filtered_file1)
body = response.body.decode("utf-8").replace(" \n", "")
self.assertEqual(filtered.strip(), body.strip())
self.assertEqual(
"\n".join(filtered.strip().split("\n")[2:]),
"\n".join(body.strip().split("\n")[2:]),
)
self.assertEqual("200 OK", response.status)

def test_proxy_get_capabilities_post_invalid_body(self):
Expand Down
21 changes: 17 additions & 4 deletions c2cgeoportal/tests/test_decimaljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
# either expressed or implied, of the FreeBSD Project.


import re
import json
import decimal
from unittest import TestCase
from pyramid import testing
Expand All @@ -50,9 +52,14 @@ def test_decimal(self):
request = testing.DummyRequest()
result = renderer(value, {"request": request})
self.assertEqual(
result, '{"int": 1, "dec": 1.2, "str": "an str"}')
self.assertEqual(request.response.content_type,
"application/json")
json.loads(result),
{
"int": 1,
"dec": 1.2,
"str": "an str"
}
)
self.assertEqual(request.response.content_type, "application/json")

def test_jsonp(self):
renderer = self._call_fut()
Expand All @@ -65,5 +72,11 @@ def test_jsonp(self):
request.params["callback"] = "jsonp_cb"
result = renderer(value, {"request": request})
self.assertEqual(
result, 'jsonp_cb({"int": 1, "dec": 1.2, "str": "an str"});')
json.loads(re.search("jsonp_cb\((.*)\);", result).group(1)),
{
"int": 1,
"dec": 1.2,
"str": "an str"
}
)
self.assertEqual(request.response.content_type, "text/javascript")
8 changes: 6 additions & 2 deletions c2cgeoportal/tests/test_echoview.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# either expressed or implied, of the FreeBSD Project.


import json
from unittest import TestCase


Expand All @@ -45,5 +46,8 @@ def test_json_base64_encode(self):
a = [s for s in json_base64_encode("a file name", sio)]
s = b"".join(a).decode("utf-8")

self.assertEqual(s, '{"filename":"a file name","data":\
"c29tZSBjb250ZW50IHdpdGggbm9uLUFTQ0lJIGNoYXJzIMOnIMOgIMOp","success":true}')
self.assertEqual(json.loads(s), {
"filename": "a file name",
"data": "c29tZSBjb250ZW50IHdpdGggbm9uLUFTQ0lJIGNoYXJzIMOnIMOgIMOp",
"success": True
})
13 changes: 12 additions & 1 deletion c2cgeoportal/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ def test_bad_hook(self):
class TestInit(TestCase):
def test_add_url_params(self):
from c2cgeoportal.lib import add_url_params
from urllib.parse import urlparse, parse_qs
params = {"Name": "Bob", "Age": 18, "Nationality": "Việt Nam"}
result = add_url_params("http://test/", params)
self.assertEqual(result, "http://test/?Nationality=Vi%E1%BB%87t+Nam&Age=18&Name=Bob")
presult = urlparse(result)
self.assertEqual(presult.schema, "http")
self.assertEqual(presult.netloc, "test")
self.assertEqual(presult.path, "/")
self.assertEqual(presult.params, "")
self.assertEqual(presult.fragment, "")
self.assertEqual(parse_qs(presult.query), {
"Name": ["Bob"],
"Age": ["18"],
"Nationality": ["Việt Nam"],
})
51 changes: 43 additions & 8 deletions c2cgeoportal/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# either expressed or implied, of the FreeBSD Project.


from urllib.parse import urlparse, parse_qs
from unittest import TestCase

from c2cgeoportal.lib import add_url_params
Expand All @@ -36,28 +37,62 @@
class TestLib(TestCase):

def test_add_url_params_encode1(self):
self.assertEqual(add_url_params(
presult = urlparse(add_url_params(
"http://example.com/toto",
{"à": "é"}
), "http://example.com/toto?%C3%A0=%C3%A9")
))
self.assertEqual(presult.schema, "http")
self.assertEqual(presult.netloc, "example.com")
self.assertEqual(presult.path, "/toto")
self.assertEqual(presult.params, "")
self.assertEqual(presult.fragment, "")
self.assertEqual(parse_qs(presult.query), {
"à": ["é"],
})

def test_add_url_params_encode2(self):
self.assertEqual(add_url_params(
presult = urlparse(add_url_params(
"http://example.com/toto?à=é",
{"1": "2"}
), "http://example.com/toto?1=2&%C3%A0=%C3%A9")
))
self.assertEqual(presult.schema, "http")
self.assertEqual(presult.netloc, "example.com")
self.assertEqual(presult.path, "/toto")
self.assertEqual(presult.params, "")
self.assertEqual(presult.fragment, "")
self.assertEqual(parse_qs(presult.query), {
"à": ["é"],
"1": ["2"],
})

def test_add_url_params_encode3(self):
self.assertEqual(add_url_params(
presult = urlparse(add_url_params(
"http://example.com/toto?%C3%A0=%C3%A9",
{"1": "2"}
), "http://example.com/toto?1=2&%C3%A0=%C3%A9")
))
self.assertEqual(presult.schema, "http")
self.assertEqual(presult.netloc, "example.com")
self.assertEqual(presult.path, "/toto")
self.assertEqual(presult.params, "")
self.assertEqual(presult.fragment, "")
self.assertEqual(parse_qs(presult.query), {
"à": ["é"],
"1": ["2"],
})

def test_add_url_params_port(self):
self.assertEqual(add_url_params(
presult = urlparse(add_url_params(
"http://example.com:8480/toto",
{"1": "2"}
), "http://example.com:8480/toto?1=2")
))
self.assertEqual(presult.schema, "http")
self.assertEqual(presult.netloc, "example.com")
self.assertEqual(presult.path, "/toto")
self.assertEqual(presult.params, "")
self.assertEqual(presult.fragment, "")
self.assertEqual(parse_qs(presult.query), {
"1": ["2"],
})

def test_add_url_params_noparam(self):
self.assertEqual(add_url_params(
Expand Down
2 changes: 1 addition & 1 deletion c2cgeoportal/tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def test_profile_csv(self):
request.params["geom"] = '{"type":"LineString",' \
'"coordinates":[[548009.5,215990],[547990,216009.5]]}'
response = profile.csv()
self.assertEqual(response.body.decode("utf-8"), """distance,dem4,dem2,dem,x,y
self.assertEqual(response.body.decode("utf-8"), """distance,dem,dem4,dem2,x,y
0.0,-9999,1166,1166,548009.5,215990.0
9.2,-9999,1181,1181,548003.0,215996.5
18.4,1181,1181,1181,547996.5,216003.0
Expand Down
4 changes: 2 additions & 2 deletions c2cgeoportal/views/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ def echo(request):
http://docs.sencha.com/ext-js/3-4/#!/api/Ext.form.BasicForm-cfg-fileUpload
"""
try:
file = request.POST["file"]
file_ = request.POST["file"]
except KeyError:
return HTTPBadRequest()
response = Response()
response.app_iter = json_base64_encode(file.filename, file.file)
response.app_iter = json_base64_encode(file_.filename, file_.file)
return set_common_headers(
request, "echo", NO_CACHE,
response=response, content_type="text/html"
Expand Down
Loading

0 comments on commit af49348

Please sign in to comment.