|
| 1 | +"""Tests for the AnswerLikes null fix hook.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +from glean.api_client import models |
| 6 | +from glean.api_client._hooks.answer_likes_null_fix_hook import AnswerLikesNullFixHook |
| 7 | +from glean.api_client.httpclient import HttpClient |
| 8 | +from glean.api_client.utils import serializers |
| 9 | + |
| 10 | + |
| 11 | +class TestAnswerLikesNullFixHook: |
| 12 | + """Test cases for the AnswerLikes null fix hook.""" |
| 13 | + |
| 14 | + def setup_method(self): |
| 15 | + """Set up test fixtures.""" |
| 16 | + self.hook = AnswerLikesNullFixHook() |
| 17 | + self.mock_client = Mock(spec=HttpClient) |
| 18 | + |
| 19 | + def test_sdk_init_returns_unchanged_params(self): |
| 20 | + """SDK init should not change the base URL or client.""" |
| 21 | + base_url = "https://api.example.com" |
| 22 | + |
| 23 | + result_url, result_client = self.hook.sdk_init(base_url, self.mock_client) |
| 24 | + |
| 25 | + assert result_url == base_url |
| 26 | + assert result_client == self.mock_client |
| 27 | + |
| 28 | + def test_patch_unmarshal_is_idempotent(self, monkeypatch): |
| 29 | + """The unmarshal patch should only be applied once.""" |
| 30 | + monkeypatch.setattr(serializers, "unmarshal", serializers.unmarshal) |
| 31 | + |
| 32 | + self.hook._patch_unmarshal() |
| 33 | + first = serializers.unmarshal |
| 34 | + |
| 35 | + self.hook._patch_unmarshal() |
| 36 | + second = serializers.unmarshal |
| 37 | + |
| 38 | + assert first is second |
| 39 | + assert getattr(first, "_glean_answer_likes_null_fix", False) is True |
| 40 | + |
| 41 | + def test_normalize_answer_likes_nulls_updates_nested_payloads(self): |
| 42 | + """Nested AnswerLikes payloads should convert null likedBy values to empty lists.""" |
| 43 | + payload = { |
| 44 | + "message": { |
| 45 | + "likes": { |
| 46 | + "likedBy": None, |
| 47 | + "likedByUser": False, |
| 48 | + "numLikes": 0, |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + normalized = self.hook._normalize_answer_likes_nulls(payload) |
| 54 | + |
| 55 | + assert normalized is payload |
| 56 | + assert payload["message"]["likes"]["likedBy"] == [] |
| 57 | + |
| 58 | + def test_normalize_answer_likes_nulls_ignores_non_matching_payloads(self): |
| 59 | + """Unrelated payloads should not be modified.""" |
| 60 | + payload = {"likedBy": None} |
| 61 | + |
| 62 | + normalized = self.hook._normalize_answer_likes_nulls(payload) |
| 63 | + |
| 64 | + assert normalized is payload |
| 65 | + assert payload["likedBy"] is None |
| 66 | + |
| 67 | + def test_patched_unmarshal_normalizes_null_liked_by(self, monkeypatch): |
| 68 | + """Patched unmarshal should make null likedBy payloads validate cleanly.""" |
| 69 | + monkeypatch.setattr(serializers, "unmarshal", serializers.unmarshal) |
| 70 | + self.hook._patch_unmarshal() |
| 71 | + |
| 72 | + likes = serializers.unmarshal( |
| 73 | + {"likedBy": None, "likedByUser": False, "numLikes": 0}, |
| 74 | + models.AnswerLikes, |
| 75 | + ) |
| 76 | + |
| 77 | + assert likes.liked_by == [] |
0 commit comments