Exemplo n.º 1
0
    def test_save(self):
        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.save(target_object)

        self.assertTrue(target_object.save.called)
Exemplo n.º 2
0
    def test_prepare(self):
        field = AttributeField(attribute='foo.bar.baz', type=int)

        related = Related()
        field.prepare(mock_context(), related)

        self.assertEqual(related._select, {'foo__bar'})
Exemplo n.º 3
0
    def test_save(self):
        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.save(target_object)

        self.assertTrue(target_object.save.called)
Exemplo n.º 4
0
    def test_add_field(self):
        field = AttributeField(attribute='foo', type=int)
        source_dict = {'foo': 3}
        filter_args = {}

        field.filter_by_item(mock_context(), filter_args, source_dict)

        self.assertEqual({'foo': 3}, filter_args)
Exemplo n.º 5
0
    def test_simple_missing_incoming(self):
        source_dict = {}

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        with self.assertRaises(ValidationError):
            field.handle_incoming(mock_context(), source_dict, target_object)
Exemplo n.º 6
0
    def test_add_field(self):
        field = AttributeField(attribute='foo', type=int)
        source_dict = {'foo': 3}
        filter_args = {}

        field.filter_by_item(mock_context(), filter_args, source_dict)

        self.assertEqual({'foo': 3}, filter_args)
Exemplo n.º 7
0
    def test_simple_missing_incoming(self):
        source_dict = {}

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        with self.assertRaises(ValidationError):
            field.handle_incoming(mock_context(), source_dict, target_object)
Exemplo n.º 8
0
    def test_prepare_with_use_prefetch(self):
        field = AttributeField(attribute='foo.bar.baz',
                               type=int,
                               use_prefetch=True)

        related = Related()
        field.prepare(mock_context(), related)

        self.assertEqual(related._prefetch, {'foo__bar'})
Exemplo n.º 9
0
    def test_incoming_read_only(self):
        source_dict = {'foo': 20}

        target_object = Mock(name='target', spec=[])

        field = AttributeField(attribute='foo', type=int, read_only=True)
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertFalse(hasattr(target_object, 'foo'))
Exemplo n.º 10
0
    def test_simple_incoming(self):
        source_dict = {'foo': 20}

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo, 20)
Exemplo n.º 11
0
    def test_prepare(self):
        field = AttributeField(attribute='foo.bar.baz', type=int)

        related = Related()
        field.prepare(mock_context(), related)

        self.assertEqual(related._select, {
            'foo__bar'
        })
Exemplo n.º 12
0
    def test_prepare_with_use_prefetch(self):
        field = AttributeField(attribute='foo.bar.baz', type=int, use_prefetch=True)

        related = Related()
        field.prepare(mock_context(), related)

        self.assertEqual(related._prefetch, {
            'foo__bar'
        })
Exemplo n.º 13
0
    def test_simple_none_outgoing(self):
        source_object = Mock()
        source_object.foo = None

        target_dict = dict()

        field = AttributeField(attribute='foo', type=int)
        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['foo'], None)
Exemplo n.º 14
0
    def test_simple_none_outgoing(self):
        source_object = Mock()
        source_object.foo = None

        target_dict = dict()

        field = AttributeField(attribute='foo', type=int)
        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['foo'], None)
Exemplo n.º 15
0
    def test_multilevel_incoming(self):
        source_dict = {'bar': 20}

        field = AttributeField(attribute='foo.bar', type=int)

        target_object = Mock(name='target')

        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo.bar, 20)
Exemplo n.º 16
0
    def test_simple_incoming(self):
        source_dict = {
            'foo': 20
        }

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo, 20)
Exemplo n.º 17
0
    def test_multilevel_outgoing(self):
        source_object = Mock()
        source_object.foo.bar = 20

        field = AttributeField(attribute='foo.bar', type=int)

        target_dict = dict()

        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['bar'], 20)
Exemplo n.º 18
0
    def test_published_property_camel_case(self):
        source_object = Mock()
        source_object.foo.bar = 20

        field = AttributeField(attribute='foo.bar', type=int, published_property='foo_bar')

        target_dict = dict()

        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['fooBar'], 20)
Exemplo n.º 19
0
    def test_incoming_read_only(self):
        source_dict = {
            'foo': 20
        }

        target_object = Mock(name='target', spec=[])

        field = AttributeField(attribute='foo', type=int, read_only=True)
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertFalse(hasattr(target_object, 'foo'))
Exemplo n.º 20
0
    def test_multilevel_outgoing(self):
        source_object = Mock()
        source_object.foo.bar = 20

        field = AttributeField(attribute='foo.bar', type=int)

        target_dict = dict()

        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['bar'], 20)
Exemplo n.º 21
0
    def test_alternate_name_incoming(self):
        source_dict = {
            'foo': 20
        }

        field = AttributeField(attribute='foo.bar', type=int, published_property='foo')

        target_object = Mock(name='target')

        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo.bar, 20)
Exemplo n.º 22
0
    def test_alternate_name_incoming(self):
        source_dict = {'foo': 20}

        field = AttributeField(attribute='foo.bar',
                               type=int,
                               published_property='foo')

        target_object = Mock(name='target')

        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo.bar, 20)
Exemplo n.º 23
0
    def test_multilevel_incoming(self):
        source_dict = {
            'bar': 20
        }

        field = AttributeField(attribute='foo.bar', type=int)

        target_object = Mock(name='target')

        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(target_object.foo.bar, 20)
Exemplo n.º 24
0
    def test_published_property_camel_case(self):
        source_object = Mock()
        source_object.foo.bar = 20

        field = AttributeField(attribute='foo.bar',
                               type=int,
                               published_property='foo_bar')

        target_dict = dict()

        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(target_dict['fooBar'], 20)
Exemplo n.º 25
0
    def test_incoming_pushes_and_pops(self):
        source_dict = {'foo': 20}
        ctx = mock_context()

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.handle_incoming(ctx, source_dict, target_object)

        ctx.assert_has_calls([
            mock.call.push(target_object),
            mock.call.pop(),
        ])
Exemplo n.º 26
0
    def test_incoming_pushes_and_pops(self):
        source_dict = {
            'foo': 20
        }
        ctx = mock_context()

        target_object = Mock(name='target')

        field = AttributeField(attribute='foo', type=int)
        field.handle_incoming(ctx, source_dict, target_object)

        ctx.assert_has_calls([
            mock.call.push(target_object),
            mock.call.pop(),
        ])
Exemplo n.º 27
0
 class Resource(ModelResource):
     model_class = Mock()
     fields = [
         AttributeField(attribute='bar', type=int),
         RelatedManagerField(attribute='baz',
                             resource_class=SubResource),
     ]
Exemplo n.º 28
0
        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

            @classmethod
            def get_by_source_dict(cls, ctx, sub_source_dict):
                return cls(mock_model)
Exemplo n.º 29
0
 class MockResource(ModelResource):
     model_class = mock_orm.Model
     fields = [AttributeField('bar.baz', type=int)]
Exemplo n.º 30
0
        class MockResource(ModelResource):
            model_class = User

            owner_field = fields.SubModelResourceField(
                attribute='owner', resource_class=UserOwnerResource)
            fields = [AttributeField(attribute='name', type=str), owner_field]
Exemplo n.º 31
0
 class Resource(ModelResource):
     model_class = Mock()
     fields = [
         AttributeField(attribute='bar', type=int),
     ]
Exemplo n.º 32
0
 class MockResource(ModelResource):
     model_class = mock_orm.Model
     fields = [
         AttributeField(attribute='bar', type=int),
     ]
Exemplo n.º 33
0
 class BarResource(ModelResource):
     model_class = mock_orm.Model
     fields = [
         AttributeField(attribute='some_attribute', type=int),
     ]