Skip to content

Commit

Permalink
fix: ensure choices/option parent is set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Jan 27, 2025
1 parent 854ffe6 commit ff902d8
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyxform/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
XForm Survey element classes for different question types.
"""

import copy
import os.path
from collections.abc import Callable, Generator, Iterable
from itertools import chain
Expand Down Expand Up @@ -372,7 +373,7 @@ def __init__(
kw_children = kwargs.pop(constants.CHILDREN, None)
choices = coalesce(kw_choices, kw_children)
if isinstance(choices, tuple) and isinstance(next(iter(choices)), Option):
self.children = choices
self.children = copy.deepcopy(choices)
elif choices:
self.children = tuple(
Option(**c) for c in combine_lists(kw_choices, kw_children)
Expand Down
124 changes: 122 additions & 2 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
from unittest import TestCase

import defusedxml.ElementTree as ETree
from pyxform import InputQuestion, Survey
from pyxform.builder import SurveyElementBuilder, create_survey_from_xls
from pyxform import InputQuestion, MultipleChoiceQuestion, Survey
from pyxform.builder import (
SurveyElementBuilder,
create_survey_from_xls,
create_survey_element_from_dict,
)
from pyxform.errors import PyXFormError
from pyxform.question import Option
from pyxform.xls2json import print_pyobj_to_json

from tests import utils
Expand Down Expand Up @@ -562,3 +567,118 @@ def test_style_added_to_body_if_present(self):
]
self.assertEqual(len(body_elms), 1)
self.assertEqual(body_elms[0].get("class"), "ltr")

def test_options_has_correct_parent(self):
survey_dict = {
"type": "survey",
"name": "data",
"title": "empty_cell",
"id_string": "empty_cell",
"sms_keyword": "empty_cell",
"default_language": "default",
"children": [
{
"type": "group",
"name": "a",
"label": "Group A",
"children": [
{
"type": "select one",
"name": "fruita",
"label": "Fruit A",
"parameters": {},
"itemset": "fruits",
"list_name": "fruits",
"choices": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
},
{
"type": "select one",
"name": "fruity",
"label": "Fruit Y",
"parameters": {},
"itemset": "fruity",
"list_name": "fruity",
"choices": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
},
],
},
{
"type": "group",
"name": "b",
"label": "Group B",
"children": [
{
"type": "select one",
"name": "fruitz",
"label": "Fruit Z",
"parameters": {},
"itemset": "fruits",
"list_name": "fruits",
"choices": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
},
{
"type": "select all that apply",
"name": "fruitb",
"label": "Fruit B",
"parameters": {},
"itemset": "fruity",
"list_name": "fruity",
"choices": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
},
],
},
{
"name": "meta",
"type": "group",
"control": {"bodyless": True},
"children": [
{
"name": "instanceID",
"bind": {"readonly": "true()", "jr:preload": "uid"},
"type": "calculate",
}
],
},
],
"choices": {
"fruits": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
"fruity": [
{"name": "orange", "label": "Orange"},
{"name": "mango", "label": "Mango"},
],
},
}
survey = create_survey_element_from_dict(survey_dict)

fruita = survey.children[0].children[0]
self.assertTrue(isinstance(fruita, MultipleChoiceQuestion))
self.assertEqual(fruita.name, "fruita")

fruita_orange = fruita.children[0]
self.assertTrue(isinstance(fruita_orange, Option))
self.assertEqual(fruita_orange.name, "orange")
self.assertEqual(fruita_orange.parent, fruita)

fruitz = survey.children[1].children[0]
self.assertTrue(isinstance(fruitz, MultipleChoiceQuestion))
self.assertEqual(fruitz.name, "fruitz")

fruitz_orange = fruitz.children[0]
self.assertTrue(isinstance(fruitz_orange, Option))
self.assertEqual(fruitz_orange.name, "orange")
self.assertEqual(fruitz_orange.parent, fruitz)

0 comments on commit ff902d8

Please sign in to comment.