Ejemplo n.º 1
0
    def test_incoming_with_add(self):
        class MockResource(ModelResource):
            key = Mock()
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = URIListResourceField(attribute='foos',
                                     resource_class=MockResource)

        source_dict = {'foos': ['uri://resources/1', 'uri://resources/2']}

        target_object = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet())
        target_object.foos = related_manager

        ctx = mock_context()
        foo1_model = Mock()
        foo2_model = Mock()
        mock_resources = Mock()
        resource1 = MockResource(foo1_model)
        resource1.key = 1
        resource2 = MockResource(foo2_model)
        resource2.key = 2
        mock_resources.side_effect = [resource1, resource2]

        ctx.resolve_resource_uri = mock_resources

        field.handle_incoming(ctx, source_dict, target_object)
        related_manager.add.assert_called_with(foo1_model, foo2_model)
Ejemplo n.º 2
0
    def test_incoming_read_only(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(
            attribute='foo',
            resource_class=MockResource,
            read_only=True,
        )

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=14)
        related_manager.all = Mock(
            return_value=mock_orm.QuerySet(related_model))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{
                '_id': '4',
                'bar': 15
            }],
        }

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

        self.assertEqual(14, related_model.bar)
Ejemplo n.º 3
0
    def test_incoming_read_only(self):

        class Resource(ModelResource):
            model_class = Mock(spec=[])
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = SubModelResourceField(
            attribute='foo',
            resource_class=Resource,
            read_only=True,
        )

        source_dict = {
            'foo': {'bar': 20},
        }

        target_object = Mock()
        target_object.foo = Mock(['save'])

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

        self.assertFalse(hasattr(target_object.foo, 'bar'))
        self.assertFalse(target_object.foo.save.called)
Ejemplo n.º 4
0
    def test_outgoing(self):
        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
                ]

        field = URIListResourceField(attribute='foos', resource_class=MockResource)

        source_object = mock_orm.Model()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            mock_orm.Model(pk=1, bar=14),
            mock_orm.Model(pk=2, bar=14)
        ))

        source_object.foos = related_manager

        ctx = mock_context()
        ctx.build_resource_uri = Mock()
        ctx.build_resource_uri.side_effect = ['uri://resources/1', 'uri://resources/2']

        target_dict = {}
        field.handle_outgoing(ctx, source_object, target_dict)

        self.assertEqual(['uri://resources/1', 'uri://resources/2'], target_dict['foos'])
Ejemplo n.º 5
0
    def test_find_existing_by_uri_incoming(self):

        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = SubModelResourceField(attribute='foo', resource_class=MockResource)

        source_dict = {
            'foo': {
                'resourceUri': 'http://testsever/api/v1/bar/20',
                'bar': 20
            },
        }

        target_object = Mock()
        target_object._meta.get_field().related.field.name = 'bar'
        ctx = mock_context()
        ctx.resolve_resource_uri = Mock()
        foo_20 = ctx.resolve_resource_uri.return_value = MockResource(Mock())
        field.handle_incoming(ctx, source_dict, target_object)

        ctx.resolve_resource_uri.assert_called_with(source_dict['foo']['resourceUri'])
        target_object.foo.save.assert_called()

        self.assertEqual(foo_20.model, target_object.foo)
        self.assertEqual(20, target_object.foo.bar)
Ejemplo n.º 6
0
    def test_outgoing_with_resource_uri(self):

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            resource_path = 'bar'
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo', resource_class=MockResource)

        source_object = mock_orm.Model()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            mock_orm.Model(pk=4, bar=14)
        ))
        source_object.foo = related_manager

        target_dict = {}

        field.handle_outgoing(mock_context(), source_object, target_dict)
        target = target_dict['foo']
        # Not testing the hash of the dictionary that is tested else were
        self.assertEqual(
            [{'resourceUri': 'uri://bar', 'bar': 14}],
            target
        )
Ejemplo n.º 7
0
    def test_new_object_incoming(self):

        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = OneToOneField(attribute='foo', resource_class=MockResource)

        source_dict = {
            'foo': {'bar': 20},
        }

        # The django ORM makes me sad that this is not a None or AttributeError
        class MockFailsOnFooAccess(Mock):
            def __getattr__(self, name):
                if name == 'foo':
                    raise ObjectDoesNotExist
                else:
                    return super(Mock, self).__getattr__(name)

        target_object = MockFailsOnFooAccess()
        target_object._meta.get_field().related.field.name = 'bar'
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(20, target_object.foo.bar)
        self.assertEqual(MockResource.model_class.return_value, target_object.foo)
        target_object.foo.save.assert_called()
Ejemplo n.º 8
0
    def test_incoming_no_id(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo', resource_class=MockResource)

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet())
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{'bar': 4}],
        }

        model_index = len(mock_orm.Model._models)
        field.handle_incoming(mock_context(), source_dict, target_obj)

        model = mock_orm.Model._models[model_index]
        self.assertEqual(4, model.bar)
        related_manager.add.assert_called_with(model)
Ejemplo n.º 9
0
    def test_incoming_with_resource_uri(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo', resource_class=MockResource)

        target_obj = mock_orm.Mock()

        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=10)
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            related_model
        ))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{
                'resourceUri': 'http://testsever/api/v2/bar/4',
                'bar': 14
            }],
        }

        model_index = len(mock_orm.Model._models)
        ctx = mock_context()
        ctx.resolve_resource_uri = Mock()
        ctx.resolve_resource_uri.return_value = MockResource(related_model)

        field.handle_incoming(ctx, source_dict, target_obj)
        model = mock_orm.Model._models[model_index - 1]
        self.assertEqual(14, model.bar)
Ejemplo n.º 10
0
    def test_handel_outgoing_count_attr_error(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mapper.get.return_value = sum
        field = RelatedCountField('name')

        count = Mock()
        count.count.return_value = 3
        query_result = Mock()
        query_result.values.return_value = count

        objects = Mock()
        objects.filter.return_value = query_result
        StubModel.objects = objects

        stub_model = StubModel()
        stub_model.id = 7

        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            3,
            target_dict['name'],
        )
        StubModel.objects.filter.assert_called_with(pk=7)
Ejemplo 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'})
Ejemplo n.º 12
0
    def test_incoming_read_only(self):
        class Resource(ModelResource):
            model_class = Mock(spec=[])
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = SubModelResourceField(
            attribute='foo',
            resource_class=Resource,
            read_only=True,
        )

        source_dict = {
            'foo': {
                'bar': 20
            },
        }

        target_object = Mock()
        target_object.foo = Mock(['save'])

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

        self.assertFalse(hasattr(target_object.foo, 'bar'))
        self.assertFalse(target_object.foo.save.called)
Ejemplo n.º 13
0
    def test_incoming_delete(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo', resource_class=MockResource)

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=14)
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            related_model
        ))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [],
        }

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

        related_manager.remove.assert_called_with(related_model)
Ejemplo n.º 14
0
    def test_incoming_delete(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo',
                                    resource_class=MockResource)

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=14)
        related_manager.all = Mock(
            return_value=mock_orm.QuerySet(related_model))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [],
        }

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

        related_manager.remove.assert_called_with(related_model)
Ejemplo n.º 15
0
    def test_incoming_no_id(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo',
                                    resource_class=MockResource)

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet())
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{
                'bar': 4
            }],
        }

        model_index = len(mock_orm.Model._models)
        field.handle_incoming(mock_context(), source_dict, target_obj)

        model = mock_orm.Model._models[model_index]
        self.assertEqual(4, model.bar)
        related_manager.add.assert_called_with(model)
Ejemplo n.º 16
0
    def test_incoming_read_only(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(
            attribute='foo',
            resource_class=MockResource,
            read_only=True,
        )

        target_obj = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=14)
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            related_model
        ))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{'_id': '4', 'bar': 15}],
        }

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

        self.assertEqual(14, related_model.bar)
        self.assertFalse(related_model.save.called)
Ejemplo n.º 17
0
    def test_new_object_incoming(self):
        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = OneToOneField(attribute='foo', resource_class=MockResource)

        source_dict = {
            'foo': {
                'bar': 20
            },
        }

        # The django ORM makes me sad that this is not a None or AttributeError
        class MockFailsOnFooAccess(Mock):
            def __getattr__(self, name):
                if name == 'foo':
                    raise ObjectDoesNotExist
                else:
                    return super(Mock, self).__getattr__(name)

        target_object = MockFailsOnFooAccess()
        target_object._meta.get_field().related.field.name = 'bar'
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(20, target_object.foo.bar)
        self.assertEqual(MockResource.model_class.return_value,
                         target_object.foo)
        target_object.foo.save.assert_called()
Ejemplo n.º 18
0
    def test_incoming_with_add(self):
        class MockResource(ModelResource):
            key = Mock()
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
                ]

        field = URIListResourceField(attribute='foos', resource_class=MockResource)

        source_dict = {
            'foos': ['uri://resources/1', 'uri://resources/2']
        }

        target_object = mock_orm.Mock()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet())
        target_object.foos = related_manager

        ctx = mock_context()
        foo1_model = Mock()
        foo2_model = Mock()
        mock_resources = Mock()
        resource1 = MockResource(foo1_model)
        resource1.key = 1
        resource2 = MockResource(foo2_model)
        resource2.key = 2
        mock_resources.side_effect = [resource1, resource2]

        ctx.resolve_resource_uri = mock_resources

        field.handle_incoming(ctx, source_dict, target_object)
        related_manager.add.assert_called_with(foo1_model, foo2_model)
Ejemplo n.º 19
0
    def test_incoming_read_only(self):

        class Resource(ModelResource):
            model_class = Mock()
            pass

        field = URIResourceField(
            attribute='foo',
            resource_class=Resource,
            read_only=True,
        )

        source_dict = {
            'foo': 'uri://resources/2'
        }
        target_object = Mock([])

        related_model = mock_orm.Model(pk=2)

        ctx = mock_context()
        ctx.resolve_resource_uri = Mock(return_value=Resource(related_model))

        field.handle_incoming(ctx, source_dict, target_object)

        self.assertFalse(ctx.resolve_resource_uri.called)
        self.assertFalse(hasattr(target_object, 'foo'))
Ejemplo n.º 20
0
    def test_find_existing_by_uri_incoming(self):
        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = SubModelResourceField(attribute='foo',
                                      resource_class=MockResource)

        source_dict = {
            'foo': {
                'resourceUri': 'http://testsever/api/v1/bar/20',
                'bar': 20
            },
        }

        target_object = Mock()
        target_object._meta.get_field().related.field.name = 'bar'
        ctx = mock_context()
        ctx.resolve_resource_uri = Mock()
        foo_20 = ctx.resolve_resource_uri.return_value = MockResource(Mock())
        field.handle_incoming(ctx, source_dict, target_object)

        ctx.resolve_resource_uri.assert_called_with(
            source_dict['foo']['resourceUri'])
        target_object.foo.save.assert_called()

        self.assertEqual(foo_20.model, target_object.foo)
        self.assertEqual(20, target_object.foo.bar)
Ejemplo n.º 21
0
    def test_outgoing(self):
        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = URIListResourceField(attribute='foos',
                                     resource_class=MockResource)

        source_object = mock_orm.Model()
        related_manager = mock_orm.Manager()
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            mock_orm.Model(pk=1, bar=14), mock_orm.Model(pk=2, bar=14)))

        source_object.foos = related_manager

        ctx = mock_context()
        ctx.build_resource_uri = Mock()
        ctx.build_resource_uri.side_effect = [
            'uri://resources/1', 'uri://resources/2'
        ]

        target_dict = {}
        field.handle_outgoing(ctx, source_object, target_dict)

        self.assertEqual(['uri://resources/1', 'uri://resources/2'],
                         target_dict['foos'])
Ejemplo n.º 22
0
    def test_handel_outgoing_count_attr_error(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mapper.get.return_value = sum
        field = RelatedCountField('name')

        count = Mock()
        count.count.return_value = 3
        query_result = Mock()
        query_result.values.return_value = count

        objects = Mock()
        objects.filter.return_value = query_result
        StubModel.objects = objects

        stub_model = StubModel()
        stub_model.id = 7

        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            3,
            target_dict['name'],
        )
        StubModel.objects.filter.assert_called_with(pk=7)
Ejemplo n.º 23
0
    def test_find_existing_incoming(self):

        mock_model = Mock()

        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)

        field = SubModelResourceField(attribute='foo', resource_class=MockResource)

        source_dict = {
            'foo': {'bar': 20},
        }

        # The django ORM makes me sad that this is not a None or AttributeError
        class MockFailsOnFooAccess(Mock):
            def __getattr__(self, name):
                if name == 'foo':
                    raise ObjectDoesNotExist
                else:
                    return super(Mock, self).__getattr__(name)

        target_object = MockFailsOnFooAccess()
        target_object._meta.get_field().related.field.name = 'bar'
        field.handle_incoming(mock_context(), source_dict, target_object)

        self.assertEqual(20, target_object.foo.bar)
        self.assertEqual(mock_model, target_object.foo)
        target_object.foo.save.assert_called()
Ejemplo n.º 24
0
    def test_incoming_with_resource_uri(self):
        del mock_orm.Model._models[:]

        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='bar', type=int),
            ]

        field = RelatedManagerField(attribute='foo',
                                    resource_class=MockResource)

        target_obj = mock_orm.Mock()

        related_manager = mock_orm.Manager()
        related_model = mock_orm.Model(pk=4, bar=10)
        related_manager.all = Mock(
            return_value=mock_orm.QuerySet(related_model))
        target_obj.foo = related_manager
        source_dict = {
            'foo': [{
                'resourceUri': 'http://testsever/api/v2/bar/4',
                'bar': 14
            }],
        }

        model_index = len(mock_orm.Model._models)
        ctx = mock_context()
        ctx.resolve_resource_uri = Mock()
        ctx.resolve_resource_uri.return_value = MockResource(related_model)

        field.handle_incoming(ctx, source_dict, target_obj)
        model = mock_orm.Model._models[model_index - 1]
        self.assertEqual(14, model.bar)
Ejemplo n.º 25
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)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
    def test_simple_incoming(self):
        field = AttributeFieldWithModel(attribute='bar.baz.potato',
                                        type=str,
                                        model=self.BazModel)

        source_dict = {'potato': 'abc'}
        field.handle_incoming(mock_context(), source_dict, self.foo)
        self.assertEqual(self.baz.potato, 'abc')
Ejemplo n.º 28
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)
Ejemplo n.º 29
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)
Ejemplo n.º 30
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)
Ejemplo n.º 31
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'))
Ejemplo n.º 32
0
    def test_incoming_with_reverse_foreign_key(self):
        class User(mock_orm.Model):
            name = Mock()

        class UserOwner(mock_orm.Model):
            user = Mock(
                django.db.models.fields.related.
                ReverseSingleRelatedObjectDescriptor(Mock()))
            type = Mock()

        class UserOwnerResource(resources.ModelResource):
            model_class = UserOwner

            fields = [
                fields.AttributeField(attribute='type', type=str),
            ]

        class MockResource(ModelResource):
            model_class = User

            owner_field = fields.SubModelResourceField(
                attribute='owner', resource_class=UserOwnerResource)
            fields = [AttributeField(attribute='name', type=str), owner_field]

        field = SubModelResourceField(attribute='user',
                                      resource_class=MockResource)

        source_dict = {
            'user': {
                'name': 'username',
                'owner': {
                    'type': 'ownertype'
                }
            },
        }

        # The django ORM makes me sad that this is not a None or AttributeError
        class MockFailsOnFooAccess(Mock):
            def model(self):
                return Mock()

            def __getattr__(self, name):
                if name == 'user':
                    raise ObjectDoesNotExist
                else:
                    return super(Mock, self).__getattr__(name)

        target_object = MockFailsOnFooAccess()
        target_object._meta.get_field().related.field.name = 'name'
        User.objects.filter().get()._meta.get_field(
        ).related.field.name = 'name'

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

        self.assertEqual('username', target_object.user.name)
        self.assertEqual('ownertype', target_object.user.owner.type)
        target_object.user.save.assert_called()
Ejemplo n.º 33
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'
        })
Ejemplo n.º 34
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'
        })
Ejemplo n.º 35
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'})
Ejemplo n.º 36
0
    def test_incoming_m2m_delete(self):
        """
        Attempting to simulate the following:

        Bar(Model):
            pass

        Foo(Model):
            bars = ManyToManyField(Bar, through='FooBar')

        FooBar(Model):
            bar: ForeignKey(Bar)
            foo: ForeignKey(Foo)

        BarResource(ModelResource):
            model_class = Bar

        FooResource(ModelResource):
            model_class = Foo

            fields = [
                RelatedManagerField('bars', BarResource),
            ]
        """
        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='some_attribute', type=int),
            ]

        field = RelatedManagerField(attribute='bars',
                                    resource_class=MockResource)

        bar = mock_orm.Model(pk=1)
        foo = mock_orm.Model(pk=2)
        foobar = mock_orm.Model(pk=3, foo=foo, bar=bar)

        foobars = mock_orm.Manager()
        foobars.all = Mock(return_value=mock_orm.QuerySet(foobar))
        foobars.filter = foobars.all
        foobar.objects = foobars

        foo.bars = mock_orm.Manager()
        foo.bars.source_field_name = 'foo'
        foo.bars.target_field_name = 'bar'
        foo.bars.all = Mock(return_value=mock_orm.QuerySet(bar))
        foo.bars.through = foobar
        delattr(foo.bars, 'remove')

        source_dict = {
            'bars': [],
        }

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

        foobar.delete.assert_called_with()
Ejemplo n.º 37
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)
Ejemplo n.º 38
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)
Ejemplo n.º 39
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)
Ejemplo n.º 40
0
    def test_incoming_m2m_delete(self):
        """
        Attempting to simulate the following:

        Bar(Model):
            pass

        Foo(Model):
            bars = ManyToManyField(Bar, through='FooBar')

        FooBar(Model):
            bar: ForeignKey(Bar)
            foo: ForeignKey(Foo)

        BarResource(ModelResource):
            model_class = Bar

        FooResource(ModelResource):
            model_class = Foo

            fields = [
                RelatedManagerField('bars', BarResource),
            ]
        """
        class MockResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='some_attribute', type=int),
            ]

        field = RelatedManagerField(attribute='bars', resource_class=MockResource)

        bar = mock_orm.Model(pk=1)
        foo = mock_orm.Model(pk=2)
        foobar = mock_orm.Model(pk=3, foo=foo, bar=bar)

        foobars = mock_orm.Manager()
        foobars.all = Mock(return_value=mock_orm.QuerySet(foobar))
        foobars.filter = foobars.all
        foobar.objects = foobars

        foo.bars = mock_orm.Manager()
        foo.bars.source_field_name = 'foo'
        foo.bars.target_field_name = 'bar'
        foo.bars.all = Mock(return_value=mock_orm.QuerySet(bar))
        foo.bars.through = foobar
        delattr(foo.bars, 'remove')

        source_dict = {
            'bars': [],
        }

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

        foobar.delete.assert_called_with()
Ejemplo n.º 41
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)
Ejemplo n.º 42
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)
Ejemplo n.º 43
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)
Ejemplo n.º 44
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'))
Ejemplo n.º 45
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)
Ejemplo n.º 46
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)
Ejemplo n.º 47
0
    def test_prepare_not_top_level(self):
        ctx = mock_context()
        related = Mock(name='related')

        field = AggregateField('name.one.two', django.db.models.Count)

        with self.assertRaises(SavoryPieError) as cm:
            field.prepare(ctx, related)

        self.assertEqual(
            'RelatedCountField can only be used on a top level ModelResource',
            str(cm.exception))
Ejemplo n.º 48
0
    def test_simple_incoming(self):
        field = AttributeFieldWithModel(
            attribute='bar.baz.potato',
            type=str,
            model=self.BazModel
        )

        source_dict = {
            'potato': 'abc'
        }
        field.handle_incoming(mock_context(), source_dict, self.foo)
        self.assertEqual(self.baz.potato, 'abc')
Ejemplo n.º 49
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)
Ejemplo n.º 50
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)
Ejemplo n.º 51
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)
Ejemplo n.º 52
0
    def test_prepare(self):
        ctx = mock_context()
        ctx.peek.side_effect = IndexError
        related = Mock(name='related')

        field = AggregateField('name.one.two', django.db.models.Count)
        field.prepare(ctx, related)

        related.annotate.assert_called_with(
            django.db.models.Count,
            'name__one__two',
            distinct=True,
        )
Ejemplo n.º 53
0
    def test_prepare_not_top_level(self):
        ctx = mock_context()
        related = Mock(name='related')

        field = AggregateField('name.one.two', django.db.models.Count)

        with self.assertRaises(SavoryPieError) as cm:
            field.prepare(ctx, related)

        self.assertEqual(
            'RelatedCountField can only be used on a top level ModelResource',
            str(cm.exception)
        )
Ejemplo n.º 54
0
    def test_raise_unknown_method(self, mapper):
        ctx = mock_context()

        mapper.get.side_effect = AttributeError

        stub_model = self._get_stub([{'name': 23}, {'name': 10}])
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        target_dict = {}

        field = AggregateField('name', mock_sum)
        with self.assertRaises(AttributeError):
            field.handle_outgoing(ctx, stub_model, target_dict)
Ejemplo n.º 55
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(),
        ])
Ejemplo n.º 56
0
    def test_raise_unknown_method(self, mapper):
        ctx = mock_context()

        mapper.get.side_effect = AttributeError

        stub_model = self._get_stub([{'name': 23}, {'name': 10}])
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        target_dict = {}

        field = AggregateField('name', mock_sum)
        with self.assertRaises(AttributeError):
            field.handle_outgoing(ctx, stub_model, target_dict)
Ejemplo n.º 57
0
    def test_prepare(self):
        ctx = mock_context()
        ctx.peek.side_effect = IndexError
        related = Mock(name='related')

        field = AggregateField('name.one.two', django.db.models.Count)
        field.prepare(ctx, related)

        related.annotate.assert_called_with(
            django.db.models.Count,
            'name__one__two',
            distinct=True,
        )
Ejemplo n.º 58
0
    def test_outgoing(self):
        class Resource(ModelResource):
            model_class = Mock()
            parent_resource_path = 'resources'

        field = URIResourceField(attribute='foo', resource_class=Resource)

        source_object = Mock()
        source_object.foo = mock_orm.Model(pk=2)

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

        self.assertEqual(target_dict['foo'], 'uri://resources/2')
Ejemplo n.º 59
0
    def test_prepare(self):
        class MockResource(ModelResource):
            model_class = Mock()
            fields = [
                AttributeField(attribute='bar.baz', type=int),
            ]

        field = SubModelResourceField(attribute='foo',
                                      resource_class=MockResource)

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

        self.assertEqual(related._select, {'foo', 'foo__bar'})
Ejemplo n.º 60
0
    def test_incoming_with_reverse_foreign_key(self):
        class User(mock_orm.Model):
            name = Mock()

        class UserOwner(mock_orm.Model):
            user = Mock(django.db.models.fields.related.ReverseSingleRelatedObjectDescriptor(Mock()))
            type = Mock()

        class UserOwnerResource(resources.ModelResource):
            model_class = UserOwner

            fields = [
                fields.AttributeField(attribute='type', type=str),
            ]

        class MockResource(ModelResource):
            model_class = User

            owner_field = fields.SubModelResourceField(attribute='owner', resource_class=UserOwnerResource)
            fields = [
                AttributeField(attribute='name', type=str),
                owner_field
            ]

        field = SubModelResourceField(attribute='user', resource_class=MockResource)

        source_dict = {
            'user': {'name': 'username', 'owner': {'type': 'ownertype'}},
        }

        # The django ORM makes me sad that this is not a None or AttributeError
        class MockFailsOnFooAccess(Mock):
            def model(self):
                return Mock()

            def __getattr__(self, name):
                if name == 'user':
                    raise ObjectDoesNotExist
                else:
                    return super(Mock, self).__getattr__(name)

        target_object = MockFailsOnFooAccess()
        target_object._meta.get_field().related.field.name = 'name'
        User.objects.filter().get()._meta.get_field().related.field.name = 'name'

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

        self.assertEqual('username', target_object.user.name)
        self.assertEqual('ownertype', target_object.user.owner.type)
        target_object.user.save.assert_called()