Ejemplo n.º 1
0
    def test_create_from_structure(self):
        owner_id = uuid.uuid4().hex
        factory = CaseFactory(
            case_defaults={
                'owner_id': owner_id,
                'create': True,
                'update': {
                    'custom_prop': 'custom_value'
                }
            })
        case_id = uuid.uuid4().hex
        child_case_id = uuid.uuid4().hex
        parent_case_id = uuid.uuid4().hex
        structures = [
            CaseStructure(case_id=case_id),
            CaseStructure(case_id=child_case_id,
                          relationships=[
                              CaseRelationship(
                                  CaseStructure(case_id=parent_case_id))
                          ])
        ]
        cases = factory.create_or_update_cases(structures)
        for case in cases:
            self.assertEqual(owner_id, case.owner_id)
            self.assertEqual('custom_value', case.custom_prop)

        [regular, child, parent] = cases
        self.assertEqual(1, len(child.indices))
        self.assertEqual(parent_case_id, child.indices[0].referenced_id)
        self.assertEqual(2, len(regular.actions))  # create + update
        self.assertEqual(2, len(parent.actions))  # create + update
        self.assertEqual(3, len(child.actions))  # create + update + index
Ejemplo n.º 2
0
 def test_defaults(self):
     relationship = CaseRelationship()
     self.assertIsNotNone(relationship.related_structure.case_id)
     self.assertEqual(relationship.DEFAULT_RELATIONSHIP,
                      relationship.relationship)
     self.assertEqual(relationship.DEFAULT_RELATED_CASE_TYPE,
                      relationship.related_type)
Ejemplo n.º 3
0
    def test_deleted_indices_removed(self):
        factory = CaseFactory(
            self.domain,
            case_defaults={
                'user_id': self.commcare_user._id,
                'owner_id': self.commcare_user._id,
                'case_type': 'a-case',
                'create': True,
            },
        )
        # create a parent/child set of cases
        parent_id, child_id = [uuid.uuid4().hex for i in range(2)]
        child, parent = factory.create_or_update_case(
            CaseStructure(case_id=child_id,
                          relationships=[
                              CaseRelationship(
                                  CaseStructure(case_id=parent_id))
                          ]))
        # confirm the child has an index, and 1 form
        self.assertEqual(1, len(child.indices))
        self.assertEqual(parent_id, child.indices[0].referenced_id)
        self.assertEqual(1, len(child.xform_ids))

        # simulate parent deletion
        parent.doc_type = 'CommCareCase-Deleted'
        parent.save()

        # call the remove index task
        remove_indices_from_deleted_cases(self.domain, [parent_id])

        # check that the index is removed via a new form
        child = CommCareCase.get(child_id)
        self.assertEqual(0, len(child.indices))
        self.assertEqual(2, len(child.xform_ids))
Ejemplo n.º 4
0
 def test_parent_child(self):
     self.user.loadtest_factor = 3
     self.user.save()
     child, parent = self.factory.create_or_update_case(
         CaseStructure(attrs={'case_name': 'parent'},
                       relationships=[
                           CaseRelationship(
                               CaseStructure(attrs={'case_name': 'child'})),
                       ]))
     restore_config = RestoreConfig(project=self.domain,
                                    user=self.user,
                                    params=RestoreParams(version=V2))
     payload_string = restore_config.get_payload().as_string()
     caseblocks = extract_caseblocks_from_xml(payload_string)
     self.assertEqual(6, len(caseblocks))
     self.assertEqual(
         1, len(filter(lambda cb: cb.get_case_id() == child._id,
                       caseblocks)))
     self.assertEqual(
         1,
         len(filter(lambda cb: cb.get_case_id() == parent._id, caseblocks)))
     self.assertEqual(
         3,
         len(filter(lambda cb: child.name in cb.get_case_name(),
                    caseblocks)))
     self.assertEqual(
         3,
         len(
             filter(lambda cb: parent.name in cb.get_case_name(),
                    caseblocks)))
Ejemplo n.º 5
0
 def test_walk_ids(self):
     case_id = uuid.uuid4().hex
     parent_case_id = uuid.uuid4().hex
     grandparent_case_id = uuid.uuid4().hex
     structure = CaseStructure(
         case_id=case_id,
         relationships=[
             CaseRelationship(
                 CaseStructure(
                     case_id=parent_case_id,
                     relationships=[
                         CaseRelationship(
                             CaseStructure(case_id=grandparent_case_id))
                     ]))
         ])
     self.assertEqual([case_id, parent_case_id, grandparent_case_id],
                      list(structure.walk_ids()))
Ejemplo n.º 6
0
 def test_create_dirty_makes_dirty(self):
     # create a case and a parent case with a different owner at the same time
     # make sure the owner becomes dirty.
     new_owner = uuid.uuid4().hex
     [child, parent] = self.factory.create_or_update_case(
         CaseStructure(relationships=[
             CaseRelationship(CaseStructure(attrs={'owner_id': new_owner}))
         ]))
     self.assert_owner_dirty()
     self.assertEqual(child._id, self.owner_cleanliness.hint)
     self._verify_set_cleanliness_flags()
Ejemplo n.º 7
0
 def test_walk_ids_ignore_related(self):
     case_id = uuid.uuid4().hex
     parent_case_id = uuid.uuid4().hex
     grandparent_case_id = uuid.uuid4().hex
     structure = CaseStructure(
         case_id=case_id,
         relationships=[
             CaseRelationship(
                 CaseStructure(
                     case_id=parent_case_id,
                     relationships=[
                         CaseRelationship(
                             CaseStructure(case_id=grandparent_case_id))
                     ]))
         ])
     structure.walk_related = False
     self.assertEqual([case_id], list(structure.walk_ids()))
     structure.walk_related = True
     structure.relationships[0].related_structure.walk_related = False
     self.assertEqual([case_id, parent_case_id], list(structure.walk_ids()))
Ejemplo n.º 8
0
 def test_add_dirty_parent_makes_dirty(self):
     # add parent with a different owner and make sure the owner becomes dirty
     new_owner = uuid.uuid4().hex
     [child, parent] = self.factory.create_or_update_case(
         CaseStructure(case_id=self.sample_case._id,
                       relationships=[
                           CaseRelationship(
                               CaseStructure(attrs={'owner_id': new_owner}))
                       ]))
     self.assert_owner_dirty()
     self.assertEqual(child._id, self.owner_cleanliness.hint)
     self._verify_set_cleanliness_flags()
Ejemplo n.º 9
0
 def test_multiple_indices(self):
     indices = [
         ('mother_case_id', 'mother', 'mother_type'),
         ('father_case_id', 'father', 'father_type'),
     ]
     structure = CaseStructure(relationships=[
         CaseRelationship(CaseStructure(case_id=i[0]),
                          relationship=i[1],
                          related_type=i[2]) for i in indices
     ])
     self.assertEqual({i[1]: (i[2], i[0])
                       for i in indices}, structure.index)
Ejemplo n.º 10
0
 def test_index(self):
     parent_case_id = uuid.uuid4().hex
     structure = CaseStructure(relationships=[
         CaseRelationship(CaseStructure(case_id=parent_case_id))
     ])
     self.assertEqual(
         {
             CaseRelationship.DEFAULT_RELATIONSHIP:
             (CaseRelationship.DEFAULT_RELATED_CASE_TYPE, parent_case_id)
         },
         structure.index,
     )
Ejemplo n.º 11
0
 def test_no_walk_related(self):
     factory = CaseFactory()
     parent = factory.create_case()
     child_updates = factory.create_or_update_case(
         CaseStructure(attrs={'create': True},
                       walk_related=False,
                       relationships=[
                           CaseRelationship(
                               CaseStructure(case_id=parent._id))
                       ]), )
     self.assertEqual(1, len(child_updates))
     self.assertEqual(parent._id, child_updates[0].indices[0].referenced_id)
Ejemplo n.º 12
0
 def test_cross_domain_both_clean(self):
     new_domain = uuid.uuid4().hex
     self.factory.domain = new_domain
     self.factory.create_or_update_case(
         CaseStructure(relationships=[
             CaseRelationship(),
         ]))
     # two clean ownership models in different domains should report clean
     self.assertTrue(
         get_cleanliness_flag_from_scratch(self.domain,
                                           self.owner_id).is_clean)
     self.assertTrue(
         get_cleanliness_flag_from_scratch(new_domain,
                                           self.owner_id).is_clean)
Ejemplo n.º 13
0
 def test_cross_domain_dirty(self):
     new_domain = uuid.uuid4().hex
     new_owner = uuid.uuid4().hex
     self.factory.domain = new_domain
     self.factory.create_or_update_case(
         CaseStructure(relationships=[
             CaseRelationship(CaseStructure(attrs={'owner_id': new_owner})),
         ]))
     # original domain should stay clean but the new one should be dirty
     self.assertTrue(
         get_cleanliness_flag_from_scratch(self.domain,
                                           self.owner_id).is_clean)
     self.assertFalse(
         get_cleanliness_flag_from_scratch(new_domain,
                                           self.owner_id).is_clean)
Ejemplo n.º 14
0
    def test_delete_with_related(self):
        factory = CaseFactory()
        parent = factory.create_case()
        [child] = factory.create_or_update_case(
            CaseStructure(attrs={'create': True},
                          walk_related=False,
                          relationships=[
                              CaseRelationship(
                                  CaseStructure(case_id=parent._id))
                          ]), )
        # deleting the parent should not be allowed because the child still references it
        with self.assertRaises(CommCareCaseError):
            safe_hard_delete(parent)

        # deleting the child is ok
        safe_hard_delete(child)
        self.assertTrue(CommCareCase.get_db().doc_exist(parent._id))
        self.assertFalse(CommCareCase.get_db().doc_exist(child._id))
Ejemplo n.º 15
0
 def test_cross_domain_on_submission(self):
     # create a form that makes a dirty owner with the same ID but in a different domain
     # make sure the original owner stays clean
     new_domain = uuid.uuid4().hex
     # initialize the new cleanliness flag
     OwnershipCleanlinessFlag.objects.create(domain=new_domain,
                                             owner_id=self.owner_id,
                                             is_clean=True)
     self.factory.domain = new_domain
     self.factory.create_or_update_case(
         CaseStructure(relationships=[
             CaseRelationship(
                 CaseStructure(attrs={'owner_id': uuid.uuid4().hex}))
         ]))
     self.assert_owner_clean()
     self.assertEqual(
         False,
         OwnershipCleanlinessFlag.objects.get(owner_id=self.owner_id,
                                              domain=new_domain).is_clean,
     )
Ejemplo n.º 16
0
 def setUp(self):
     super(OwnerCleanlinessTest, self).setUp()
     # ensure that randomization is on
     OWNERSHIP_CLEANLINESS.randomness = 1
     self.owner_id = uuid.uuid4().hex
     self.synclog_id = uuid.uuid4().hex
     self.domain = uuid.uuid4().hex
     self.factory = CaseFactory(domain=self.domain,
                                case_defaults={
                                    'create': True,
                                    'owner_id': self.owner_id,
                                    'user_id': self.owner_id,
                                })
     self.assert_owner_clean(
     )  # this first call creates the OwnershipCleanliness doc
     self.sample_case = self.factory.create_case()
     self.child, self.parent = self.factory.create_or_update_case(
         CaseStructure(relationships=[
             CaseRelationship(),
         ]))
     self.assert_owner_clean()  # this is an actual assertion
Ejemplo n.º 17
0
 def test_add_clean_parent_stays_clean(self):
     # add a parent with the same owner, should remain clean
     self.factory.create_or_update_case(
         CaseStructure(relationships=[CaseRelationship()]))
     self.assert_owner_clean()
     self._verify_set_cleanliness_flags()
Ejemplo n.º 18
0
 def test_default_type_from_relationship(self):
     relationship = CaseRelationship(
         CaseStructure(attrs={'case_type': 'custom_type'}))
     self.assertEqual('custom_type', relationship.related_type)