'Verify that updating preferences is transactional when a failure happens.'

@patch('openedx.core.djangoapps.user_api.models.UserPreference.delete')
def test_update_preferences_rollback(self, delete_user_preference):

    set_user_preference(self.user, 'a', '1')
    set_user_preference(self.user, 'b', '2')
    set_user_preference(self.user, 'c', '3')
    delete_user_preference.side_effect = [Exception, None]
    self.client.login(username=self.user.username, password=TEST_PASSWORD)
    json_data = {'a': '2', 'b': None, 'c': '1'}
    response = self.client.patch(self.url, data=json.dumps(json_data), content_type='application/merge-patch+json')
    self.assertEqual(400, response.status_code)
    response = self.client.get(self.url)
    expected_preferences = {'a': '1', 'b': '2', 'c': '3'}
    self.assertEqual(expected_preferences, response.data)
