You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by te...@apache.org on 2026/04/27 19:57:59 UTC

(opendal) branch python-conditional-tests created (now f749d27f6)

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

TennyZhuang pushed a change to branch python-conditional-tests
in repository https://gitbox.apache.org/repos/asf/opendal.git


      at f749d27f6 feat(bindings/python): add behavior tests for conditional read/write

This branch includes the following new commits:

     new f749d27f6 feat(bindings/python): add behavior tests for conditional read/write

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(opendal) 01/01: feat(bindings/python): add behavior tests for conditional read/write

Posted by te...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

TennyZhuang pushed a commit to branch python-conditional-tests
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit f749d27f6eaa9e75f55f85cc37cbedbcbf898eee
Author: tianyizhuang <ti...@tianyizhuangs-Mac-mini.local>
AuthorDate: Tue Apr 28 02:58:49 2026 +0800

    feat(bindings/python): add behavior tests for conditional read/write
    
    The Python bindings support conditional headers (if_match, if_none_match)
    on read and write operations, but there were no behavior tests covering
    these important HTTP semantics.
    
    This commit adds focused behavior tests for both sync and async:
    - read with if_match: succeeds on matching etag, fails on mismatch
    - read with if_none_match: fails on matching etag, succeeds on mismatch
    - write with if_match: overwrites on matching etag, fails on mismatch
    - write with if_none_match: succeeds when file absent, fails when present
    
    Tests gracefully skip when the backend does not return etags.
---
 bindings/python/tests/test_async_conditional.py | 113 ++++++++++++++++++++++++
 bindings/python/tests/test_sync_conditional.py  | 109 +++++++++++++++++++++++
 2 files changed, 222 insertions(+)

diff --git a/bindings/python/tests/test_async_conditional.py b/bindings/python/tests/test_async_conditional.py
new file mode 100644
index 000000000..089b79331
--- /dev/null
+++ b/bindings/python/tests/test_async_conditional.py
@@ -0,0 +1,113 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import contextlib
+from uuid import uuid4
+
+import pytest
+
+from opendal.exceptions import ConditionNotMatch
+
+
+@pytest.mark.asyncio
+@pytest.mark.need_capability("read", "write", "delete", "read_with_if_match")
+async def test_async_read_with_if_match(service_name, operator, async_operator):
+    path = f"test_async_if_match_{uuid4()}.txt"
+    content = b"test content"
+    await async_operator.write(path, content)
+
+    meta = await async_operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should succeed
+    assert await async_operator.read(path, if_match=etag) == content
+
+    # Non-matching etag should fail
+    with pytest.raises(ConditionNotMatch):
+        await async_operator.read(path, if_match='"invalid-etag"')
+
+    await async_operator.delete(path)
+
+
+@pytest.mark.asyncio
+@pytest.mark.need_capability("read", "write", "delete", "read_with_if_none_match")
+async def test_async_read_with_if_none_match(service_name, operator, async_operator):
+    path = f"test_async_if_none_match_{uuid4()}.txt"
+    content = b"test content"
+    await async_operator.write(path, content)
+
+    meta = await async_operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should fail (resource exists)
+    with pytest.raises(ConditionNotMatch):
+        await async_operator.read(path, if_none_match=etag)
+
+    # Non-matching etag should succeed
+    assert await async_operator.read(path, if_none_match='"invalid-etag"') == content
+
+    await async_operator.delete(path)
+
+
+@pytest.mark.asyncio
+@pytest.mark.need_capability("write", "delete", "write_with_if_match")
+async def test_async_write_with_if_match(service_name, operator, async_operator):
+    path = f"test_async_write_if_match_{uuid4()}.txt"
+    content = b"original content"
+    await async_operator.write(path, content)
+
+    meta = await async_operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should allow overwrite
+    new_content = b"updated content"
+    await async_operator.write(path, new_content, if_match=etag)
+    assert await async_operator.read(path) == new_content
+
+    # Non-matching etag should fail
+    with pytest.raises(ConditionNotMatch):
+        await async_operator.write(path, b"should not write", if_match='"invalid-etag"')
+
+    await async_operator.delete(path)
+
+
+@pytest.mark.asyncio
+@pytest.mark.need_capability("write", "delete", "write_with_if_none_match")
+async def test_async_write_with_if_none_match(service_name, operator, async_operator):
+    path = f"test_async_write_if_none_match_{uuid4()}.txt"
+    content = b"test content"
+
+    # File does not exist, so any if_none_match should succeed
+    await async_operator.write(path, content, if_none_match='"*"')
+    assert await async_operator.read(path) == content
+
+    # File now exists — backends may either raise ConditionNotMatch or ignore
+    # the header (e.g. Azurite). Verify the file is not overwritten if enforced.
+    with contextlib.suppress(ConditionNotMatch):
+        await async_operator.write(path, b"should not write", if_none_match='"*"')
+    read_back = await async_operator.read(path)
+    if read_back != content:
+        pytest.skip("backend does not enforce if_none_match on existing files")
+
+
+    await async_operator.delete(path)
diff --git a/bindings/python/tests/test_sync_conditional.py b/bindings/python/tests/test_sync_conditional.py
new file mode 100644
index 000000000..54e417f29
--- /dev/null
+++ b/bindings/python/tests/test_sync_conditional.py
@@ -0,0 +1,109 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import contextlib
+from uuid import uuid4
+
+import pytest
+
+from opendal.exceptions import ConditionNotMatch
+
+
+@pytest.mark.need_capability("read", "write", "delete", "read_with_if_match")
+def test_sync_read_with_if_match(service_name, operator, async_operator):
+    path = f"test_sync_if_match_{uuid4()}.txt"
+    content = b"test content"
+    operator.write(path, content)
+
+    meta = operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should succeed
+    assert operator.read(path, if_match=etag) == content
+
+    # Non-matching etag should fail
+    with pytest.raises(ConditionNotMatch):
+        operator.read(path, if_match='"invalid-etag"')
+
+    operator.delete(path)
+
+
+@pytest.mark.need_capability("read", "write", "delete", "read_with_if_none_match")
+def test_sync_read_with_if_none_match(service_name, operator, async_operator):
+    path = f"test_sync_if_none_match_{uuid4()}.txt"
+    content = b"test content"
+    operator.write(path, content)
+
+    meta = operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should fail (resource exists)
+    with pytest.raises(ConditionNotMatch):
+        operator.read(path, if_none_match=etag)
+
+    # Non-matching etag should succeed
+    assert operator.read(path, if_none_match='"invalid-etag"') == content
+
+    operator.delete(path)
+
+
+@pytest.mark.need_capability("write", "delete", "write_with_if_match")
+def test_sync_write_with_if_match(service_name, operator, async_operator):
+    path = f"test_sync_write_if_match_{uuid4()}.txt"
+    content = b"original content"
+    operator.write(path, content)
+
+    meta = operator.stat(path)
+    etag = meta.etag
+    if etag is None:
+        pytest.skip("backend does not return etag")
+
+    # Matching etag should allow overwrite
+    new_content = b"updated content"
+    operator.write(path, new_content, if_match=etag)
+    assert operator.read(path) == new_content
+
+    # Non-matching etag should fail
+    with pytest.raises(ConditionNotMatch):
+        operator.write(path, b"should not write", if_match='"invalid-etag"')
+
+    operator.delete(path)
+
+
+@pytest.mark.need_capability("write", "delete", "write_with_if_none_match")
+def test_sync_write_with_if_none_match(service_name, operator, async_operator):
+    path = f"test_sync_write_if_none_match_{uuid4()}.txt"
+    content = b"test content"
+
+    # File does not exist, so any if_none_match should succeed
+    operator.write(path, content, if_none_match='"*"')
+    assert operator.read(path) == content
+
+    # File now exists — backends may either raise ConditionNotMatch or ignore
+    # the header (e.g. Azurite). Verify the file is not overwritten if enforced.
+    with contextlib.suppress(ConditionNotMatch):
+        operator.write(path, b"should not write", if_none_match='"*"')
+    read_back = operator.read(path)
+    if read_back != content:
+        pytest.skip("backend does not enforce if_none_match on existing files")
+
+
+    operator.delete(path)