Skip to content

Commit

Permalink
Issue #25: Method removed, test coverage improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
boonya committed Oct 25, 2016
1 parent 9f419cd commit 1712d9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
31 changes: 7 additions & 24 deletions domain_models/views.py
Expand Up @@ -89,35 +89,18 @@ def check_properties(mcs, attributes):
:type attributes: dict
"""
include, exclude = mcs.get_prepared_include_exclude(attributes)

if include:
intersections = mcs.get_intersections(attributes, include)
attr = '__include__'
elif exclude:
intersections = mcs.get_intersections(attributes, exclude)
attr = '__exclude__'
else:
return None

properties = mcs.get_properties(attributes)
intersections = list(
set(properties).intersection(include if include else exclude))
if not intersections:
return None

attr_name = '__include__' if include else '__exclude__'

raise AttributeError(
"It is not allowed to mention already defined properties: "
"{0} in {1} attributes.".format(", ".join(intersections), attr))

@classmethod
def get_intersections(mcs, attributes, attr):
"""Return intersection with defined properties if exists.
:type attributes: dict
:type attr: list
:rtype: list
"""
if not attr:
return []
properties = mcs.get_properties(attributes)
return list(set(properties).intersection(attr))
"{0} in {1} attributes.".format(", ".join(intersections),
attr_name))


@six.add_metaclass(ContextViewMetaClass)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_context_view.py
Expand Up @@ -156,6 +156,15 @@ class WrongContext(views.ContextView):
__model_cls__ = Profile
__exclude__ = [Profile.name]

with self.assertRaises(AttributeError):
class WrongContext(views.ContextView):
__model_cls__ = Profile
__include__ = (Profile.id, Profile.name)

@property
def name(self):
return "Any name"

with self.assertRaises(AttributeError):
class WrongContext(views.ContextView):
__model_cls__ = Profile
Expand Down

0 comments on commit 1712d9b

Please sign in to comment.