Ejemplo n.º 1
0
 def test_get_save_refs_dont_mutate_app(self):
     case_refs = CaseReferences(
         load={}, save={'p1': CaseSaveReference(properties=['p1', 'p2'])})
     save_ref = next(case_refs.get_save_references())
     save_ref.properties.append('p3')
     self.assertEqual(['p1', 'p2', 'p3'], save_ref.properties)
     self.assertEqual(['p1', 'p2'], case_refs.save['p1'].properties)
Ejemplo n.º 2
0
 def test_get_save_refs_dont_mutate_app(self):
     case_refs = CaseReferences(load={}, save={
         'p1': CaseSaveReference(properties=['p1', 'p2'])
     })
     save_ref = next(case_refs.get_save_references())
     save_ref.properties.append('p3')
     self.assertEqual(['p1', 'p2', 'p3'], save_ref.properties)
     self.assertEqual(['p1', 'p2'], case_refs.save['p1'].properties)
Ejemplo n.º 3
0
 def test_get_load_refs(self):
     load = {
         'path1': ['p1', 'p2'],
         'path2': ['p3'],
     }
     case_refs = CaseReferences(load=load, )
     load_refs = list(case_refs.get_load_references())
     self.assertEqual(2, len(load_refs))
     for load_ref in load_refs:
         self.assertEqual(load[load_ref.path], load_ref.properties)
Ejemplo n.º 4
0
 def test_get_load_refs(self):
     load = {
         'path1': ['p1', 'p2'],
         'path2': ['p3'],
     }
     case_refs = CaseReferences(
         load=load,
     )
     load_refs = list(case_refs.get_load_references())
     self.assertEqual(2, len(load_refs))
     for load_ref in load_refs:
         self.assertEqual(load[load_ref.path], load_ref.properties)
Ejemplo n.º 5
0
 def test_get_save_refs(self):
     save = {
         'path1': CaseSaveReference(case_type='foo', properties=['p1', 'p2']),
         'path2': CaseSaveReference(properties=['p3'], create=True, close=True),
     }
     case_refs = CaseReferences(
         save=save,
     )
     save_refs = list(case_refs.get_save_references())
     self.assertEqual(2, len(save_refs))
     for save_ref in save_refs:
         orig_ref = save[save_ref.path]
         for attr in ('case_type', 'properties', 'create', 'close'):
             self.assertEqual(getattr(orig_ref, attr), getattr(save_ref, attr))
Ejemplo n.º 6
0
 def test_get_save_refs(self):
     save = {
         'path1': CaseSaveReference(case_type='foo', properties=['p1', 'p2']),
         'path2': CaseSaveReference(properties=['p3'], create=True, close=True),
     }
     case_refs = CaseReferences(
         save=save,
     )
     save_refs = list(case_refs.get_save_references())
     self.assertEqual(2, len(save_refs))
     for save_ref in save_refs:
         orig_ref = save[save_ref.path]
         for attr in ('case_type', 'properties', 'create', 'close'):
             self.assertEqual(getattr(orig_ref, attr), getattr(save_ref, attr))
Ejemplo n.º 7
0
def migrate_preloads(app, form, preload_items, form_ix, dry):
    xform = XForm(form.source)
    if form.case_references:
        load_refs = form.case_references.load
    else:
        load_refs = {}
        form.case_references = CaseReferences(load=load_refs)
    for hashtag, preloads in preload_items:
        if hashtag == "#case/":
            xform.add_case_preloads(preloads)
        elif hashtag == "#user/":
            xform.add_casedb()
            for nodeset, prop in preloads.items():
                assert '/' not in prop, (app.id, form.unique_id, prop)
                xform.add_setvalue(ref=nodeset, value=USERPROP_PREFIX + prop)
        else:
            raise ValueError("unknown hashtag: " + hashtag)
        for nodeset, prop in preloads.items():
            load_refs.setdefault(nodeset, []).append(hashtag + prop)
            logger.info("%s/%s %s setvalue %s = %s", app.domain, app._id,
                        form_ix, nodeset, hashtag + prop)
    if dry:
        logger.info(
            "setvalue XML: %s",
            " ".join(line.strip()
                     for line in ET.tostring(xform.xml).split("\n")
                     if "setvalue" in line))
    else:
        save_xform(app, form, ET.tostring(xform.xml))
Ejemplo n.º 8
0
    def test_case_references_open_close(self):
        app = Application.new_app('domain', 'New App')
        app._id = uuid.uuid4().hex
        app.version = 1
        m0 = self._make_module(app, 0, 'household')
        m0f1 = m0.new_form(
            'save to case',
            'en',
            attachment=self.get_xml('standard_questions').decode('utf-8'))
        m0f1.case_references = CaseReferences.wrap(
            {'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                }
            }})
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertEqual({}, meta_type.opened_by)
        self.assertEqual({}, meta_type.closed_by)

        m0f1.case_references = CaseReferences.wrap({
            'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                    "create": True
                }
            }
        })
        app.version = 2
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertTrue(m0f1.unique_id in meta_type.opened_by)
        self.assertEqual({}, meta_type.closed_by)

        m0f1.case_references = CaseReferences.wrap({
            'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                    "close": True
                }
            }
        })
        app.version = 3
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertEqual({}, meta_type.opened_by)
        self.assertTrue(m0f1.unique_id in meta_type.closed_by)
Ejemplo n.º 9
0
    def test_case_references_open_close(self):
        app = Application.new_app('domain', 'New App')
        app._id = uuid.uuid4().hex
        app.version = 1
        m0 = self._make_module(app, 0, 'household')
        m0f1 = m0.new_form('save to case', 'en', attachment=self.get_xml('standard_questions').decode('utf-8'))
        m0f1.case_references = CaseReferences.wrap({
            'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                }
            }
        })
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertEqual({}, meta_type.opened_by)
        self.assertEqual({}, meta_type.closed_by)

        m0f1.case_references = CaseReferences.wrap({
            'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                    "create": True
                }
            }
        })
        app.version = 2
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertTrue(m0f1.unique_id in meta_type.opened_by)
        self.assertEqual({}, meta_type.closed_by)

        m0f1.case_references = CaseReferences.wrap({
            'save': {
                "/data/question1": {
                    "case_type": "save_to_case",
                    "close": True
                }
            }
        })
        app.version = 3
        meta_type = app.get_case_metadata().get_type('save_to_case')
        self.assertEqual({}, meta_type.opened_by)
        self.assertTrue(m0f1.unique_id in meta_type.closed_by)
Ejemplo n.º 10
0
def migrate_preloads(app, form, preloads):
    xform = XForm(form.source)
    for kwargs in preloads:
        hashtag = kwargs.pop("hashtag")
        xform.add_case_preloads(**kwargs)
        refs = {path: [hashtag + case_property]
                for path, case_property in kwargs["preloads"].iteritems()}
        if form.case_references:
            form.case_references.load.update(refs)
        else:
            form.case_references = CaseReferences(load=refs)
    save_xform(app, form, ET.tostring(xform.xml))
Ejemplo n.º 11
0
def _get_case_references(data):
    if "references" in data:
        # old/deprecated format
        preload = json.loads(data['references'])["preload"]
        refs = {"load": {k: [v] for k, v in preload.items()}}
    else:
        refs = json.loads(data.get('case_references', '{}'))

    try:
        references = CaseReferences.wrap(refs)
        references.validate()
        return references
    except Exception:
        raise ValueError("bad case references data: {!r}".format(refs))
Ejemplo n.º 12
0
def _get_case_references(data):
    if "references" in data:
        # old/deprecated format
        preload = json.loads(data['references'])["preload"]
        refs = {
            "load": {k: [v] for k, v in preload.items()}
        }
    else:
        refs = json.loads(data.get('case_references', '{}'))

    try:
        references = CaseReferences.wrap(refs)
        references.validate()
        return references
    except Exception:
        raise ValueError("bad case references data: {!r}".format(refs))
Ejemplo n.º 13
0
 def test_case_references(self):
     app = Application.new_app('domain', 'New App')
     app._id = uuid.uuid4().hex
     app.version = 1
     m0 = self._make_module(app, 0, 'household')
     m0f1 = m0.new_form('save to case', 'en', attachment=self.get_xml('standard_questions').decode('utf-8'))
     m0f1.case_references = CaseReferences.wrap({
         'save': {
             "/data/question1": {
                 "case_type": "household",
                 "properties": [
                     "save_to_case_p1",
                     "save_to_case_p2"
                 ],
             }
         }
     })
     self._assert_properties(app.get_case_metadata(), {'name', 'save_to_case_p1', 'save_to_case_p2'})
Ejemplo n.º 14
0
 def setUpClass(cls):
     super(TestAppCasePropertyReferences, cls).setUpClass()
     factory = AppFactory(domain=cls.domain)
     m0 = factory.new_basic_module('save_to_case', cls.case_type, with_form=False)
     m0f1 = m0.new_form('save to case', 'en', attachment=cls.get_xml('basic_form').decode('utf-8'))
     m0f1.case_references = CaseReferences.wrap({
         'save': {
             "/data/question1": {
                 "case_type": cls.case_type,
                 "properties": [
                     "save_to_case_p1",
                     "save_to_case_p2"
                 ],
             }
         }
     })
     cls.current_app = factory.app
     cls.current_app.save()
Ejemplo n.º 15
0
 def test_case_references(self):
     app = Application.new_app('domain', 'New App')
     app._id = uuid.uuid4().hex
     app.version = 1
     m0 = self._make_module(app, 0, 'household')
     m0f1 = m0.new_form('save to case', 'en', attachment=self.get_xml('standard_questions'))
     m0f1.case_references = CaseReferences.wrap({
         'save': {
             "/data/question1": {
                 "case_type": "household",
                 "properties": [
                     "save_to_case_p1",
                     "save_to_case_p2"
                 ],
             }
         }
     })
     self._assert_properties(app.get_case_metadata(), {'name', 'save_to_case_p1', 'save_to_case_p2'})
Ejemplo n.º 16
0
 def setUpClass(cls):
     super(TestAppCasePropertyReferences, cls).setUpClass()
     factory = AppFactory(domain=cls.domain)
     m0 = factory.new_basic_module('save_to_case', cls.case_type, with_form=False)
     m0f1 = m0.new_form('save to case', 'en', attachment=cls.get_xml('basic_form'))
     m0f1.case_references = CaseReferences.wrap({
         'save': {
             "/data/question1": {
                 "case_type": cls.case_type,
                 "properties": [
                     "save_to_case_p1",
                     "save_to_case_p2"
                 ],
             }
         }
     })
     cls.current_app = factory.app
     cls.current_app.save()
Ejemplo n.º 17
0
 def test_case_references_advanced(self):
     app = Application.new_app('domain', 'New App')
     app._id = uuid.uuid4().hex
     app.version = 1
     m0 = app.add_module(AdvancedModule.new_module('Module3', lang='en'))
     m0.case_type = 'household_advanced'
     m0f1 = m0.new_form('save to case', 'en', attachment=self.get_xml('standard_questions').decode('utf-8'))
     m0f1.case_references = CaseReferences.wrap({
         'save': {
             "/data/question1": {
                 "case_type": "household_advanced",
                 "properties": [
                     "save_to_case_p1",
                     "save_to_case_p2"
                 ],
             }
         }
     })
     self._assert_properties(app.get_case_metadata(), {'name', 'save_to_case_p1', 'save_to_case_p2'})
Ejemplo n.º 18
0
def _assert_references_equal(testcase, wrapped_references,
                             unwrapped_references):
    testcase.assertEqual(
        wrapped_references.to_json(),
        CaseReferences.wrap(unwrapped_references).to_json(),
    )
Ejemplo n.º 19
0
def _assert_references_equal(testcase, wrapped_references, unwrapped_references):
    testcase.assertEqual(
        wrapped_references.to_json(),
        CaseReferences.wrap(unwrapped_references).to_json(),
    )