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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
import pytest
from sqlalchemy_utils import Ltree
class TestLtree:
def test_init(self):
assert Ltree('path.path') == Ltree(Ltree('path.path'))
def test_constructor_with_wrong_type(self):
with pytest.raises(TypeError) as e:
Ltree(None)
assert str(e.value) == (
"Ltree() argument must be a string or an Ltree, not 'NoneType'"
)
def test_constructor_with_invalid_code(self):
with pytest.raises(ValueError) as e:
Ltree('..')
assert str(e.value) == "'..' is not a valid ltree path."
@pytest.mark.parametrize(
'code',
(
'path',
'path.path',
'1_.2',
'_._',
)
)
def test_validate_with_valid_codes(self, code):
Ltree.validate(code)
@pytest.mark.parametrize(
'path',
(
'',
'.',
'path.',
'path..path',
'path.path..path',
'path.path..',
'path.รครถ',
)
)
def test_validate_with_invalid_path(self, path):
with pytest.raises(ValueError) as e:
Ltree.validate(path)
assert str(e.value) == (
f"'{path}' is not a valid ltree path."
)
@pytest.mark.parametrize(
('path', 'length'),
(
('path', 1),
('1.1', 2),
('1.2.3', 3)
)
)
def test_length(self, path, length):
assert len(Ltree(path)) == length
@pytest.mark.parametrize(
('path', 'subpath', 'index'),
(
('path.path', 'path', 0),
('1.2.3', '2.3', 1),
('1.2.3.4', '2.3', 1),
('1.2.3.4', '3.4', 2)
)
)
def test_index(self, path, subpath, index):
assert Ltree(path).index(subpath) == index
@pytest.mark.parametrize(
('path', 'item_slice', 'result'),
(
('path.path', 0, 'path'),
('1.1.2.3', slice(1, 3), '1.2'),
('1.1.2.3', slice(1, None), '1.2.3'),
)
)
def test_getitem(self, path, item_slice, result):
assert Ltree(path)[item_slice] == result
@pytest.mark.parametrize(
('path', 'others', 'result'),
(
('1.2.3', ['1.2.3', '1.2'], '1'),
('1.2.3.4.5', ['1.2.3', '1.2.3.4'], '1.2'),
('1.2.3.4.5', ['3.4', '1.2.3.4'], None),
('1.2', ['1.2.3', '1.2.4'], '1')
)
)
def test_lca(self, path, others, result):
assert Ltree(path).lca(*others) == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1.2.3', '4.5', '1.2.3.4.5'),
('1', '1', '1.1'),
)
)
def test_add(self, path, other, result):
assert Ltree(path) + other == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1.2.3', '4.5', '4.5.1.2.3'),
('1', '1', '1.1'),
)
)
def test_radd(self, path, other, result):
assert other + Ltree(path) == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1.2.3', '4.5', '1.2.3.4.5'),
('1', '1', '1.1'),
)
)
def test_iadd(self, path, other, result):
ltree = Ltree(path)
ltree += other
assert ltree == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1.2.3', '2', True),
('1.2.3', '3', True),
('1', '1', True),
('1', '2', False),
)
)
def test_contains(self, path, other, result):
assert (other in Ltree(path)) == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1', '1.2.3', True),
('1.2', '1.2.3', True),
('1.2.3', '1.2.3', True),
('1.2.3', '1', False),
('1.2.3', '1.2', False),
('1', '1', True),
('1', '2', False),
)
)
def test_ancestor_of(self, path, other, result):
assert Ltree(path).ancestor_of(other) == result
@pytest.mark.parametrize(
('path', 'other', 'result'),
(
('1', '1.2.3', False),
('1.2', '1.2.3', False),
('1.2', '1.2.3', False),
('1.2.3', '1', True),
('1.2.3', '1.2', True),
('1.2.3', '1.2.3', True),
('1', '1', True),
('1', '2', False),
)
)
def test_descendant_of(self, path, other, result):
assert Ltree(path).descendant_of(other) == result
def test_getitem_with_other_than_slice_or_in(self):
with pytest.raises(TypeError):
Ltree('1.2')['something']
def test_index_raises_value_error_if_subpath_not_found(self):
with pytest.raises(ValueError):
Ltree('1.2').index('3')
def test_equality_operator(self):
assert Ltree('path.path') == 'path.path'
assert 'path.path' == Ltree('path.path')
assert Ltree('path.path') == Ltree('path.path')
def test_non_equality_operator(self):
assert Ltree('path.path') != 'path.'
assert not (Ltree('path.path') != 'path.path')
def test_hash(self):
assert hash(Ltree('path')) == hash('path')
def test_repr(self):
assert repr(Ltree('path')) == "Ltree('path')"
def test_str(self):
ltree = Ltree('path.path')
assert str(ltree) == 'path.path'
def test_lt(self):
assert Ltree('1') < Ltree('2')
assert Ltree('1.2.3') < Ltree('1.2.4')
assert Ltree('1.2.3') < Ltree('1.2.3.4')
def test_lte(self):
assert Ltree('1.2.3') <= Ltree('1.2.4')
assert Ltree('1') <= Ltree('1')
def test_gt(self):
assert Ltree('2') > Ltree('1')
assert Ltree('1.2.3') > Ltree('1.2.2')
assert Ltree('1.2.3.4') > Ltree('1.2.3')
def test_gte(self):
assert Ltree('1.2.3') >= Ltree('1.2.2')
assert Ltree('1') >= Ltree('1')
|