<s> from collections import namedtuple <EOL> import re <EOL> from xml . etree import ElementTree <EOL> import datetime <EOL> from django . test . utils import override_settings <EOL> from casexml . apps . case . mock import CaseBlock , CaseBlockError , IndexAttrs , ChildIndexAttrs <EOL> from casexml . apps . case . models import CommCareCase <EOL> from casexml . apps . case . sharedmodels import CommCareCaseIndex <EOL> from casexml . apps . case . tests . util import check_user_has_case <EOL> from casexml . apps . case . util import post_case_blocks <EOL> from casexml . apps . phone . models import User <EOL> from django . test import TestCase , SimpleTestCase <EOL> from corehq . form_processor . tests . utils import FormProcessorTestUtils <EOL> USER_ID = '<STR_LIT>' <EOL> class IndexSimpleTest ( SimpleTestCase ) : <EOL> def setUp ( self ) : <EOL> self . i1 = CommCareCaseIndex ( <EOL> identifier = '<STR_LIT>' , <EOL> referenced_type = '<STR_LIT>' , <EOL> referenced_id = '<STR_LIT>' <EOL> ) <EOL> self . i2 = CommCareCaseIndex ( <EOL> identifier = '<STR_LIT>' , <EOL> referenced_type = '<STR_LIT>' , <EOL> referenced_id = '<STR_LIT>' <EOL> ) <EOL> self . case = CommCareCase ( indices = [ self . i1 , self . i2 ] ) <EOL> def testHasIndex ( self ) : <EOL> self . assertEqual ( True , self . case . has_index ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( True , self . case . has_index ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( False , self . case . has_index ( '<STR_LIT>' ) ) <EOL> def testGetIndex ( self ) : <EOL> self . assertEqual ( self . i1 , self . case . get_index ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . i2 , self . case . get_index ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( None , self . case . get_index ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( None , self . case . get_index ( '<STR_LIT>' ) ) <EOL> def testGetIndexByRef ( self ) : <EOL> self . assertEqual ( self . i1 , self . case . get_index_by_ref_id ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( self . i2 , self . case . get_index_by_ref_id ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( None , self . case . get_index_by_ref_id ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( None , self . case . get_index_by_ref_id ( '<STR_LIT>' ) ) <EOL> def testRemoveIndexByRef ( self ) : <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . case . indices ) ) <EOL> self . case . remove_index_by_ref_id ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . case . indices ) ) <EOL> self . assertRaises ( ValueError , self . case . remove_index_by_ref_id , '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , self . case . remove_index_by_ref_id , '<STR_LIT>' ) <EOL> @ override_settings ( CASEXML_FORCE_DOMAIN_CHECK = False ) <EOL> class IndexTest ( TestCase ) : <EOL> CASE_ID = '<STR_LIT>' <EOL> MOTHER_CASE_ID = '<STR_LIT>' <EOL> FATHER_CASE_ID = '<STR_LIT>' <EOL> def tearDown ( self ) : <EOL> FormProcessorTestUtils . delete_all_cases ( ) <EOL> def testIndexes ( self ) : <EOL> user = User ( user_id = USER_ID , username = "<STR_LIT>" , password = "<STR_LIT>" , date_joined = "<STR_LIT>" ) <EOL> for prereq in [ self . MOTHER_CASE_ID , self . FATHER_CASE_ID ] : <EOL> post_case_blocks ( [ <EOL> CaseBlock ( create = True , case_id = prereq , user_id = USER_ID ) . as_xml ( ) <EOL> ] ) <EOL> create_index = CaseBlock ( <EOL> create = True , <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> owner_id = USER_ID , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , self . MOTHER_CASE_ID ) } , <EOL> ) . as_xml ( ) <EOL> post_case_blocks ( [ create_index ] ) <EOL> check_user_has_case ( self , user , create_index ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> update_index = CaseBlock ( <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' : ( '<STR_LIT>' , self . FATHER_CASE_ID ) } , <EOL> date_modified = now , <EOL> ) . as_xml ( ) <EOL> update_index_expected = CaseBlock ( <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> owner_id = USER_ID , <EOL> create = True , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , self . FATHER_CASE_ID ) } , <EOL> date_modified = now , <EOL> ) . as_xml ( ) <EOL> post_case_blocks ( [ update_index ] ) <EOL> check_user_has_case ( self , user , update_index_expected ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> update_index = CaseBlock ( <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , self . MOTHER_CASE_ID ) } , <EOL> date_modified = now , <EOL> ) . as_xml ( ) <EOL> update_index_expected = CaseBlock ( <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> owner_id = USER_ID , <EOL> create = True , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , self . MOTHER_CASE_ID ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , self . FATHER_CASE_ID ) } , <EOL> date_modified = now , <EOL> ) . as_xml ( ) <EOL> post_case_blocks ( [ update_index ] ) <EOL> check_user_has_case ( self , user , update_index_expected ) <EOL> def testBadIndexReferenceDomain ( self ) : <EOL> case_in_other_domain = self . MOTHER_CASE_ID <EOL> parent_domain = '<STR_LIT>' <EOL> child_domain = '<STR_LIT>' <EOL> post_case_blocks ( [ <EOL> CaseBlock ( create = True , case_id = case_in_other_domain , user_id = USER_ID ) . as_xml ( ) <EOL> ] , form_extras = { '<STR_LIT>' : parent_domain } ) <EOL> block = CaseBlock ( create = True , case_id = '<STR_LIT>' , user_id = USER_ID , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , case_in_other_domain ) } ) <EOL> xform , _ = post_case_blocks ( [ block . as_xml ( ) ] , <EOL> form_extras = { '<STR_LIT>' : child_domain } ) <EOL> self . assertTrue ( xform . is_error ) <EOL> self . assertEqual ( xform . doc_type , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , xform . problem ) <EOL> self . assertIn ( '<STR_LIT>' , xform . problem ) <EOL> def testRelationshipGetsSet ( self ) : <EOL> user = User ( user_id = USER_ID , username = "<STR_LIT>" , password = "<STR_LIT>" , date_joined = "<STR_LIT>" ) <EOL> create_index = CaseBlock ( <EOL> create = True , <EOL> case_id = self . CASE_ID , <EOL> user_id = USER_ID , <EOL> owner_id = USER_ID , <EOL> index = { '<STR_LIT>' : ( '<STR_LIT>' , self . MOTHER_CASE_ID , '<STR_LIT>' ) } , <EOL> ) . as_xml ( ) <EOL> post_case_blocks ( [ create_index ] ) <EOL> check_user_has_case ( self , user , create_index ) <EOL> class CaseBlockIndexRelationshipTests ( SimpleTestCase ) : <EOL> def test_case_block_index_supports_relationship ( self ) : <EOL> """<STR_LIT>""" <EOL> case_block = CaseBlock ( <EOL> case_id = '<STR_LIT>' , <EOL> case_type = '<STR_LIT>' , <EOL> date_modified = '<STR_LIT>' , <EOL> index = { <EOL> '<STR_LIT:host>' : IndexAttrs ( case_type = '<STR_LIT>' , case_id = '<STR_LIT>' , relationship = '<STR_LIT>' ) <EOL> } , <EOL> ) <EOL> self . assertEqual ( <EOL> ElementTree . tostring ( case_block . as_xml ( ) ) , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT>' , """<STR_LIT>""" ) <EOL> ) <EOL> def test_case_block_index_omit_child ( self ) : <EOL> """<STR_LIT>""" <EOL> case_block = CaseBlock ( <EOL> case_id = '<STR_LIT>' , <EOL> case_type = '<STR_LIT>' , <EOL> date_modified = '<STR_LIT>' , <EOL> index = { <EOL> '<STR_LIT>' : IndexAttrs ( case_type = '<STR_LIT>' , case_id = '<STR_LIT>' , relationship = '<STR_LIT>' ) <EOL> } , <EOL> ) <EOL> self . assertEqual ( <EOL> ElementTree . tostring ( case_block . as_xml ( ) ) , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT>' , """<STR_LIT>""" ) <EOL> ) <EOL> def test_case_block_index_default_relationship ( self ) : <EOL> """<STR_LIT>""" <EOL> case_block = CaseBlock ( <EOL> case_id = '<STR_LIT>' , <EOL> case_type = '<STR_LIT>' , <EOL> date_modified = '<STR_LIT>' , <EOL> index = { <EOL> '<STR_LIT>' : ChildIndexAttrs ( case_type = '<STR_LIT>' , case_id = '<STR_LIT>' ) <EOL> } , <EOL> ) <EOL> self . assertEqual ( <EOL> ElementTree . tostring ( case_block . as_xml ( ) ) , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT>' , """<STR_LIT>""" ) <EOL> ) <EOL> def test_case_block_index_valid_relationship ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaisesRegexp ( CaseBlockError , <EOL> '<STR_LIT>' ) : <EOL> CaseBlock ( <EOL> case_id = '<STR_LIT>' , <EOL> case_type = '<STR_LIT>' , <EOL> date_modified = '<STR_LIT>' , <EOL> index = { <EOL> '<STR_LIT:host>' : IndexAttrs ( case_type = '<STR_LIT>' , case_id = '<STR_LIT>' , relationship = '<STR_LIT>' ) <EOL> } , <EOL> ) </s>
