Skip to content

Commit 8f32dd7

Browse files
test: add failing tests for replace/upsert with complex values (dicts/lists)
1 parent 58f1fca commit 8f32dd7

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

tests/test_document.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,67 @@ def test_replace_missing_raises(self):
188188
doc.replace("missing", value="bar")
189189

190190

191+
class TestDocumentReplaceComplex:
192+
def test_replace_with_dict(self):
193+
doc = Document("config:\n key: value\n")
194+
doc2 = doc.replace("config", value={"key": "new", "extra": "field"})
195+
assert doc2["config"] == {"key": "new", "extra": "field"}
196+
197+
def test_replace_with_list(self):
198+
doc = Document("repos: []\n")
199+
doc2 = doc.replace(
200+
"repos", value=[{"repo": "local", "hooks": [{"id": "my-hook"}]}]
201+
)
202+
result = doc2["repos"]
203+
assert len(result) == 1
204+
assert result[0]["repo"] == "local"
205+
assert result[0]["hooks"] == [{"id": "my-hook"}]
206+
207+
def test_replace_with_nested_dict_in_list(self):
208+
doc = Document("data:\n - old\n")
209+
doc2 = doc.replace("data", value=[{"a": {"b": [1, 2, 3]}}])
210+
assert doc2["data"] == [{"a": {"b": [1, 2, 3]}}]
211+
212+
def test_replace_complex_preserves_other_keys(self):
213+
doc = Document("name: foo\nconfig:\n key: value\nversion: 1\n")
214+
doc2 = doc.replace("config", value={"new_key": "new_val"})
215+
assert doc2["name"] == "foo"
216+
assert doc2["version"] == 1
217+
assert doc2["config"] == {"new_key": "new_val"}
218+
219+
def test_replace_complex_preserves_comments_on_other_keys(self):
220+
doc = Document("name: foo # keep this\nconfig: old\n")
221+
doc2 = doc.replace("config", value={"a": 1})
222+
assert "# keep this" in doc2.source
223+
224+
def test_replace_nested_key_with_dict(self):
225+
doc = Document("outer:\n inner: old\n")
226+
doc2 = doc.replace("outer", "inner", value={"a": 1, "b": 2})
227+
assert doc2["outer", "inner"] == {"a": 1, "b": 2}
228+
229+
def test_replace_complex_with_complex(self):
230+
doc = Document("config:\n a: 1\n b: 2\n")
231+
doc2 = doc.replace("config", value={"x": 10, "y": 20})
232+
assert doc2["config"] == {"x": 10, "y": 20}
233+
234+
def test_replace_scalar_with_list(self):
235+
doc = Document("items: none\n")
236+
doc2 = doc.replace("items", value=["a", "b", "c"])
237+
assert doc2["items"] == ["a", "b", "c"]
238+
239+
def test_replace_deeply_nested_with_dict(self):
240+
doc = Document("a:\n b:\n c: old\n")
241+
doc2 = doc.replace("a", "b", "c", value={"deep": "value"})
242+
assert doc2["a", "b", "c"] == {"deep": "value"}
243+
244+
def test_replace_comment_relocation(self):
245+
doc = Document("repos: [] # managed by tool\n")
246+
doc2 = doc.replace("repos", value=[{"repo": "local"}])
247+
assert "# managed by tool" in doc2.source
248+
result = doc2["repos"]
249+
assert result == [{"repo": "local"}]
250+
251+
191252
class TestDocumentAdd:
192253
def test_add_key(self):
193254
doc = Document("name: foo")
@@ -218,6 +279,31 @@ def test_upsert_missing(self):
218279
assert doc2["age"] == 30
219280

220281

282+
class TestDocumentUpsertComplex:
283+
def test_upsert_existing_with_dict(self):
284+
doc = Document("config:\n key: value\n")
285+
doc2 = doc.upsert("config", value={"key": "new", "extra": "field"})
286+
assert doc2["config"] == {"key": "new", "extra": "field"}
287+
288+
def test_upsert_existing_with_list(self):
289+
doc = Document("repos: []\n")
290+
doc2 = doc.upsert("repos", value=[{"repo": "local"}])
291+
assert doc2["repos"] == [{"repo": "local"}]
292+
293+
def test_upsert_missing_with_dict(self):
294+
"""This already works via Op.add — verify it stays working."""
295+
doc = Document("name: foo\n")
296+
doc2 = doc.upsert("config", value={"a": 1})
297+
assert doc2["config"] == {"a": 1}
298+
assert doc2["name"] == "foo"
299+
300+
def test_upsert_missing_with_list(self):
301+
"""This already works via Op.add — verify it stays working."""
302+
doc = Document("name: foo\n")
303+
doc2 = doc.upsert("items", value=["a", "b"])
304+
assert doc2["items"] == ["a", "b"]
305+
306+
221307
class TestDocumentRemove:
222308
def test_remove_key(self):
223309
doc = Document("name: foo\nage: 30")

0 commit comments

Comments
 (0)