Skip to content

Commit 73b4904

Browse files
committed
Unit tests - upgrade all to pytest + fix paths logic
setup.py - use pkg_config to setup 'cairo' paths + bump min macOS to 13
1 parent 60aadcc commit 73b4904

41 files changed

Lines changed: 794 additions & 1015 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

setup.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import subprocess
88
import sys
99
import glob
10+
import pkg_resources
11+
1012
from distutils import sysconfig
1113
from ctypes.util import find_library
1214

@@ -230,19 +232,21 @@ def run(self):
230232
if os.environ.get("PYCAIRO", "false") == "true":
231233
try:
232234
extra_comp_args.append('-DHAVE_PYCAIRO')
233-
print("-I%s/include/pycairo".format(sys.exec_prefix))
234-
extra_comp_args.append("-I{0}/include/pycairo".format(sys.exec_prefix))
235+
dist = pkg_resources.get_distribution('pycairo')
236+
print(dist.location)
237+
print("-I%s/cairo/include".format(dist.location))
238+
extra_comp_args.append("-I{0}/cairo/include".format(dist.location))
235239
#extra_comp_args.extend(check_output(["pkg-config", '--cflags', 'pycairo']).strip().split(' '))
236240
#linkflags.extend(check_output(["pkg-config", '--libs', 'pycairo']).strip().split(' '))
237241
except:
238242
raise Exception("Failed to find compiler options for pycairo")
239243

240244
if sys.platform == 'darwin':
241-
extra_comp_args.append('-mmacosx-version-min=10.11')
245+
extra_comp_args.append('-mmacosx-version-min=13.0')
242246
# silence warning coming from boost python macros which
243247
# would is hard to silence via pragma
244248
extra_comp_args.append('-Wno-parentheses-equality')
245-
linkflags.append('-mmacosx-version-min=10.11')
249+
linkflags.append('-mmacosx-version-min=13.0')
246250
else:
247251
linkflags.append('-lrt')
248252
linkflags.append('-Wl,-z,origin')

test/python_tests/cairo_test.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import os
22
import shutil
33
import mapnik
4+
import pytest
5+
from .utilities import execution_path
6+
7+
@pytest.fixture(scope="module")
8+
def setup():
9+
# All of the paths used are relative, if we run the tests
10+
# from another directory we need to chdir()
11+
os.chdir(execution_path('.'))
12+
yield
13+
414

515
def make_tmp_map():
616
m = mapnik.Map(512, 512)
@@ -75,12 +85,12 @@ def cairo_color(c):
7585
if mapnik.has_pycairo():
7686
import cairo
7787

78-
def test_passing_pycairo_context_svg():
88+
def test_passing_pycairo_context_svg(setup):
7989
m = make_tmp_map()
8090
m.zoom_to_box(mapnik.Box2d(-180, -90, 180, 90))
8191
test_cairo_file = '/tmp/mapnik-cairo-context-test.svg'
8292
surface = cairo.SVGSurface(test_cairo_file, m.width, m.height)
83-
expected_cairo_file = './test/python_tests/images/pycairo/cairo-cairo-expected.svg'
93+
expected_cairo_file = 'images/pycairo/cairo-cairo-expected.svg'
8494
context = cairo.Context(surface)
8595
mapnik.render(m, context)
8696
draw_title(m, context, "Hello Map", size=20)
@@ -102,7 +112,7 @@ def test_passing_pycairo_context_pdf():
102112
m.zoom_to_box(mapnik.Box2d(-180, -90, 180, 90))
103113
test_cairo_file = '/tmp/mapnik-cairo-context-test.pdf'
104114
surface = cairo.PDFSurface(test_cairo_file, m.width, m.height)
105-
expected_cairo_file = './test/python_tests/images/pycairo/cairo-cairo-expected.pdf'
115+
expected_cairo_file = 'images/pycairo/cairo-cairo-expected.pdf'
106116
context = cairo.Context(surface)
107117
mapnik.render(m, context)
108118
draw_title(m, context, "Hello Map", size=20)
@@ -124,8 +134,8 @@ def test_passing_pycairo_context_png():
124134
m.zoom_to_box(mapnik.Box2d(-180, -90, 180, 90))
125135
test_cairo_file = '/tmp/mapnik-cairo-context-test.png'
126136
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, m.width, m.height)
127-
expected_cairo_file = './test/python_tests/images/pycairo/cairo-cairo-expected.png'
128-
expected_cairo_file2 = './test/python_tests/images/pycairo/cairo-cairo-expected-reduced.png'
137+
expected_cairo_file = 'images/pycairo/cairo-cairo-expected.png'
138+
expected_cairo_file2 = 'images/pycairo/cairo-cairo-expected-reduced.png'
129139
context = cairo.Context(surface)
130140
mapnik.render(m, context)
131141
draw_title(m, context, "Hello Map", size=20)
@@ -163,10 +173,10 @@ def test_passing_pycairo_context_png():
163173
def _pycairo_surface(type, sym):
164174
test_cairo_file = '/tmp/mapnik-cairo-surface-test.%s.%s' % (
165175
sym, type)
166-
expected_cairo_file = './test/python_tests/images/pycairo/cairo-surface-expected.%s.%s' % (
176+
expected_cairo_file = 'images/pycairo/cairo-surface-expected.%s.%s' % (
167177
sym, type)
168178
m = mapnik.Map(256, 256)
169-
mapnik.load_map(m, './test/data/good_maps/%s_symbolizer.xml' % sym)
179+
mapnik.load_map(m, '../data/good_maps/%s_symbolizer.xml' % sym)
170180
m.zoom_all()
171181
if hasattr(cairo, '%sSurface' % type.upper()):
172182
surface = getattr(

test/python_tests/compositing_test.py

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
# encoding: utf8
2-
3-
from __future__ import print_function
4-
51
import os
6-
7-
from nose.tools import eq_
8-
92
import mapnik
3+
import pytest
4+
from .utilities import (get_unique_colors, pixel2channels, side_by_side_image, execution_path)
105

11-
from .utilities import (execution_path, get_unique_colors, pixel2channels,
12-
run_all, side_by_side_image)
13-
14-
6+
@pytest.fixture(scope="module")
157
def setup():
168
# All of the paths used are relative, if we run the tests
179
# from another directory we need to chdir()
1810
os.chdir(execution_path('.'))
19-
11+
yield
2012

2113
def is_pre(color, alpha):
2214
return (color * 255.0 / alpha) <= 255
@@ -93,14 +85,14 @@ def validate_pixels_are_premultiplied(image):
9385
return (num_bad == 0, bad_pixels)
9486

9587

96-
def test_compare_images():
97-
b = mapnik.Image.open('./images/support/b.png')
88+
def test_compare_images(setup):
89+
b = mapnik.Image.open('images/support/b.png')
9890
b.premultiply()
9991
num_ops = len(mapnik.CompositeOp.names)
10092
successes = []
10193
fails = []
10294
for name in mapnik.CompositeOp.names:
103-
a = mapnik.Image.open('./images/support/a.png')
95+
a = mapnik.Image.open('images/support/a.png')
10496
a.premultiply()
10597
a.composite(b, getattr(mapnik.CompositeOp, name))
10698
actual = '/tmp/mapnik-comp-op-test-' + name + '.png'
@@ -132,53 +124,53 @@ def test_compare_images():
132124
name +
133125
'.fail.png',
134126
'png32')
135-
eq_(len(successes), num_ops, '\n' + '\n'.join(fails))
127+
assert len(successes) == num_ops, '\n' + '\n'.join(fails)
136128
b.demultiply()
137129
# b will be slightly modified by pre and then de multiplication rounding errors
138130
# TODO - write test to ensure the image is 99% the same.
139131
#expected_b = mapnik.Image.open('./images/support/b.png')
140132
# b.save('/tmp/mapnik-comp-op-test-original-mask.png')
141-
#eq_(b.tostring('png32'),expected_b.tostring('png32'), '/tmp/mapnik-comp-op-test-original-mask.png is no longer equivalent to original mask: ./images/support/b.png')
133+
#assert b.tostring('png32') == expected_b.tostring('png32'), '/tmp/mapnik-comp-op-test-original-mask.png is no longer equivalent to original mask: ./images/support/b.png'
142134

143135

144136
def test_pre_multiply_status():
145-
b = mapnik.Image.open('./images/support/b.png')
137+
b = mapnik.Image.open('images/support/b.png')
146138
# not premultiplied yet, should appear that way
147139
result = validate_pixels_are_not_premultiplied(b)
148-
eq_(result, True)
140+
assert result
149141
# not yet premultiplied therefore should return false
150142
result = validate_pixels_are_premultiplied(b)
151-
eq_(result[0], False)
143+
assert not result[0]
152144
# now actually premultiply the pixels
153145
b.premultiply()
154146
# now checking if premultiplied should succeed
155147
result = validate_pixels_are_premultiplied(b)
156-
eq_(result[0], True)
148+
assert result[0]
157149
# should now not appear to look not premultiplied
158150
result = validate_pixels_are_not_premultiplied(b)
159-
eq_(result, False)
151+
assert not result
160152
# now actually demultiply the pixels
161153
b.demultiply()
162154
# should now appear demultiplied
163155
result = validate_pixels_are_not_premultiplied(b)
164-
eq_(result, True)
156+
assert result
165157

166158

167159
def test_pre_multiply_status_of_map1():
168160
m = mapnik.Map(256, 256)
169161
im = mapnik.Image(m.width, m.height)
170-
eq_(validate_pixels_are_not_premultiplied(im), True)
162+
assert validate_pixels_are_not_premultiplied(im)
171163
mapnik.render(m, im)
172-
eq_(validate_pixels_are_not_premultiplied(im), True)
164+
assert validate_pixels_are_not_premultiplied(im)
173165

174166

175167
def test_pre_multiply_status_of_map2():
176168
m = mapnik.Map(256, 256)
177169
m.background = mapnik.Color(1, 1, 1, 255)
178170
im = mapnik.Image(m.width, m.height)
179-
eq_(validate_pixels_are_not_premultiplied(im), True)
171+
assert validate_pixels_are_not_premultiplied(im)
180172
mapnik.render(m, im)
181-
eq_(validate_pixels_are_not_premultiplied(im), True)
173+
assert validate_pixels_are_not_premultiplied(im)
182174

183175
if 'shape' in mapnik.DatasourceCache.plugin_names():
184176
def test_style_level_comp_op():
@@ -215,7 +207,7 @@ def test_style_level_comp_op():
215207
name +
216208
'.fail.png',
217209
'png32')
218-
eq_(len(fails), 0, '\n' + '\n'.join(fails))
210+
assert len(fails) == 0, '\n' + '\n'.join(fails)
219211

220212
def test_style_level_opacity():
221213
m = mapnik.Map(512, 512)
@@ -228,35 +220,33 @@ def test_style_level_opacity():
228220
expected = 'images/support/mapnik-style-level-opacity.png'
229221
im.save(actual, 'png32')
230222
expected_im = mapnik.Image.open(expected)
231-
eq_(im.tostring('png32'),
232-
expected_im.tostring('png32'),
233-
'failed comparing actual (%s) and expected (%s)' % (actual,
234-
'tests/python_tests/' + expected))
223+
assert im.tostring('png32') == expected_im.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual,
224+
'tests/python_tests/' + expected)
235225

236226

237227
def test_rounding_and_color_expectations():
238228
m = mapnik.Map(1, 1)
239229
m.background = mapnik.Color('rgba(255,255,255,.4999999)')
240230
im = mapnik.Image(m.width, m.height)
241231
mapnik.render(m, im)
242-
eq_(get_unique_colors(im), ['rgba(255,255,255,127)'])
232+
assert get_unique_colors(im) == ['rgba(255,255,255,127)']
243233
m = mapnik.Map(1, 1)
244234
m.background = mapnik.Color('rgba(255,255,255,.5)')
245235
im = mapnik.Image(m.width, m.height)
246236
mapnik.render(m, im)
247-
eq_(get_unique_colors(im), ['rgba(255,255,255,128)'])
237+
assert get_unique_colors(im) == ['rgba(255,255,255,128)']
248238
im_file = mapnik.Image.open('../data/images/stripes_pattern.png')
249-
eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
239+
assert get_unique_colors(im_file) == ['rgba(0,0,0,0)', 'rgba(74,74,74,255)']
250240
# should have no effect
251241
im_file.premultiply()
252-
eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
242+
assert get_unique_colors(im_file) == ['rgba(0,0,0,0)', 'rgba(74,74,74,255)']
253243
im_file.apply_opacity(.5)
254244
# should have effect now that image has transparency
255245
im_file.premultiply()
256-
eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(37,37,37,127)'])
246+
assert get_unique_colors(im_file) == ['rgba(0,0,0,0)', 'rgba(37,37,37,127)']
257247
# should restore to original nonpremultiplied colors
258248
im_file.demultiply()
259-
eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,127)'])
249+
assert get_unique_colors(im_file) == ['rgba(0,0,0,0)', 'rgba(74,74,74,127)']
260250

261251

262252
def test_background_image_and_background_color():
@@ -265,7 +255,7 @@ def test_background_image_and_background_color():
265255
m.background_image = '../data/images/stripes_pattern.png'
266256
im = mapnik.Image(m.width, m.height)
267257
mapnik.render(m, im)
268-
eq_(get_unique_colors(im), ['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])
258+
assert get_unique_colors(im) == ['rgba(255,255,255,128)', 'rgba(74,74,74,255)']
269259

270260

271261
def test_background_image_with_alpha_and_background_color():
@@ -274,7 +264,7 @@ def test_background_image_with_alpha_and_background_color():
274264
m.background_image = '../data/images/yellow_half_trans.png'
275265
im = mapnik.Image(m.width, m.height)
276266
mapnik.render(m, im)
277-
eq_(get_unique_colors(im), ['rgba(255,255,85,191)'])
267+
assert get_unique_colors(im) == ['rgba(255,255,85,191)']
278268

279269

280270
def test_background_image_with_alpha_and_background_color_against_composited_control():
@@ -295,8 +285,4 @@ def test_background_image_with_alpha_and_background_color_against_composited_con
295285
# compare image rendered (compositing in `agg_renderer<T>::setup`)
296286
# vs image composited via python bindings
297287
#raise Todo("looks like we need to investigate PNG color rounding when saving")
298-
# eq_(get_unique_colors(im),get_unique_colors(im1))
299-
300-
if __name__ == "__main__":
301-
setup()
302-
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
288+
assert get_unique_colors(im) == get_unique_colors(im1)

test/python_tests/csv_test.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22
import os
33
import mapnik
44
import pytest
5+
from .utilities import execution_path
6+
7+
@pytest.fixture(scope="module")
8+
def setup():
9+
# All of the paths used are relative, if we run the tests
10+
# from another directory we need to chdir()
11+
os.chdir(execution_path('.'))
12+
yield
513

614
if 'csv' in mapnik.DatasourceCache.plugin_names():
715

816
def get_csv_ds(filename):
917
return mapnik.Datasource(
10-
type='csv', file=os.path.join('./test/data/csv/', filename))
18+
type='csv', file=os.path.join('../data/csv/', filename))
1119

12-
def test_broken_files(visual=False):
13-
broken = glob.glob("./test/data/csv/fails/*.*")
14-
broken.extend(glob.glob("./test/data/csv/warns/*.*"))
20+
def test_broken_files(setup, visual=False):
21+
broken = glob.glob("../data/csv/fails/*.*")
22+
broken.extend(glob.glob("../data/csv/warns/*.*"))
1523

1624
# Add a filename that doesn't exist
17-
broken.append("./test/data/csv/fails/does_not_exist.csv")
25+
broken.append("../data/csv/fails/does_not_exist.csv")
1826

1927
for csv in broken:
2028
if visual:
@@ -24,10 +32,10 @@ def test_broken_files(visual=False):
2432
except Exception:
2533
print('\x1b[1;32m✓ \x1b[0m', csv)
2634

27-
def test_good_files(visual=False):
28-
good_files = glob.glob("./test/data/csv/*.*")
29-
good_files.extend(glob.glob("./test/data/csv/warns/*.*"))
30-
ignorable = os.path.join('./test', 'data', 'csv', 'long_lat.vrt')
35+
def test_good_files(setup, visual=False):
36+
good_files = glob.glob("../data/csv/*.*")
37+
good_files.extend(glob.glob("../data/csv/warns/*.*"))
38+
ignorable = os.path.join('..', 'data', 'csv', 'long_lat.vrt')
3139
print("ignorable:", ignorable)
3240
good_files.remove(ignorable)
3341
for f in good_files:
@@ -469,7 +477,7 @@ def test_that_fewer_headers_than_rows_throws(**kwargs):
469477

470478
def test_that_feature_id_only_incremented_for_valid_rows(**kwargs):
471479
ds = mapnik.Datasource(type='csv',
472-
file=os.path.join('./test/data/csv/warns', 'feature_id_counting.csv'))
480+
file=os.path.join('../data/csv/warns', 'feature_id_counting.csv'))
473481
assert len(ds.fields()) == 3
474482
assert ds.fields(), ['x', 'y' == 'id']
475483
assert ds.field_types(), ['int', 'int' == 'int']
@@ -491,7 +499,7 @@ def test_that_feature_id_only_incremented_for_valid_rows(**kwargs):
491499
def test_dynamically_defining_headers1(**kwargs):
492500
ds = mapnik.Datasource(type='csv',
493501
file=os.path.join(
494-
'./test/data/csv/fails', 'needs_headers_two_lines.csv'),
502+
'../data/csv/fails', 'needs_headers_two_lines.csv'),
495503
headers='x,y,name')
496504
assert len(ds.fields()) == 3
497505
assert ds.fields(), ['x', 'y' == 'name']
@@ -508,7 +516,7 @@ def test_dynamically_defining_headers1(**kwargs):
508516
def test_dynamically_defining_headers2(**kwargs):
509517
ds = mapnik.Datasource(type='csv',
510518
file=os.path.join(
511-
'./test/data/csv/fails', 'needs_headers_one_line.csv'),
519+
'../data/csv/fails', 'needs_headers_one_line.csv'),
512520
headers='x,y,name')
513521
assert len(ds.fields()) == 3
514522
assert ds.fields(), ['x', 'y' == 'name']
@@ -525,7 +533,7 @@ def test_dynamically_defining_headers2(**kwargs):
525533
def test_dynamically_defining_headers3(**kwargs):
526534
ds = mapnik.Datasource(type='csv',
527535
file=os.path.join(
528-
'./test/data/csv/fails', 'needs_headers_one_line_no_newline.csv'),
536+
'../data/csv/fails', 'needs_headers_one_line_no_newline.csv'),
529537
headers='x,y,name')
530538
assert len(ds.fields()) == 3
531539
assert ds.fields(), ['x', 'y' == 'name']

0 commit comments

Comments
 (0)