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.

112 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. import logging
  22. from odoo.tests import common
  23. _logger = logging.getLogger(__name__)
  24. class SearchParentTestCase(common.TransactionCase):
  25. def setUp(self):
  26. super(SearchParentTestCase, self).setUp()
  27. self.model = self.env["res.partner.category"]
  28. self.parent = self.model.create(
  29. {"parent_id": False, "name": "Parent", "active": True}
  30. )
  31. self.child = self.model.create(
  32. {"parent_id": self.parent.id, "name": "Child", "active": True}
  33. )
  34. self.child_parent = self.model.create(
  35. {"parent_id": self.parent.id, "name": "Child Parent", "active": True}
  36. )
  37. self.child_parent_child = self.model.create(
  38. {
  39. "parent_id": self.child_parent.id,
  40. "name": "Child Parent Child",
  41. "active": True,
  42. }
  43. )
  44. self.ids = [
  45. self.parent.id,
  46. self.child.id,
  47. self.child_parent.id,
  48. self.child_parent_child.id,
  49. ]
  50. self.domain = [("id", "in", self.ids)]
  51. def tearDown(self):
  52. super(SearchParentTestCase, self).tearDown()
  53. def _evaluate_parent_result(self, parents, records):
  54. for parent in parents:
  55. self.assertTrue(
  56. not parent.parent_id or parent.parent_id.id not in records.ids
  57. )
  58. def test_search_parents(self):
  59. records = self.model.search([])
  60. parents = self.model.search_parents([])
  61. self._evaluate_parent_result(parents, records)
  62. def test_search_parents_domain(self):
  63. records = self.model.search([("id", "!=", 1)])
  64. parents = self.model.search_parents([("id", "!=", 1)])
  65. self._evaluate_parent_result(parents, records)
  66. def test_search_parents_domain(self):
  67. records = self.model.search([("id", "!=", 1)])
  68. parents = self.model.search_parents([("id", "!=", 1)])
  69. self._evaluate_parent_result(parents, records)
  70. def test_search_parents_args(self):
  71. records = self.model.search([], offset=1, limit=1, order="name desc")
  72. parents = self.model.search_parents(offset=1, limit=1, order="name desc")
  73. self._evaluate_parent_result(parents, records)
  74. def test_search_parents_count(self):
  75. parents = self.model.search_parents(self.domain, count=False)
  76. parent_count = self.model.search_parents(self.domain, count=True)
  77. self.assertTrue(len(parents) == parent_count)
  78. def test_search_parents_access_rights(self):
  79. model = self.model.with_user(self.browse_ref("base.user_admin"))
  80. parents = model.search_parents(self.domain)
  81. self._evaluate_parent_result(parents, model.browse(self.ids))
  82. self.assertTrue(len(parents) == 1 and parents.id == self.parent.id)
  83. access_rule = self.env["ir.rule"].create(
  84. {
  85. "model_id": self.browse_ref("base.model_res_partner_category").id,
  86. "domain_force": [("id", "!=", self.parent.id)],
  87. "name": "Restrict Access",
  88. }
  89. )
  90. records = model.search(self.domain)
  91. parents = model.search_parents(self.domain)
  92. self._evaluate_parent_result(parents, records)
  93. self.assertTrue(set(parents.ids) == {self.child.id, self.child_parent.id})
  94. def test_search_read_parents(self):
  95. parents = self.model.search_parents([])
  96. read_names = parents.read(["name"])
  97. search_names = self.model.search_read_parents([], ["name"])
  98. self.assertTrue(read_names == search_names)