<s> import uuid <EOL> from django . test import TestCase , SimpleTestCase <EOL> from casexml . apps . case . exceptions import IllegalCaseId <EOL> from casexml . apps . case . mock import CaseBlock <EOL> from casexml . apps . case . models import CommCareCase <EOL> from casexml . apps . case . util import post_case_blocks <EOL> from corehq . form_processor . backends . couch . casedb import CaseDbCacheCouch <EOL> from corehq . form_processor . backends . sql . casedb import CaseDbCacheSQL <EOL> from corehq . form_processor . interfaces . dbaccessors import CaseAccessors <EOL> from corehq . form_processor . interfaces . processor import FormProcessorInterface <EOL> from corehq . form_processor . tests . utils import run_with_all_backends <EOL> class CaseDbCacheTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . interface = FormProcessorInterface ( ) <EOL> @ run_with_all_backends <EOL> def testDomainCheck ( self ) : <EOL> id = uuid . uuid4 ( ) . hex <EOL> post_case_blocks ( [ <EOL> CaseBlock ( <EOL> create = True , case_id = id , <EOL> user_id = '<STR_LIT>' <EOL> ) . as_xml ( ) <EOL> ] , { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> bad_cache = self . interface . casedb_cache ( domain = '<STR_LIT>' ) <EOL> try : <EOL> bad_cache . get ( id ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except IllegalCaseId : <EOL> pass <EOL> good_cache = self . interface . casedb_cache ( domain = '<STR_LIT>' ) <EOL> case = good_cache . get ( id ) <EOL> self . assertEqual ( '<STR_LIT>' , case . user_id ) <EOL> def testDocTypeCheck ( self ) : <EOL> id = uuid . uuid4 ( ) . hex <EOL> CommCareCase . get_db ( ) . save_doc ( { <EOL> "<STR_LIT>" : id , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } ) <EOL> doc_back = CommCareCase . get_db ( ) . get ( id ) <EOL> self . assertEqual ( "<STR_LIT>" , doc_back [ '<STR_LIT>' ] ) <EOL> cache = CaseDbCacheCouch ( ) <EOL> try : <EOL> cache . get ( id ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except IllegalCaseId : <EOL> pass <EOL> @ run_with_all_backends <EOL> def testGetPopulatesCache ( self ) : <EOL> case_ids = _make_some_cases ( <NUM_LIT:3> ) <EOL> cache = self . interface . casedb_cache ( ) <EOL> for id in case_ids : <EOL> self . assertFalse ( cache . in_cache ( id ) ) <EOL> for i , id in enumerate ( case_ids ) : <EOL> case = cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . dynamic_case_properties ( ) [ '<STR_LIT>' ] ) <EOL> for id in case_ids : <EOL> self . assertTrue ( cache . in_cache ( id ) ) <EOL> @ run_with_all_backends <EOL> def testSetPopulatesCache ( self ) : <EOL> case_ids = _make_some_cases ( <NUM_LIT:3> ) <EOL> cache = self . interface . casedb_cache ( ) <EOL> for id in case_ids : <EOL> self . assertFalse ( cache . in_cache ( id ) ) <EOL> for id in case_ids : <EOL> cache . set ( id , CaseAccessors ( ) . get_case ( id ) ) <EOL> for i , id in enumerate ( case_ids ) : <EOL> self . assertTrue ( cache . in_cache ( id ) ) <EOL> case = cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . dynamic_case_properties ( ) [ '<STR_LIT>' ] ) <EOL> @ run_with_all_backends <EOL> def testPopulate ( self ) : <EOL> case_ids = _make_some_cases ( <NUM_LIT:3> ) <EOL> cache = self . interface . casedb_cache ( ) <EOL> for id in case_ids : <EOL> self . assertFalse ( cache . in_cache ( id ) ) <EOL> cache . populate ( case_ids ) <EOL> for id in case_ids : <EOL> self . assertTrue ( cache . in_cache ( id ) ) <EOL> for i , id in enumerate ( case_ids ) : <EOL> case = cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . dynamic_case_properties ( ) [ '<STR_LIT>' ] ) <EOL> def testStripHistory ( self ) : <EOL> case_ids = _make_some_cases ( <NUM_LIT:3> ) <EOL> history_cache = self . interface . casedb_cache ( ) <EOL> for i , id in enumerate ( case_ids ) : <EOL> self . assertFalse ( history_cache . in_cache ( id ) ) <EOL> case = history_cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . my_index ) <EOL> self . assertTrue ( len ( case . actions ) > <NUM_LIT:0> ) <EOL> nohistory_cache = self . interface . casedb_cache ( strip_history = True ) <EOL> for i , id in enumerate ( case_ids ) : <EOL> self . assertFalse ( nohistory_cache . in_cache ( id ) ) <EOL> case = nohistory_cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . my_index ) <EOL> self . assertTrue ( len ( case . actions ) == <NUM_LIT:0> ) <EOL> more_case_ids = _make_some_cases ( <NUM_LIT:3> ) <EOL> history_cache . populate ( more_case_ids ) <EOL> nohistory_cache . populate ( more_case_ids ) <EOL> for i , id in enumerate ( more_case_ids ) : <EOL> self . assertTrue ( history_cache . in_cache ( id ) ) <EOL> case = history_cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . my_index ) <EOL> self . assertTrue ( len ( case . actions ) > <NUM_LIT:0> ) <EOL> for i , id in enumerate ( more_case_ids ) : <EOL> self . assertTrue ( nohistory_cache . in_cache ( id ) ) <EOL> case = nohistory_cache . get ( id ) <EOL> self . assertEqual ( str ( i ) , case . my_index ) <EOL> self . assertTrue ( len ( case . actions ) == <NUM_LIT:0> ) <EOL> def test_nowrap ( self ) : <EOL> case_ids = _make_some_cases ( <NUM_LIT:1> ) <EOL> cache = self . interface . casedb_cache ( wrap = False ) <EOL> case = cache . get ( case_ids [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( isinstance ( case , dict ) ) <EOL> self . assertFalse ( isinstance ( case , CommCareCase ) ) <EOL> class CaseDbCacheNoDbTest ( SimpleTestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . interface = FormProcessorInterface ( ) <EOL> def test_couch_wrap_lock_dependency_couch ( self ) : <EOL> CaseDbCacheCouch ( domain = '<STR_LIT>' , lock = False , wrap = True ) <EOL> CaseDbCacheCouch ( domain = '<STR_LIT>' , lock = False , wrap = False ) <EOL> CaseDbCacheCouch ( domain = '<STR_LIT>' , lock = True , wrap = True ) <EOL> with self . assertRaises ( ValueError ) : <EOL> CaseDbCacheCouch ( domain = '<STR_LIT>' , lock = True , wrap = False ) <EOL> def test_sql_wrap_support ( self ) : <EOL> CaseDbCacheSQL ( domain = '<STR_LIT>' , wrap = True ) <EOL> with self . assertRaises ( ValueError ) : <EOL> CaseDbCacheSQL ( domain = '<STR_LIT>' , wrap = False ) <EOL> def _make_some_cases ( howmany , domain = '<STR_LIT>' ) : <EOL> ids = [ uuid . uuid4 ( ) . hex for i in range ( howmany ) ] <EOL> post_case_blocks ( [ <EOL> CaseBlock ( <EOL> create = True , <EOL> case_id = ids [ i ] , <EOL> user_id = '<STR_LIT>' , <EOL> update = { <EOL> '<STR_LIT>' : i , <EOL> } <EOL> ) . as_xml ( ) for i in range ( howmany ) <EOL> ] , { '<STR_LIT>' : domain } ) <EOL> return ids </s>
