<s> from django . test import TestCase <EOL> import os <EOL> from django . test . testcases import SimpleTestCase <EOL> from django . test . utils import override_settings <EOL> from casexml . apps . case . xml import V3 <EOL> from casexml . apps . phone . tests . utils import generate_restore_payload <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . receiverwrapper import submit_form_locally <EOL> from casexml . apps . case . tests . util import check_xml_line_by_line , delete_all_cases , delete_all_sync_logs <EOL> from casexml . apps . phone . models import SyncLog <EOL> from casexml . apps . phone . restore import FileRestoreResponse <EOL> from casexml . apps . phone . tests . dummy import dummy_restore_xml , dummy_user <EOL> @ override_settings ( CASEXML_FORCE_DOMAIN_CHECK = False ) <EOL> class OtaV3RestoreTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . domain = '<STR_LIT>' <EOL> delete_all_cases ( ) <EOL> delete_all_sync_logs ( ) <EOL> def testUserRestoreWithCase ( self ) : <EOL> file_path = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:data>" , "<STR_LIT>" ) <EOL> with open ( file_path , "<STR_LIT:rb>" ) as f : <EOL> xml_data = f . read ( ) <EOL> submit_form_locally ( xml_data , self . domain ) <EOL> expected_case_block = """<STR_LIT>""" <EOL> restore_payload = generate_restore_payload ( <EOL> project = Domain ( name = self . domain ) , <EOL> user = dummy_user ( ) , <EOL> items = True , <EOL> version = V3 <EOL> ) <EOL> sync_log_id = SyncLog . view ( <EOL> "<STR_LIT>" , <EOL> include_docs = True , <EOL> reduce = False , <EOL> ) . one ( ) . get_id <EOL> check_xml_line_by_line ( <EOL> self , <EOL> dummy_restore_xml ( sync_log_id , expected_case_block , items = <NUM_LIT:4> ) , <EOL> restore_payload <EOL> ) <EOL> class TestRestoreResponse ( SimpleTestCase ) : <EOL> def _expected ( self , username , body , items = None ) : <EOL> items_text = '<STR_LIT>' . format ( items ) if items is not None else '<STR_LIT>' <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) . format ( <EOL> username = username , <EOL> body = body , <EOL> items = items_text <EOL> ) <EOL> def test_no_items ( self ) : <EOL> user = '<STR_LIT>' <EOL> body = '<STR_LIT>' <EOL> expected = self . _expected ( user , body , items = None ) <EOL> with FileRestoreResponse ( user , False ) as response : <EOL> response . append ( body ) <EOL> response . finalize ( ) <EOL> self . assertEqual ( expected , str ( response ) ) <EOL> def test_items ( self ) : <EOL> user = '<STR_LIT>' <EOL> body = '<STR_LIT>' <EOL> expected = self . _expected ( user , body , items = <NUM_LIT:2> ) <EOL> response = FileRestoreResponse ( user , True ) <EOL> response . append ( body ) <EOL> response . finalize ( ) <EOL> self . assertEqual ( expected , str ( response ) ) <EOL> def test_add ( self ) : <EOL> user = '<STR_LIT>' <EOL> body1 = '<STR_LIT>' <EOL> body2 = '<STR_LIT>' <EOL> expected = self . _expected ( user , body1 + body2 , items = <NUM_LIT:3> ) <EOL> response1 = FileRestoreResponse ( user , True ) <EOL> response1 . append ( body1 ) <EOL> response2 = FileRestoreResponse ( user , True ) <EOL> response2 . append ( body2 ) <EOL> added = response1 + response2 <EOL> added . finalize ( ) <EOL> self . assertEqual ( expected , str ( added ) ) </s>
