@run_with_all_backends
def test_case_phone_number_updates(self):
    extra_number = PhoneNumber.objects.create(domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True)
    self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1)
    with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case:
        self.assertIsNone(self.get_case_phone_number(case))
        case = self.set_case_property(case, 'contact_phone_number', '99987658765')
        self.assertPhoneNumberDetails(case, '99987658765', None, None, False, False, False)
        pk = self.get_case_phone_number(case).pk
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_phone_number_is_verified', '1')
        self.assertPhoneNumberDetails(case, '99987658765', None, None, True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_phone_number', '99987698769')
        self.assertPhoneNumberDetails(case, '99987698769', None, None, True, False, True)
        pk = self.get_case_phone_number(case).pk
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_backend_id', 'sms-backend')
        self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', None, True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_ivr_backend_id', 'ivr-backend')
        self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', 'ivr-backend', True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        v = self.get_case_phone_number(case)
        v.contact_last_modified += timedelta(days=1)
        v.save()
        with patch('corehq.apps.sms.models.PhoneNumber.save') as mock_save:
            case = self.set_case_property(case, 'contact_phone_number', '99912341234')
            self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
            mock_save.assert_not_called()
    self.assertEqual(PhoneNumber.get_two_way_number('999123'), extra_number)
@run_with_all_backends
def test_case_phone_number_updates(self):
    extra_number = PhoneNumber.objects.create(domain=self.domain, owner_doc_type='X', owner_id='X', phone_number='999123', verified=True, pending_verification=False, is_two_way=True)
    self.assertEqual(PhoneNumber.count_by_domain(self.domain), 1)
    with create_test_case(self.domain, 'participant', 'test1', drop_signals=False) as case:
        self.assertIsNone(self.get_case_phone_number(case))
        case = self.set_case_property(case, 'contact_phone_number', '99987658765')
        self.assertPhoneNumberDetails(case, '99987658765', None, None, False, False, False)
        pk = self.get_case_phone_number(case).pk
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_phone_number_is_verified', '1')
        self.assertPhoneNumberDetails(case, '99987658765', None, None, True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_phone_number', '99987698769')
        self.assertPhoneNumberDetails(case, '99987698769', None, None, True, False, True)
        pk = self.get_case_phone_number(case).pk
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_backend_id', 'sms-backend')
        self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', None, True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        case = self.set_case_property(case, 'contact_ivr_backend_id', 'ivr-backend')
        self.assertPhoneNumberDetails(case, '99987698769', 'sms-backend', 'ivr-backend', True, False, True, pk=pk)
        self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
        v = self.get_case_phone_number(case)
        v.contact_last_modified += timedelta(days=1)
        v.save()
        with patch('corehq.apps.sms.models.PhoneNumber.save') as mock_save:
            case = self.set_case_property(case, 'contact_phone_number', '99912341234')
            self.assertEqual(PhoneNumber.count_by_domain(self.domain), 2)
            mock_save.assert_not_called()
    self.assertEqual(PhoneNumber.get_two_way_number('999123'), extra_number)