<s> from django . test import TestCase <EOL> from casexml . apps . case . dbaccessors import get_open_case_docs_in_domain , get_open_case_ids_in_domain <EOL> from casexml . apps . case . models import CommCareCase <EOL> from casexml . apps . case . util import create_real_cases_from_dummy_cases <EOL> from corehq . apps . hqcase . dbaccessors import get_number_of_cases_in_domain , get_case_ids_in_domain , get_case_types_for_domain , get_cases_in_domain , get_case_ids_in_domain_by_owner , get_all_case_owner_ids , get_case_properties <EOL> from couchforms . models import XFormInstance <EOL> class DBAccessorsTest ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . domain = '<STR_LIT>' <EOL> cases = [ <EOL> CommCareCase ( domain = cls . domain , type = '<STR_LIT>' , name = '<STR_LIT>' , user_id = '<STR_LIT>' , <EOL> prop_a = True , prop_b = True ) , <EOL> CommCareCase ( domain = cls . domain , type = '<STR_LIT>' , name = '<STR_LIT>' , user_id = '<STR_LIT>' , <EOL> prop_a = True , prop_c = True ) , <EOL> CommCareCase ( domain = cls . domain , type = '<STR_LIT>' , name = '<STR_LIT>' , user_id = '<STR_LIT>' ) , <EOL> CommCareCase ( domain = cls . domain , type = '<STR_LIT>' , name = '<STR_LIT>' , user_id = '<STR_LIT>' , closed = True ) , <EOL> CommCareCase ( domain = '<STR_LIT>' , type = '<STR_LIT>' , name = '<STR_LIT>' , user_id = '<STR_LIT>' , <EOL> prop_y = True ) <EOL> ] <EOL> cls . forms , cls . cases = create_real_cases_from_dummy_cases ( cases ) <EOL> assert len ( cls . cases ) == len ( cases ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> CommCareCase . get_db ( ) . bulk_delete ( cls . cases ) <EOL> XFormInstance . get_db ( ) . bulk_delete ( cls . forms ) <EOL> def test_get_number_of_cases_in_domain ( self ) : <EOL> self . assertEqual ( <EOL> get_number_of_cases_in_domain ( self . domain ) , <EOL> len ( [ case for case in self . cases if case . domain == self . domain ] ) <EOL> ) <EOL> def test_get_number_of_cases_in_domain__type ( self ) : <EOL> self . assertEqual ( <EOL> get_number_of_cases_in_domain ( self . domain , type = '<STR_LIT>' ) , <EOL> len ( [ case for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' ] ) <EOL> ) <EOL> def test_get_case_ids_in_domain ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain ( self . domain ) ) , <EOL> { case . get_id for case in self . cases if case . domain == self . domain } <EOL> ) <EOL> def test_get_case_ids_in_domain__type ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain ( self . domain , type = '<STR_LIT>' ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' } <EOL> ) <EOL> def assert_doc_list_equal ( self , doc_list_1 , doc_list_2 , raw_json = False ) : <EOL> if not raw_json : <EOL> doc_list_1 = [ doc . to_json ( ) for doc in doc_list_1 ] <EOL> doc_list_2 = [ doc . to_json ( ) for doc in doc_list_2 ] <EOL> doc_list_1 = sorted ( doc_list_1 , key = lambda doc : doc [ '<STR_LIT>' ] ) <EOL> doc_list_2 = sorted ( doc_list_2 , key = lambda doc : doc [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( doc_list_1 , doc_list_2 ) <EOL> def test_get_cases_in_domain ( self ) : <EOL> self . assert_doc_list_equal ( <EOL> get_cases_in_domain ( self . domain ) , <EOL> [ case for case in self . cases if case . domain == self . domain ] <EOL> ) <EOL> def test_get_cases_in_domain__type ( self ) : <EOL> self . assert_doc_list_equal ( <EOL> get_cases_in_domain ( self . domain , type = '<STR_LIT>' ) , <EOL> [ case for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' ] , <EOL> ) <EOL> def test_get_open_case_ids_in_domain ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_open_case_ids_in_domain ( self . domain , '<STR_LIT>' ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' <EOL> and not case . closed } , <EOL> ) <EOL> def test_get_open_case_ids_in_domain__owner_id ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_open_case_ids_in_domain ( self . domain , '<STR_LIT>' , owner_id = '<STR_LIT>' ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' <EOL> and not case . closed and case . user_id == '<STR_LIT>' } , <EOL> ) <EOL> self . assertEqual ( <EOL> set ( get_open_case_ids_in_domain ( self . domain , owner_id = '<STR_LIT>' ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain <EOL> and not case . closed and case . user_id == '<STR_LIT>' } , <EOL> ) <EOL> def test_get_open_case_docs_by_type ( self ) : <EOL> self . assert_doc_list_equal ( <EOL> get_open_case_docs_in_domain ( self . domain , '<STR_LIT>' ) , <EOL> [ case . to_json ( ) for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' <EOL> and not case . closed ] , <EOL> raw_json = True <EOL> ) <EOL> def test_get_open_case_docs_by_type__owner_id ( self ) : <EOL> self . assert_doc_list_equal ( <EOL> get_open_case_docs_in_domain ( self . domain , '<STR_LIT>' , owner_id = '<STR_LIT>' ) , <EOL> [ case . to_json ( ) for case in self . cases <EOL> if case . domain == self . domain and case . type == '<STR_LIT>' <EOL> and not case . closed and case . user_id == '<STR_LIT>' ] , <EOL> raw_json = True <EOL> ) <EOL> def test_get_case_types_for_domain ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_case_types_for_domain ( self . domain ) ) , <EOL> { case . type for case in self . cases if case . domain == self . domain } <EOL> ) <EOL> def test_get_case_ids_in_domain_by_owner ( self ) : <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain_by_owner ( self . domain , owner_id = '<STR_LIT>' ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . user_id == '<STR_LIT>' } <EOL> ) <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain_by_owner ( <EOL> self . domain , owner_id__in = [ '<STR_LIT>' ] ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . user_id == '<STR_LIT>' } <EOL> ) <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain_by_owner ( self . domain , owner_id = '<STR_LIT>' , <EOL> closed = False ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . user_id == '<STR_LIT>' <EOL> and case . closed is False } <EOL> ) <EOL> self . assertEqual ( <EOL> set ( get_case_ids_in_domain_by_owner ( self . domain , owner_id = '<STR_LIT>' , <EOL> closed = True ) ) , <EOL> { case . get_id for case in self . cases <EOL> if case . domain == self . domain and case . user_id == '<STR_LIT>' <EOL> and case . closed is True } <EOL> ) <EOL> def test_get_all_case_owner_ids ( self ) : <EOL> self . assertEqual ( <EOL> get_all_case_owner_ids ( self . domain ) , <EOL> set ( case . user_id for case in self . cases <EOL> if case . domain == self . domain ) <EOL> ) <EOL> self . assertEqual ( <EOL> get_all_case_owner_ids ( self . domain ) , <EOL> { '<STR_LIT>' , '<STR_LIT>' } , <EOL> ) <EOL> def test_get_case_properties ( self ) : <EOL> self . assertItemsEqual ( <EOL> get_case_properties ( self . domain ) , <EOL> { prop <EOL> for case in self . cases if case . domain == self . domain <EOL> for action in case . actions <EOL> for prop in ( action . updated_known_properties . keys ( ) + <EOL> action . updated_unknown_properties . keys ( ) ) } <EOL> ) </s>
