-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathutils_test.py
105 lines (82 loc) · 2.96 KB
/
utils_test.py
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
# coding=utf-8
import logging
import sys
from datetime import timedelta
import pytest
from errbot.backend_plugin_manager import BackendPluginManager
from errbot.backends.test import ShallowConfig
from errbot.bootstrap import CORE_STORAGE, bot_config_defaults
from errbot.storage import StoreMixin
from errbot.storage.base import StoragePluginBase
from errbot.utils import version2tuple, format_timedelta, split_string_after
log = logging.getLogger(__name__)
@pytest.mark.parametrize(
"v1,v2",
[
("2.0.0", "2.0.1"),
("2.0.0", "2.1.0"),
("2.0.0", "3.0.0"),
("2.0.0-alpha", "2.0.0-beta"),
("2.0.0-beta", "2.0.0-rc1"),
("2.0.0-rc1", "2.0.0-rc2"),
("2.0.0-rc2", "2.0.0-rc3"),
("2.0.0-rc2", "2.0.0"),
("2.0.0-beta", "2.0.1"),
],
)
def test_version_check(v1, v2):
assert version2tuple(v1) < version2tuple(v2)
@pytest.mark.parametrize(
"version",
[
"1.2.3.4",
"1.2",
"1.2.-beta",
"1.2.3-toto",
"1.2.3-rc",
],
)
def test_version_check_negative(version):
with pytest.raises(ValueError):
version2tuple(version)
def test_formattimedelta():
td = timedelta(0, 60 * 60 + 13 * 60)
assert "1 hours and 13 minutes" == format_timedelta(td)
def test_storage():
key = "test"
__import__("errbot.config-template")
config = ShallowConfig()
config.__dict__.update(sys.modules["errbot.config-template"].__dict__)
bot_config_defaults(config)
spm = BackendPluginManager(
config, "errbot.storage", "Memory", StoragePluginBase, CORE_STORAGE
)
storage_plugin = spm.load_plugin()
persistent_object = StoreMixin()
persistent_object.open_storage(storage_plugin, "test")
persistent_object[key] = "à value"
assert persistent_object[key] == "à value"
assert key in persistent_object
del persistent_object[key]
assert key not in persistent_object
assert len(persistent_object) == 0
def test_split_string_after_returns_original_string_when_chunksize_equals_string_size():
str_ = "foobar2000" * 2
splitter = split_string_after(str_, len(str_))
split = [chunk for chunk in splitter]
assert [str_] == split
def test_split_string_after_returns_original_string_when_chunksize_equals_string_size_plus_one():
str_ = "foobar2000" * 2
splitter = split_string_after(str_, len(str_) + 1)
split = [chunk for chunk in splitter]
assert [str_] == split
def test_split_string_after_returns_two_chunks_when_chunksize_equals_string_size_minus_one():
str_ = "foobar2000" * 2
splitter = split_string_after(str_, len(str_) - 1)
split = [chunk for chunk in splitter]
assert ["foobar2000foobar200", "0"] == split
def test_split_string_after_returns_two_chunks_when_chunksize_equals_half_length_of_string():
str_ = "foobar2000" * 2
splitter = split_string_after(str_, int(len(str_) / 2))
split = [chunk for chunk in splitter]
assert ["foobar2000", "foobar2000"] == split