123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- # -*- coding: utf-8 -*-
- from odoo.tests import common
- class TestGroupBooleans(common.TransactionCase):
- def setUp(self):
- super(TestGroupBooleans, self).setUp()
- self.Model = self.env['test_read_group.aggregate.boolean']
- def test_no_value(self):
- groups = self.Model.read_group(
- domain=[],
- fields=['key', 'bool_and', 'bool_or', 'bool_array'],
- groupby=['key'],
- )
- self.assertEqual([], groups)
- def test_agg_and(self):
- # and(true, true)
- self.Model.create({
- 'key': 1,
- 'bool_and': True
- })
- self.Model.create({
- 'key': 1,
- 'bool_and': True
- })
- # and(true, false)
- self.Model.create({'key': 2, 'bool_and': True})
- self.Model.create({'key': 2, 'bool_and': False})
- # and(false, false)
- self.Model.create({'key': 3, 'bool_and': False})
- self.Model.create({'key': 3, 'bool_and': False})
- groups = self.Model.read_group(
- domain=[],
- fields=['key', 'bool_and'],
- groupby=['key'],
- )
- self.assertEqual([
- {
- 'key_count': 2,
- '__domain': [('key', '=', 1)],
- 'key': 1,
- 'bool_and': True
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 2)],
- 'key': 2,
- 'bool_and': False
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 3)],
- 'key': 3,
- 'bool_and': False
- },
- ], groups)
- def test_agg_or(self):
- # or(true, true)
- self.Model.create({
- 'key': 1,
- 'bool_or': True
- })
- self.Model.create({
- 'key': 1,
- 'bool_or': True
- })
- # or(true, false)
- self.Model.create({'key': 2, 'bool_or': True})
- self.Model.create({'key': 2, 'bool_or': False})
- # or(false, false)
- self.Model.create({'key': 3, 'bool_or': False})
- self.Model.create({'key': 3, 'bool_or': False})
- groups = self.Model.read_group(
- domain=[],
- fields=['key', 'bool_or'],
- groupby=['key'],
- )
- self.assertEqual([
- {
- 'key_count': 2,
- '__domain': [('key', '=', 1)],
- 'key': 1,
- 'bool_or': True
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 2)],
- 'key': 2,
- 'bool_or': True
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 3)],
- 'key': 3,
- 'bool_or': False
- },
- ], groups)
- def test_agg_array(self):
- # array(true, true)
- self.Model.create({
- 'key': 1,
- 'bool_array': True
- })
- self.Model.create({
- 'key': 1,
- 'bool_array': True
- })
- # array(true, false)
- self.Model.create({'key': 2, 'bool_array': True})
- self.Model.create({'key': 2, 'bool_array': False})
- # array(false, false)
- self.Model.create({'key': 3, 'bool_array': False})
- self.Model.create({'key': 3, 'bool_array': False})
- groups = self.Model.read_group(
- domain=[],
- fields=['key', 'bool_array'],
- groupby=['key'],
- )
- self.assertEqual([
- {
- 'key_count': 2,
- '__domain': [('key', '=', 1)],
- 'key': 1,
- 'bool_array': [True, True]
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 2)],
- 'key': 2,
- 'bool_array': [True, False]
- },
- {
- 'key_count': 2,
- '__domain': [('key', '=', 3)],
- 'key': 3,
- 'bool_array': [False, False]
- },
- ], groups)
- def test_group_by_aggregable(self):
- self.Model.create({'bool_and': False, 'key': 1, 'bool_array': True})
- self.Model.create({'bool_and': False, 'key': 2, 'bool_array': True})
- self.Model.create({'bool_and': False, 'key': 2, 'bool_array': True})
- self.Model.create({'bool_and': True, 'key': 2, 'bool_array': True})
- self.Model.create({'bool_and': True, 'key': 3, 'bool_array': True})
- self.Model.create({'bool_and': True, 'key': 3, 'bool_array': True})
- groups = self.Model.read_group(
- domain=[],
- fields=['key', 'bool_and', 'bool_array'],
- groupby=['bool_and', 'key'],
- lazy=False
- )
- self.assertEqual([
- {
- 'bool_and': False,
- 'key': 1,
- 'bool_array': [True],
- '__count': 1,
- '__domain': ['&', ('bool_and', '=', False), ('key', '=', 1)]
- },
- {
- 'bool_and': False,
- 'key': 2,
- 'bool_array': [True, True],
- '__count': 2,
- '__domain': ['&', ('bool_and', '=', False), ('key', '=', 2)]
- },
- {
- 'bool_and': True,
- 'key': 2,
- 'bool_array': [True],
- '__count': 1,
- '__domain': ['&', ('bool_and', '=', True), ('key', '=', 2)]
- },
- {
- 'bool_and': True,
- 'key': 3,
- 'bool_array': [True, True],
- '__count': 2,
- '__domain': ['&', ('bool_and', '=', True), ('key', '=', 3)]
- }
- ], groups)
- class TestAggregate(common.TransactionCase):
- def setUp(self):
- super(TestAggregate, self).setUp()
- self.foo = self.env['res.partner'].create({'name': 'Foo'})
- self.bar = self.env['res.partner'].create({'name': 'Bar'})
- self.Model = self.env['test_read_group.aggregate']
- self.Model.create({'key': 1, 'value': 1, 'partner_id': False})
- self.Model.create({'key': 1, 'value': 2, 'partner_id': self.foo.id})
- self.Model.create({'key': 1, 'value': 3, 'partner_id': self.foo.id})
- self.Model.create({'key': 1, 'value': 4, 'partner_id': self.bar.id})
- def test_agg_default(self):
- """ test default aggregation on fields """
- fields = ['key', 'value', 'partner_id']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value': 10,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- def test_agg_explicit(self):
- """ test explicit aggregation on fields """
- fields = ['key', 'value:max', 'partner_id']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value': 4,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- fields = ['key', 'value', 'partner_id:array_agg']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value': 10,
- 'partner_id': [None, self.foo.id, self.foo.id, self.bar.id],
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- fields = ['key', 'value', 'partner_id:count']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value': 10,
- 'partner_id': 3,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- fields = ['key', 'value', 'partner_id:count_distinct']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value': 10,
- 'partner_id': 2,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- def test_agg_multi(self):
- """ test multiple aggregation on fields """
- fields = ['key', 'value_min:min(value)', 'value_max:max(value)', 'partner_id']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'value_min': 1,
- 'value_max': 4,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
- fields = ['key', 'ids:array_agg(id)']
- groups = self.Model.read_group([], fields, ['key'])
- self.assertEqual(groups, [{
- 'key': 1,
- 'ids': self.Model.search([]).ids,
- 'key_count': 4,
- '__domain': [('key', '=', 1)],
- }])
|