def form_requires_case(form, case_type=None, parent_case_type=None, update=None, preload=None): if form.form_type == 'module_form': form.requires = 'case' if update: form.actions.update_case = UpdateCaseAction(update=update) form.actions.update_case.condition.type = 'always' if preload: form.actions.case_preload = PreloadAction(preload=preload) form.actions.case_preload.condition.type = 'always' if parent_case_type: module = form.get_module() module.parent_select.active = True parent_select_module = next( module for module in module.get_app().get_modules() if module.case_type == parent_case_type ) module.parent_select.module_id = parent_select_module.unique_id else: case_type = case_type or form.get_module().case_type index = len([load for load in form.actions.load_update_cases if load.case_type == case_type]) kwargs = {'case_type': case_type, 'case_tag': 'load_{}_{}'.format(case_type, index)} if update: kwargs['case_properties'] = update if preload: kwargs['preload'] = preload action = LoadUpdateAction(**kwargs) if parent_case_type: parent_action = form.actions.load_update_cases[-1] assert parent_action.case_type == parent_case_type action.case_index = CaseIndex(tag=parent_action.case_tag) form.actions.load_update_cases.append(action)
def form_updates_case(form, case_type=None, parent_case_type=None): if form.form_type == 'module_form': form.requires = 'case' form.actions.update_case = UpdateCaseAction(update={'question1': '/data/question1'}) form.actions.update_case.condition.type = 'always' if parent_case_type: module = form.get_module() module.parent_select.active = True parent_select_module = next( module for module in module.get_app().get_modules() if module.case_type == parent_case_type ) module.parent_select.module_id = parent_select_module.unique_id else: case_type = case_type or form.get_module().case_type index = len([load for load in form.actions.load_update_cases if load.case_type == case_type]) action = LoadUpdateAction( case_type=case_type, case_tag='load_{}_{}'.format(case_type, index), ) if parent_case_type: parent_action = form.actions.load_update_cases[-1] assert parent_action.case_type == parent_case_type action.parent_tag = parent_action.case_tag form.actions.load_update_cases.append(action)
def test_parent_tag(self): """ LoadUpdateAction migration should create a CaseIndex if a parent tag is given """ action = LoadUpdateAction.wrap({ 'case_type': 'spam', 'case_tag': 'ham', 'parent_tag': 'eggs', }) self.assertEqual(action.case_index.tag, 'eggs')
def form_requires_case(form, case_type=None, parent_case_type=None, update=None, preload=None): if form.form_type == 'module_form': form.requires = 'case' if update: form.actions.update_case = UpdateCaseAction(update=update) form.actions.update_case.condition.type = 'always' if preload: form.actions.case_preload = PreloadAction(preload=preload) form.actions.case_preload.condition.type = 'always' if parent_case_type: module = form.get_module() module.parent_select.active = True parent_select_module = next( module for module in module.get_app().get_modules() if module.case_type == parent_case_type) module.parent_select.module_id = parent_select_module.unique_id else: case_type = case_type or form.get_module().case_type index = len([ load for load in form.actions.load_update_cases if load.case_type == case_type ]) kwargs = { 'case_type': case_type, 'case_tag': 'load_{}_{}'.format(case_type, index) } if update: kwargs['case_properties'] = update if preload: kwargs['preload'] = preload action = LoadUpdateAction(**kwargs) if parent_case_type: parent_action = form.actions.load_update_cases[-1] assert parent_action.case_type == parent_case_type action.case_index = CaseIndex(tag=parent_action.case_tag) form.actions.load_update_cases.append(action)
def test_update_parent_case(self): self.form.actions.load_update_cases.append( LoadUpdateAction(case_type=self.module.case_type, case_tag='load_1', case_properties={ 'question1': '/data/question1', 'parent/question1': '/data/question1' })) self.assertXmlEqual(self.get_xml('update_parent_case'), self.form.render_xform())
def advanced_form_autoloads(form, mode, value_key, value_source=None): """See corehq.apps.app_manager.models.AutoSelectCase """ assert isinstance(form, AdvancedForm) form.actions.load_update_cases.append( LoadUpdateAction(case_tag='auto_select_{}'.format(mode), auto_select=AutoSelectCase( mode=mode, value_source=value_source, value_key=value_key)))
def test_defaults(self): """ LoadUpdateAction migration should create a CaseIndex with property defaults """ action = LoadUpdateAction.wrap({ 'case_type': 'spam', 'case_tag': 'ham', 'parent_tag': 'eggs', }) self.assertEqual(action.case_index.reference_id, 'parent') self.assertEqual(action.case_index.relationship, 'child')
def test_advanced_action_no_parent_tag(self): """ LoadUpdateAction migration should not create a CaseIndex without parent_tag """ action = LoadUpdateAction.wrap({ 'case_type': 'spam', 'case_tag': 'ham', 'parent_reference_id': 'spam', 'relationship': 'extension', }) self.assertFalse(bool(action.case_index.tag))
def test_registration_form_subcase(self): self.form.actions.load_update_cases.append( LoadUpdateAction(case_type="parent", case_tag="parent")) self.form.actions.open_cases = [ AdvancedOpenCaseAction(case_tag="child", case_type="child", name_path="/data/question1", case_indices=[CaseIndex(tag="parent")]) ] self.assertTrue(self.form.is_registration_form())
def test_update_preload_multiple_case(self): self.form.actions.load_update_cases.append( LoadUpdateAction( case_type=self.module.case_type, case_tag='load_1', case_properties={ 'question1': ConditionalCaseUpdate(question_path='/data/question1') }, preload={'/data/question1': 'question1'})) self.form.actions.load_update_cases.append( LoadUpdateAction( case_type=self.module.case_type, case_tag='load_2', case_properties={ 'question2': ConditionalCaseUpdate(question_path='/data/question2') }, preload={'/data/question2': 'question2'})) self.assertXmlEqual(self.get_xml('update_preload_case_multiple'), self.form.render_xform())
def test_update_attachment(self): self.form.source = self.get_xml('attachment').decode('utf-8') self.form.actions.load_update_cases.append( LoadUpdateAction( case_type=self.module.case_type, case_tag='load_1', case_properties={ 'photo': ConditionalCaseUpdate(question_path='/data/thepicture') })) self.assertXmlEqual(self.get_xml('update_attachment_case'), self.form.render_xform())
def test_registration_form_autoload_subcase(self): self.form.actions.load_update_cases = [ LoadUpdateAction( case_type="parent", case_tag="parent" ), LoadUpdateAction( auto_select=AutoSelectCase(mode=AUTO_SELECT_USERCASE, value_key=""), ) ] self.form.actions.open_cases = [ AdvancedOpenCaseAction( case_tag="child", case_type="child", name_path="/data/question1", case_indices=[CaseIndex(tag="parent")] ) ] self.assertTrue(self.form.is_registration_form())
def test_advanced_suite_auto_select_case(self): app = Application.wrap(self.get_json('suite-advanced')) load_update_cases = app.get_module(1).get_form( 0).actions.load_update_cases load_update_cases.append( LoadUpdateAction(case_tag='auto_selected', auto_select=AutoSelectCase( mode=AUTO_SELECT_CASE, value_source=load_update_cases[0].case_tag, value_key='case_id_index'))) self.assertXmlEqual(self.get_xml('suite-advanced-autoselect-case'), app.create_suite())
def test_subcase(self): self.form.actions.load_update_cases.append(LoadUpdateAction( case_type=self.parent_module.case_type, case_tag='load_1', )) self.form.actions.open_cases.append(AdvancedOpenCaseAction( case_type=self.module.case_type, case_tag='open_1', name_path='/data/mother_name', case_indices=[CaseIndex(tag='load_1')] )) self.form.actions.open_cases[0].open_condition.type = 'always' self.assertXmlEqual(self.get_xml('subcase'), self.form.render_xform())
def test_advanced_form_get_action_type(self): app = Application.new_app('domain', "Untitled Application") parent_module = app.add_module(AdvancedModule.new_module('parent', None)) parent_module.case_type = 'parent' parent_module.unique_id = 'id_parent_module' form = app.new_form(0, "Untitled Form", None) form.xmlns = 'http://id_m1-f0' form.actions.load_update_cases.append(LoadUpdateAction(case_type="clinic", case_tag='load_0')) modules, errors = get_app_summary_formdata('domain', app) self.assertEqual(modules[0]['forms'][0]['action_type'], 'load (load_0)')
def test_ext_case_update_host_case(self): """ A extension case should be able to save host case properties in an advanced module """ self.form.actions.load_update_cases.append(LoadUpdateAction( case_type=self.module.case_type, case_tag='load_1', case_properties={ 'question1': '/data/question1', 'host/question1': '/data/question1' }, )) self.assertXmlEqual(self.get_xml('update_host_case_adv'), self.form.render_xform())
def test_schedule_index(self): self.module.has_schedule = True form = self.app.new_form(0, 'New Form', lang='en') form.source = self.get_xml('original_form', override_path=('data', )) form.actions.load_update_cases.append( LoadUpdateAction(case_type=self.module.case_type, case_tag='load_1', case_properties={'question1': '/data/question1'})) form.schedule = FormSchedule(anchor='edd') xml = self.get_xml('schedule').format(form_id=form.schedule_form_id, form_index=2) self.assertXmlEqual(xml, form.render_xform())
def test_subcase_repeat(self): self.form.actions.load_update_cases.append(LoadUpdateAction( case_type=self.parent_module.case_type, case_tag='load_1', )) self.form.actions.open_cases.append(AdvancedOpenCaseAction( case_type=self.module.case_type, case_tag='open_1', name_path='/data/mother_name', parent_tag='load_1', repeat_context="/data/child" )) self.form.actions.open_cases[0].open_condition.type = 'always' self.assertXmlEqual(self.get_xml('subcase-repeat'), self.form.render_xform())
def test_advanced_suite_load_case_from_fixture_with_report_fixture(self): app = Application.wrap(self.get_json('suite-advanced')) app.get_module(1).get_form(0).actions.load_update_cases.append(LoadUpdateAction( case_tag="", case_type="clinic", load_case_from_fixture=LoadCaseFromFixture( fixture_nodeset="instance('commcare:reports')/reports/report[@id='some-report-guid']/rows/row", fixture_tag="selected_row", fixture_variable="index", ) )) suite = app.create_suite() self.assertXmlPartialEqual(self.get_xml('load_case_from_report_fixture_session'), suite, './entry[2]/session') self.assertXmlPartialEqual(self.get_xml('load_case_from_report_fixture_instance'), suite, './entry[2]/instance')
def test_advanced_suite_auto_select_with_filter(self): """ Form filtering should be done using the last 'non-autoload' case being loaded. """ app = Application.wrap(self.get_json('suite-advanced')) app.get_module(1).get_form(0).actions.load_update_cases.append( LoadUpdateAction(case_tag='autoload', auto_select=AutoSelectCase(mode=AUTO_SELECT_USER, value_key='case_id'))) form = app.get_module(1).get_form(0) form.form_filter = "./edd = '123'" self.assertXmlEqual( self.get_xml('suite-advanced-autoselect-with-filter'), app.create_suite())
def test_properties(self): """ LoadUpdateAction migration should create a CaseIndex with given properties """ action = LoadUpdateAction.wrap({ 'case_type': 'spam', 'case_tag': 'ham', 'parent_tag': 'eggs', 'parent_reference_id': 'spam', 'relationship': 'extension', }) self.assertEqual(action.case_index.tag, 'eggs') self.assertEqual(action.case_index.reference_id, 'spam') self.assertEqual(action.case_index.relationship, 'extension')
def setUp(self): self.factory = AppFactory() self.advanced_module, self.form0 = self.factory.new_advanced_module( 'advanced_module', 'patient') self.form0.xmlns = 'http://openrosa.org/formdesigner/firstform' self.form0.actions.open_cases = [ AdvancedOpenCaseAction( case_type="patient", case_tag="open__0", ) ] self.form0.actions.load_update_cases = [ LoadUpdateAction( case_type="patient", case_tag="load_0", case_properties={ "name": ConditionalCaseUpdate(question_path="/data/name") }, preload={"/data/name": "name"}, details_module=self.advanced_module.unique_id, ) ] self.shadow_form = self.factory.new_shadow_form(self.advanced_module) self.shadow_form.shadow_parent_form_id = self.form0.unique_id # Shadow form load_update_case actions should contain all case tags from the parent self.shadow_form.extra_actions.load_update_cases = [ LoadUpdateAction( case_type="patient", case_tag="load_0", details_module=self.advanced_module.unique_id, ) ] self.basic_module = self.factory.new_basic_module("basic_module", "doctor", with_form=False)
def test_shadow_form_action_modifications(self, *args): # Confirm that shadow form action modifications are reflected in the suite file original_actions = self.shadow_form.extra_actions.load_update_cases try: self.shadow_form.extra_actions.load_update_cases = [ LoadUpdateAction(case_tag="load_0", case_type="doctor", details_module=self.basic_module.unique_id) ] suite = self.factory.app.create_suite() finally: # reset the actions self.shadow_form.extra_actions.load_update_cases = original_actions # Confirm that the source session has not changed: expected_source_session = """ <partial> <session> <datum detail-confirm="m0_case_long" detail-select="m0_case_short" id="case_id_load_0" nodeset="instance('casedb')/casedb/case[@case_type='patient'][@status='open']" value="./@case_id" /> <datum function="uuid()" id="case_id_new_patient_0"/> </session> </partial> """ self.assertXmlPartialEqual(expected_source_session, suite, "./entry/command[@id='m0-f0']/../session") # Confirm that the shadow session HAS changed: expected_shadow_session = """ <partial> <session> <datum detail-confirm="m1_case_long" detail-select="m1_case_short" id="case_id_load_0" nodeset="instance('casedb')/casedb/case[@case_type='doctor'][@status='open']" value="./@case_id" /> <datum function="uuid()" id="case_id_new_patient_0"/> </session> </partial> """ self.assertXmlPartialEqual(expected_shadow_session, suite, "./entry/command[@id='m0-f1']/../session")
def test_relationship_added_to_form(self): self.form.actions.load_update_cases.append(LoadUpdateAction( case_type=self.parent_module.case_type, case_tag='load_1', )) self.form.actions.open_cases.append(AdvancedOpenCaseAction( case_type='child1', case_tag='open_1', name_path='/data/mother_name', case_indices=[CaseIndex(tag='load_1'), CaseIndex(tag='load_1', reference_id='host', relationship='extension')], repeat_context="/data/child", )) self.assertXmlEqual(self.get_xml('extension-case'), self.form.render_xform())
def test_registration_form_autoload(self): self.form.actions.load_update_cases = [ LoadUpdateAction(auto_select=AutoSelectCase( mode=AUTO_SELECT_USERCASE, value_key=""), ) ] self.form.actions.open_cases = [ AdvancedOpenCaseAction( case_tag="child", case_type="child", name_update=ConditionalCaseUpdate( question_path="/data/question1"), ) ] self.assertTrue(self.form.is_registration_form())
def test_advanced_suite_load_from_fixture(self): nodeset = "instance('item-list:table_tag')/calendar/year/month/day[@date > 735992 and @date < 736000]" app = Application.wrap(self.get_json('suite-advanced')) app.get_module(1).get_form(0).actions.load_update_cases.append(LoadUpdateAction( case_type="clinic", load_case_from_fixture=LoadCaseFromFixture( fixture_nodeset=nodeset, fixture_tag="selected_date", fixture_variable="date", case_property="adherence_event_date", auto_select=True, ) )) suite = app.create_suite() self.assertXmlPartialEqual(self.get_xml('load_from_fixture_session'), suite, './entry[2]/session') self.assertXmlPartialEqual(self.get_xml('load_from_fixture_instance'), suite, './entry[2]/instance')
def test_subcase_repeat_sharing(self): self.form.actions.load_update_cases.append( LoadUpdateAction( case_type=self.parent_module.case_type, case_tag='load_1', )) self.form.actions.open_cases.append( AdvancedOpenCaseAction(case_type=self.module.case_type, case_tag='open_1', name_update=ConditionalCaseUpdate( question_path='/data/mother_name'), case_indices=[CaseIndex(tag='load_1')], repeat_context="/data/child")) self.form.actions.open_cases[0].open_condition.type = 'always' self.app.case_sharing = True self.assertXmlEqual(self.get_xml('subcase-repeat-sharing'), self.form.render_xform())
def test_registration_form_subcase_multiple(self): self.form.actions.load_update_cases.append( LoadUpdateAction(case_type="parent", case_tag="parent")) self.form.actions.open_cases = [ AdvancedOpenCaseAction(case_tag="child", case_type="child", name_update=ConditionalCaseUpdate( question_path="/data/question1"), case_indices=[CaseIndex(tag="parent")]), AdvancedOpenCaseAction( case_tag="grandchild", case_type="grandchild", name_update=ConditionalCaseUpdate( question_path="/data/children/question1"), case_indices=[CaseIndex(tag="child")]) ] self.assertFalse(self.form.is_registration_form())
def test_case_properties_advanced(self): app = Application.new_app('domain', 'New App') app._id = uuid.uuid4().hex app.version = 1 m0 = app.add_module(AdvancedModule.new_module('Module0', lang='en')) m0.case_type = 'child' m0f0 = m0.new_form( 'other form', 'en', attachment=self.get_xml('standard_questions').decode('utf-8')) m0f0.actions.load_update_cases.append( LoadUpdateAction( case_type='parent', case_tag='parent', case_properties={ 'case_name': ConditionalCaseUpdate(question_path='/data/question1'), 'other': ConditionalCaseUpdate(question_path='/data/question2') })) m0f0.actions.open_cases.append( AdvancedOpenCaseAction( name_update=ConditionalCaseUpdate( question_path='/data/question1'), case_type='child', case_indices=[CaseIndex(tag='parent', reference_id='father')], case_properties={ 'child_other': ConditionalCaseUpdate(question_path='/data/question2') })) m0f0.actions.open_cases[0].open_condition.type = 'always' meta = app.get_case_metadata() self.assertEqual(2, len(meta.case_types)) child_type = meta.get_type('child') parent_type = meta.get_type('parent') self.assertFalse(child_type.has_errors) self.assertFalse(parent_type.has_errors) self.assertEqual({'name', 'child_other'}, {p.name for p in child_type.properties}) self.assertEqual({'case_name', 'other'}, {p.name for p in parent_type.properties})
def test_case_list_registration_form_advanced_autoload(self): factory = AppFactory(build_version='2.9.0') register_module, register_form = factory.new_advanced_module( 'register_dugong', 'dugong') factory.form_opens_case(register_form) register_form.actions.load_update_cases.append( LoadUpdateAction(case_tag='usercase', auto_select=AutoSelectCase( mode=AUTO_SELECT_USERCASE, ))) case_module, update_form = factory.new_advanced_module( 'update_dugong', 'dugong') factory.form_requires_case(update_form) case_module.case_list_form.form_id = register_form.get_unique_id() case_module.case_list_form.label = {'en': 'Register another Dugong'} self.assertXmlEqual(self.get_xml('case-list-form-advanced-autoload'), factory.app.create_suite())
def test_child_module_adjusted_datums_advanced_module(self): """ Testing that the session variable name for the case_id is correct since it will have been adjusted in the suite.xml to match the variable name in the root module. """ module = self.app.add_module( AdvancedModule.new_module('New Module', lang='en')) module.case_type = 'test_case_type' form = module.new_form( "Untitled Form", "en", self.get_xml('original_form', override_path=('data', )).decode('utf-8')) form.actions.load_update_cases.append( LoadUpdateAction( case_type=module.case_type, case_tag='load_1', case_properties={ 'question1': ConditionalCaseUpdate(question_path='/data/question1') })) root_module = self.app.add_module( Module.new_module('root module', None)) root_module.unique_id = 'm_root' root_module.case_type = module.case_type root_module_form = root_module.new_form('root module form', None) root_module_form.requires = 'case' root_module_form.actions.update_case = UpdateCaseAction( update={ 'question1': ConditionalCaseUpdate( question_path='/data/question1') }) root_module_form.actions.update_case.condition.type = 'always' # make module a child module of root_module module.root_module_id = root_module.unique_id self.assertXmlEqual( self.get_xml('child_module_adjusted_case_id_advanced'), form.render_xform())
def get_test_app(self): app = Application.new_app('domain', 'New App') app._id = uuid.uuid4().hex app.version = 1 m0 = self._make_module(app, 0, 'parent') m0.get_form(0).actions.subcases.append(OpenSubCaseAction( case_type='child', reference_id='parent' )) m1 = self._make_module(app, 1, 'child') m1.get_form(0).actions.subcases.append(OpenSubCaseAction( case_type='grand child', reference_id='parent' )) m2 = self._make_module(app, 2, 'grand child') m3 = app.add_module(AdvancedModule.new_module('Module3', lang='en')) m3.case_type = 'other grand child' m3f0 = m3.new_form('other form', 'en') m3f0.actions.load_update_cases.append(LoadUpdateAction( case_type='child', case_tag='child')) m3f0.actions.open_cases.append(AdvancedOpenCaseAction( name_path='/data/question1', case_type='other grand child', case_indices=[CaseIndex(tag='child')] )) m3f0.actions.open_cases[0].open_condition.type = 'always' m2.parent_select = ParentSelect(active=True, module_id=m1.unique_id) m1.parent_select = ParentSelect(active=True, module_id=m0.unique_id) expected_hierarchy = { 'parent': { 'child': { 'grand child': {}, 'other grand child': {} } } } return app, expected_hierarchy
def _load_case(self, child_module_form, case_type, parent_module=None): action = LoadUpdateAction(case_tag=case_type, case_type=case_type) if parent_module: action.parent_tag = parent_module.case_type child_module_form.actions.load_update_cases.append(action)