Skip to content

Commit

Permalink
fix: use mapby args if literal, fix calculated test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Borer committed Jan 11, 2021
1 parent 7937220 commit cb49617
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
5 changes: 3 additions & 2 deletions caluma/caluma_core/jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pyjexl
from pyjexl.analysis import ValidatingAnalyzer
from pyjexl.exceptions import ParseError
from pyjexl.parser import Literal
from rest_framework import exceptions

log = getLogger(__name__)
Expand Down Expand Up @@ -111,7 +112,7 @@ def __init__(self, config, transforms=None):

def visit_Transform(self, transform):
if not self.transforms or transform.name in self.transforms:
# can only extract subject's value if subject is not a transform itself
# can only extract subject's value if subject is a Literal
if not isinstance(transform.subject, type(transform)):
yield transform.subject.value

Expand All @@ -123,7 +124,7 @@ def visit_Transform(self, transform):
transform.name == "mapby"
and transform.subject
and transform.subject.name == "answer"
and not isinstance(transform.args[0], type(transform))
and isinstance(transform.args[0], Literal)
):
yield transform.args[0].value

Expand Down
14 changes: 5 additions & 9 deletions caluma/caluma_form/jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _get_referenced_fields(self, field: Field, expr: str):
f"Question `{slug}` could not be found in form {field.form}"
)

return deps, referenced_fields
return referenced_fields

def is_hidden(self, field: Field):
"""Return True if the given field is hidden.
Expand All @@ -128,13 +128,11 @@ def is_hidden(self, field: Field):
# expression. If all dependencies are hidden,
# there is no way to evaluate our own visibility, so we default to
# hidden state as well.
deps, referenced_fields = self._get_referenced_fields(
field, field.question.is_hidden
)
referenced_fields = self._get_referenced_fields(field, field.question.is_hidden)

# all() returns True for the empty set, thus we need to
# check that we have some deps at all first
all_deps_hidden = bool(deps) and all(
all_deps_hidden = bool(referenced_fields) and all(
self.is_hidden(ref_field) for ref_field in referenced_fields
)
if all_deps_hidden:
Expand Down Expand Up @@ -163,13 +161,11 @@ def is_required(self, field: Field):
if cache_key in self._cache["required"]:
return self._cache["required"][cache_key]

deps, referenced_fields = self._get_referenced_fields(
field, question.is_required
)
referenced_fields = self._get_referenced_fields(field, question.is_required)

# all() returns True for the empty set, thus we need to
# check that we have some deps at all first
all_deps_hidden = bool(deps) and all(
all_deps_hidden = bool(referenced_fields) and all(
self.is_hidden(ref_field) for ref_field in referenced_fields
)
if all_deps_hidden:
Expand Down
11 changes: 9 additions & 2 deletions caluma/caluma_form/tests/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,20 @@ def test_calculated_question_answer_document(
assert calc_ans.value is None

# adding another row will make make the expression valid
row_doc = document_factory(form=row_form)
answer_factory(document=row_doc, question=column, value=200)
row_doc = document_factory(form=row_form, family=document)
column_a2 = answer_factory(document=row_doc, question_id=column.slug, value=200)
table_a.documents.add(row_doc)

calc_ans.refresh_from_db()
assert calc_ans.value == 300

column_a2.value = 100
column_a2.save()
calc_ans.refresh_from_db()
column.refresh_from_db()
assert column.calc_dependents == ["calc_question"]
assert calc_ans.value == 200

# removing the row will make it invalid again
table_a.documents.remove(row_doc)
row_doc.delete()
Expand Down

0 comments on commit cb49617

Please sign in to comment.