1- # encoding: utf8
2-
3- from __future__ import print_function
4-
51import os
6-
7- from nose .tools import eq_
8-
92import 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" )
157def 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
2113def 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
144136def 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
167159def 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
175167def 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
183175if '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
237227def 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
262252def 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
271261def 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
280270def 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 )
0 commit comments