def test_save_patch(self, base_mock): base_mock.configure_mock(id=1, slug='test', _original_values={'slug': 'test-old'}) base_mock.WRITABLE_FIELDS = ('slug', ) base_mock.api = MagicMock(**{'patch.return_value': {'id': 1, 'slug': 'test'}}) BaseResource.save(base_mock) self.assertEqual(base_mock._update_fields.call_count, 1)
def test_save_post_update_protected(self, base_mock): base_mock.configure_mock(id=None, slug='test', update_protected_dict=None, _original_values={}) base_mock.READ_ONLY_FIELDS = ('id', ) base_mock.UPDATE_PROTECTED_FIELDS = ('update_protected_dict', ) base_mock.WRITABLE_FIELDS = ('slug', ) base_mock.fields.return_value = ('id', 'slug', 'update_protected_dict') base_mock.update_protected_dict = {'create': 'create-allowed'} base_mock.api = MagicMock( **{ 'post.return_value': { 'id': 1, 'slug': 'test', 'update_protected_dict': { 'create': 'create-allowed' } } }) BaseResource.save(base_mock) self.assertEqual(base_mock.update_protected_dict, {'create': 'create-allowed'})
def test_save_post_client_error(self, base_mock): base_mock.configure_mock(id=None, slug='test') base_mock.api = MagicMock(**{'post.side_effect': slumber.exceptions.HttpClientError( message='', content='', response='')}) with self.assertRaises(slumber.exceptions.HttpClientError): BaseResource.save(base_mock) self.assertEqual(base_mock._update_fields.call_count, 0)
def test_save_post(self, base_mock): base_mock.configure_mock(id=None, slug='test') base_mock.WRITABLE_FIELDS = ('slug', ) base_mock.api = MagicMock( **{'post.return_value': { 'id': 1, 'slug': 'test' }}) BaseResource.save(base_mock) self.assertEqual(base_mock._update_fields.call_count, 1)
def test_save_post_read_only(self, base_mock): base_mock.configure_mock(id=None, slug='test', read_only_dict=None, _original_values={}) base_mock.READ_ONLY_FIELDS = ('id', 'read_only_dict') base_mock.UPDATE_PROTECTED_FIELDS = () base_mock.WRITABLE_FIELDS = ('slug', ) base_mock.fields.return_value = ('id', 'slug', 'read_only_dict') base_mock.read_only_dict = {'change': 'change-not-allowed'} message = 'Not allowed to change read only fields read_only_dict' with six.assertRaisesRegex(self, ValueError, message): BaseResource.save(base_mock)
def test_save_patch_read_only(self, base_mock): base_mock.READ_ONLY_FIELDS = ('id', 'read_only_dict') base_mock.UPDATE_PROTECTED_FIELDS = () base_mock.WRITABLE_FIELDS = ('slug', ) base_mock.fields.return_value = ('id', 'slug', 'read_only_dict') read_only_dict = {} BaseResource._update_fields(base_mock, {'id': 1, 'slug': 'test', 'read_only_dict': read_only_dict}) base_mock.read_only_dict['change'] = 'change-not-allowed' message = 'Not allowed to change read only fields read_only_dict' with six.assertRaisesRegex(self, ValueError, message): BaseResource.save(base_mock)