added tests and other improve

This commit is contained in:
Andrei 2022-12-14 01:16:52 +03:00
parent bc3354e8d0
commit 7ead480ea4
7 changed files with 110 additions and 169 deletions

View File

@ -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:

View File

@ -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'

53
.github/workflows/test.yml vendored Normal file
View File

@ -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

35
.github/workflows/test_build.yml vendored Normal file
View File

@ -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

View File

@ -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 \

View File

@ -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"

View File

@ -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):