|
| 1 | +""" |
| 2 | +Unit tests for GlobalField.fetch method in contentstack.globalfields |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from contentstack.globalfields import GlobalField |
| 8 | +from urllib.parse import urlencode |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_http_instance(): |
| 13 | + """ |
| 14 | + Fixture to provide a mock http_instance with required attributes. |
| 15 | + """ |
| 16 | + mock = MagicMock() |
| 17 | + mock.endpoint = "https://api.contentstack.io/v3" |
| 18 | + mock.headers = {"environment": "test_env"} |
| 19 | + mock.get = MagicMock(return_value={"global_field": "data"}) |
| 20 | + return mock |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def global_field_uid(): |
| 25 | + """ |
| 26 | + Fixture to provide a sample global_field_uid. |
| 27 | + """ |
| 28 | + return "sample_uid" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def global_field(mock_http_instance, global_field_uid): |
| 33 | + """ |
| 34 | + Fixture to provide a GlobalField instance with a mock http_instance and uid. |
| 35 | + """ |
| 36 | + return GlobalField(mock_http_instance, global_field_uid) |
| 37 | + |
| 38 | + |
| 39 | +class TestGlobalFieldFetch: |
| 40 | + # ------------------- Happy Path Tests ------------------- |
| 41 | + |
| 42 | + def test_fetch_returns_expected_result(self, global_field): |
| 43 | + """ |
| 44 | + Test that fetch returns the result from http_instance.get with correct URL and params. |
| 45 | + """ |
| 46 | + result = global_field.fetch() |
| 47 | + assert result == {"global_field": "data"} |
| 48 | + assert global_field.local_param["environment"] == "test_env" |
| 49 | + expected_params = urlencode({"environment": "test_env"}) |
| 50 | + expected_url = f"https://api.contentstack.io/v3/global_fields/sample_uid?{expected_params}" |
| 51 | + global_field.http_instance.get.assert_called_once_with(expected_url) |
| 52 | + |
| 53 | + def test_fetch_with_different_environment(self, mock_http_instance, global_field_uid): |
| 54 | + """ |
| 55 | + Test fetch with a different environment value in headers. |
| 56 | + """ |
| 57 | + mock_http_instance.headers["environment"] = "prod_env" |
| 58 | + gf = GlobalField(mock_http_instance, global_field_uid) |
| 59 | + result = gf.fetch() |
| 60 | + assert result == {"global_field": "data"} |
| 61 | + expected_params = urlencode({"environment": "prod_env"}) |
| 62 | + expected_url = f"https://api.contentstack.io/v3/global_fields/sample_uid?{expected_params}" |
| 63 | + mock_http_instance.get.assert_called_once_with(expected_url) |
| 64 | + |
| 65 | + def test_fetch_preserves_existing_local_param(self, global_field): |
| 66 | + """ |
| 67 | + Test that fetch overwrites only the 'environment' key in local_param, preserving others. |
| 68 | + """ |
| 69 | + global_field.local_param = {"foo": "bar"} |
| 70 | + result = global_field.fetch() |
| 71 | + assert result == {"global_field": "data"} |
| 72 | + assert global_field.local_param["foo"] == "bar" |
| 73 | + assert global_field.local_param["environment"] == "test_env" |
| 74 | + expected_params = urlencode({"foo": "bar", "environment": "test_env"}) |
| 75 | + expected_url = f"https://api.contentstack.io/v3/global_fields/sample_uid?{expected_params}" |
| 76 | + global_field.http_instance.get.assert_called_once_with(expected_url) |
| 77 | + |
| 78 | + # ------------------- Edge Case Tests ------------------- |
| 79 | + |
| 80 | + def test_fetch_raises_keyerror_when_uid_is_none(self, mock_http_instance): |
| 81 | + """ |
| 82 | + Test that fetch raises KeyError if global_field_uid is None. |
| 83 | + """ |
| 84 | + gf = GlobalField(mock_http_instance, None) |
| 85 | + with pytest.raises(KeyError, match="global_field_uid can not be None"): |
| 86 | + gf.fetch() |
| 87 | + |
| 88 | + def test_fetch_raises_keyerror_when_uid_is_explicitly_set_to_none(self, mock_http_instance): |
| 89 | + """ |
| 90 | + Test that fetch raises KeyError if global_field_uid is explicitly set to None after init. |
| 91 | + """ |
| 92 | + gf = GlobalField(mock_http_instance, "not_none") |
| 93 | + gf._GlobalField__global_field_uid = None # forcibly set to None |
| 94 | + with pytest.raises(KeyError, match="global_field_uid can not be None"): |
| 95 | + gf.fetch() |
| 96 | + |
| 97 | + def test_fetch_handles_special_characters_in_params(self, global_field): |
| 98 | + """ |
| 99 | + Test that fetch correctly encodes special characters in local_param. |
| 100 | + """ |
| 101 | + global_field.local_param = {"foo": "bar baz", "qux": "a&b"} |
| 102 | + result = global_field.fetch() |
| 103 | + assert result == {"global_field": "data"} |
| 104 | + expected_params = urlencode({"foo": "bar baz", "qux": "a&b", "environment": "test_env"}) |
| 105 | + expected_url = f"https://api.contentstack.io/v3/global_fields/sample_uid?{expected_params}" |
| 106 | + global_field.http_instance.get.assert_called_once_with(expected_url) |
| 107 | + |
| 108 | + def test_fetch_raises_keyerror_if_environment_header_missing(self, mock_http_instance, global_field_uid): |
| 109 | + """ |
| 110 | + Test that fetch raises KeyError if 'environment' is missing from http_instance.headers. |
| 111 | + """ |
| 112 | + del mock_http_instance.headers["environment"] |
| 113 | + gf = GlobalField(mock_http_instance, global_field_uid) |
| 114 | + with pytest.raises(KeyError): |
| 115 | + gf.fetch() |
| 116 | + |
| 117 | + def test_fetch_propagates_http_instance_get_exception(self, global_field): |
| 118 | + """ |
| 119 | + Test that fetch propagates exceptions raised by http_instance.get. |
| 120 | + """ |
| 121 | + global_field.http_instance.get.side_effect = RuntimeError("Network error") |
| 122 | + with pytest.raises(RuntimeError, match="Network error"): |
| 123 | + global_field.fetch() |
| 124 | + |
0 commit comments