Add direction and uselist properties
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||||
|
from sqlalchemy.util.langhelpers import symbol
|
||||||
from .utils import str_coercible
|
from .utils import str_coercible
|
||||||
|
|
||||||
|
|
||||||
@@ -105,9 +106,22 @@ class AttrPath(object):
|
|||||||
if el is element:
|
if el is element:
|
||||||
return index
|
return index
|
||||||
|
|
||||||
|
@property
|
||||||
|
def direction(self):
|
||||||
|
symbols = [part.property.direction for part in self.parts]
|
||||||
|
if symbol('MANYTOMANY') in symbols:
|
||||||
|
return symbol('MANYTOMANY')
|
||||||
|
elif symbol('MANYTOONE') in symbols and symbol('ONETOMANY') in symbols:
|
||||||
|
return symbol('MANYTOMANY')
|
||||||
|
return symbols[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def uselist(self):
|
||||||
|
return any(part.property.uselist for part in self.parts)
|
||||||
|
|
||||||
def __getitem__(self, slice):
|
def __getitem__(self, slice):
|
||||||
result = self.parts[slice]
|
result = self.parts[slice]
|
||||||
if isinstance(result, list):
|
if isinstance(result, list) and result:
|
||||||
if result[0] is self.parts[0]:
|
if result[0] is self.parts[0]:
|
||||||
class_ = self.class_
|
class_ = self.class_
|
||||||
else:
|
else:
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import six
|
import six
|
||||||
from pytest import mark
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from pytest import mark
|
||||||
|
from sqlalchemy.util.langhelpers import symbol
|
||||||
from sqlalchemy_utils.path import Path, AttrPath
|
from sqlalchemy_utils.path import Path, AttrPath
|
||||||
from tests import TestCase
|
from tests import TestCase
|
||||||
|
|
||||||
@@ -41,6 +42,17 @@ class TestAttrPath(TestCase):
|
|||||||
self.Section = Section
|
self.Section = Section
|
||||||
self.SubSection = SubSection
|
self.SubSection = SubSection
|
||||||
|
|
||||||
|
@mark.parametrize(
|
||||||
|
('class_', 'path', 'direction'),
|
||||||
|
(
|
||||||
|
('SubSection', 'section', symbol('MANYTOONE')),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_direction(self, class_, path, direction):
|
||||||
|
assert (
|
||||||
|
AttrPath(getattr(self, class_), path).direction == direction
|
||||||
|
)
|
||||||
|
|
||||||
def test_invert(self):
|
def test_invert(self):
|
||||||
path = ~ AttrPath(self.SubSection, 'section.document')
|
path = ~ AttrPath(self.SubSection, 'section.document')
|
||||||
assert path.parts == [
|
assert path.parts == [
|
||||||
|
Reference in New Issue
Block a user