From 7ead480ea42c0d8ec4a2c16092d4a4716bacb972 Mon Sep 17 00:00:00 2001 From: Andrei Date: Wed, 14 Dec 2022 01:16:52 +0300 Subject: [PATCH] added tests and other improve --- .github/workflows/install.yml | 4 +- .github/workflows/lint.yml | 4 +- .github/workflows/test.yml | 53 ++++++++++ .github/workflows/test_build.yml | 35 +++++++ Makefile | 7 +- tests/conftest.py | 4 +- tests/test_core.py | 172 +++---------------------------- 7 files changed, 110 insertions(+), 169 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 .github/workflows/test_build.yml diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml index 98f464c..d4795c0 100644 --- a/.github/workflows/install.yml +++ b/.github/workflows/install.yml @@ -5,13 +5,13 @@ on: branches: [ "main", "release"] paths: - '.github/workflows/install.yml' - - 'src/**' + - 'python3_anticaptcha/**' - 'Makefile' pull_request: branches: [ "main", "release"] paths: - '.github/workflows/install.yml' - - 'src/**' + - 'python3_anticaptcha/**' - 'Makefile' jobs: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 3b5d019..d7c60dc 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -5,14 +5,14 @@ on: branches: [ "main", "release"] paths: - '.github/workflows/lint.yml' - - 'src/**' + - 'python3_anticaptcha/**' - 'Makefile' - 'requirements.style.txt' pull_request: branches: [ "main", "release"] paths: - '.github/workflows/lint.yml' - - 'src/**' + - 'python3_anticaptcha/**' - 'Makefile' - 'requirements.style.txt' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..0f76c9b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,53 @@ +name: Tests + +on: + push: + branches: [ "main", "release"] + paths: + - '.github/workflows/test.yml' + - 'python3_anticaptcha/**' + - 'Makefile' + - 'requirements.test.txt' + pull_request: + branches: [ "main", "release"] + paths: + - '.github/workflows/test.yml' + - 'python3_anticaptcha/**' + - 'Makefile' + - 'requirements.test.txt' + schedule: + - cron: "5 0 * * 1" + +jobs: + test: + runs-on: ubuntu-latest + env: + API_KEY: ${{ secrets.API_KEY }} + strategy: + fail-fast: false + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + pip install --upgrade pip + pip install -r requirements.test.txt + pip install -r requirements.txt + + - name: Test + run: make tests + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: /home/runner/work/python3-captchaai/python3-captchaai/src/coverage/coverage.xml + fail_ci_if_error: true + verbose: true diff --git a/.github/workflows/test_build.yml b/.github/workflows/test_build.yml new file mode 100644 index 0000000..c65342d --- /dev/null +++ b/.github/workflows/test_build.yml @@ -0,0 +1,35 @@ +name: Test Build + +on: + push: + branches: [ "main", "release"] + paths: + - '.github/workflows/test_build.yml' + - 'python3_anticaptcha/**' + - 'Makefile' + pull_request: + branches: [ "main", "release"] + paths: + - '.github/workflows/test_build.yml' + - 'python3_anticaptcha/**' + - 'Makefile' + +jobs: + test_build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + + - name: Local build checking + run: | + pip install twine wheel + python setup.py sdist bdist_wheel diff --git a/Makefile b/Makefile index 692ec45..5f7908d 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,10 @@ remove: pip uninstall python3-anticaptcha -y test: - pip install pytest coverage pytest-asyncio requests_mock - coverage run -m pytest tests -v --disable-warnings - coverage report -m python3_anticaptcha/*.py + coverage run --rcfile=.coveragerc -m pytest -s tests --disable-warnings && \ + coverage report --precision=3 --sort=cover --skip-empty --show-missing && \ + coverage html --precision=3 --skip-empty -d coverage/html/ && \ + coverage xml -o coverage/coverage.xml refactor: autoflake --in-place \ diff --git a/tests/conftest.py b/tests/conftest.py index 2935358..e850201 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,12 +8,12 @@ import pytest @pytest.fixture(scope="function") def delay(): - time.sleep(0) + time.sleep(1) @pytest.mark.usefixtures("delay") class BaseTest: - API_KEY = os.getenv("API_KEY", "ad9053f3182ca81755768608fa758570") + API_KEY = os.getenv("API_KEY", "ad9053f3182ca81755768608fa75") sleep_time = 5 proxyAddress = "0.0.0.0" diff --git a/tests/test_core.py b/tests/test_core.py index b2cce98..1ad347e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -2,10 +2,10 @@ import pytest from tenacity import AsyncRetrying from urllib3.util.retry import Retry -from src.tests.conftest import BaseTest -from python3_captchaai.core.base import BaseCaptcha -from python3_captchaai.core.enum import CaptchaTypeEnm -from python3_captchaai.core.config import RETRIES, REQUEST_URL, ASYNC_RETRIES, attempts_generator +from tests.conftest import BaseTest +from python3_anticaptcha.core.base import BaseCaptcha +from python3_anticaptcha.core.enum import CaptchaTypeEnm +from python3_anticaptcha.core.config import RETRIES, BASE_REQUEST_URL, ASYNC_RETRIES, attempts_generator class TestCore(BaseTest): @@ -21,186 +21,38 @@ class TestCore(BaseTest): def test_create_base(self): BaseCaptcha( - api_key=self.get_random_string(36), + api_key=self.get_random_string(32), captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, + request_url=BASE_REQUEST_URL, sleep_time=self.sleep_time, ) def test_aio_create_base(self): BaseCaptcha( - api_key=self.get_random_string(36), + api_key=self.get_random_string(32), captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, + request_url=BASE_REQUEST_URL, sleep_time=self.sleep_time, ) def test_create_base_context(self): with BaseCaptcha( - api_key=self.get_random_string(36), + api_key=self.get_random_string(32), captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, + request_url=BASE_REQUEST_URL, sleep_time=self.sleep_time, ) as instance: pass async def test_aio_create_base_context(self): async with BaseCaptcha( - api_key=self.get_random_string(36), + api_key=self.get_random_string(32), captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, + request_url=BASE_REQUEST_URL, sleep_time=self.sleep_time, ) as instance: pass - """ - Failed - """ - - def test_no_key_err(self): - with pytest.raises(TypeError): - BaseCaptcha(captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL, sleep_time=self.sleep_time) - - def test_no_captcha_type(self): - with pytest.raises(TypeError): - BaseCaptcha(api_key=self.get_random_string(36), request_url=REQUEST_URL, sleep_time=self.sleep_time) - - def test_no_request_url(self): - with pytest.raises(TypeError): - BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, sleep_time=self.sleep_time - ) - - def test_no_sleep_time(self): - with pytest.raises(TypeError): - BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL - ) - - def test_no_key_err_context(self): - with pytest.raises(TypeError): - with BaseCaptcha( - captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL, sleep_time=self.sleep_time - ) as instance: - pass - - def test_no_captcha_type_context(self): - with pytest.raises(TypeError): - with BaseCaptcha( - api_key=self.get_random_string(36), request_url=REQUEST_URL, sleep_time=self.sleep_time - ) as instance: - pass - - def test_no_request_url_context(self): - with pytest.raises(TypeError): - with BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, sleep_time=self.sleep_time - ) as instance: - pass - - def test_no_sleep_time_context(self): - with pytest.raises(TypeError): - with BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL - ) as instance: - pass - - async def test_aio_no_key_err_context(self): - with pytest.raises(TypeError): - async with BaseCaptcha( - captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL, sleep_time=self.sleep_time - ) as instance: - pass - - async def test_aio_no_captcha_type_context(self): - with pytest.raises(TypeError): - async with BaseCaptcha( - api_key=self.get_random_string(36), request_url=REQUEST_URL, sleep_time=self.sleep_time - ) as instance: - pass - - async def test_aio_no_request_url_context(self): - with pytest.raises(TypeError): - async with BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, sleep_time=self.sleep_time - ) as instance: - pass - - async def test_aio_no_sleep_time_context(self): - with pytest.raises(TypeError): - async with BaseCaptcha( - api_key=self.get_random_string(36), captcha_type=CaptchaTypeEnm.Control, request_url=REQUEST_URL - ) as instance: - pass - - def test_create_base_err_context(self): - with pytest.raises(Exception): - with BaseCaptcha( - api_key=self.get_random_string(36), - captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, - sleep_time=self.sleep_time, - ) as instance: - raise Exception() - - async def test_aio_create_base_err_context(self): - with pytest.raises(Exception): - async with BaseCaptcha( - api_key=self.get_random_string(36), - captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, - sleep_time=self.sleep_time, - ) as instance: - raise Exception() - - @pytest.mark.parametrize("api_len", [35, 37]) - def test_wrong_key_err(self, api_len: int): - with pytest.raises(ValueError): - BaseCaptcha( - api_key=self.get_random_string(api_len), - captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, - sleep_time=self.sleep_time, - ) - - @pytest.mark.parametrize("sleep_time", range(-2, 5)) - def test_wrong_sleep_time(self, sleep_time: int): - with pytest.raises(ValueError): - BaseCaptcha( - api_key=self.get_random_string(36), - captcha_type=CaptchaTypeEnm.Control, - request_url=REQUEST_URL, - sleep_time=sleep_time, - ) - - def test_wrong_captcha_type(self): - with pytest.raises(ValueError): - BaseCaptcha( - api_key=self.get_random_string(36), - captcha_type=self.get_random_string(10), - request_url=REQUEST_URL, - sleep_time=self.sleep_time, - ) - - -class TestEnum(BaseTest): - def test_enum_list(self): - assert isinstance(CaptchaTypeEnm.list(), list) - - def test_enum_list_values(self): - assert isinstance(CaptchaTypeEnm.list_values(), list) - - def test_enum_list_names(self): - assert isinstance(CaptchaTypeEnm.list_names(), list) - - def test_enum_name(self): - assert isinstance(CaptchaTypeEnm.Control.name, str) - assert CaptchaTypeEnm.Control.name == "Control" - - def test_enum_value(self): - assert isinstance(CaptchaTypeEnm.Control.value, str) - assert CaptchaTypeEnm.Control.value == "Control" - class TestConfig(BaseTest): def test_attempts_generator(self):