-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(binding/python): new behavior testing for python (#3245)
* test(bindings/python): New behavior testing. 1. Add pytest 1. Add operator fixture 1. Add `test_sync_write` 1. Update `service_test_s3.yaml` * refactor(bindings/python): Rename file 1. Rename `test_rw.py` to `test_write.py` * chore(bindings/python): Add license headers 1. Add license headers to `test_write.py` * test(bindings/python): Add more cases. * test(bindings/python): Add redis workflow * test(bindings/python): Remove redis test. it seems that this Python binding dose not currently support redis feature. * test(bindings/python): Load `.env` file before run tests. * test(bindings/python): Remove `random_file` fixture * refactor(bindings/python): Use `os.urandom` to create bytes * test(bindings/python): Update `operator` fixture 1. Find service name by match `OPENDAL_XXX_TEST` * test(bindings/python): Switch test to xunit-style 1. Switch test to xunit-style. 1. Remove try...except block. 1. Add random part in every file name or dir name. 1. Use `pytest -k TestXXX` select service. 1. Use random size between 1..1024 when generate random file. --------- Co-authored-by: Xuanwo <[email protected]>
- Loading branch information
Showing
5 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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. | ||
|
||
from dotenv import load_dotenv | ||
|
||
|
||
load_dotenv() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# 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 os | ||
from abc import ABC | ||
from uuid import uuid4 | ||
from random import randint | ||
|
||
import opendal | ||
import pytest | ||
|
||
|
||
class AbstractTestSuite(ABC): | ||
service_name = "" | ||
|
||
def setup_method(self): | ||
# Read arguments from envs. | ||
prefix = f"opendal_{self.service_name}_" | ||
self.config = {} | ||
for key in os.environ.keys(): | ||
if key.lower().startswith(prefix): | ||
self.config[key[len(prefix) :].lower()] = os.environ.get(key) | ||
|
||
# Check if current test be enabled. | ||
test_flag = self.config.get("test", "") | ||
if test_flag != "on" and test_flag != "true": | ||
raise ValueError(f"Service {self.service_name} test is not enabled.") | ||
|
||
self.operator = opendal.Operator(self.service_name, **self.config) | ||
|
||
def test_sync_read(self): | ||
size = randint(1, 1024) | ||
filename = f"random_file_{str(uuid4())}" | ||
content = os.urandom(size) | ||
self.operator.write(filename, content) | ||
|
||
read_content = self.operator.read(filename) | ||
assert read_content is not None | ||
assert read_content == content | ||
|
||
def test_sync_read_stat(self): | ||
size = randint(1, 1024) | ||
filename = f"random_file_{str(uuid4())}" | ||
content = os.urandom(size) | ||
self.operator.write(filename, content) | ||
|
||
metadata = self.operator.stat(filename) | ||
assert metadata is not None | ||
assert metadata.content_length == len(content) | ||
assert metadata.mode.is_file() | ||
|
||
def test_sync_read_not_exists(self): | ||
with pytest.raises(FileNotFoundError): | ||
self.operator.read(str(uuid4())) | ||
|
||
def test_sync_write(self): | ||
size = randint(1, 1024) | ||
filename = f"test_file_{str(uuid4())}.txt" | ||
content = os.urandom(size) | ||
size = len(content) | ||
self.operator.write(filename, content) | ||
metadata = self.operator.stat(filename) | ||
assert metadata is not None | ||
assert metadata.mode.is_file() | ||
assert metadata.content_length == size | ||
|
||
self.operator.delete(filename) | ||
|
||
def test_sync_write_with_non_ascii_name(self): | ||
size = randint(1, 1024) | ||
filename = f"❌😱中文_{str(uuid4())}.test" | ||
content = os.urandom(size) | ||
size = len(content) | ||
self.operator.write(filename, content) | ||
metadata = self.operator.stat(filename) | ||
assert metadata is not None | ||
assert metadata.mode.is_file() | ||
assert metadata.content_length == size | ||
|
||
self.operator.delete(filename) | ||
|
||
def test_sync_create_dir(self): | ||
path = f"test_dir_{str(uuid4())}/" | ||
self.operator.create_dir(path) | ||
metadata = self.operator.stat(path) | ||
assert metadata is not None | ||
assert metadata.mode.is_dir() | ||
|
||
self.operator.delete(path) | ||
|
||
def test_sync_delete(self): | ||
size = randint(1, 1024) | ||
filename = f"test_file_{str(uuid4())}.txt" | ||
content = os.urandom(size) | ||
size = len(content) | ||
self.operator.write(filename, content) | ||
self.operator.delete(filename) | ||
with pytest.raises(FileNotFoundError): | ||
self.operator.stat(filename) | ||
|
||
|
||
class TestS3(AbstractTestSuite): | ||
service_name = "s3" | ||
|
||
|
||
class TestFS(AbstractTestSuite): | ||
service_name = "fs" | ||
|
||
|
||
class TestMemory(AbstractTestSuite): | ||
service_name = "memory" |