<s> from datetime import datetime , timedelta <EOL> import os <EOL> import uuid <EOL> from django . conf import settings <EOL> from django . test import TestCase <EOL> from mock import patch <EOL> from couchdbkit import RequestFailed <EOL> from casexml . apps . case . mock import CaseBlock <EOL> from corehq . apps . hqcase . utils import submit_case_blocks <EOL> from corehq . apps . receiverwrapper import submit_form_locally <EOL> from corehq . form_processor . interfaces . dbaccessors import CaseAccessors , FormAccessors <EOL> from couchforms . models import ( <EOL> UnfinishedSubmissionStub , <EOL> ) <EOL> from corehq . form_processor . interfaces . processor import FormProcessorInterface <EOL> from corehq . form_processor . tests . utils import FormProcessorTestUtils , run_with_all_backends , post_xform <EOL> from corehq . util . test_utils import TestFileMixin <EOL> class EditFormTest ( TestCase , TestFileMixin ) : <EOL> ID = '<STR_LIT>' <EOL> domain = '<STR_LIT>' <EOL> file_path = ( '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> root = os . path . dirname ( __file__ ) <EOL> def setUp ( self ) : <EOL> self . interface = FormProcessorInterface ( self . domain ) <EOL> self . casedb = CaseAccessors ( self . domain ) <EOL> self . formdb = FormAccessors ( self . domain ) <EOL> def tearDown ( self ) : <EOL> FormProcessorTestUtils . delete_all_xforms ( self . domain ) <EOL> FormProcessorTestUtils . delete_all_cases ( self . domain ) <EOL> UnfinishedSubmissionStub . objects . all ( ) . delete ( ) <EOL> @ run_with_all_backends <EOL> def test_basic_edit ( self ) : <EOL> original_xml = self . get_xml ( '<STR_LIT>' ) <EOL> edit_xml = self . get_xml ( '<STR_LIT>' ) <EOL> xform = post_xform ( original_xml , domain = self . domain ) <EOL> self . assertEqual ( self . ID , xform . form_id ) <EOL> self . assertTrue ( xform . is_normal ) <EOL> self . assertEqual ( "<STR_LIT>" , xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( "<STR_LIT>" , xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> xform = post_xform ( edit_xml , domain = self . domain ) <EOL> self . assertEqual ( self . ID , xform . form_id ) <EOL> self . assertTrue ( xform . is_normal ) <EOL> self . assertEqual ( "<STR_LIT:100>" , xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( "<STR_LIT>" , xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> deprecated_xform = self . formdb . get_form ( xform . deprecated_form_id ) <EOL> self . assertEqual ( self . ID , deprecated_xform . orig_id ) <EOL> self . assertNotEqual ( self . ID , deprecated_xform . form_id ) <EOL> self . assertTrue ( deprecated_xform . is_deprecated ) <EOL> self . assertEqual ( "<STR_LIT>" , deprecated_xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( "<STR_LIT>" , deprecated_xform . form_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( xform . received_on , deprecated_xform . received_on ) <EOL> self . assertEqual ( xform . deprecated_form_id , deprecated_xform . form_id ) <EOL> self . assertTrue ( xform . edited_on > deprecated_xform . received_on ) <EOL> self . assertEqual ( <EOL> deprecated_xform . get_xml ( ) , <EOL> original_xml <EOL> ) <EOL> self . assertEqual ( xform . get_xml ( ) , edit_xml ) <EOL> @ run_with_all_backends <EOL> def test_broken_save ( self ) : <EOL> """<STR_LIT>""" <EOL> original_xml = self . get_xml ( '<STR_LIT>' ) <EOL> edit_xml = self . get_xml ( '<STR_LIT>' ) <EOL> _ , xform , _ = submit_form_locally ( original_xml , self . domain ) <EOL> self . assertEqual ( self . ID , xform . form_id ) <EOL> self . assertTrue ( xform . is_normal ) <EOL> self . assertEqual ( self . domain , xform . domain ) <EOL> self . assertEqual ( <EOL> UnfinishedSubmissionStub . objects . filter ( xform_id = self . ID ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> with patch . object ( self . interface . processor , '<STR_LIT>' , side_effect = RequestFailed ) : <EOL> with self . assertRaises ( RequestFailed ) : <EOL> submit_form_locally ( edit_xml , self . domain ) <EOL> self . assertIsNone ( getattr ( xform , '<STR_LIT>' , None ) ) <EOL> xform = self . formdb . get_form ( self . ID ) <EOL> self . assertIsNotNone ( xform ) <EOL> self . assertEqual ( <EOL> UnfinishedSubmissionStub . objects . filter ( xform_id = self . ID , <EOL> saved = False ) . count ( ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> self . assertEqual ( <EOL> UnfinishedSubmissionStub . objects . filter ( xform_id = self . ID ) . count ( ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> @ run_with_all_backends <EOL> def test_case_management ( self ) : <EOL> form_id = uuid . uuid4 ( ) . hex <EOL> case_id = uuid . uuid4 ( ) . hex <EOL> owner_id = uuid . uuid4 ( ) . hex <EOL> case_block = CaseBlock ( <EOL> create = True , <EOL> case_id = case_id , <EOL> case_type = '<STR_LIT>' , <EOL> owner_id = owner_id , <EOL> update = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) . as_string ( ) <EOL> submit_case_blocks ( case_block , domain = self . domain , form_id = form_id ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( case . type , '<STR_LIT>' ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( [ form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( <NUM_LIT:2> , len ( case . actions ) ) <EOL> for a in case . actions : <EOL> self . assertEqual ( form_id , a . xform_id ) <EOL> case_block = CaseBlock ( <EOL> create = True , <EOL> case_id = case_id , <EOL> case_type = '<STR_LIT>' , <EOL> owner_id = owner_id , <EOL> update = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) . as_string ( ) <EOL> submit_case_blocks ( case_block , domain = self . domain , form_id = form_id ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( case . type , '<STR_LIT>' ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( [ form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( <NUM_LIT:2> , len ( case . actions ) ) <EOL> for a in case . actions : <EOL> self . assertEqual ( form_id , a . xform_id ) <EOL> @ run_with_all_backends <EOL> def test_second_edit_fails ( self ) : <EOL> form_id = uuid . uuid4 ( ) . hex <EOL> case_id = uuid . uuid4 ( ) . hex <EOL> case_block = CaseBlock ( <EOL> create = True , <EOL> case_id = case_id , <EOL> case_type = '<STR_LIT>' , <EOL> ) . as_string ( ) <EOL> submit_case_blocks ( case_block , domain = self . domain , form_id = form_id ) <EOL> case_block = CaseBlock ( <EOL> create = True , <EOL> case_id = '<STR_LIT>' , <EOL> case_type = '<STR_LIT>' , <EOL> ) . as_string ( ) <EOL> submit_case_blocks ( case_block , domain = self . domain , form_id = form_id ) <EOL> xform = self . formdb . get_form ( form_id ) <EOL> self . assertTrue ( xform . is_error ) <EOL> deprecated_xform = self . formdb . get_form ( xform . deprecated_form_id ) <EOL> self . assertTrue ( deprecated_xform . is_deprecated ) <EOL> @ run_with_all_backends <EOL> def test_case_management_ordering ( self ) : <EOL> case_id = uuid . uuid4 ( ) . hex <EOL> owner_id = uuid . uuid4 ( ) . hex <EOL> case_block = CaseBlock ( <EOL> create = True , <EOL> case_id = case_id , <EOL> case_type = '<STR_LIT>' , <EOL> owner_id = owner_id , <EOL> ) . as_string ( ) <EOL> create_form_id = submit_case_blocks ( case_block , domain = self . domain ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( [ create_form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( [ create_form_id ] , [ a . xform_id for a in case . actions ] ) <EOL> for a in case . actions : <EOL> self . assertEqual ( create_form_id , a . xform_id ) <EOL> edit_date = datetime . utcnow ( ) <EOL> case_block = CaseBlock ( <EOL> create = False , <EOL> case_id = case_id , <EOL> date_modified = edit_date , <EOL> update = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) . as_string ( ) <EOL> edit_form_id = submit_case_blocks ( case_block , domain = self . domain ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( [ create_form_id , edit_form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( [ create_form_id , edit_form_id ] , [ a . xform_id for a in case . actions ] ) <EOL> case_block = CaseBlock ( <EOL> create = False , <EOL> case_id = case_id , <EOL> update = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) . as_string ( ) <EOL> second_edit_form_id = submit_case_blocks ( case_block , domain = self . domain ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( [ create_form_id , edit_form_id , second_edit_form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( <EOL> [ create_form_id , edit_form_id , second_edit_form_id ] , <EOL> [ a . xform_id for a in case . actions ] <EOL> ) <EOL> case_block = CaseBlock ( <EOL> create = False , <EOL> case_id = case_id , <EOL> date_modified = edit_date , <EOL> update = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) . as_string ( ) <EOL> submit_case_blocks ( case_block , domain = self . domain , form_id = edit_form_id ) <EOL> case = self . casedb . get_case ( case_id ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( case . dynamic_case_properties ( ) [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( [ create_form_id , edit_form_id , second_edit_form_id ] , case . xform_ids ) <EOL> if not settings . TESTS_SHOULD_USE_SQL_BACKEND : <EOL> self . assertEqual ( <EOL> [ create_form_id , edit_form_id , second_edit_form_id ] , <EOL> [ a . xform_id for a in case . actions ] <EOL> ) </s>
