1 7
import param
2 7
import pytest
3

4 7
from panel.io.location import Location
5 7
from panel.util import edit_readonly
6

7

8 7
@pytest.fixture
9 2
def location():
10 7
    loc = Location()
11 7
    with edit_readonly(loc):
12 7
        loc.href = "http://localhost:5006"
13 7
        loc.hostname = "localhost"
14 7
        loc.pathname = ""
15 7
        loc.protocol = 'http'
16 7
        loc.search = ""
17 7
        loc.hash = ""
18 7
    return loc
19

20

21 7
class SyncParameterized(param.Parameterized):
22

23 7
    integer = param.Integer(default=None)
24

25 7
    string = param.String(default=None)
26

27

28 7
def test_location_update_query(location):
29 7
    location.update_query(a=1)
30 7
    assert location.search == "?a=1"
31 7
    location.update_query(b='c')
32 7
    assert location.search == "?a=1&b=c"
33

34 7
def test_location_sync_query_init(location):
35 7
    p = SyncParameterized(integer=1, string='abc')
36 7
    location.sync(p)
37 7
    assert location.search == "?integer=1&string=abc"
38 7
    location.unsync(p)
39 7
    assert location._synced == []
40

41 7
def test_location_unsync(location):
42 7
    p = SyncParameterized(integer=1, string='abc')
43 7
    location.sync(p)
44 7
    assert location.search == "?integer=1&string=abc"
45 7
    location.unsync(p)
46 7
    location.update_query(integer=2, string='def')
47 7
    assert p.integer == 1
48 7
    assert p.string == "abc"
49 7
    p.integer = 3
50 7
    p.string = "ghi"
51 7
    assert location.search == "?integer=2&string=def"
52

53 7
def test_location_unsync_partial(location):
54 7
    p = SyncParameterized(integer=1, string='abc')
55 7
    location.sync(p)
56 7
    assert location.search == "?integer=1&string=abc"
57 7
    location.unsync(p, ['string'])
58 7
    location.update_query(integer=2, string='def')
59 7
    assert p.integer == 2
60 7
    assert p.string == "abc"
61 7
    p.integer = 3
62 7
    p.string = "ghi"
63 7
    assert location.search == "?integer=3&string=def"
64

65 7
def test_location_sync_query_init_partial(location):
66 7
    p = SyncParameterized(integer=1, string='abc')
67 7
    location.sync(p, ['integer'])
68 7
    assert location.search == "?integer=1"
69 7
    location.unsync(p)
70 7
    assert location._synced == []
71

72 7
def test_location_sync_query_init_rename(location):
73 7
    p = SyncParameterized(integer=1, string='abc')
74 7
    location.sync(p, {'integer': 'int', 'string': 'str'})
75 7
    assert location.search == "?int=1&str=abc"
76 7
    location.unsync(p)
77 7
    assert location._synced == []
78

79 7
def test_location_sync_query(location):
80 7
    p = SyncParameterized()
81 7
    location.sync(p)
82 7
    p.integer = 2
83 7
    assert location.search == "?integer=2"
84 7
    location.unsync(p)
85 7
    assert location._synced == []
86

87 7
def test_location_sync_param_init(location):
88 7
    p = SyncParameterized()
89 7
    location.search = "?integer=1&string=abc"
90 7
    location.sync(p)
91 7
    assert p.integer == 1
92 7
    assert p.string == "abc"
93 7
    location.unsync(p)
94 7
    assert location._synced == []
95

96 7
def test_location_sync_param_init_partial(location):
97 7
    p = SyncParameterized()
98 7
    location.search = "?integer=1&string=abc"
99 7
    location.sync(p, ['integer'])
100 7
    assert p.integer == 1
101 7
    assert p.string is None
102 7
    location.unsync(p)
103 7
    assert location._synced == []
104

105 7
def test_location_sync_param_init_rename(location):
106 7
    p = SyncParameterized()
107 7
    location.search = "?int=1&str=abc"
108 7
    location.sync(p, {'integer': 'int', 'string': 'str'})
109 7
    assert p.integer == 1
110 7
    assert p.string == 'abc'
111 7
    location.unsync(p)
112 7
    assert location._synced == []
113

114 7
def test_location_sync_param_update(location):
115 7
    p = SyncParameterized()
116 7
    location.sync(p)
117 7
    location.search = "?integer=1&string=abc"
118 7
    assert p.integer == 1
119 7
    assert p.string == "abc"
120 7
    location.unsync(p)
121 7
    assert location._synced == []

Read our documentation on viewing source code .

Loading