|
1 | | -#!/usr/bin/env python |
2 | | -# -*- coding: utf-8 -*- |
3 | | - |
4 | | -import os |
5 | | - |
6 | | -from nose.tools import eq_, raises |
7 | | - |
8 | 1 | import mapnik |
9 | | - |
10 | | -from .utilities import execution_path, run_all |
11 | | - |
12 | | -try: |
13 | | - import json |
14 | | -except ImportError: |
15 | | - import simplejson as json |
16 | | - |
17 | | - |
18 | | -def setup(): |
19 | | - # All of the paths used are relative, if we run the tests |
20 | | - # from another directory we need to chdir() |
21 | | - os.chdir(execution_path('.')) |
| 2 | +import json |
| 3 | +import pytest |
22 | 4 |
|
23 | 5 | if mapnik.has_grid_renderer(): |
24 | 6 | def show_grids(name, g1, g2): |
@@ -384,27 +366,26 @@ def test_render_grid(): |
384 | 366 | grid = mapnik.Grid(m.width, m.height, key='Name') |
385 | 367 | mapnik.render_layer(m, grid, layer=0, fields=['Name']) |
386 | 368 | utf1 = grid.encode('utf', resolution=4) |
387 | | - eq_(utf1, grid_correct_new3, show_grids( |
388 | | - 'new-markers', utf1, grid_correct_new3)) |
| 369 | + assert utf1 == grid_correct_new3, show_grids('new-markers', utf1, grid_correct_new3) |
389 | 370 |
|
390 | 371 | # check a full view is the same as a full image |
391 | 372 | grid_view = grid.view(0, 0, width, height) |
392 | 373 | # for kicks check at full res too |
393 | 374 | utf3 = grid.encode('utf', resolution=1) |
394 | 375 | utf4 = grid_view.encode('utf', resolution=1) |
395 | | - eq_(utf3['grid'], utf4['grid']) |
396 | | - eq_(utf3['keys'], utf4['keys']) |
397 | | - eq_(utf3['data'], utf4['data']) |
| 376 | + assert utf3['grid'] == utf4['grid'] |
| 377 | + assert utf3['keys'] == utf4['keys'] |
| 378 | + assert utf3['data'] == utf4['data'] |
398 | 379 |
|
399 | | - eq_(resolve(utf4, 0, 0), None) |
| 380 | + assert resolve(utf4, 0, 0) == None |
400 | 381 |
|
401 | 382 | # resolve some center points in the |
402 | 383 | # resampled view |
403 | 384 | utf5 = grid_view.encode('utf', resolution=4) |
404 | | - eq_(resolve(utf5, 25, 10), {"Name": "North West"}) |
405 | | - eq_(resolve(utf5, 25, 46), {"Name": "North East"}) |
406 | | - eq_(resolve(utf5, 38, 10), {"Name": "South West"}) |
407 | | - eq_(resolve(utf5, 38, 46), {"Name": "South East"}) |
| 385 | + assert resolve(utf5, 25, 10) == {"Name": "North West"} |
| 386 | + assert resolve(utf5, 25, 46) == {"Name": "North East"} |
| 387 | + assert resolve(utf5, 38, 10) == {"Name": "South West"} |
| 388 | + assert resolve(utf5, 38, 46) == {"Name": "South East"} |
408 | 389 |
|
409 | 390 | grid_feat_id = { |
410 | 391 | 'keys': [ |
@@ -670,25 +651,25 @@ def test_render_grid3(): |
670 | 651 | grid = mapnik.Grid(m.width, m.height, key='__id__') |
671 | 652 | mapnik.render_layer(m, grid, layer=0, fields=['__id__', 'Name']) |
672 | 653 | utf1 = grid.encode('utf', resolution=4) |
673 | | - eq_(utf1, grid_feat_id3, show_grids('id-markers', utf1, grid_feat_id3)) |
| 654 | + assert utf1 == grid_feat_id3, show_grids('id-markers', utf1 == grid_feat_id3) |
674 | 655 | # check a full view is the same as a full image |
675 | 656 | grid_view = grid.view(0, 0, width, height) |
676 | 657 | # for kicks check at full res too |
677 | 658 | utf3 = grid.encode('utf', resolution=1) |
678 | 659 | utf4 = grid_view.encode('utf', resolution=1) |
679 | | - eq_(utf3['grid'], utf4['grid']) |
680 | | - eq_(utf3['keys'], utf4['keys']) |
681 | | - eq_(utf3['data'], utf4['data']) |
| 660 | + assert utf3['grid'] == utf4['grid'] |
| 661 | + assert utf3['keys'] == utf4['keys'] |
| 662 | + assert utf3['data'] == utf4['data'] |
682 | 663 |
|
683 | | - eq_(resolve(utf4, 0, 0), None) |
| 664 | + assert resolve(utf4, 0, 0) == None |
684 | 665 |
|
685 | 666 | # resolve some center points in the |
686 | 667 | # resampled view |
687 | 668 | utf5 = grid_view.encode('utf', resolution=4) |
688 | | - eq_(resolve(utf5, 25, 10), {"Name": "North West", "__id__": 3}) |
689 | | - eq_(resolve(utf5, 25, 46), {"Name": "North East", "__id__": 4}) |
690 | | - eq_(resolve(utf5, 38, 10), {"Name": "South West", "__id__": 2}) |
691 | | - eq_(resolve(utf5, 38, 46), {"Name": "South East", "__id__": 1}) |
| 669 | + assert resolve(utf5, 25, 10) == {"Name": "North West", "__id__": 3} |
| 670 | + assert resolve(utf5, 25, 46) == {"Name": "North East", "__id__": 4} |
| 671 | + assert resolve(utf5, 38, 10) == {"Name": "South West", "__id__": 2} |
| 672 | + assert resolve(utf5, 38, 46) == {"Name": "South East", "__id__": 1} |
692 | 673 |
|
693 | 674 | def gen_grid_for_id(pixel_key): |
694 | 675 | ds = mapnik.MemoryDatasource() |
@@ -718,39 +699,39 @@ def gen_grid_for_id(pixel_key): |
718 | 699 |
|
719 | 700 | def test_negative_id(): |
720 | 701 | grid = gen_grid_for_id(-1) |
721 | | - eq_(grid.get_pixel(128, 128), -1) |
| 702 | + assert grid.get_pixel(128, 128) == -1 |
722 | 703 | utf1 = grid.encode('utf', resolution=4) |
723 | | - eq_(utf1['keys'], ['-1']) |
| 704 | + assert utf1['keys'] == ['-1'] |
724 | 705 |
|
725 | 706 | def test_32bit_int_id(): |
726 | 707 | int32 = 2147483647 |
727 | 708 | grid = gen_grid_for_id(int32) |
728 | | - eq_(grid.get_pixel(128, 128), int32) |
| 709 | + assert grid.get_pixel(128, 128) == int32 |
729 | 710 | utf1 = grid.encode('utf', resolution=4) |
730 | | - eq_(utf1['keys'], [str(int32)]) |
| 711 | + assert utf1['keys'] == [str(int32)] |
731 | 712 | max_neg = -(int32) |
732 | 713 | grid = gen_grid_for_id(max_neg) |
733 | | - eq_(grid.get_pixel(128, 128), max_neg) |
| 714 | + assert grid.get_pixel(128, 128) == max_neg |
734 | 715 | utf1 = grid.encode('utf', resolution=4) |
735 | | - eq_(utf1['keys'], [str(max_neg)]) |
| 716 | + assert utf1['keys'] == [str(max_neg)] |
736 | 717 |
|
737 | 718 | def test_64bit_int_id(): |
738 | 719 | int64 = 0x7FFFFFFFFFFFFFFF |
739 | 720 | grid = gen_grid_for_id(int64) |
740 | | - eq_(grid.get_pixel(128, 128), int64) |
| 721 | + assert grid.get_pixel(128, 128) == int64 |
741 | 722 | utf1 = grid.encode('utf', resolution=4) |
742 | | - eq_(utf1['keys'], [str(int64)]) |
| 723 | + assert utf1['keys'] == [str(int64)] |
743 | 724 | max_neg = -(int64) |
744 | 725 | grid = gen_grid_for_id(max_neg) |
745 | | - eq_(grid.get_pixel(128, 128), max_neg) |
| 726 | + assert grid.get_pixel(128, 128) == max_neg |
746 | 727 | utf1 = grid.encode('utf', resolution=4) |
747 | | - eq_(utf1['keys'], [str(max_neg)]) |
| 728 | + assert utf1['keys'] == [str(max_neg)] |
748 | 729 |
|
749 | 730 | def test_id_zero(): |
750 | 731 | grid = gen_grid_for_id(0) |
751 | | - eq_(grid.get_pixel(128, 128), 0) |
| 732 | + assert grid.get_pixel(128, 128) == 0 |
752 | 733 | utf1 = grid.encode('utf', resolution=4) |
753 | | - eq_(utf1['keys'], ['0']) |
| 734 | + assert utf1['keys'] == ['0'] |
754 | 735 |
|
755 | 736 | line_expected = { |
756 | 737 | "keys": [ |
@@ -852,7 +833,7 @@ def test_line_rendering(): |
852 | 833 | grid = mapnik.Grid(m.width, m.height, key='__id__') |
853 | 834 | mapnik.render_layer(m, grid, layer=0, fields=['Name']) |
854 | 835 | utf1 = grid.encode() |
855 | | - eq_(utf1, line_expected, show_grids('line', utf1, line_expected)) |
| 836 | + assert utf1 == line_expected, show_grids('line', utf1, line_expected) |
856 | 837 |
|
857 | 838 | point_expected = { |
858 | 839 | "data": { |
@@ -939,61 +920,57 @@ def test_line_rendering(): |
939 | 920 | def test_point_symbolizer_grid(): |
940 | 921 | width, height = 256, 256 |
941 | 922 | sym = mapnik.PointSymbolizer() |
942 | | - sym.file = '../data/images/dummy.png' |
| 923 | + sym.file = './test/data/images/dummy.png' |
943 | 924 | m = create_grid_map(width, height, sym) |
944 | 925 | ul_lonlat = mapnik.Coord(142.30, -38.20) |
945 | 926 | lr_lonlat = mapnik.Coord(143.40, -38.80) |
946 | 927 | m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat)) |
947 | 928 | grid = mapnik.Grid(m.width, m.height) |
948 | 929 | mapnik.render_layer(m, grid, layer=0, fields=['Name']) |
949 | 930 | utf1 = grid.encode() |
950 | | - eq_(utf1, point_expected, show_grids('point-sym', utf1, point_expected)) |
| 931 | + assert utf1 == point_expected, show_grids('point-sym', utf1, point_expected) |
951 | 932 |
|
952 | 933 | test_point_symbolizer_grid.requires_data = True |
953 | 934 |
|
954 | 935 | # should throw because this is a mis-usage |
955 | 936 | # https://github.com/mapnik/mapnik/issues/1325 |
956 | | - @raises(RuntimeError) |
957 | 937 | def test_render_to_grid_multiple_times(): |
958 | | - # create map with two layers |
959 | | - m = mapnik.Map(256, 256) |
960 | | - s = mapnik.Style() |
961 | | - r = mapnik.Rule() |
962 | | - sym = mapnik.MarkersSymbolizer() |
963 | | - sym.allow_overlap = True |
964 | | - r.symbols.append(sym) |
965 | | - s.rules.append(r) |
966 | | - m.append_style('points', s) |
967 | | - |
968 | | - # NOTE: we use a csv datasource here |
969 | | - # because the memorydatasource fails silently for |
970 | | - # queries requesting fields that do not exist in the datasource |
971 | | - ds1 = mapnik.Datasource(**{"type": "csv", "inline": ''' |
972 | | - wkt,Name |
973 | | - "POINT (143.10 -38.60)",South East'''}) |
974 | | - lyr1 = mapnik.Layer('One') |
975 | | - lyr1.datasource = ds1 |
976 | | - lyr1.styles.append('points') |
977 | | - m.layers.append(lyr1) |
978 | | - |
979 | | - ds2 = mapnik.Datasource(**{"type": "csv", "inline": ''' |
980 | | - wkt,Value |
981 | | - "POINT (142.48 -38.60)",South West'''}) |
982 | | - lyr2 = mapnik.Layer('Two') |
983 | | - lyr2.datasource = ds2 |
984 | | - lyr2.styles.append('points') |
985 | | - m.layers.append(lyr2) |
986 | | - |
987 | | - ul_lonlat = mapnik.Coord(142.30, -38.20) |
988 | | - lr_lonlat = mapnik.Coord(143.40, -38.80) |
989 | | - m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat)) |
990 | | - grid = mapnik.Grid(m.width, m.height) |
991 | | - mapnik.render_layer(m, grid, layer=0, fields=['Name']) |
992 | | - # should throw right here since Name will be a property now on the `grid` object |
993 | | - # and it is not found on the second layer |
994 | | - mapnik.render_layer(m, grid, layer=1, fields=['Value']) |
995 | | - grid.encode() |
996 | | - |
997 | | -if __name__ == "__main__": |
998 | | - setup() |
999 | | - exit(run_all(eval(x) for x in dir() if x.startswith("test_"))) |
| 938 | + with pytest.raises(RuntimeError): |
| 939 | + # create map with two layers |
| 940 | + m = mapnik.Map(256, 256) |
| 941 | + s = mapnik.Style() |
| 942 | + r = mapnik.Rule() |
| 943 | + sym = mapnik.MarkersSymbolizer() |
| 944 | + sym.allow_overlap = True |
| 945 | + r.symbols.append(sym) |
| 946 | + s.rules.append(r) |
| 947 | + m.append_style('points', s) |
| 948 | + |
| 949 | + # NOTE: we use a csv datasource here |
| 950 | + # because the memorydatasource fails silently for |
| 951 | + # queries requesting fields that do not exist in the datasource |
| 952 | + ds1 = mapnik.Datasource(**{"type": "csv", "inline": ''' |
| 953 | + wkt,Name |
| 954 | + "POINT (143.10 -38.60)",South East'''}) |
| 955 | + lyr1 = mapnik.Layer('One') |
| 956 | + lyr1.datasource = ds1 |
| 957 | + lyr1.styles.append('points') |
| 958 | + m.layers.append(lyr1) |
| 959 | + |
| 960 | + ds2 = mapnik.Datasource(**{"type": "csv", "inline": ''' |
| 961 | + wkt,Value |
| 962 | + "POINT (142.48 -38.60)",South West'''}) |
| 963 | + lyr2 = mapnik.Layer('Two') |
| 964 | + lyr2.datasource = ds2 |
| 965 | + lyr2.styles.append('points') |
| 966 | + m.layers.append(lyr2) |
| 967 | + |
| 968 | + ul_lonlat = mapnik.Coord(142.30, -38.20) |
| 969 | + lr_lonlat = mapnik.Coord(143.40, -38.80) |
| 970 | + m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat)) |
| 971 | + grid = mapnik.Grid(m.width, m.height) |
| 972 | + mapnik.render_layer(m, grid, layer=0, fields=['Name']) |
| 973 | + # should throw right here since Name will be a property now on the `grid` object |
| 974 | + # and it is not found on the second layer |
| 975 | + mapnik.render_layer(m, grid, layer=1, fields=['Value']) |
| 976 | + grid.encode() |
0 commit comments