diff options
| author | CoprDistGit <infra@openeuler.org> | 2023-04-11 16:57:06 +0000 |
|---|---|---|
| committer | CoprDistGit <infra@openeuler.org> | 2023-04-11 16:57:06 +0000 |
| commit | d10e46afbcf31c1741ed89e290e84931fda5d61e (patch) | |
| tree | c4d7c5058d5675858153af99ea6ba01767e1c824 | |
| parent | a251c54250caebea825557bb4c2bd69526a8316d (diff) | |
automatic import of python-pytest-schema
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | python-pytest-schema.spec | 596 | ||||
| -rw-r--r-- | sources | 1 |
3 files changed, 598 insertions, 0 deletions
@@ -0,0 +1 @@ +/pytest-schema-0.1.1.tar.gz diff --git a/python-pytest-schema.spec b/python-pytest-schema.spec new file mode 100644 index 0000000..b2e1d53 --- /dev/null +++ b/python-pytest-schema.spec @@ -0,0 +1,596 @@ +%global _empty_manifest_terminate_build 0 +Name: python-pytest-schema +Version: 0.1.1 +Release: 1 +Summary: 👍 Validate return values against a schema-like object in testing +License: MIT +URL: https://github.com/codedawi/pytest-schema +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/cf/59/bd7eab0c50bc5d883ce789f590478bd526f14585fbe35c8aad86bd731592/pytest-schema-0.1.1.tar.gz +BuildArch: noarch + +Requires: python3-pytest +Requires: python3-schema + +%description +# pytest-schema + + [](https://pypi.python.org/pypi/pytest-schema/) [](https://pypi.python.org/pypi/pytest-schema/) [](https://github.com/psf/black) + + +👍 Validate return values against a schema-like object in testing + +[`keleshev/schema`](https://github.com/keleshev/schema) is a library for validating Python data structures, such as +those obtained from config-files, forms, external services or +command-line parsing, converted from JSON/YAML (or something else) to Python data-types. + +## Install + +```bash +pip install pytest-schema +``` + +## Basic Example + +Here is a quick example of using **`schema`**: +```python +from pytest_schema import schema, exact, like + +article_v1 = { + "id": int, + "title": str, + "completed": bool, + "engagement": { + "viewer": list, + "rating": float, + }, + "metadata": dict +} + +def test_article_v1_endpoint(test_client): + """ + Test calling v1 endpoint and validating the response + is in the correctly/expected format. + """ + response_v1 = test_client.get("/api/v1/article/1") + assert exact(article_v1) == response_v1 + # Same as: + # assert schema(article_v1) == response_v1 + +article_v2 = { + **article_v1, + "someNewField": int +} + +def test_article_v2_endpoint(test_client): + """ + Test calling v2 endpoint is backwards compatible with v1 + """ + response_v2 = test_client.get("/api/v2/article/1") + + assert like(article_v1) == value + +``` +## Full Example + +Here is a more complex example of using **`schema`**: + +``` python +from pytest_schema import schema, And, Enum, Optional, Or, Regex + +# single user schema +user = { + # id must be int + "id": int, + # name must be type str + "name": str, + # email must be type str or nullable + "description": Or(None, str), + # email valid str format + "email": Regex(r".*?@.*?\.[A-Za-z]{2,6}"), + # age converted to int then validated gt 18 lt 99 and must be type str + "age": And(int, lambda n: 18 <= n <= 99), + # gender key is optional but must be str + Optional("gender"): str, + # role of enum values + "role": Enum(["user", "super-user", "admin"]), + # list of ids ref friends + "friends": [ int ], + # nested dict to valid as address + "address": { + "street": str, + Optional("street2"): str, + "city": str, + "state": And(str, lambda s: len(s) == 2), + "zipcode": str, + } + +} + +# multiple users schema +users = [ user ] + +def test_users_endpoint(): + """ + Test calling a users endpoint and validating its + response of users info is correct format. + """ + response = [ + # ✅ Valid + { + "id": 2, + "name": "Sue", + "age": 28, + "email": "sue@gmail.com", + "gender": "female", + "role": "admin", + "friends": [5, 6], + "address": { + "street": "123 Washington Ave.", + "city": "New York", + "state": "NY", + "zipcode": "099012", + } + }, + # ✅ Valid + { + "id": 5 + "name": "Sam", + "age": 42, + "email": "sam@aol.com", + "role": "user", + "friends": [2, 6, 7], + "address": { + "street": "5 Sunset St.", + "street2": "Apt # 55-b", + "city": "San Jose", + "state": "CA", + "zipcode": "054053", + } + }, + ] + + assert schema(users) == response + +def test_users_endpoint_INVALID(): + """ + Test calling a users endpoint and validating its + response of users info is INVALID format. + """ + response = [ + # ❌ Invalid + { + "id": "null", + "name": None, + "age": 0, + "email": "unknown@msn", + "role": "unknown", + "friends": None, + "address": "5 Sunset St., San Jose, CA, 054053", + }, + ] + + # Option 1: + assert schema(users) != response + + # Option 2: + with pytest.raises(SchemaError): + schema(users) == response + +``` + +If data is **`valid`**, it will return the `True`. +If data is **`invalid`**, it will raise `SchemaError` exception. + + +## Supported validations + +See: [keleshev/schema](https://github.com/keleshev/schema) full documentation. + + + +%package -n python3-pytest-schema +Summary: 👍 Validate return values against a schema-like object in testing +Provides: python-pytest-schema +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-pytest-schema +# pytest-schema + + [](https://pypi.python.org/pypi/pytest-schema/) [](https://pypi.python.org/pypi/pytest-schema/) [](https://github.com/psf/black) + + +👍 Validate return values against a schema-like object in testing + +[`keleshev/schema`](https://github.com/keleshev/schema) is a library for validating Python data structures, such as +those obtained from config-files, forms, external services or +command-line parsing, converted from JSON/YAML (or something else) to Python data-types. + +## Install + +```bash +pip install pytest-schema +``` + +## Basic Example + +Here is a quick example of using **`schema`**: +```python +from pytest_schema import schema, exact, like + +article_v1 = { + "id": int, + "title": str, + "completed": bool, + "engagement": { + "viewer": list, + "rating": float, + }, + "metadata": dict +} + +def test_article_v1_endpoint(test_client): + """ + Test calling v1 endpoint and validating the response + is in the correctly/expected format. + """ + response_v1 = test_client.get("/api/v1/article/1") + assert exact(article_v1) == response_v1 + # Same as: + # assert schema(article_v1) == response_v1 + +article_v2 = { + **article_v1, + "someNewField": int +} + +def test_article_v2_endpoint(test_client): + """ + Test calling v2 endpoint is backwards compatible with v1 + """ + response_v2 = test_client.get("/api/v2/article/1") + + assert like(article_v1) == value + +``` +## Full Example + +Here is a more complex example of using **`schema`**: + +``` python +from pytest_schema import schema, And, Enum, Optional, Or, Regex + +# single user schema +user = { + # id must be int + "id": int, + # name must be type str + "name": str, + # email must be type str or nullable + "description": Or(None, str), + # email valid str format + "email": Regex(r".*?@.*?\.[A-Za-z]{2,6}"), + # age converted to int then validated gt 18 lt 99 and must be type str + "age": And(int, lambda n: 18 <= n <= 99), + # gender key is optional but must be str + Optional("gender"): str, + # role of enum values + "role": Enum(["user", "super-user", "admin"]), + # list of ids ref friends + "friends": [ int ], + # nested dict to valid as address + "address": { + "street": str, + Optional("street2"): str, + "city": str, + "state": And(str, lambda s: len(s) == 2), + "zipcode": str, + } + +} + +# multiple users schema +users = [ user ] + +def test_users_endpoint(): + """ + Test calling a users endpoint and validating its + response of users info is correct format. + """ + response = [ + # ✅ Valid + { + "id": 2, + "name": "Sue", + "age": 28, + "email": "sue@gmail.com", + "gender": "female", + "role": "admin", + "friends": [5, 6], + "address": { + "street": "123 Washington Ave.", + "city": "New York", + "state": "NY", + "zipcode": "099012", + } + }, + # ✅ Valid + { + "id": 5 + "name": "Sam", + "age": 42, + "email": "sam@aol.com", + "role": "user", + "friends": [2, 6, 7], + "address": { + "street": "5 Sunset St.", + "street2": "Apt # 55-b", + "city": "San Jose", + "state": "CA", + "zipcode": "054053", + } + }, + ] + + assert schema(users) == response + +def test_users_endpoint_INVALID(): + """ + Test calling a users endpoint and validating its + response of users info is INVALID format. + """ + response = [ + # ❌ Invalid + { + "id": "null", + "name": None, + "age": 0, + "email": "unknown@msn", + "role": "unknown", + "friends": None, + "address": "5 Sunset St., San Jose, CA, 054053", + }, + ] + + # Option 1: + assert schema(users) != response + + # Option 2: + with pytest.raises(SchemaError): + schema(users) == response + +``` + +If data is **`valid`**, it will return the `True`. +If data is **`invalid`**, it will raise `SchemaError` exception. + + +## Supported validations + +See: [keleshev/schema](https://github.com/keleshev/schema) full documentation. + + + +%package help +Summary: Development documents and examples for pytest-schema +Provides: python3-pytest-schema-doc +%description help +# pytest-schema + + [](https://pypi.python.org/pypi/pytest-schema/) [](https://pypi.python.org/pypi/pytest-schema/) [](https://github.com/psf/black) + + +👍 Validate return values against a schema-like object in testing + +[`keleshev/schema`](https://github.com/keleshev/schema) is a library for validating Python data structures, such as +those obtained from config-files, forms, external services or +command-line parsing, converted from JSON/YAML (or something else) to Python data-types. + +## Install + +```bash +pip install pytest-schema +``` + +## Basic Example + +Here is a quick example of using **`schema`**: +```python +from pytest_schema import schema, exact, like + +article_v1 = { + "id": int, + "title": str, + "completed": bool, + "engagement": { + "viewer": list, + "rating": float, + }, + "metadata": dict +} + +def test_article_v1_endpoint(test_client): + """ + Test calling v1 endpoint and validating the response + is in the correctly/expected format. + """ + response_v1 = test_client.get("/api/v1/article/1") + assert exact(article_v1) == response_v1 + # Same as: + # assert schema(article_v1) == response_v1 + +article_v2 = { + **article_v1, + "someNewField": int +} + +def test_article_v2_endpoint(test_client): + """ + Test calling v2 endpoint is backwards compatible with v1 + """ + response_v2 = test_client.get("/api/v2/article/1") + + assert like(article_v1) == value + +``` +## Full Example + +Here is a more complex example of using **`schema`**: + +``` python +from pytest_schema import schema, And, Enum, Optional, Or, Regex + +# single user schema +user = { + # id must be int + "id": int, + # name must be type str + "name": str, + # email must be type str or nullable + "description": Or(None, str), + # email valid str format + "email": Regex(r".*?@.*?\.[A-Za-z]{2,6}"), + # age converted to int then validated gt 18 lt 99 and must be type str + "age": And(int, lambda n: 18 <= n <= 99), + # gender key is optional but must be str + Optional("gender"): str, + # role of enum values + "role": Enum(["user", "super-user", "admin"]), + # list of ids ref friends + "friends": [ int ], + # nested dict to valid as address + "address": { + "street": str, + Optional("street2"): str, + "city": str, + "state": And(str, lambda s: len(s) == 2), + "zipcode": str, + } + +} + +# multiple users schema +users = [ user ] + +def test_users_endpoint(): + """ + Test calling a users endpoint and validating its + response of users info is correct format. + """ + response = [ + # ✅ Valid + { + "id": 2, + "name": "Sue", + "age": 28, + "email": "sue@gmail.com", + "gender": "female", + "role": "admin", + "friends": [5, 6], + "address": { + "street": "123 Washington Ave.", + "city": "New York", + "state": "NY", + "zipcode": "099012", + } + }, + # ✅ Valid + { + "id": 5 + "name": "Sam", + "age": 42, + "email": "sam@aol.com", + "role": "user", + "friends": [2, 6, 7], + "address": { + "street": "5 Sunset St.", + "street2": "Apt # 55-b", + "city": "San Jose", + "state": "CA", + "zipcode": "054053", + } + }, + ] + + assert schema(users) == response + +def test_users_endpoint_INVALID(): + """ + Test calling a users endpoint and validating its + response of users info is INVALID format. + """ + response = [ + # ❌ Invalid + { + "id": "null", + "name": None, + "age": 0, + "email": "unknown@msn", + "role": "unknown", + "friends": None, + "address": "5 Sunset St., San Jose, CA, 054053", + }, + ] + + # Option 1: + assert schema(users) != response + + # Option 2: + with pytest.raises(SchemaError): + schema(users) == response + +``` + +If data is **`valid`**, it will return the `True`. +If data is **`invalid`**, it will raise `SchemaError` exception. + + +## Supported validations + +See: [keleshev/schema](https://github.com/keleshev/schema) full documentation. + + + +%prep +%autosetup -n pytest-schema-0.1.1 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-pytest-schema -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Tue Apr 11 2023 Python_Bot <Python_Bot@openeuler.org> - 0.1.1-1 +- Package Spec generated @@ -0,0 +1 @@ +d577763b8381898013c9b7ecbf041e44 pytest-schema-0.1.1.tar.gz |
