Skip to content

Commit

Permalink
Fix regex test
Browse files Browse the repository at this point in the history
refactor some tests
  • Loading branch information
Stéphane Brunner committed Jun 2, 2013
1 parent 944163b commit 75559f5
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 127 deletions.
118 changes: 64 additions & 54 deletions tilecloud_chain/__init__.py
Expand Up @@ -187,33 +187,34 @@ def __init__(self, config_file, options=None, layer_name=None):
layer['meta_size'] = 1
else:
error = self.validate(layer, name, 'meta_size', attribute_type=int, default=8) or error
error = self.validate(
layer, name, 'meta_buffer', attribute_type=int,
default=0 if layer['type'] == 'mapnik' else 128
) or error

if layer['type'] == 'wms':
error = self.validate(layer, name, 'url', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'layers', attribute_type=str, required=True) or error
if layer['type'] == 'mapnik':
error = self.validate(layer, name, 'mapfile', attribute_type=str, required=True) or error
if 'type' in layer:
error = self.validate(
layer, name, 'output_format', attribute_type=str, default='png',
enumeration=['png', 'png256', 'jpeg', 'grid']
layer, name, 'meta_buffer', attribute_type=int,
default=0 if layer['type'] == 'mapnik' else 128
) or error
error = self.validate(layer, name, 'data_buffer', attribute_type=int, default=128) or error
if layer['output_format'] == 'grid':
error = self.validate(layer, name, 'resolution', attribute_type=int, default=4) or error
error = self.validate(layer, name, 'layers_fields', attribute_type=dict, default={}) or error

if layer['type'] == 'wms':
error = self.validate(layer, name, 'url', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'layers', attribute_type=str, required=True) or error
if layer['type'] == 'mapnik':
error = self.validate(layer, name, 'mapfile', attribute_type=str, required=True) or error
error = self.validate(
layer, name, 'drop_empty_utfgrid', attribute_type=bool, default=False
layer, name, 'output_format', attribute_type=str, default='png',
enumeration=['png', 'png256', 'jpeg', 'grid']
) or error
if layer['meta']:
logger.error(
"The layer '%s' is of type Mapnik/Grid, that can't support matatiles." %
(layer['name'])
)
error = True
error = self.validate(layer, name, 'data_buffer', attribute_type=int, default=128) or error
if layer['output_format'] == 'grid':
error = self.validate(layer, name, 'resolution', attribute_type=int, default=4) or error
error = self.validate(layer, name, 'layers_fields', attribute_type=dict, default={}) or error
error = self.validate(
layer, name, 'drop_empty_utfgrid', attribute_type=bool, default=False
) or error
if layer['meta']:
logger.error(
"The layer '%s' is of type Mapnik/Grid, that can't support matatiles." %
(layer['name'])
)
error = True

error = self.validate(layer, name, 'extension', attribute_type=str, required=True) or error
error = self.validate(layer, name, 'mime_type', attribute_type=str, required=True) or error
Expand All @@ -224,7 +225,7 @@ def __init__(self, config_file, options=None, layer_name=None):
for d in layer['dimensions']:
dname = name + ".dimensions[%s]" % d.get('name', '')
error = self.validate(
d, dname, 'name', attribute_type=str, required=True, regex="^[a-zA-Z0-9_]+$"
d, dname, 'name', attribute_type=str, required=True, regex="^[A-Z0-9_]+$"
) or error
error = self.validate(
d, dname, 'value', attribute_type=str, required=True, regex="^[a-zA-Z0-9_]+$"
Expand Down Expand Up @@ -489,8 +490,8 @@ def _validate_type(self, value, attribute_type, enumeration, regex=None):
return (True, None, str(attribute_type))
if typ != str:
value = str(value)
if regex:
if re.search(regex, value) is None: # pragma: no cover
if regex is not None:
if re.search(regex, value) is None:
return (True, None, "value '%s' don't respect regex '%s'" % (value, regex))
else:
if type(value) != attribute_type:
Expand All @@ -502,35 +503,44 @@ def _validate_type(self, value, attribute_type, enumeration, regex=None):
def validate(
self, obj, obj_name, attribute, attribute_type=None, is_array=False,
default=None, required=False, enumeration=None, **kargs):
if attribute in obj:
if is_array:
if type(obj[attribute]) == list:
for n, v in enumerate(obj[attribute]):
result, value, type_error = self._validate_type(v, attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s has an element who is not a %s." %
(attribute, obj_name, type_error)
)
return True
obj[attribute][n] = value
else:
logger.error("The attribute '%s' of the object %s is not an array." % (attribute, obj_name))
return True
if attribute not in obj:
if required:
logger.error("The attribute '%s' is required in the object %s." % (attribute, obj_name))
return True
elif default is False:
# no value
obj[attribute] = False
# no test
return False
elif default is not None:
obj[attribute] = default
else:
result, value, type_error = self._validate_type(obj[attribute], attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s is not a %s." %
(attribute, obj_name, type_error)
)
return True
obj[attribute] = value
elif required:
logger.error("The attribute '%s' is required in the object %s." % (attribute, obj_name))
return True
elif default is not None:
obj[attribute] = default
# no value to test
return False

if is_array:
if type(obj[attribute]) == list:
for n, v in enumerate(obj[attribute]):
result, value, type_error = self._validate_type(v, attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s has an element who is not a %s." %
(attribute, obj_name, type_error)
)
return True
obj[attribute][n] = value
else:
logger.error("The attribute '%s' of the object %s is not an array." % (attribute, obj_name))
return True
else:
result, value, type_error = self._validate_type(obj[attribute], attribute_type, enumeration, **kargs)
if result:
logger.error(
"The attribute '%s' of the object %s is not a %s." %
(attribute, obj_name, type_error)
)
return True
obj[attribute] = value
return False

def set_layer(self, layer, options):
Expand Down
46 changes: 23 additions & 23 deletions tilecloud_chain/tests/__init__.py
Expand Up @@ -13,27 +13,27 @@

class CompareCase(TestCase):

def assert_result_equals(self, result, content, regexp=False):
content = unicode(content.decode('utf-8'))
content = re.sub(u'\n[^\n]*\r', u'\n', content)
content = re.sub(u'^[^\n]*\r', u'', content)
content = content.split('\n')
value = result.split('\n')
for n, test in enumerate(zip(content, value)):
def assert_result_equals(self, result, expected, regex=False):
expected = expected.split('\n')
result = unicode(result.decode('utf-8'))
result = re.sub(u'\n[^\n]*\r', u'\n', result)
result = re.sub(u'^[^\n]*\r', u'', result)
result = result.split('\n')
for n, test in enumerate(zip(expected, result)):
if test[0] != 'PASS...':
try:
if regexp:
self.assertRegexpMatches(test[0].strip(), test[1].strip())
if regex:
self.assertRegexpMatches(test[1].strip(), '^%s$' % test[0].strip())
else:
self.assertEquals(test[0].strip(), test[1].strip())
except AssertionError as e: # pragma: no cover
for i in range(max(0, n - 10), min(len(content), n + 11)):
for i in range(max(0, n - 10), min(len(result), n + 11)):
if i == n:
log.info("> %i %s" % (i, content[i]))
log.info("> %i %s" % (i, result[i]))
else:
log.info(" %i %s" % (i, content[i]))
log.info(" %i %s" % (i, result[i]))
raise e
self.assertEquals(len(value), len(content))
self.assertEquals(len(expected), len(result))

def run_cmd(self, cmd, main_func):
old_stdout = sys.stdout
Expand All @@ -51,26 +51,26 @@ def assert_cmd_equals(self, cmd, main_func, empty_err=False, **kargs):
out, err = self.run_cmd(cmd, main_func)
if empty_err:
self.assertEquals(err, '')
self.assert_result_equals(content=out, **kargs)
self.assert_result_equals(result=out, **kargs)

def assert_cmd_exit_equals(self, cmd, main_func, result):
def assert_cmd_exit_equals(self, cmd, main_func, expected):
sys.argv = cmd.split(' ')
try:
main_func()
assert("exit() not called.") # pragma: no cover
except SystemExit as e:
self.assertEquals(e.message, result)
self.assertEquals(e.message, expected)

def assert_main_equals(self, cmd, main_func, results, **kargs):
def assert_main_equals(self, cmd, main_func, expected, **kargs):
sys.argv = cmd.split(' ')
try:
main_func()
except SystemExit:
pass
if results:
for result in results:
f = open(result[0], 'r')
self.assert_result_equals(f.read(), result[1], **kargs)
if expected:
for expect in expected:
f = open(expect[0], 'r')
self.assert_result_equals(f.read(), expect[1], **kargs)

def assert_yaml_equals(self, content, value):
import yaml
Expand All @@ -91,8 +91,8 @@ def assert_tiles_generated(self, directory, **kargs):

self.assert_tiles_generated_deleted(directory=directory, **kargs)

def assert_tiles_generated_deleted(self, directory, tiles_pattern, tiles, result='', **kargs):
self.assert_cmd_equals(result=result, **kargs)
def assert_tiles_generated_deleted(self, directory, tiles_pattern, tiles, expected='', **kargs):
self.assert_cmd_equals(expected=expected, **kargs)
count = 0
for path, dirs, files in os.walk(directory):
if len(files) != 0:
Expand Down
28 changes: 14 additions & 14 deletions tilecloud_chain/tests/test_controller.py
Expand Up @@ -27,9 +27,9 @@ def test_capabilities(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --capabilities -c tilegeneration/test.yaml',
main_func=controller.main,
results=[[
expected=[[
'/tmp/tiles/1.0.0/WMTSCapabilities.xml',
"""<?xml version="1.0" encoding="UTF-8"?>
u"""<?xml version="1.0" encoding="UTF-8"?>
<Capabilities version="1.0.0" xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
Expand Down Expand Up @@ -434,7 +434,7 @@ def test_capabilities(self):
</Contents>
</Capabilities>"""]])

MULTIHOST_CAPABILITIES = """<?xml version="1.0" encoding="UTF-8"?>
MULTIHOST_CAPABILITIES = u"""<?xml version="1.0" encoding="UTF-8"?>
<Capabilities version="1.0.0" xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
Expand Down Expand Up @@ -938,7 +938,7 @@ def test_multi_host_capabilities(self):
cmd='./buildout/bin/generate_controller --capabilities -c tilegeneration/test.yaml '
'--destination-cache multi_host',
main_func=controller.main,
results=[['/tmp/tiles/1.0.0/WMTSCapabilities.xml', self.MULTIHOST_CAPABILITIES]])
expected=[['/tmp/tiles/1.0.0/WMTSCapabilities.xml', self.MULTIHOST_CAPABILITIES]])

@attr(multi_url_capabilities=True)
@attr(general=True)
Expand All @@ -947,15 +947,15 @@ def test_multi_url_capabilities(self):
cmd='./buildout/bin/generate_controller --capabilities -c tilegeneration/test.yaml '
'--destination-cache multi_url',
main_func=controller.main,
results=[['/tmp/tiles/1.0.0/WMTSCapabilities.xml', self.MULTIHOST_CAPABILITIES]])
expected=[['/tmp/tiles/1.0.0/WMTSCapabilities.xml', self.MULTIHOST_CAPABILITIES]])

@attr(mapcache=True)
@attr(general=True)
def test_mapcache(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --mapcache -c tilegeneration/test.yaml',
main_func=controller.main,
results=[['mapcache.xml', """<?xml version="1.0" encoding="UTF-8"?>
expected=[['mapcache.xml', u"""<?xml version="1.0" encoding="UTF-8"?>
<mapcache>
<cache name="default" type="memcache">
<server>
Expand Down Expand Up @@ -1242,7 +1242,7 @@ def test_apache(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --apache -c tilegeneration/test.yaml',
main_func=controller.main,
results=[['tiles.conf.in', """<Location /${vars:instanceid}/tiles>
expected=[['tiles.conf.in', u"""<Location /${vars:instanceid}/tiles>
ExpiresActive on
ExpiresDefault "now plus 8 hours"
</Location>
Expand All @@ -1254,7 +1254,7 @@ def test_apache(self):
MapCacheAlias /${vars:instanceid}/mapcache "${buildout:directory}/mapcache.xml"
"""]])

CONFIG = """apache: {config_file: tiles.conf.in, expires: 8}
CONFIG = u"""apache: {config_file: tiles.conf.in, expires: 8}
caches:
local: {folder: /tmp/tiles, hosts: false, http_url: 'http://taurus/tiles', http_urls: false,
name: local, type: filesystem, wmtscapabilities_file: /1.0.0/WMTSCapabilities.xml}
Expand Down Expand Up @@ -1584,7 +1584,7 @@ def test_apache(self):
type: wms
url: http://localhost/mapserv
wmts_style: default
mapcache: {config_file: mapcache.xml, memcache_host: localhost, memcache_port: '11211'}
mapcache: {config_file: mapcache.xml, memcache_host: localhost, memcache_port: 11211}
openlayers: {center_x: 600000.0, center_y: 200000.0, srs: 'epsg:21781'}
sns: {region: eu-west-1, topic: sns_topic}"""

Expand All @@ -1605,7 +1605,7 @@ def test_config_layer(self):
@attr(openlayers=True)
@attr(general=True)
def test_openlayers(self):
html = """<!DOCTYPE html>
html = u"""<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Expand Down Expand Up @@ -1634,7 +1634,7 @@ def test_openlayers(self):
<script src="wmts.js"></script>
</body>
</html>"""
js = """var callback = function(infoLookup) {
js = u"""var callback = function(infoLookup) {
var msg = "";
if (infoLookup) {
var info;
Expand Down Expand Up @@ -1751,7 +1751,7 @@ def test_openlayers(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --ol -c tilegeneration/test.yaml',
main_func=controller.main,
results=[
expected=[
['/tmp/tiles/index.html', html],
['/tmp/tiles/wmts.js', js % 'http://taurus/tiles/1.0.0/WMTSCapabilities.xml']
]
Expand All @@ -1760,7 +1760,7 @@ def test_openlayers(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --ol -c tilegeneration/test.yaml --cache multi_host',
main_func=controller.main,
results=[
expected=[
['/tmp/tiles/index.html', html],
['/tmp/tiles/wmts.js', js % 'http://wmts1/tiles/1.0.0/WMTSCapabilities.xml']
]
Expand All @@ -1769,7 +1769,7 @@ def test_openlayers(self):
self.assert_main_equals(
cmd='./buildout/bin/generate_controller --ol -c tilegeneration/test.yaml --cache multi_url',
main_func=controller.main,
results=[
expected=[
['/tmp/tiles/index.html', html],
['/tmp/tiles/wmts.js', js % 'http://wmts1/tiles/1.0.0/WMTSCapabilities.xml']
]
Expand Down

0 comments on commit 75559f5

Please sign in to comment.