test_group_operator.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # -*- coding: utf-8 -*-
  2. from odoo.tests import common
  3. class TestGroupBooleans(common.TransactionCase):
  4. def setUp(self):
  5. super(TestGroupBooleans, self).setUp()
  6. self.Model = self.env['test_read_group.aggregate.boolean']
  7. def test_no_value(self):
  8. groups = self.Model.read_group(
  9. domain=[],
  10. fields=['key', 'bool_and', 'bool_or', 'bool_array'],
  11. groupby=['key'],
  12. )
  13. self.assertEqual([], groups)
  14. def test_agg_and(self):
  15. # and(true, true)
  16. self.Model.create({
  17. 'key': 1,
  18. 'bool_and': True
  19. })
  20. self.Model.create({
  21. 'key': 1,
  22. 'bool_and': True
  23. })
  24. # and(true, false)
  25. self.Model.create({'key': 2, 'bool_and': True})
  26. self.Model.create({'key': 2, 'bool_and': False})
  27. # and(false, false)
  28. self.Model.create({'key': 3, 'bool_and': False})
  29. self.Model.create({'key': 3, 'bool_and': False})
  30. groups = self.Model.read_group(
  31. domain=[],
  32. fields=['key', 'bool_and'],
  33. groupby=['key'],
  34. )
  35. self.assertEqual([
  36. {
  37. 'key_count': 2,
  38. '__domain': [('key', '=', 1)],
  39. 'key': 1,
  40. 'bool_and': True
  41. },
  42. {
  43. 'key_count': 2,
  44. '__domain': [('key', '=', 2)],
  45. 'key': 2,
  46. 'bool_and': False
  47. },
  48. {
  49. 'key_count': 2,
  50. '__domain': [('key', '=', 3)],
  51. 'key': 3,
  52. 'bool_and': False
  53. },
  54. ], groups)
  55. def test_agg_or(self):
  56. # or(true, true)
  57. self.Model.create({
  58. 'key': 1,
  59. 'bool_or': True
  60. })
  61. self.Model.create({
  62. 'key': 1,
  63. 'bool_or': True
  64. })
  65. # or(true, false)
  66. self.Model.create({'key': 2, 'bool_or': True})
  67. self.Model.create({'key': 2, 'bool_or': False})
  68. # or(false, false)
  69. self.Model.create({'key': 3, 'bool_or': False})
  70. self.Model.create({'key': 3, 'bool_or': False})
  71. groups = self.Model.read_group(
  72. domain=[],
  73. fields=['key', 'bool_or'],
  74. groupby=['key'],
  75. )
  76. self.assertEqual([
  77. {
  78. 'key_count': 2,
  79. '__domain': [('key', '=', 1)],
  80. 'key': 1,
  81. 'bool_or': True
  82. },
  83. {
  84. 'key_count': 2,
  85. '__domain': [('key', '=', 2)],
  86. 'key': 2,
  87. 'bool_or': True
  88. },
  89. {
  90. 'key_count': 2,
  91. '__domain': [('key', '=', 3)],
  92. 'key': 3,
  93. 'bool_or': False
  94. },
  95. ], groups)
  96. def test_agg_array(self):
  97. # array(true, true)
  98. self.Model.create({
  99. 'key': 1,
  100. 'bool_array': True
  101. })
  102. self.Model.create({
  103. 'key': 1,
  104. 'bool_array': True
  105. })
  106. # array(true, false)
  107. self.Model.create({'key': 2, 'bool_array': True})
  108. self.Model.create({'key': 2, 'bool_array': False})
  109. # array(false, false)
  110. self.Model.create({'key': 3, 'bool_array': False})
  111. self.Model.create({'key': 3, 'bool_array': False})
  112. groups = self.Model.read_group(
  113. domain=[],
  114. fields=['key', 'bool_array'],
  115. groupby=['key'],
  116. )
  117. self.assertEqual([
  118. {
  119. 'key_count': 2,
  120. '__domain': [('key', '=', 1)],
  121. 'key': 1,
  122. 'bool_array': [True, True]
  123. },
  124. {
  125. 'key_count': 2,
  126. '__domain': [('key', '=', 2)],
  127. 'key': 2,
  128. 'bool_array': [True, False]
  129. },
  130. {
  131. 'key_count': 2,
  132. '__domain': [('key', '=', 3)],
  133. 'key': 3,
  134. 'bool_array': [False, False]
  135. },
  136. ], groups)
  137. def test_group_by_aggregable(self):
  138. self.Model.create({'bool_and': False, 'key': 1, 'bool_array': True})
  139. self.Model.create({'bool_and': False, 'key': 2, 'bool_array': True})
  140. self.Model.create({'bool_and': False, 'key': 2, 'bool_array': True})
  141. self.Model.create({'bool_and': True, 'key': 2, 'bool_array': True})
  142. self.Model.create({'bool_and': True, 'key': 3, 'bool_array': True})
  143. self.Model.create({'bool_and': True, 'key': 3, 'bool_array': True})
  144. groups = self.Model.read_group(
  145. domain=[],
  146. fields=['key', 'bool_and', 'bool_array'],
  147. groupby=['bool_and', 'key'],
  148. lazy=False
  149. )
  150. self.assertEqual([
  151. {
  152. 'bool_and': False,
  153. 'key': 1,
  154. 'bool_array': [True],
  155. '__count': 1,
  156. '__domain': ['&', ('bool_and', '=', False), ('key', '=', 1)]
  157. },
  158. {
  159. 'bool_and': False,
  160. 'key': 2,
  161. 'bool_array': [True, True],
  162. '__count': 2,
  163. '__domain': ['&', ('bool_and', '=', False), ('key', '=', 2)]
  164. },
  165. {
  166. 'bool_and': True,
  167. 'key': 2,
  168. 'bool_array': [True],
  169. '__count': 1,
  170. '__domain': ['&', ('bool_and', '=', True), ('key', '=', 2)]
  171. },
  172. {
  173. 'bool_and': True,
  174. 'key': 3,
  175. 'bool_array': [True, True],
  176. '__count': 2,
  177. '__domain': ['&', ('bool_and', '=', True), ('key', '=', 3)]
  178. }
  179. ], groups)
  180. class TestAggregate(common.TransactionCase):
  181. def setUp(self):
  182. super(TestAggregate, self).setUp()
  183. self.foo = self.env['res.partner'].create({'name': 'Foo'})
  184. self.bar = self.env['res.partner'].create({'name': 'Bar'})
  185. self.Model = self.env['test_read_group.aggregate']
  186. self.Model.create({'key': 1, 'value': 1, 'partner_id': False})
  187. self.Model.create({'key': 1, 'value': 2, 'partner_id': self.foo.id})
  188. self.Model.create({'key': 1, 'value': 3, 'partner_id': self.foo.id})
  189. self.Model.create({'key': 1, 'value': 4, 'partner_id': self.bar.id})
  190. def test_agg_default(self):
  191. """ test default aggregation on fields """
  192. fields = ['key', 'value', 'partner_id']
  193. groups = self.Model.read_group([], fields, ['key'])
  194. self.assertEqual(groups, [{
  195. 'key': 1,
  196. 'value': 10,
  197. 'key_count': 4,
  198. '__domain': [('key', '=', 1)],
  199. }])
  200. def test_agg_explicit(self):
  201. """ test explicit aggregation on fields """
  202. fields = ['key', 'value:max', 'partner_id']
  203. groups = self.Model.read_group([], fields, ['key'])
  204. self.assertEqual(groups, [{
  205. 'key': 1,
  206. 'value': 4,
  207. 'key_count': 4,
  208. '__domain': [('key', '=', 1)],
  209. }])
  210. fields = ['key', 'value', 'partner_id:array_agg']
  211. groups = self.Model.read_group([], fields, ['key'])
  212. self.assertEqual(groups, [{
  213. 'key': 1,
  214. 'value': 10,
  215. 'partner_id': [None, self.foo.id, self.foo.id, self.bar.id],
  216. 'key_count': 4,
  217. '__domain': [('key', '=', 1)],
  218. }])
  219. fields = ['key', 'value', 'partner_id:count']
  220. groups = self.Model.read_group([], fields, ['key'])
  221. self.assertEqual(groups, [{
  222. 'key': 1,
  223. 'value': 10,
  224. 'partner_id': 3,
  225. 'key_count': 4,
  226. '__domain': [('key', '=', 1)],
  227. }])
  228. fields = ['key', 'value', 'partner_id:count_distinct']
  229. groups = self.Model.read_group([], fields, ['key'])
  230. self.assertEqual(groups, [{
  231. 'key': 1,
  232. 'value': 10,
  233. 'partner_id': 2,
  234. 'key_count': 4,
  235. '__domain': [('key', '=', 1)],
  236. }])
  237. def test_agg_multi(self):
  238. """ test multiple aggregation on fields """
  239. fields = ['key', 'value_min:min(value)', 'value_max:max(value)', 'partner_id']
  240. groups = self.Model.read_group([], fields, ['key'])
  241. self.assertEqual(groups, [{
  242. 'key': 1,
  243. 'value_min': 1,
  244. 'value_max': 4,
  245. 'key_count': 4,
  246. '__domain': [('key', '=', 1)],
  247. }])
  248. fields = ['key', 'ids:array_agg(id)']
  249. groups = self.Model.read_group([], fields, ['key'])
  250. self.assertEqual(groups, [{
  251. 'key': 1,
  252. 'ids': self.Model.search([]).ids,
  253. 'key_count': 4,
  254. '__domain': [('key', '=', 1)],
  255. }])