Debian Codegraph @

tests/utils_test.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # coding=utf-8
2 from datetime import timedelta
3
4 import pytest
5
6 from errbot.backend_plugin_manager import BackendPluginManager
7 from errbot.backends.test import ShallowConfig
8 from errbot.bootstrap import CORE_STORAGE, bot_config_defaults
9 from errbot.storage import StoreMixin
10 from errbot.storage.base import StoragePluginBase
11 from errbot.utils import *
12
13 log = logging.getLogger(__name__)
14
15
16 @pytest.mark.parametrize(
17 "v1,v2",
18 [
19 ("2.0.0", "2.0.1"),
20 ("2.0.0", "2.1.0"),
21 ("2.0.0", "3.0.0"),
22 ("2.0.0-alpha", "2.0.0-beta"),
23 ("2.0.0-beta", "2.0.0-rc1"),
24 ("2.0.0-rc1", "2.0.0-rc2"),
25 ("2.0.0-rc2", "2.0.0-rc3"),
26 ("2.0.0-rc2", "2.0.0"),
27 ("2.0.0-beta", "2.0.1"),
28 ],
29 )
30 def test_version_check(v1, v2):
31 assert version2tuple(v1) < version2tuple(v2)
32
33
34 @pytest.mark.parametrize(
35 "version",
36 [
37 "1.2.3.4",
38 "1.2",
39 "1.2.-beta",
40 "1.2.3-toto",
41 "1.2.3-rc",
42 ],
43 )
44 def test_version_check_negative(version):
45 with pytest.raises(ValueError):
46 version2tuple(version)
47
48
49 def test_formattimedelta():
50 td = timedelta(0, 60 * 60 + 13 * 60)
51 assert "1 hours and 13 minutes" == format_timedelta(td)
52
53
54 def test_storage():
55 key = "test"
56
57 __import__("errbot.config-template")
58 config = ShallowConfig()
59 config.__dict__.update(sys.modules["errbot.config-template"].__dict__)
60 bot_config_defaults(config)
61
62 spm = BackendPluginManager(
63 config, "errbot.storage", "Memory", StoragePluginBase, CORE_STORAGE
64 )
65 storage_plugin = spm.load_plugin()
66
67 persistent_object = StoreMixin()
68 persistent_object.open_storage(storage_plugin, "test")
69 persistent_object[key] = "à value"
70 assert persistent_object[key] == "à value"
71 assert key in persistent_object
72 del persistent_object[key]
73 assert key not in persistent_object
74 assert len(persistent_object) == 0
75
76
77 def test_split_string_after_returns_original_string_when_chunksize_equals_string_size():
78 str_ = "foobar2000" * 2
79 splitter = split_string_after(str_, len(str_))
80 split = [chunk for chunk in splitter]
81 assert [str_] == split
82
83
84 def test_split_string_after_returns_original_string_when_chunksize_equals_string_size_plus_one():
85 str_ = "foobar2000" * 2
86 splitter = split_string_after(str_, len(str_) + 1)
87 split = [chunk for chunk in splitter]
88 assert [str_] == split
89
90
91 def test_split_string_after_returns_two_chunks_when_chunksize_equals_string_size_minus_one():
92 str_ = "foobar2000" * 2
93 splitter = split_string_after(str_, len(str_) - 1)
94 split = [chunk for chunk in splitter]
95 assert ["foobar2000foobar200", "0"] == split
96
97
98 def test_split_string_after_returns_two_chunks_when_chunksize_equals_half_length_of_string():
99 str_ = "foobar2000" * 2
100 splitter = split_string_after(str_, int(len(str_) / 2))
101 split = [chunk for chunk in splitter]
102 assert ["foobar2000", "foobar2000"] == split