File: json_feature_properties_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20240222-5ab32f020-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,364 kB
  • sloc: python: 11,685; cpp: 5,776; sh: 242; makefile: 10
file content (95 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import mapnik
try:
    import json
except ImportError:
    import simplejson as json

chars = [
    {
        "name": "single_quote",
        "test": "string with ' quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \' quote"}}'
    },
    {
        "name": "escaped_single_quote",
        "test": "string with \' quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \' quote"}}'
    },
    {
        "name": "double_quote",
        "test": 'string with " quote',
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\" quote"}}'
    },
    {
        "name": "double_quote2",
        "test": "string with \" quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\" quote"}}'
    },
    {
        "name": "reverse_solidus",  # backslash
        "test": "string with \\ quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\\ quote"}}'
    },
    {
        "name": "solidus",  # forward slash
        "test": "string with / quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with / quote"}}'
    },
    {
        "name": "backspace",
        "test": "string with \b quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\b quote"}}'
    },
    {
        "name": "formfeed",
        "test": "string with \f quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\f quote"}}'
    },
    {
        "name": "newline",
        "test": "string with \n quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\n quote"}}'
    },
    {
        "name": "carriage_return",
        "test": "string with \r quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\r quote"}}'
    },
    {
        "name": "horiztonal_tab",
        "test": "string with \t quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\t quote"}}'
    },
    # remainder are c++ reserved, but not json
    {
        "name": "vert_tab",
        "test": "string with \v quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\u000b quote"}}'
    },
    {
        "name": "alert",
        "test": "string with \a quote",
        "json": '{"type":"Feature","id":1,"geometry":null,"properties":{"name":"string with \\u0007 quote"}}'
    }
]

ctx = mapnik.Context()
ctx.push('name')

def test_char_escaping():
    for char in chars:
        feat = mapnik.Feature(ctx, 1)
        expected = char['test']
        feat["name"] = expected
        assert feat["name"] ==  expected
        # confirm the python json module
        # is working as we would expect
        pyjson2 = json.loads(char['json'])
        assert pyjson2['properties']['name'] ==  expected
        # confirm our behavior is the same as python json module
        # for the original string
        geojson_feat_string = feat.to_geojson()
        assert  geojson_feat_string ==  char['json'], "Mapnik's json escaping is not to spec: actual(%s) and expected(%s) for %s" % (geojson_feat_string, char['json'], char['name'])
        # and the round tripped string
        pyjson = json.loads(geojson_feat_string)
        assert pyjson['properties']['name'] ==  expected