Skip to content

Commit

Permalink
Merge pull request #19 from dianakhuang/invert-rule
Browse files Browse the repository at this point in the history
Adding inversion operator and a test to verify that it works.
  • Loading branch information
daisylb committed May 22, 2020
2 parents 2aa0a90 + 85089bc commit 1f066ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bridgekeeper/rule_attribute_tests.py
Expand Up @@ -49,3 +49,15 @@ def test_when_called_without_object():
user = UserFactory(username='a')
p = Attribute('name', lambda u: u.username)
assert not p.check(user)


@pytest.mark.django_db
def test_inversion_of_attribute():
u1 = UserFactory(username='a')
u2 = UserFactory(username='b')
s1 = StoreFactory(name='a')
s2 = StoreFactory(name='b')
p = ~Attribute('name', lambda u: u.username)

assert p.check(u1, s2)
assert p.check(u2, s1)
6 changes: 6 additions & 0 deletions bridgekeeper/rules.py
Expand Up @@ -131,6 +131,9 @@ def __or__(self, other):
def __not__(self):
return Not(self)

def __invert__(self):
return Not(self)


class BinaryCompositeRule(Rule):
def __init__(self, left, right):
Expand Down Expand Up @@ -220,6 +223,9 @@ def query(self, user):
def check(self, user, instance=None):
return not self.base.check(user, instance)

def __invert__(self):
return self.base


class blanket_rule(Rule): # noqa: used as a decorator, so should be lowercase
"""A decorator for rules that don't depend on objects.
Expand Down

0 comments on commit 1f066ea

Please sign in to comment.