<s> from Queue import Queue <EOL> import threading <EOL> from couchdbkit import Database <EOL> from django . test import TestCase <EOL> from casexml . apps . case . models import CommCareCase <EOL> from corehq . util . couch_helpers import OverrideDB , _override_db <EOL> from django . conf import settings <EOL> class OverrideDBTest ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . other_db_1 = Database ( settings . COUCH_DATABASE + '<STR_LIT>' , create = True ) <EOL> cls . other_db_2 = Database ( settings . COUCH_DATABASE + '<STR_LIT>' , create = True ) <EOL> cls . normal_db = CommCareCase . get_db ( ) <EOL> cls . normal_get_db = CommCareCase . get_db <EOL> def setUp ( self ) : <EOL> self . assertEqual ( self . normal_db . dbname , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . other_db_1 . server . delete_db ( cls . other_db_1 . dbname ) <EOL> cls . other_db_2 . server . delete_db ( cls . other_db_2 . dbname ) <EOL> def test_nested ( self ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertEqual ( CommCareCase . get_db , self . normal_get_db ) <EOL> with OverrideDB ( CommCareCase , self . other_db_1 ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . other_db_1 ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_get_db ) <EOL> with OverrideDB ( CommCareCase , self . other_db_2 ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . other_db_2 ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_get_db ) <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . other_db_1 ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_get_db ) <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertEqual ( CommCareCase . get_db , self . normal_get_db ) <EOL> def test_series ( self ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertEqual ( CommCareCase . get_db , self . normal_get_db ) <EOL> with OverrideDB ( CommCareCase , self . other_db_1 ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . other_db_1 ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_get_db ) <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertEqual ( CommCareCase . get_db , self . normal_get_db ) <EOL> with OverrideDB ( CommCareCase , self . other_db_2 ) : <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . other_db_2 ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertNotEqual ( CommCareCase . get_db ( ) , self . normal_get_db ) <EOL> self . assertEqual ( CommCareCase . get_db ( ) , self . normal_db ) <EOL> self . assertEqual ( CommCareCase . get_db , self . normal_get_db ) <EOL> def test_threading ( self ) : <EOL> result_queue = Queue ( ) <EOL> with OverrideDB ( CommCareCase , self . other_db_1 ) : <EOL> obj = _override_db . class_to_db <EOL> def run ( ) : <EOL> with OverrideDB ( CommCareCase , self . other_db_2 ) : <EOL> result_queue . put ( _override_db . class_to_db ) <EOL> t = threading . Thread ( target = run ) <EOL> t . start ( ) <EOL> t . join ( ) <EOL> result = result_queue . get_nowait ( ) <EOL> self . assertNotEqual ( id ( obj ) , id ( result ) ) </s>
