File: test_style.py

package info (click to toggle)
metview-python 1.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,896 kB
  • sloc: python: 11,306; makefile: 84; ansic: 51; sh: 7
file content (164 lines) | stat: -rw-r--r-- 4,841 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# (C) Copyright 2017- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#


import metview as mv
from metview import bindings
from metview.style import Style, Visdef


def test_visdef_object():
    vd = Visdef(
        "msymb",
        {
            "symbol_type": "text",
            "symbol_table_mode": "advanced",
            "symbol_advanced_table_text_font_colour": "black",
        },
    )

    # clone
    v = vd.clone()
    assert v.verb == vd.verb
    assert v.params == vd.params
    assert id(v) != id(vd)
    assert id(v.params) != id(vd.params)

    # change
    vd.change("msymb", "symbol_advanced_table_text_font_colour", "red")
    assert vd.params["symbol_advanced_table_text_font_colour"] == "red"
    vd.change("mcont", "symbol_advanced_table_text_font_colour", "blue")
    assert vd.params["symbol_advanced_table_text_font_colour"] == "red"

    # from request
    r = mv.msymb(
        {
            "symbol_type": "text",
            "symbol_table_mode": "advanced",
            "symbol_advanced_table_text_font_colour": "black",
        }
    )

    v = Visdef.from_request(r)
    assert v.verb == vd.verb
    for k, v in v.params.items():
        assert r[k.upper()].lower() == v.lower()


def test_style_object():
    vd = Visdef(
        "msymb",
        {
            "symbol_type": "text",
            "symbol_table_mode": "advanced",
            "symbol_advanced_table_text_font_colour": "black",
        },
    )

    # create
    s = Style("super", vd)
    assert s.name == "super"
    assert len(s.visdefs) == 1
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"

    # update
    ns = s.update({"symbol_advanced_table_text_font_colour": "red"})
    assert id(ns) != id(s)
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"
    assert ns.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "red"

    ns = s.update({"symbol_advanced_table_text_font_colour": "red"}, verb="mcont")
    assert id(ns) != id(s)
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"
    assert ns.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"

    ns = s.update({"symbol_advanced_table_text_font_colour": "red"}, verb="msymb")
    assert id(ns) != id(s)
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"
    assert ns.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "red"

    # multiple visdefs

    vd1 = Visdef(
        "msymb",
        {
            "symbol_type": "text",
            "symbol_table_mode": "advanced",
            "symbol_advanced_table_text_font_colour": "black",
        },
    )
    vd2 = Visdef("mgraph", {"graph_line_colour": "blue", "graph_line_thickness": 4})

    s = Style("super", [vd1, vd2])
    assert s.name == "super"
    assert len(s.visdefs) == 2
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"
    assert s.visdefs[1].params["graph_line_colour"] == "blue"

    # update
    ns = s.update({"symbol_advanced_table_text_font_colour": "red"}, verb="msymb")
    assert id(ns) != id(s)
    assert s.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "black"
    assert ns.visdefs[0].params["symbol_advanced_table_text_font_colour"] == "red"

    ns = s.update(
        {
            "graph_line_colour": "yellow",
        },
        verb="mgraph",
    )
    assert id(ns) != id(s)
    assert s.visdefs[1].params["graph_line_colour"] == "blue"
    assert ns.visdefs[1].params["graph_line_colour"] == "yellow"


def test_style_set_grib_id():
    # mcont
    vd = Visdef(
        "mcont",
        {
            "legend": "on",
        },
    )

    s = Style("super", vd)
    ns = s.set_data_id("11")
    assert id(s) != id(ns)
    assert ns.visdefs[0].verb == "mcont"
    assert ns.visdefs[0].params["grib_id"] == "11"
    assert s.visdefs[0].params.get("grib_id", None) is None

    # mwind
    vd = Visdef(
        "mwind",
        {
            "legend": "on",
        },
    )

    s = Style("super", vd)
    ns = s.set_data_id("12")
    assert id(s) != id(ns)
    assert ns.visdefs[0].verb == "mwind"
    assert ns.visdefs[0].params["grib_id"] == "12"
    assert s.visdefs[0].params.get("grib_id", None) is None

    # other
    vd = Visdef(
        "msymb",
        {
            "legend": "on",
        },
    )

    s = Style("super", vd)
    ns = s.set_data_id("10")
    assert id(s) == id(ns)
    assert s.visdefs[0].params.get("grib_id", None) is None