This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-gcs-write-preconditions-v2-main
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 3770d2e54df8d9330959100c820d0abfd8a2e882
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 30 21:55:06 2026 +0100

    test(bindings/python): match conditional writes to the selected identity
    
    Use one stat snapshot to check a conflicting equality field, perform a 
guarded update, and reject a stale write. Use a different opaque value for each 
conflict and check the unsupported ETag separately when the selected identity 
is a version.
---
 bindings/python/tests/test_write_conditional.py | 103 +++++++++++++++---------
 1 file changed, 64 insertions(+), 39 deletions(-)

diff --git a/bindings/python/tests/test_write_conditional.py 
b/bindings/python/tests/test_write_conditional.py
index c5781fb1b6..c4000662d6 100644
--- a/bindings/python/tests/test_write_conditional.py
+++ b/bindings/python/tests/test_write_conditional.py
@@ -24,10 +24,15 @@ from opendal.exceptions import ConditionNotMatch, 
ConfigInvalid, Unsupported
 VERSION_CONDITIONS = ("if_version_match", "if_version_not_match")
 
 
-def has_identity(meta):
-    return meta.version is not None or (
-        meta.etag is not None and not meta.etag.startswith('W/"')
-    )
+def conflicting_write_condition(meta, cap):
+    version = meta.version
+    etag = meta.etag
+    has_strong_etag = etag is not None and not etag.startswith('W/"')
+    if version is not None and (cap.write_with_if_version_match or not 
has_strong_etag):
+        return "if_version_match", "2" if version == "1" else "1"
+    if has_strong_etag:
+        return "if_match", '"another"' if etag == '"other"' else '"other"'
+    return None
 
 
 @pytest.mark.need_capability("write", "stat", "write_with_if_match")
@@ -150,28 +155,37 @@ async def 
test_async_write_with_version_conditions(async_operator):
 def test_write_if_not_changed(operator):
     path = f"test_write_if_not_changed_{uuid4()}.txt"
     operator.write(path, b"first")
-    stale = operator.stat(path)
-    operator.write(path, b"second")
     try:
+        observed = operator.stat(path)
         cap = operator.capability()
-        if not has_identity(stale):
+        conflict = conflicting_write_condition(observed, cap)
+        if conflict is None:
             with pytest.raises(ConfigInvalid):
-                operator.write(path, b"rejected", if_not_changed=stale)
+                operator.write(path, b"rejected", if_not_changed=observed)
             with pytest.raises(ConfigInvalid):
-                operator.open(path, "wb", if_not_changed=stale)
-        elif not (cap.write_with_if_match or cap.write_with_if_version_match):
-            with pytest.raises(Unsupported):
-                operator.write(path, b"rejected", if_not_changed=stale)
-        else:
-            with pytest.raises(ConditionNotMatch):
-                operator.write(path, b"rejected", if_not_changed=stale)
-            fresh = operator.stat(path)
-            with pytest.raises(ConditionNotMatch, match="conflicts"):
-                operator.open(path, "wb", if_not_changed=fresh, 
if_match='"other"')
-            with operator.open(path, "wb", if_not_changed=fresh) as file:
-                file.write(b"third")
-            assert operator.read(path) == b"third"
-        assert operator.read(path) != b"rejected"
+                operator.open(path, "wb", if_not_changed=observed)
+            assert operator.read(path) == b"first"
+            return
+
+        name, value = conflict
+        if not getattr(cap, f"write_with_{name}"):
+            with pytest.raises(Unsupported, match=name):
+                operator.write(path, b"rejected", if_not_changed=observed)
+            assert operator.read(path) == b"first"
+            return
+
+        with pytest.raises(ConditionNotMatch):
+            operator.open(path, "wb", if_not_changed=observed, **{name: value})
+        if name == "if_version_match" and not cap.write_with_if_match:
+            with pytest.raises(Unsupported, match="if_match"):
+                operator.open(path, "wb", if_not_changed=observed, 
if_match='"other"')
+        assert operator.read(path) == b"first"
+
+        with operator.open(path, "wb", if_not_changed=observed) as file:
+            file.write(b"second")
+        with pytest.raises(ConditionNotMatch):
+            operator.write(path, b"rejected", if_not_changed=observed)
+        assert operator.read(path) == b"second"
     finally:
         operator.delete(path)
 
@@ -181,28 +195,39 @@ def test_write_if_not_changed(operator):
 async def test_async_write_if_not_changed(async_operator):
     path = f"test_async_write_if_not_changed_{uuid4()}.txt"
     await async_operator.write(path, b"first")
-    stale = await async_operator.stat(path)
-    await async_operator.write(path, b"second")
     try:
+        observed = await async_operator.stat(path)
         cap = async_operator.capability()
-        if not has_identity(stale):
+        conflict = conflicting_write_condition(observed, cap)
+        if conflict is None:
             with pytest.raises(ConfigInvalid):
-                await async_operator.write(path, b"rejected", 
if_not_changed=stale)
+                await async_operator.write(path, b"rejected", 
if_not_changed=observed)
             with pytest.raises(ConfigInvalid):
-                await async_operator.open(path, "wb", if_not_changed=stale)
-        elif not (cap.write_with_if_match or cap.write_with_if_version_match):
-            with pytest.raises(Unsupported):
-                await async_operator.write(path, b"rejected", 
if_not_changed=stale)
-        else:
-            with pytest.raises(ConditionNotMatch):
-                await async_operator.write(path, b"rejected", 
if_not_changed=stale)
-            fresh = await async_operator.stat(path)
-            with pytest.raises(ConditionNotMatch, match="conflicts"):
+                await async_operator.open(path, "wb", if_not_changed=observed)
+            assert await async_operator.read(path) == b"first"
+            return
+
+        name, value = conflict
+        if not getattr(cap, f"write_with_{name}"):
+            with pytest.raises(Unsupported, match=name):
+                await async_operator.write(path, b"rejected", 
if_not_changed=observed)
+            assert await async_operator.read(path) == b"first"
+            return
+
+        with pytest.raises(ConditionNotMatch, match="conflict"):
+            await async_operator.write(
+                path, b"rejected", if_not_changed=observed, **{name: value}
+            )
+        if name == "if_version_match" and not cap.write_with_if_match:
+            with pytest.raises(Unsupported, match="if_match"):
                 await async_operator.write(
-                    path, b"rejected", if_not_changed=fresh, if_match='"other"'
+                    path, b"rejected", if_not_changed=observed, 
if_match='"other"'
                 )
-            await async_operator.write(path, b"third", if_not_changed=fresh)
-            assert await async_operator.read(path) == b"third"
-        assert await async_operator.read(path) != b"rejected"
+        assert await async_operator.read(path) == b"first"
+
+        await async_operator.write(path, b"second", if_not_changed=observed)
+        with pytest.raises(ConditionNotMatch):
+            await async_operator.write(path, b"rejected", 
if_not_changed=observed)
+        assert await async_operator.read(path) == b"second"
     finally:
         await async_operator.delete(path)

Reply via email to