| 1 |
# coding=utf-8 |
| 2 |
import logging |
| 3 |
import sys |
| 4 |
|
| 5 |
from datetime import timedelta |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from errbot.backend_plugin_manager import BackendPluginManager |
| 10 |
from errbot.backends.test import ShallowConfig |
| 11 |
from errbot.bootstrap import CORE_STORAGE, bot_config_defaults |
| 12 |
from errbot.storage import StoreMixin |
| 13 |
from errbot.storage.base import StoragePluginBase |
| 14 |
from errbot.utils import ( |
| 15 |
entry_point_plugins, |
| 16 |
format_timedelta, |
| 17 |
split_string_after, |
| 18 |
version2tuple, |
| 19 |
) |
| 20 |
|
| 21 |
log = logging.getLogger(__name__) |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"v1,v2", |
| 26 |
[ |
| 27 |
("2.0.0", "2.0.1"), |
| 28 |
("2.0.0", "2.1.0"), |
| 29 |
("2.0.0", "3.0.0"), |
| 30 |
("2.0.0-alpha", "2.0.0-beta"), |
| 31 |
("2.0.0-beta", "2.0.0-rc1"), |
| 32 |
("2.0.0-rc1", "2.0.0-rc2"), |
| 33 |
("2.0.0-rc2", "2.0.0-rc3"), |
| 34 |
("2.0.0-rc2", "2.0.0"), |
| 35 |
("2.0.0-beta", "2.0.1"), |
| 36 |
], |
| 37 |
) |
| 38 |
def test_version_check(v1, v2): |
| 39 |
assert version2tuple(v1) < version2tuple(v2) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize( |
| 43 |
"version", |
| 44 |
[ |
| 45 |
"1.2.3.4", |
| 46 |
"1.2", |
| 47 |
"1.2.-beta", |
| 48 |
"1.2.3-toto", |
| 49 |
"1.2.3-rc", |
| 50 |
], |
| 51 |
) |
| 52 |
def test_version_check_negative(version): |
| 53 |
with pytest.raises(ValueError): |
| 54 |
version2tuple(version) |
| 55 |
|
| 56 |
|
| 57 |
def test_formattimedelta(): |
| 58 |
td = timedelta(0, 60 * 60 + 13 * 60) |
| 59 |
assert "1 hours and 13 minutes" == format_timedelta(td) |
| 60 |
|
| 61 |
|
| 62 |
def test_storage(): |
| 63 |
key = "test" |
| 64 |
|
| 65 |
__import__("errbot.config-template") |
| 66 |
config = ShallowConfig() |
| 67 |
config.__dict__.update(sys.modules["errbot.config-template"].__dict__) |
| 68 |
bot_config_defaults(config) |
| 69 |
|
| 70 |
spm = BackendPluginManager( |
| 71 |
config, "errbot.storage", "Memory", StoragePluginBase, CORE_STORAGE |
| 72 |
) |
| 73 |
storage_plugin = spm.load_plugin() |
| 74 |
|
| 75 |
persistent_object = StoreMixin() |
| 76 |
persistent_object.open_storage(storage_plugin, "test") |
| 77 |
persistent_object[key] = "à value" |
| 78 |
assert persistent_object[key] == "à value" |
| 79 |
assert key in persistent_object |
| 80 |
del persistent_object[key] |
| 81 |
assert key not in persistent_object |
| 82 |
assert len(persistent_object) == 0 |
| 83 |
|
| 84 |
|
| 85 |
def test_split_string_after_returns_original_string_when_chunksize_equals_string_size(): |
| 86 |
str_ = "foobar2000" * 2 |
| 87 |
splitter = split_string_after(str_, len(str_)) |
| 88 |
split = [chunk for chunk in splitter] |
| 89 |
assert [str_] == split |
| 90 |
|
| 91 |
|
| 92 |
def test_split_string_after_returns_original_string_when_chunksize_equals_string_size_plus_one(): |
| 93 |
str_ = "foobar2000" * 2 |
| 94 |
splitter = split_string_after(str_, len(str_) + 1) |
| 95 |
split = [chunk for chunk in splitter] |
| 96 |
assert [str_] == split |
| 97 |
|
| 98 |
|
| 99 |
def test_split_string_after_returns_two_chunks_when_chunksize_equals_string_size_minus_one(): |
| 100 |
str_ = "foobar2000" * 2 |
| 101 |
splitter = split_string_after(str_, len(str_) - 1) |
| 102 |
split = [chunk for chunk in splitter] |
| 103 |
assert ["foobar2000foobar200", "0"] == split |
| 104 |
|
| 105 |
|
| 106 |
def test_split_string_after_returns_two_chunks_when_chunksize_equals_half_length_of_string(): |
| 107 |
str_ = "foobar2000" * 2 |
| 108 |
splitter = split_string_after(str_, int(len(str_) / 2)) |
| 109 |
split = [chunk for chunk in splitter] |
| 110 |
assert ["foobar2000", "foobar2000"] == split |
| 111 |
|
| 112 |
|
| 113 |
def test_entry_point_plugins_no_groups(): |
| 114 |
result = entry_point_plugins("does_not_exist") |
| 115 |
assert [] == result |
| 116 |
|
| 117 |
|
| 118 |
def test_entry_point_plugins_valid_groups(): |
| 119 |
results = entry_point_plugins("console_scripts") |
| 120 |
match = False |
| 121 |
for result in results: |
| 122 |
if result.endswith("errbot/errbot.cli"): |
| 123 |
match = True |
| 124 |
assert match |