示例#1
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))
示例#2
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)
        )
示例#3
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,
        )
示例#4
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)
示例#5
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,
        )
示例#6
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)
示例#7
0
    def test_handle_outgoing(self):
        ctx = mock_context()

        mock_model = Mock(name='model')
        mock_model.name__count = 14
        target_dict = {}

        field = AggregateField('name', django.db.models.Count)
        field.handle_outgoing(ctx, mock_model, target_dict)

        self.assertEqual(
            14,
            target_dict['name'],
        )
示例#8
0
    def test_handle_outgoing(self):
        ctx = mock_context()

        mock_model = Mock(name='model')
        mock_model.name__count = 14
        target_dict = {}

        field = AggregateField('name', django.db.models.Count)
        field.handle_outgoing(ctx, mock_model, target_dict)

        self.assertEqual(
            14,
            target_dict['name'],
        )
示例#9
0
    def test_handel_outgoing_sum_non(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        mapper.get.return_value = sum
        field = AggregateField('name', mock_sum)

        stub_model = self._get_stub([{'name': None}])
        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            0,
            target_dict['name'],
        )
示例#10
0
    def test_handel_outgoing_sum_non(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        mapper.get.return_value = sum
        field = AggregateField('name', mock_sum)

        stub_model = self._get_stub([{'name': None}])
        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            0,
            target_dict['name'],
        )
示例#11
0
    def test_handel_outgoing_sum_attr_error(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        mapper.get.return_value = sum
        field = AggregateField('name', mock_sum)

        stub_model = self._get_stub([{'name': 23}, {'name': 10}])
        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            33,
            target_dict['name'],
        )
        StubModel.objects.filter.assert_called_with(pk=2)
示例#12
0
    def test_handel_outgoing_sum_attr_error(self, mapper):
        ctx = mock_context()

        target_dict = {}
        mock_sum = Mock(spec='name')
        mock_sum.name = 'sum'
        mapper.get.return_value = sum
        field = AggregateField('name', mock_sum)

        stub_model = self._get_stub([{'name': 23}, {'name': 10}])
        field.handle_outgoing(ctx, stub_model, target_dict)

        self.assertEqual(
            33,
            target_dict['name'],
        )
        StubModel.objects.filter.assert_called_with(pk=2)