Skip to content

Commit

Permalink
tests: add tests for the conditional disable generators
Browse files Browse the repository at this point in the history
  • Loading branch information
max-moser committed Feb 14, 2023
1 parent 58ec996 commit b250e67
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
AnyUserIfPublic,
AuthenticatedUser,
Disable,
DisableIf,
DisableIfReadOnly,
Generator,
RecordOwners,
SystemProcess,
Expand Down Expand Up @@ -176,3 +178,67 @@ def test_allowedbyaccesslevels_query_filter(mocker):
)

assert query_filter == []


def test_disable_if_scalar_value(app):
generator = DisableIf(False)
assert generator.needs() == []
assert generator.excludes() == []
assert generator.query_filter() == []

generator = DisableIf(True)
assert generator.needs() == []
assert generator.excludes() == [any_user]
assert generator.query_filter().to_dict() == {"match_none": {}}


def test_disable_if_function(app):
def _if_two_kwargs(**kwargs):
return len(kwargs) == 2

# The generator should only disable if 2 keyword arguments are supplied
generator = DisableIf(_if_two_kwargs)
assert generator.excludes(record=None) == []
assert generator.query_filter(record=None) == []

assert generator.excludes(record=None, second_arg=None) == [any_user]
assert generator.query_filter(record=None, second_arg=None).to_dict() == {
"match_none": {}
}

assert generator.excludes(record=None, second_arg=None, third_arg=None) == []
assert generator.query_filter(record=None, second_arg=None, third_arg=None) == []


def test_disable_if_read_only(app):
generator = DisableIfReadOnly()

# Normal operation
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = False
assert generator.excludes(record=None) == []
assert generator.query_filter(record=None) == []

# System is in read-only mode
app.config["RECORDS_PERMISSIONS_READ_ONLY"] = True
assert generator.excludes(record=None) == [any_user]
assert generator.query_filter(record=None).to_dict() == {"match_none": {}}


def test_disable_if_read_only_function(app):
def _some_function(record, **kwargs):
return record is None and not kwargs.get("exception", False)

app.config["RECORDS_PERMISSIONS_READ_ONLY"] = _some_function
generator = DisableIfReadOnly()

# Don't deny on non-None records
assert generator.excludes(record={}) == []
assert generator.query_filter(record={}) == []

# Deny if the record is None
assert generator.excludes(record=None) == [any_user]
assert generator.query_filter(record=None).to_dict() == {"match_none": {}}

# Make an exception if requested
assert generator.excludes(record=None, exception=True) == []
assert generator.query_filter(record=None, exception=True) == []

0 comments on commit b250e67

Please sign in to comment.