Skip to content

Commit

Permalink
Can we add comparison functions to Ltrees? (#668)
Browse files Browse the repository at this point in the history
* Add some comparison tests
* Add some comparison tests
* Fix a bug
* Add comparison functions
* Add a load more tests
  • Loading branch information
salimfadhley committed Feb 12, 2023
1 parent aa40fda commit 0b00cca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sqlalchemy_utils/primitives/ltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,15 @@ def __unicode__(self):

def __contains__(self, label):
return label in self.path.split('.')

def __gt__(self, other):
return self.path > other.path

def __lt__(self, other):
return self.path < other.path

def __ge__(self, other):
return self.path >= other.path

def __le__(self, other):
return self.path <= other.path
18 changes: 18 additions & 0 deletions tests/primitives/test_ltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,21 @@ def test_repr(self):
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')

0 comments on commit 0b00cca

Please sign in to comment.