Debian Codegraph @

tests/test_apprise_pickle.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 import pickle
31 import sys
32
33 from apprise import Apprise, AppriseAsset, AppriseLocale
34
35 logging.disable(logging.CRITICAL)
36
37 # Ensure we don't create .pyc files for these tests
38 sys.dont_write_bytecode = True
39
40
41 def test_apprise_pickle_asset(tmpdir):
42 """pickle: AppriseAsset"""
43 asset = AppriseAsset()
44 serialized = pickle.dumps(asset)
45 new_asset = pickle.loads(serialized)
46
47 # iterate over some keys to verify they're still the same:
48 keys = (
49 "app_id",
50 "app_desc",
51 "app_url",
52 "html_notify_map",
53 "ascii_notify_map",
54 "default_html_color",
55 "default_extension",
56 "theme",
57 "image_url_mask",
58 "image_url_logo",
59 "image_path_mask",
60 "body_format",
61 "async_mode",
62 "interpret_escapes",
63 "encoding",
64 "secure_logging",
65 "_recursion",
66 )
67
68 for key in keys:
69 assert getattr(asset, key) == getattr(new_asset, key)
70
71
72 def test_apprise_pickle_locale(tmpdir):
73 """pickle: AppriseLocale"""
74 locale = AppriseLocale()
75 serialized = pickle.dumps(locale)
76 new_locale = pickle.loads(serialized)
77
78 assert locale.lang == new_locale.lang
79
80 # Ensure internal functions still call in new object
81 new_locale.detect_language()
82
83
84 def test_apprise_pickle_core(tmpdir):
85 """pickle: Apprise"""
86 asset = AppriseAsset(app_id="default")
87 apobj = Apprise(asset=asset)
88
89 # Create a custom asset so we can verify it gets correctly serialized
90 xml_asset = AppriseAsset(app_id="xml")
91
92 # Store our Entries
93 apobj.add("json://localhost")
94 apobj.add("xml://localhost", asset=xml_asset)
95 apobj.add("form://localhost")
96 apobj.add("mailto://user:pass@localhost", tag="email")
97 serialized = pickle.dumps(apobj)
98
99 # Unserialize our object
100 new_apobj = pickle.loads(serialized)
101
102 # Verify that it loaded our URLs back
103 assert len(new_apobj) == 4
104
105 # Our assets were kept (note the XML altered entry)
106 assert apobj[0].app_id == "default"
107 assert apobj[1].app_id == "xml"
108 assert apobj[2].app_id == "default"
109 assert apobj[3].app_id == "default"
110
111 # Our tag was kept
112 assert "email" in apobj[3].tags