Support subset operator between index definitions

This commit is contained in:
Cédric Krier committed 2023-05-22 14:32:43 +02:00
1 parent e9ba0a436e
commit 6417fba801
3 files changed
+100

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Support subset operator between index definitions
* Add sum_tree helper
* Convert sum attribute into boolean
* Store records selection on export
+20
View File
@@ -189,6 +189,26 @@ class Index:
return str(value) if isinstance(value, Expression) else value
return {k: _format(v) for k, v in self.options.items()}
def __lt__(self, other):
if not isinstance(other, self.__class__):
return NotImplementedError
if (self.table != other.table
or self._options_cmp != other._options_cmp):
return False
if self == other:
return False
if len(self.expressions) >= len(other.expressions):
return False
for (c, u), (oc, ou) in zip(self.expressions, other.expressions):
if (str(c), u) != (str(oc), ou):
return False
return True
def __le__(self, other):
if not isinstance(other, self.__class__):
return NotImplementedError
return self < other or self == other
class Unaccent(Expression):
"Unaccent function if database support for index"
__slots__ = ('_expression',)
+79
View File
@@ -129,6 +129,85 @@ class ModelIndexTestCase(unittest.TestCase):
self.assertEqual(index1, index2)
def test_index_subset(self):
"Test Index subset"
table = Table('test')
index1 = Index(
table,
(table.foo, Index.Equality()))
index2 = Index(
table,
(table.foo, Index.Equality()),
(table.bar, Index.Equality()))
self.assertEqual(index1 < index2, True)
self.assertEqual(index2 < index1, False)
def test_index_subset_expressions(self):
"Test Index subset"
table = Table('test')
index1 = Index(
table,
(table.foo, Index.Equality()))
index2 = Index(
table,
(table.bar, Index.Equality()),
(table.foo, Index.Equality()))
self.assertEqual(index1 < index2, False)
self.assertEqual(index2 < index1, False)
def test_index_subset_same(self):
"Test Index subset"
table = Table('test')
index1 = Index(
table,
(table.foo, Index.Equality()))
index2 = Index(
table,
(table.foo, Index.Equality()))
self.assertEqual(index1 < index2, False)
self.assertEqual(index2 < index1, False)
self.assertLessEqual(index1, index2)
def test_index_subset_other(self):
"Test Index subset with other type"
table = Table('test')
index = Index(table)
self.assertEqual(index < 0, NotImplementedError)
def test_index_subset_table(self):
"Test Index subset on different table"
table1 = Table('foo')
table2 = Table('bar')
index1 = Index(
table1,
(table1.name, Index.Equality()))
index2 = Index(
table2,
(table2.name, Index.Equality()))
self.assertEqual(index1 < index2, False)
self.assertEqual(index2 < index1, False)
def test_index_subset_options(self):
"Test Index subset with different options"
table1 = Table('foo')
table2 = Table('bar')
index1 = Index(
table1,
(table1.name, Index.Equality()),
where=table1.name == 'bar')
index2 = Index(
table2,
(table2.name, Index.Equality()),
where=table2.name == 'foo')
self.assertEqual(index1 < index2, False)
self.assertEqual(index2 < index1, False)
def test_index_hash(self):
"Test Index hash"
table1 = Table('test')