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)
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)
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)
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 )
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)
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)
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)
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)
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)
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()
def test_prepare(self): class MockResource(ModelResource): model_class = mock_orm.Model fields = [AttributeField('bar.baz', type=int)] class MockQuerySetResource(QuerySetResource): resource_class = MockResource field = RelatedManagerField(attribute='foo', resource_class=MockQuerySetResource) related = Related() field.prepare(mock_context(), related) self.assertEqual(related._prefetch, {'foo', 'foo__bar'})
class Resource(ModelResource): model_class = Mock() fields = [ AttributeField(attribute='bar', type=int), RelatedManagerField(attribute='baz', resource_class=SubResource), ]
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'])
def test_prepare(self): class MockResource(ModelResource): model_class = mock_orm.Model fields = [ AttributeField('bar.baz', type=int) ] class MockQuerySetResource(QuerySetResource): resource_class = MockResource field = RelatedManagerField(attribute='foo', resource_class=MockQuerySetResource) related = Related() field.prepare(mock_context(), related) self.assertEqual(related._prefetch, { 'foo', 'foo__bar' })
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'])
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)
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)
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)