Skip to content

Commit 34b8892

Browse files
committed
partner_stage_only_confirmed: Fixup tests
1 parent d36050f commit 34b8892

1 file changed

Lines changed: 181 additions & 1 deletion

File tree

partner_stage_only_confirmed/tests/test_partner_filter.py

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,162 @@ def test_filter_various_view_types(self):
250250
)
251251
self.assertFalse(partner_without_filter._filter_only_confirmed())
252252

253+
def test_get_view_non_form_type(self):
254+
"""Test that get_view doesn't modify non-form views"""
255+
# When view_type is not 'form', the filtering logic should not execute
256+
# This means the method should call super().get_view without modifications
257+
result = self.Partner.get_view(view_type="search")
258+
self.assertIn("arch", result) # Should return a valid result
259+
# The filtering should not apply to search views
260+
261+
def test_get_view_filter_disabled(self):
262+
"""Test that get_view doesn't modify views when filtering is disabled"""
263+
# When _filter_only_confirmed returns False, no modifications should occur
264+
result = self.Partner.with_context(only_confirmed_partners=False).get_view(
265+
view_id=self.partner_form_view.id, view_type="form"
266+
)
267+
268+
# Parse the result to check that no domain was added
269+
xml = etree.XML(result["arch"])
270+
parent_field = xml.xpath("//field[@name='parent_id']")
271+
if parent_field: # If the field exists in the view
272+
domain = parent_field[0].get("domain") or ""
273+
# Should not contain the state condition if filtering is disabled
274+
self.assertNotIn("'state'", domain)
275+
self.assertNotIn("'confirmed'", domain)
276+
277+
def test_get_view_non_partner_many2one_field(self):
278+
"""Test that non-partner Many2one fields are not modified"""
279+
# Create a view with a non-partner Many2one field
280+
test_view = self.View.create(
281+
{
282+
"name": "test.non_partner_field",
283+
"model": "res.partner",
284+
"arch": """
285+
<form>
286+
<sheet>
287+
<field name="user_id"/>
288+
</sheet>
289+
</form>
290+
""",
291+
"type": "form",
292+
}
293+
)
294+
295+
result = self.Partner.with_context(only_confirmed_partners=True).get_view(
296+
view_id=test_view.id, view_type="form"
297+
)
298+
xml = etree.XML(result["arch"])
299+
field = xml.xpath("//field[@name='user_id']")[0]
300+
domain = field.get("domain") or ""
301+
302+
# Should not contain state condition for non-partner field
303+
self.assertNotIn("'state'", domain)
304+
self.assertNotIn("'confirmed'", domain)
305+
306+
def test_get_view_domain_extension(self):
307+
"""Test that existing domains are properly extended"""
308+
# Create a view with a field that has an existing domain
309+
view_with_domain = self.View.create(
310+
{
311+
"name": "test.partner.with_domain",
312+
"model": "res.partner",
313+
"arch": """
314+
<form>
315+
<sheet>
316+
<field name="parent_id" domain="[('customer_rank', '>', 0)]"/>
317+
</sheet>
318+
</form>
319+
""",
320+
"type": "form",
321+
}
322+
)
323+
324+
result = self.Partner.with_context(only_confirmed_partners=True).get_view(
325+
view_id=view_with_domain.id, view_type="form"
326+
)
327+
xml = etree.XML(result["arch"])
328+
field = xml.xpath("//field[@name='parent_id']")[0]
329+
domain = field.get("domain")
330+
331+
# Should contain both the original domain and the new condition
332+
self.assertIn("'customer_rank'", domain)
333+
self.assertIn("'state'", domain)
334+
self.assertIn("'confirmed'", domain)
335+
336+
def test_get_view_empty_domain_list(self):
337+
"""Test that empty list domain [] gets replaced properly"""
338+
view_with_empty_domain = self.View.create(
339+
{
340+
"name": "test.partner.empty_domain_list",
341+
"model": "res.partner",
342+
"arch": """
343+
<form>
344+
<sheet>
345+
<field name="parent_id" domain="[]"/>
346+
</sheet>
347+
</form>
348+
""",
349+
"type": "form",
350+
}
351+
)
352+
353+
result = self.Partner.with_context(only_confirmed_partners=True).get_view(
354+
view_id=view_with_empty_domain.id, view_type="form"
355+
)
356+
xml = etree.XML(result["arch"])
357+
field = xml.xpath("//field[@name='parent_id']")[0]
358+
domain = field.get("domain")
359+
360+
# Should be replaced with the confirmed state condition
361+
self.assertEqual(domain, "[('state', '=', 'confirmed')]")
362+
363+
def test_get_view_no_domain(self):
364+
"""Test field with no initial domain gets the state condition"""
365+
view_with_no_domain = self.View.create(
366+
{
367+
"name": "test.partner.no_domain",
368+
"model": "res.partner",
369+
"arch": """
370+
<form>
371+
<sheet>
372+
<field name="parent_id"/>
373+
</sheet>
374+
</form>
375+
""",
376+
"type": "form",
377+
}
378+
)
379+
380+
result = self.Partner.with_context(only_confirmed_partners=True).get_view(
381+
view_id=view_with_no_domain.id, view_type="form"
382+
)
383+
xml = etree.XML(result["arch"])
384+
field = xml.xpath("//field[@name='parent_id']")[0]
385+
domain = field.get("domain")
386+
387+
# Should have the confirmed state condition added
388+
self.assertEqual(domain, "[('state', '=', 'confirmed')]")
389+
390+
def test_filter_with_non_boolean_context_value(self):
391+
"""Test behavior with non-boolean context value"""
392+
# Test with context value that converts to True in bool()
393+
result = self.Partner.with_context(
394+
only_confirmed_partners="any_string"
395+
)._filter_only_confirmed()
396+
self.assertTrue(result)
397+
398+
# Test with context value that converts to False in bool()
399+
result = self.Partner.with_context(
400+
only_confirmed_partners=""
401+
)._filter_only_confirmed()
402+
self.assertFalse(result)
403+
404+
result = self.Partner.with_context(
405+
only_confirmed_partners=0
406+
)._filter_only_confirmed()
407+
self.assertFalse(result)
408+
253409
def test_empty_context_config_parameter(self):
254410
"""Test behavior when config parameter is empty string"""
255411
self.ConfigParam.set_param("partner_stage.only_confirmed_partners", "")
@@ -275,7 +431,10 @@ def test_false_config_parameter_values(self):
275431

276432
def test_true_config_parameter_values(self):
277433
"""Test behavior with various true-like config parameter values"""
278-
true_values = ["True", "true", "1", "yes", "on", "anything_else"]
434+
# Note: "no" and "off" are treated as truthy by the current implementation
435+
# Only "False", "false", "0" are considered falsy
436+
# (empty string follows default path)
437+
true_values = ["True", "true", "1", "yes", "on", "no", "off", "anything_else"]
279438

280439
for true_val in true_values:
281440
with self.subTest(true_val=true_val):
@@ -284,3 +443,24 @@ def test_true_config_parameter_values(self):
284443
)
285444
result = self.Partner._filter_only_confirmed()
286445
self.assertTrue(result, f"Failed for value: {true_val}")
446+
447+
def test_filter_with_integer_context(self):
448+
"""Test behavior with integer context values"""
449+
# Test with integer 1 (should be truthy)
450+
result = self.Partner.with_context(
451+
only_confirmed_partners=1
452+
)._filter_only_confirmed()
453+
self.assertTrue(result)
454+
455+
# Test with integer 0 (should be falsy)
456+
result = self.Partner.with_context(
457+
only_confirmed_partners=0
458+
)._filter_only_confirmed()
459+
self.assertFalse(result)
460+
461+
def test_filter_with_none_context(self):
462+
"""Test behavior with None context value"""
463+
result = self.Partner.with_context(
464+
only_confirmed_partners=None
465+
)._filter_only_confirmed()
466+
self.assertFalse(result) # bool(None) is False

0 commit comments

Comments
 (0)