Exemplo n.º 1
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'])
Exemplo n.º 2
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()
Exemplo n.º 3
0
    def test_handle_outgoing_multi_level(self):

        # TODO: Need to alter savory_pie.fields.IterableField.handle_outgoing to not use manager.all()
        from savory_pie.tests.django import mock_orm

        class ORMModelMock(mock_orm.Model):
            pass

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

        field = IterableField(attribute='foo.fu', resource_class=MockResource)

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

        class InterimObject(object):
            pass

        source_object.foo = InterimObject()
        source_object.foo.fu = related_manager

        target_dict = {}
        field.handle_outgoing(mock_context(), source_object, target_dict)
        self.assertEqual([{'_id': '4', 'bar': 14}], target_dict['foo.fu'])
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def test_outgoing(self):
        class MockResource(ModelResource):
            model_class = mock_orm.Model
            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)
        self.assertEqual([{'_id': '4', 'bar': 14}], target_dict['foo'])
Exemplo n.º 8
0
    def test_incoming_with_delete(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.remove = Mock()
        related_model1 = mock_orm.Model(pk=1, bar=11)
        related_model2 = mock_orm.Model(pk=2, bar=12)
        related_model3 = mock_orm.Model(pk=3, bar=13)
        mock_resource1 = MockResource(related_model1)
        mock_resource1.key = 1
        mock_resource2 = MockResource(related_model2)
        mock_resource2.key = 2
        mock_resource3 = MockResource(related_model3)
        mock_resource3.key = 3

        field._resource_class = Mock()
        field._resource_class.side_effect = [
            mock_resource1, mock_resource2, mock_resource3
        ]
        related_manager.all = Mock(return_value=mock_orm.QuerySet(
            related_model1, related_model2, related_model3))
        target_object.foos = related_manager

        ctx = mock_context()
        mock_resources = Mock()
        mock_resources.side_effect = [mock_resource1, mock_resource2]

        ctx.resolve_resource_uri = mock_resources

        field.handle_incoming(ctx, source_dict, target_object)
        related_manager.remove.assert_called_with(related_model3)
Exemplo n.º 9
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)
Exemplo n.º 10
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')
Exemplo n.º 11
0
    def test_incoming(self):
        class Resource(ModelResource):
            model_class = Mock()
            pass

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

        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)

        ctx.resolve_resource_uri.assert_called_with('uri://resources/2')
        self.assertEqual(target_object.foo, related_model)
Exemplo n.º 12
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'))
Exemplo n.º 13
0
    def test_incoming_m2m_add(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 BarResource(ModelResource):
            model_class = mock_orm.Model
            fields = [
                AttributeField(attribute='some_attribute', type=int),
            ]

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

        bar = mock_orm.Model(pk=1, someAttribute=1, resourceUri='bar/1')
        foo = mock_orm.Model(pk=2)

        BarResource.key = 'bar'
        BarResource.model = bar

        foobars = mock_orm.QuerySet()
        foobars.create = Mock(
            return_value=mock_orm.Model(pk=3, foo=foo, bar=bar))

        foobar = mock_orm.Model()
        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())
        foo.bars.through = foobar
        delattr(foo.bars, 'add')

        source_dict = {
            'bars': [bar.__dict__],
        }

        ctx = mock_context()
        ctx.resolve_resource_uri = Mock(return_value=BarResource)

        field.handle_incoming(ctx, source_dict, foo)

        foobars.create.assert_called_with(foo=foo, bar=bar)