You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.5 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. from odoo.tests import common
  24. _logger = logging.getLogger(__name__)
  25. class SearchParentTestCase(common.TransactionCase):
  26. def setUp(self):
  27. super(SearchParentTestCase, self).setUp()
  28. self.model = self.env["res.partner.category"]
  29. self.parent = self.model.create(
  30. {"parent_id": False, "name": "Parent", "active": True}
  31. )
  32. self.child = self.model.create(
  33. {"parent_id": self.parent.id, "name": "Child", "active": True}
  34. )
  35. self.child_parent = self.model.create(
  36. {"parent_id": self.parent.id, "name": "Child Parent", "active": True}
  37. )
  38. self.child_parent_child = self.model.create(
  39. {
  40. "parent_id": self.child_parent.id,
  41. "name": "Child Parent Child",
  42. "active": True,
  43. }
  44. )
  45. self.ids = [
  46. self.parent.id,
  47. self.child.id,
  48. self.child_parent.id,
  49. self.child_parent_child.id,
  50. ]
  51. self.domain = [("id", "in", self.ids)]
  52. def tearDown(self):
  53. super(SearchParentTestCase, self).tearDown()
  54. def _evaluate_parent_result(self, parents, records):
  55. for parent in parents:
  56. self.assertTrue(
  57. not parent.parent_id or parent.parent_id.id not in records.ids
  58. )
  59. def test_search_parents(self):
  60. records = self.model.search([])
  61. parents = self.model.search_parents([])
  62. self._evaluate_parent_result(parents, records)
  63. def test_search_parents_domain(self):
  64. records = self.model.search([("id", "!=", 1)])
  65. parents = self.model.search_parents([("id", "!=", 1)])
  66. self._evaluate_parent_result(parents, records)
  67. def test_search_parents_domain(self):
  68. records = self.model.search([("id", "!=", 1)])
  69. parents = self.model.search_parents([("id", "!=", 1)])
  70. self._evaluate_parent_result(parents, records)
  71. def test_search_parents_args(self):
  72. records = self.model.search([], offset=1, limit=1, order="name desc")
  73. parents = self.model.search_parents(offset=1, limit=1, order="name desc")
  74. self._evaluate_parent_result(parents, records)
  75. def test_search_parents_count(self):
  76. parents = self.model.search_parents(self.domain, count=False)
  77. parent_count = self.model.search_parents(self.domain, count=True)
  78. self.assertTrue(len(parents) == parent_count)
  79. def test_search_parents_access_rights(self):
  80. model = self.model.with_user(self.browse_ref("base.user_admin"))
  81. parents = model.search_parents(self.domain)
  82. self._evaluate_parent_result(parents, model.browse(self.ids))
  83. self.assertTrue(len(parents) == 1 and parents.id == self.parent.id)
  84. access_rule = self.env["ir.rule"].create(
  85. {
  86. "model_id": self.browse_ref("base.model_res_partner_category").id,
  87. "domain_force": [("id", "!=", self.parent.id)],
  88. "name": "Restrict Access",
  89. }
  90. )
  91. records = model.search(self.domain)
  92. parents = model.search_parents(self.domain)
  93. self._evaluate_parent_result(parents, records)
  94. self.assertTrue(set(parents.ids) == {self.child.id, self.child_parent.id})
  95. def test_search_read_parents(self):
  96. parents = self.model.search_parents([])
  97. read_names = parents.read(["name"])
  98. search_names = self.model.search_read_parents([], ["name"])
  99. self.assertTrue(read_names == search_names)