templates.test_maps

 1# Maps - Example for illustrative purposes only.
 2
 3import smartpy as sp
 4
 5
 6@sp.module
 7def main():
 8    class Test_maps(sp.Contract):
 9        def __init__(self):
10            self.data.x = "a"
11            self.data.y = None
12            self.data.z = sp.Some("na")
13            self.data.m = {}
14
15        @sp.entrypoint
16        def test_map_get(self, params):
17            sp.cast(params, map[sp.int, sp.string])
18            self.data.x = params[12]
19
20        @sp.entrypoint
21        def test_map_get2(self, params):
22            sp.cast(params, sp.map[int, sp.string])
23            self.data.x = params[12]
24
25        @sp.entrypoint
26        def test_map_get_opt(self, params):
27            sp.cast(params, map[int, sp.string])
28            self.data.y = params.get_opt(12)
29
30        @sp.entrypoint
31        def test_map_get_default_values(self, params):
32            sp.cast(params, map[int, sp.string])
33            self.data.x = params.get(12, default="abc")
34
35        @sp.entrypoint
36        def test_map_get_missing_value(self, params):
37            sp.cast(params, map[int, sp.string])
38            self.data.x = params.get(12, error="missing 12")
39
40        @sp.entrypoint
41        def test_map_get_missing_value2(self, params):
42            sp.cast(params, map[int, sp.string])
43            self.data.x = params.get(12, error=1234)
44
45        @sp.entrypoint
46        def test_update_map(self):
47            self.data.m = sp.update_map(1, sp.Some("one"), self.data.m)
48
49        @sp.entrypoint
50        def test_get_and_update(self):
51            (previous_value, new_map) = sp.get_and_update(
52                1, sp.Some("one"), self.data.m
53            )
54            self.data.z = previous_value
55
56
57@sp.add_test()
58def test():
59    scenario = sp.test_scenario("Maps", main)
60    c1 = main.Test_maps()
61    scenario.h1("Maps")
62    scenario += c1
63    c1.test_map_get_opt({})
64    c1.test_map_get_opt({12: "A"})