<s> from mock import patch <EOL> from corehq . apps . locations . models import LOCATION_REPORTING_PREFIX <EOL> from corehq . apps . locations . fixtures import location_fixture_generator <EOL> from corehq . apps . locations . tests . util import make_loc <EOL> from corehq . apps . locations . tests . test_locations import LocationTestBase <EOL> from corehq import toggles <EOL> from corehq . apps . groups . exceptions import CantSaveException <EOL> from corehq . apps . users . models import CommCareUser <EOL> class LocationGroupTest ( LocationTestBase ) : <EOL> def setUp ( self ) : <EOL> super ( LocationGroupTest , self ) . setUp ( ) <EOL> self . test_state = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT:state>' , <EOL> domain = self . domain . name <EOL> ) <EOL> self . test_village = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> parent = self . test_state , <EOL> domain = self . domain . name <EOL> ) <EOL> self . test_outlet = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> parent = self . test_village , <EOL> domain = self . domain . name <EOL> ) <EOL> toggles . MULTIPLE_LOCATIONS_PER_USER . set ( "<STR_LIT>" . format ( self . domain . name ) , True ) <EOL> def test_group_name ( self ) : <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> self . test_state . sql_location . case_sharing_group_object ( ) . name <EOL> ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> self . test_outlet . sql_location . case_sharing_group_object ( ) . name <EOL> ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> self . test_outlet . sql_location . reporting_group_object ( ) . name <EOL> ) <EOL> def test_id_assignment ( self ) : <EOL> self . assertEqual ( <EOL> self . test_outlet . _id , <EOL> self . test_outlet . sql_location . case_sharing_group_object ( ) . _id <EOL> ) <EOL> self . assertEqual ( <EOL> LOCATION_REPORTING_PREFIX + self . test_outlet . _id , <EOL> self . test_outlet . sql_location . reporting_group_object ( ) . _id <EOL> ) <EOL> def test_group_properties ( self ) : <EOL> self . assertTrue ( <EOL> self . test_outlet . sql_location . case_sharing_group_object ( ) . case_sharing <EOL> ) <EOL> self . assertFalse ( <EOL> self . test_outlet . sql_location . case_sharing_group_object ( ) . reporting <EOL> ) <EOL> self . assertFalse ( <EOL> self . test_outlet . sql_location . reporting_group_object ( ) . case_sharing <EOL> ) <EOL> self . assertTrue ( <EOL> self . test_outlet . sql_location . reporting_group_object ( ) . reporting <EOL> ) <EOL> self . assertEqual ( <EOL> self . domain . name , <EOL> self . test_outlet . sql_location . reporting_group_object ( ) . domain <EOL> ) <EOL> self . assertEqual ( <EOL> self . domain . name , <EOL> self . test_outlet . sql_location . case_sharing_group_object ( ) . domain <EOL> ) <EOL> def test_accessory_methods ( self ) : <EOL> expected_id = self . loc . sql_location . case_sharing_group_object ( ) . _id <EOL> self . assertEqual ( <EOL> expected_id , <EOL> self . loc . group_id <EOL> ) <EOL> def test_not_real_groups ( self ) : <EOL> group_obj = self . test_outlet . sql_location . case_sharing_group_object ( ) <EOL> self . assertNotEqual ( group_obj . doc_type , '<STR_LIT>' ) <EOL> def test_cant_save_wont_save ( self ) : <EOL> group_obj = self . test_outlet . sql_location . case_sharing_group_object ( ) <EOL> with self . assertRaises ( CantSaveException ) : <EOL> group_obj . save ( ) <EOL> def test_get_owner_ids ( self ) : <EOL> loc_type = self . loc . location_type_object <EOL> self . assertFalse ( loc_type . shares_cases ) <EOL> owner_ids = self . user . get_owner_ids ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( owner_ids ) ) <EOL> self . assertEqual ( self . user . _id , owner_ids [ <NUM_LIT:0> ] ) <EOL> loc_type . shares_cases = True <EOL> loc_type . save ( ) <EOL> user = CommCareUser . wrap ( self . user . to_json ( ) ) <EOL> owner_ids = user . get_owner_ids ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( owner_ids ) ) <EOL> self . assertEqual ( self . loc . location_id , owner_ids [ <NUM_LIT:1> ] ) <EOL> loc_type . shares_cases = False <EOL> loc_type . save ( ) <EOL> def test_custom_data ( self ) : <EOL> self . loc . metadata = { <EOL> '<STR_LIT:foo>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . loc . save ( ) <EOL> self . assertDictEqual ( <EOL> { <EOL> '<STR_LIT>' : self . loc . location_type , <EOL> '<STR_LIT>' : self . loc . name , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> self . loc . sql_location . case_sharing_group_object ( ) . metadata <EOL> ) <EOL> self . assertDictEqual ( <EOL> { <EOL> '<STR_LIT>' : self . loc . location_type , <EOL> '<STR_LIT>' : self . loc . name , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> self . loc . sql_location . reporting_group_object ( ) . metadata <EOL> ) <EOL> @ patch ( '<STR_LIT>' , lambda : True ) <EOL> def test_location_fixture_generator ( self ) : <EOL> """<STR_LIT>""" <EOL> self . domain . commtrack_enabled = True <EOL> self . domain . save ( ) <EOL> self . loc . delete ( ) <EOL> state = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT:state>' , <EOL> domain = self . domain . name <EOL> ) <EOL> district = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = state <EOL> ) <EOL> block = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = district <EOL> ) <EOL> village = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = block <EOL> ) <EOL> outlet1 = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = village <EOL> ) <EOL> outlet2 = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = village <EOL> ) <EOL> outlet3 = make_loc ( <EOL> '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> domain = self . domain . name , <EOL> parent = village <EOL> ) <EOL> self . user . set_location ( outlet2 ) <EOL> self . user . add_location_delegate ( outlet1 ) <EOL> self . user . add_location_delegate ( outlet2 ) <EOL> self . user . add_location_delegate ( outlet3 ) <EOL> self . user . add_location_delegate ( state ) <EOL> self . user . save ( ) <EOL> fixture = location_fixture_generator ( self . user , '<STR_LIT>' ) <EOL> self . assertEquals ( len ( fixture [ <NUM_LIT:0> ] . findall ( '<STR_LIT>' ) ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( len ( fixture [ <NUM_LIT:0> ] . findall ( '<STR_LIT>' ) ) , <NUM_LIT:3> ) <EOL> @ patch ( '<STR_LIT>' , lambda : True ) <EOL> def test_location_fixture_generator_no_user_location ( self ) : <EOL> """<STR_LIT>""" <EOL> self . domain . commtrack_enabled = True <EOL> self . domain . save ( ) <EOL> self . loc . delete ( ) <EOL> fixture = location_fixture_generator ( self . user , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( fixture ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( len ( fixture [ <NUM_LIT:0> ] . findall ( '<STR_LIT>' ) ) , <NUM_LIT:0> ) <EOL> def test_location_fixture_generator_domain_no_locations ( self ) : <EOL> """<STR_LIT>""" <EOL> self . domain . save ( ) <EOL> self . loc . delete ( ) <EOL> fixture = location_fixture_generator ( self . user , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( fixture ) , <NUM_LIT:0> ) </s>
