From 321d4ce27c17aee7fceb444538cee30c5c2b53dd Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 16 Apr 2009 17:22:38 +0000 Subject: [PATCH] Add more tests, cleanup existing tests and add TODO functionality to the test runner. --- tests/python_tests/__init__.py | 0 tests/python_tests/font_test.py | 13 +++ tests/python_tests/load_map_test.py | 4 - tests/python_tests/object_test.py | 139 ++++++++++++++++++++++++-- tests/python_tests/projection_test.py | 27 +++++ tests/python_tests/render_test.py | 44 ++++++++ tests/python_tests/save_map_test.py | 7 -- tests/python_tests/utilities.py | 12 +++ tests/run_tests.py | 7 +- 9 files changed, 234 insertions(+), 19 deletions(-) create mode 100644 tests/python_tests/__init__.py create mode 100644 tests/python_tests/font_test.py create mode 100644 tests/python_tests/projection_test.py create mode 100644 tests/python_tests/render_test.py diff --git a/tests/python_tests/__init__.py b/tests/python_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/python_tests/font_test.py b/tests/python_tests/font_test.py new file mode 100644 index 000000000..c0d8efa0e --- /dev/null +++ b/tests/python_tests/font_test.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +from nose.tools import * + +import mapnik, pickle + +# Tests that exercise fonts. + +# Trac Ticket #31 +# Todo: Add logic to use this TextSymbolizer in a rendering +#@raises(UserWarning) +#def test_invalid_font(): +# ts = mapnik.TextSymbolizer('Name', 'Invalid Font Name', int(8), mapnik.Color('black')) diff --git a/tests/python_tests/load_map_test.py b/tests/python_tests/load_map_test.py index 7e291ebb0..e00bbd205 100644 --- a/tests/python_tests/load_map_test.py +++ b/tests/python_tests/load_map_test.py @@ -15,8 +15,6 @@ def setup(): def assert_loads_successfully(file): m = mapnik.Map(512, 512) - print "Loading %s" % (file) - strict = True mapnik.load_map(m, file, strict) @@ -27,8 +25,6 @@ def assert_loads_successfully(file): def assert_raises_userwarning(file): m = mapnik.Map(512, 512) - print "Loading %s" % (file) - strict = True mapnik.load_map(m, file, strict) diff --git a/tests/python_tests/object_test.py b/tests/python_tests/object_test.py index ea758166e..930aac1c7 100644 --- a/tests/python_tests/object_test.py +++ b/tests/python_tests/object_test.py @@ -1,11 +1,132 @@ #!/usr/bin/env python from nose.tools import * +from utilities import Todo -import mapnik +import mapnik, pickle # Tests that exercise the functionality of Mapnik classes. +# Shapefile initialization +def test_shapefile_init(): + s = mapnik.Shapefile(file='../../demo/data/boundaries', encoding='latin1') + + e = s.envelope() + + assert_almost_equal(e.minx, -11121.6896651, places=7) + assert_almost_equal(e.miny, -724724.216526, places=6) + assert_almost_equal(e.maxx, 2463000.67866, places=5) + assert_almost_equal(e.maxy, 1649661.267, places=3) + +# TextSymbolizer initialization +def test_textsymbolizer_init(): + ts = mapnik.TextSymbolizer('Name', 'Font Name', 8, mapnik.Color('black')) + + eq_(ts.name, 'Name') + eq_(ts.face_name, 'Font Name') + eq_(ts.text_size, 8) + eq_(ts.fill, mapnik.Color('black')) + +# Map initialization +def test_map_init(): + m = mapnik.Map(256, 256) + + eq_(m.width, 256) + eq_(m.height, 256) + eq_(m.srs, '+proj=latlong +datum=WGS84') + + m = mapnik.Map(256, 256, '+proj=latlong') + + eq_(m.width, 256) + eq_(m.height, 256) + eq_(m.srs, '+proj=latlong') + +# Map initialization from string +# Trac Ticket #99 +def test_map_init_from_string(): + map_string = ''' + + + My Style + + shape + ../../demo/data/boundaries + + + ''' + + m = mapnik.Map(600, 300) + + # TODO: Test some properties here + mapnik.load_map_from_string(m, map_string) + mapnik.load_map_from_string(m, map_string, True) + +# Map pickling +def test_map_pickle(): + # Fails due to scale() not matching, possibly other things + raise(Todo("Not implemented yet (Tickets #167, #233).")) + + m = mapnik.Map(256, 256) + + eq_(pickle.loads(pickle.dumps(m)), m) + + m = mapnik.Map(256, 256, '+proj=latlong') + + eq_(pickle.loads(pickle.dumps(m)), m) + +# Color initialization +def test_color_init(): + c = mapnik.Color('blue') + + eq_(c.a, 255) + eq_(c.r, 0) + eq_(c.g, 0) + eq_(c.b, 255) + + eq_(c.to_hex_string(), '#0000ff') + + c = mapnik.Color(0, 64, 128) + + eq_(c.a, 255) + eq_(c.r, 0) + eq_(c.g, 64) + eq_(c.b, 128) + + eq_(c.to_hex_string(), '#004080') + + c = mapnik.Color(0, 64, 128, 192) + + eq_(c.a, 192) + eq_(c.r, 0) + eq_(c.g, 64) + eq_(c.b, 128) + + eq_(c.to_hex_string(), '#004080') + +# Color pickling +def test_color_pickle(): + c = mapnik.Color('blue') + + eq_(pickle.loads(pickle.dumps(c)), c) + + c = mapnik.Color(0, 64, 128) + + eq_(pickle.loads(pickle.dumps(c)), c) + + c = mapnik.Color(0, 64, 128, 192) + + eq_(pickle.loads(pickle.dumps(c)), c) + # Rule initialization def test_rule_init(): min_scale = 5 @@ -17,35 +138,35 @@ def test_rule_init(): eq_(r.title, '') eq_(r.min_scale, 0) eq_(r.max_scale, float('inf')) - + r = mapnik.Rule("Name") eq_(r.name, 'Name') eq_(r.title, '') eq_(r.min_scale, 0) eq_(r.max_scale, float('inf')) - + r = mapnik.Rule("Name", "Title") eq_(r.name, 'Name') eq_(r.title, 'Title') eq_(r.min_scale, 0) eq_(r.max_scale, float('inf')) - + r = mapnik.Rule("Name", "Title", min_scale) eq_(r.name, 'Name') eq_(r.title, 'Title') eq_(r.min_scale, min_scale) eq_(r.max_scale, float('inf')) - + r = mapnik.Rule("Name", "Title", min_scale, max_scale) eq_(r.name, 'Name') eq_(r.title, 'Title') eq_(r.min_scale, min_scale) eq_(r.max_scale, max_scale) - + # Coordinate initialization def test_coord_init(): c = mapnik.Coord(100, 100) @@ -91,6 +212,12 @@ def test_envelope_init(): eq_(c.x, 150) eq_(c.y, 150) +# Envelope pickling +def test_envelope_pickle(): + e = mapnik.Envelope(100, 100, 200, 200) + + eq_(pickle.loads(pickle.dumps(e)), e) + # Envelope multiplication def test_envelope_multiplication(): e = mapnik.Envelope(100, 100, 200, 200) diff --git a/tests/python_tests/projection_test.py b/tests/python_tests/projection_test.py new file mode 100644 index 000000000..d020ecab2 --- /dev/null +++ b/tests/python_tests/projection_test.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +from nose.tools import * + +import mapnik, pickle + +# Tests that exercise map projections. + +# Trac Ticket #128 +def test_wgs84_inverse_forward(): + p = mapnik.Projection('+init=epsg:4326') + + c = mapnik.Coord(3.01331418311, 43.3333092669) + e = mapnik.Envelope(-122.54345245, 45.12312553, 68.2335581353, 48.231231233) + + # It appears that the y component changes very slightly, is this OK? + eq_(p.inverse(c), c) + eq_(p.forward(c), c) + + eq_(p.inverse(e), e) + eg_(p.forward(e), e) + + eq_(c.inverse(p), c) + eq_(c.forward(p), c) + + eq_(e.inverse(p), e) + eq_(e.forward(p), e) diff --git a/tests/python_tests/render_test.py b/tests/python_tests/render_test.py new file mode 100644 index 000000000..c1589d07a --- /dev/null +++ b/tests/python_tests/render_test.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +from nose.tools import * + +import os, mapnik + +def test_simplest_render(): + m = mapnik.Map(256, 256) + i = mapnik.Image(m.width, m.height) + + mapnik.render(m, i) + + s = i.tostring() + + eq_(s, 256 * 256 * '\x00\x00\x00\x00') + +def test_render_image_to_string(): + i = mapnik.Image(256, 256) + + i.background = mapnik.Color('black') + + s = i.tostring() + + eq_(s, 256 * 256 * '\x00\x00\x00\xff') + + s = i.tostring('png') + +def test_render_image_to_file(): + i = mapnik.Image(256, 256) + + i.background = mapnik.Color('black') + + i.save('test.jpg') + i.save('test.png', 'png') + + if os.path.exists('test.jpg'): + os.remove('test.jpg') + else: + return False + + if os.path.exists('test.png'): + os.remove('test.png') + else: + return False diff --git a/tests/python_tests/save_map_test.py b/tests/python_tests/save_map_test.py index 37032aae5..5109fc6ad 100644 --- a/tests/python_tests/save_map_test.py +++ b/tests/python_tests/save_map_test.py @@ -19,22 +19,15 @@ def test(): map = mapnik.Map(256, 256) in_map = "../data/good_maps/osm-styles.xml" - print "Loading map '%s' ... " % in_map - mapnik.load_map(map, in_map) test_map = "test_map.xml" - print "Saving map '%s' ... " % test_map - mapnik.save_map(map, test_map) new_map = mapnik.Map(256, 256) - print "Reloading map '%s' ... " % test_map - mapnik.load_map(new_map, test_map) if os.path.exists(test_map): - print "Removing '%s'" % test_map os.remove(test_map) else: # Fail, the map wasn't written diff --git a/tests/python_tests/utilities.py b/tests/python_tests/utilities.py index 56b4f6ae6..addc2638c 100644 --- a/tests/python_tests/utilities.py +++ b/tests/python_tests/utilities.py @@ -1,4 +1,16 @@ +#!/usr/bin/env python + +from nose.plugins.errorclass import ErrorClass, ErrorClassPlugin + import os, sys, inspect def execution_path(filename): return os.path.join(os.path.dirname(sys._getframe(1).f_code.co_filename), filename) + +class Todo(Exception): + pass + +class TodoPlugin(ErrorClassPlugin): + name = "todo" + + todo = ErrorClass(Todo, label='TODO', isfailure=False) diff --git a/tests/run_tests.py b/tests/run_tests.py index aee4f4c07..62580c4f2 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -1,4 +1,7 @@ #!/usr/bin/env python + +from python_tests.utilities import TodoPlugin + import nose, sys, os, getopt def usage(): @@ -49,7 +52,7 @@ def main(): print "- Running nosetests:" print - argv = [__file__, '--exe'] + argv = [__file__, '--exe', '--with-todo'] if not quiet: argv.append('-v') @@ -59,7 +62,7 @@ def main(): argv.append('-v') argv.append('-v') - nose.run(argv=argv) + nose.run(argv=argv, plugins=[TodoPlugin()]) if __name__ == "__main__": main()