Add direction and uselist properties

This commit is contained in:
Konsta Vesterinen
2014-12-12 10:13:48 +02:00
parent 2c22e69edc
commit a30b42421d
2 changed files with 28 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import sqlalchemy as sa
from sqlalchemy.orm.attributes import InstrumentedAttribute
from sqlalchemy.util.langhelpers import symbol
from .utils import str_coercible
@@ -105,9 +106,22 @@ class AttrPath(object):
if el is element:
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):
result = self.parts[slice]
if isinstance(result, list):
if isinstance(result, list) and result:
if result[0] is self.parts[0]:
class_ = self.class_
else:

View File

@@ -1,6 +1,7 @@
import six
from pytest import mark
import sqlalchemy as sa
from pytest import mark
from sqlalchemy.util.langhelpers import symbol
from sqlalchemy_utils.path import Path, AttrPath
from tests import TestCase
@@ -41,6 +42,17 @@ class TestAttrPath(TestCase):
self.Section = Section
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):
path = ~ AttrPath(self.SubSection, 'section.document')
assert path.parts == [