|
1 | | -import pytest |
2 | 1 | from dataclasses import dataclass |
3 | 2 | from unittest.mock import MagicMock |
4 | 3 |
|
| 4 | +import pytest |
5 | 5 | from pydantic import BaseModel |
6 | 6 |
|
7 | 7 | from codesphere.core.base import CamelModel, ResourceBase, ResourceList |
@@ -91,6 +91,96 @@ class SampleModel(CamelModel): |
91 | 91 | assert model.is_private is False |
92 | 92 |
|
93 | 93 |
|
| 94 | +class TestCamelModelExport: |
| 95 | + """Tests for CamelModel export methods.""" |
| 96 | + |
| 97 | + def test_to_dict_default(self): |
| 98 | + """to_dict should export with camelCase keys by default.""" |
| 99 | + |
| 100 | + class SampleModel(CamelModel): |
| 101 | + team_id: int |
| 102 | + user_name: str |
| 103 | + |
| 104 | + model = SampleModel(team_id=1, user_name="test") |
| 105 | + result = model.to_dict() |
| 106 | + |
| 107 | + assert result == {"teamId": 1, "userName": "test"} |
| 108 | + |
| 109 | + def test_to_dict_snake_case(self): |
| 110 | + """to_dict with by_alias=False should export with snake_case keys.""" |
| 111 | + |
| 112 | + class SampleModel(CamelModel): |
| 113 | + team_id: int |
| 114 | + user_name: str |
| 115 | + |
| 116 | + model = SampleModel(team_id=1, user_name="test") |
| 117 | + result = model.to_dict(by_alias=False) |
| 118 | + |
| 119 | + assert result == {"team_id": 1, "user_name": "test"} |
| 120 | + |
| 121 | + def test_to_dict_exclude_none(self): |
| 122 | + """to_dict with exclude_none=True should omit None values.""" |
| 123 | + |
| 124 | + class SampleModel(CamelModel): |
| 125 | + team_id: int |
| 126 | + optional_field: str | None = None |
| 127 | + |
| 128 | + model = SampleModel(team_id=1, optional_field=None) |
| 129 | + result = model.to_dict(exclude_none=True) |
| 130 | + |
| 131 | + assert result == {"teamId": 1} |
| 132 | + assert "optionalField" not in result |
| 133 | + |
| 134 | + def test_to_json_default(self): |
| 135 | + """to_json should export as JSON string with camelCase keys.""" |
| 136 | + |
| 137 | + class SampleModel(CamelModel): |
| 138 | + team_id: int |
| 139 | + |
| 140 | + model = SampleModel(team_id=42) |
| 141 | + result = model.to_json() |
| 142 | + |
| 143 | + assert result == '{"teamId":42}' |
| 144 | + |
| 145 | + def test_to_json_with_indent(self): |
| 146 | + """to_json with indent should format output.""" |
| 147 | + |
| 148 | + class SampleModel(CamelModel): |
| 149 | + team_id: int |
| 150 | + |
| 151 | + model = SampleModel(team_id=42) |
| 152 | + result = model.to_json(indent=2) |
| 153 | + |
| 154 | + assert '"teamId": 42' in result |
| 155 | + assert "\n" in result |
| 156 | + |
| 157 | + def test_to_yaml_import_error(self): |
| 158 | + """to_yaml should raise ImportError if PyYAML is not installed.""" |
| 159 | + import sys |
| 160 | + from unittest.mock import patch |
| 161 | + |
| 162 | + class SampleModel(CamelModel): |
| 163 | + team_id: int |
| 164 | + |
| 165 | + model = SampleModel(team_id=1) |
| 166 | + |
| 167 | + with patch.dict(sys.modules, {"yaml": None}): |
| 168 | + # Force reimport to trigger ImportError |
| 169 | + with pytest.raises(ImportError, match="PyYAML is required"): |
| 170 | + # We need to actually make the import fail |
| 171 | + import builtins |
| 172 | + |
| 173 | + original_import = builtins.__import__ |
| 174 | + |
| 175 | + def mock_import(name, *args, **kwargs): |
| 176 | + if name == "yaml": |
| 177 | + raise ImportError("No module named 'yaml'") |
| 178 | + return original_import(name, *args, **kwargs) |
| 179 | + |
| 180 | + with patch.object(builtins, "__import__", mock_import): |
| 181 | + model.to_yaml() |
| 182 | + |
| 183 | + |
94 | 184 | class TestResourceList: |
95 | 185 | def test_create_with_list(self): |
96 | 186 | """ResourceList should be created with a list of items.""" |
@@ -150,6 +240,74 @@ class Item(BaseModel): |
150 | 240 | assert list(resource_list) == [] |
151 | 241 |
|
152 | 242 |
|
| 243 | +class TestResourceListExport: |
| 244 | + """Tests for ResourceList export methods.""" |
| 245 | + |
| 246 | + def test_to_list_default(self): |
| 247 | + """to_list should export items as list of dicts with camelCase keys.""" |
| 248 | + |
| 249 | + class Item(CamelModel): |
| 250 | + item_id: int |
| 251 | + item_name: str |
| 252 | + |
| 253 | + items = [Item(item_id=1, item_name="a"), Item(item_id=2, item_name="b")] |
| 254 | + resource_list = ResourceList[Item](root=items) |
| 255 | + result = resource_list.to_list() |
| 256 | + |
| 257 | + assert result == [ |
| 258 | + {"itemId": 1, "itemName": "a"}, |
| 259 | + {"itemId": 2, "itemName": "b"}, |
| 260 | + ] |
| 261 | + |
| 262 | + def test_to_list_snake_case(self): |
| 263 | + """to_list with by_alias=False should use snake_case keys.""" |
| 264 | + |
| 265 | + class Item(CamelModel): |
| 266 | + item_id: int |
| 267 | + |
| 268 | + items = [Item(item_id=1)] |
| 269 | + resource_list = ResourceList[Item](root=items) |
| 270 | + result = resource_list.to_list(by_alias=False) |
| 271 | + |
| 272 | + assert result == [{"item_id": 1}] |
| 273 | + |
| 274 | + def test_to_json_default(self): |
| 275 | + """to_json should export as JSON array string.""" |
| 276 | + |
| 277 | + class Item(CamelModel): |
| 278 | + item_id: int |
| 279 | + |
| 280 | + items = [Item(item_id=1), Item(item_id=2)] |
| 281 | + resource_list = ResourceList[Item](root=items) |
| 282 | + result = resource_list.to_json() |
| 283 | + |
| 284 | + assert result == '[{"itemId": 1}, {"itemId": 2}]' |
| 285 | + |
| 286 | + def test_to_json_with_indent(self): |
| 287 | + """to_json with indent should format output.""" |
| 288 | + |
| 289 | + class Item(CamelModel): |
| 290 | + item_id: int |
| 291 | + |
| 292 | + items = [Item(item_id=1)] |
| 293 | + resource_list = ResourceList[Item](root=items) |
| 294 | + result = resource_list.to_json(indent=2) |
| 295 | + |
| 296 | + assert "\n" in result |
| 297 | + assert '"itemId": 1' in result |
| 298 | + |
| 299 | + def test_to_list_empty(self): |
| 300 | + """to_list should handle empty lists.""" |
| 301 | + |
| 302 | + class Item(CamelModel): |
| 303 | + item_id: int |
| 304 | + |
| 305 | + resource_list = ResourceList[Item](root=[]) |
| 306 | + result = resource_list.to_list() |
| 307 | + |
| 308 | + assert result == [] |
| 309 | + |
| 310 | + |
153 | 311 | class TestResourceBase: |
154 | 312 | def test_initialization_with_http_client(self): |
155 | 313 | """ResourceBase should store the HTTP client.""" |
|
0 commit comments