URB-3649 Fix TypeError in selecte2widget - #578
Conversation
📝 WalkthroughWalkthroughThis PR prevents a ChangesSelect2Widget List Value TypeError Fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
| widget = Select2Widget() | ||
| field = Mock() | ||
| field.__name__ = "test_field" | ||
| field.getAccessor.return_value.return_value = ["value1", "value2"] | ||
|
|
||
| with patch( | ||
| "Products.urban.widget.select2widget.resolve_vocabulary", | ||
| return_value="value1, value2", | ||
| ): | ||
| result = widget.view(Mock(), field, Mock()) | ||
| self.assertEqual(result, "value1, value2") |
There was a problem hiding this comment.
There is too much complexity in this test, here is an example of how it should be done:
context = Mock()
context.test_field = ["value1", "value2"]
field = Mock()
field.__name__ = "test_field"
with patch(
"Products.urban.widget.select2widget.resolve_vocabulary",
return_value="value1, value2",
):
result = Select2Widget().view(context, field, Mock())
self.assertEqual(result, "value1, value2")
Also:
- you should test from without mocking
Products.urban.widget.select2widget.resolve_vocabulary - the test file should be under
Products/urban/widget/tests/
| if isinstance(value, (list, tuple)): | ||
| values = list(value) | ||
| else: | ||
| values = [value] |
There was a problem hiding this comment.
This is correct but should be in the other way to ensure that even if we get a iterable value, we get a string (first value).
resolve_vocabulary should be adapted to accept either a string or a iterable.
| @@ -0,0 +1,2 @@ | |||
| Fix TypeError when opening dossiers | |||
There was a problem hiding this comment.
Please improve the changelog to express exactly what was the issue
This PR fixes a
TypeErrorinSelect2Widget.view()when opening dossiers and adds a regression test to ensure the fix continues to work correctly.