<commit_before>import uuid
from django.test import SimpleTestCase, TestCase
from casexml.apps.case.mock import CaseStructure, CaseRelationship, CaseFactory
from casexml.apps.case.models import CommCareCase


class CaseRelationshipTest(SimpleTestCase):

    def test_defaults(self):
        relationship = CaseRelationship(CaseStructure())
        self.assertEqual(relationship.relationship, relationship.DEFAULT_RELATIONSHIP)
        self.assertEqual(relationship.related_type, relationship.DEFAULT_RELATED_CASE_TYPE)


class CaseStructureTest(SimpleTestCase):

    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,
        )

    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
        )

    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())
        )


class CaseFactoryTest(TestCase):

    def test_simple_create(self):
        factory = CaseFactory()
        case = factory.create_case()
        self.assertTrue(CommCareCase.get_db().doc_exist(case._id))

    def test_create_overrides(self):
        factory = CaseFactory()
        case = factory.create_case(owner_id='somebody', update={'custom_prop': 'custom_value'})
        self.assertEqual('somebody', case.owner_id)
        self.assertEqual('custom_value', case.custom_prop)

    def test_domain(self):
        domain = uuid.uuid4().hex
        factory = CaseFactory(domain=domain)
        case = factory.create_case()
        self.assertEqual(domain, case.domain)

    def test_factory_defaults(self):
        owner_id = uuid.uuid4().hex
        factory = CaseFactory(case_defaults={'owner_id': owner_id})
        case = factory.create_case()
        self.assertEqual(owner_id, case.owner_id)

    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'}<commit_msg>test for overriding factory defaults<commit_after>import uuid
from django.test import SimpleTestCase, TestCase
from casexml.apps.case.mock import CaseStructure, CaseRelationship, CaseFactory
from casexml.apps.case.models import CommCareCase


class CaseRelationshipTest(SimpleTestCase):

    def test_defaults(self):
        relationship = CaseRelationship(CaseStructure())
        self.assertEqual(relationship.relationship, relationship.DEFAULT_RELATIONSHIP)
        self.assertEqual(relationship.related_type, relationship.DEFAULT_RELATED_CASE_TYPE)


class CaseStructureTest(SimpleTestCase):

    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,
        )

    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
        )

    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())
        )


class CaseFactoryTest(TestCase):

    def test_simple_create(self):
        factory = CaseFactory()
        case = factory.create_case()
        self.assertTrue(CommCareCase.get_db().doc_exist(case._id))

    def test_create_overrides(self):
        factory = CaseFactory()
        case = factory.create_case(owner_id='somebody', update={'custom_prop': 'custom_value'})
        self.assertEqual('somebody', case.owner_id)
        self.assertEqual('custom_value', case.custom_prop)

    def test_domain(self):
        domain = uuid.uuid4().hex
        factory = CaseFactory(domain=domain)
        case = factory.create_case()
        self.assertEqual(domain, case.domain)

    def test_factory_defaults(self):
        owner_id = uuid.uuid4().hex
        factory = CaseFactory(case_defaults={'owner_id': owner_id})
        case = factory.create_case()
        self.assertEqual(owner_id, case.owner_id)

    def test_override_defaults(self):
        owner_id = uuid.uuid4().hex
        factory = CaseFactory(case_defaults={'owner_id': owner_id})
        case = factory.create_case(owner_id='notthedefault')
        self.assertEqual('notthedefault', case.owner_id)

    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'}
{'fraction_non_alphanumeric': 0.10002920560747663, 'fraction_numerical': 0.003942757009345794, 'mean_word_length': 2.4997445068983137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6814367', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1876, 'n_words': 306}