Skip to content

Commit

Permalink
feat: implement proxy_checker unittests plus test and coverage github…
Browse files Browse the repository at this point in the history
… actions
  • Loading branch information
amirvalhalla committed Jul 19, 2024
1 parent e3981d2 commit 6696834
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
comment:
layout: header, changes, diff, sunburst
coverage:
status:
patch:
default:
threshold: "10%"
only_pulls: true

project:
default:
target: auto
threshold: "10%"
37 changes: 37 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Unit Test

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
testing:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Set up Python V3.12
uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make devtools
- name: Run Unit Tests with Coverage
run: |
coverage run -m unittest discover tests
coverage report
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: coverage.xml
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Unit Test

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
testing:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Set up Python V3.12
uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make devtools
- name: Run Unit Tests
run: |
make unittest
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ lint:
@echo "Formatting the Code via Black! \n"
black .
@echo "Linting the Code via Pylint! \n"
pylint proxy_miner/
pylint proxy_miner/


unittest:
@echo "Running Unittests \n"
python3 -m unittest discover tests
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ black==24.4.2
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
coverage==7.6.0
dill==0.3.8
idna==3.7
isort==5.13.2
Expand Down
Empty file added tests/__init__.py
Empty file.
123 changes: 123 additions & 0 deletions tests/test_proxy_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import unittest
from unittest.mock import patch, MagicMock
import requests
from proxy_miner.proxy_checker.proxy_checker import ProxyChecker
from proxy_miner.proxy_enum.proxy_type import ProxyType


class TestProxyChecker(unittest.TestCase):

@patch("proxy_miner.proxy_checker.proxy_checker.requests.get")
def test_validate_http_proxy_success(self, mock_get):
# Setup
proxies = [
(ProxyType.HTTP, ["http_proxy1", "http_proxy2"]),
(ProxyType.HTTPS, ["https_proxy1"]),
]
url = "http://example.com"
timeout = 5.0
ssl_verify = False
checker = ProxyChecker(proxies, url, timeout, ssl_verify)

# Mock response for successful request
mock_response = MagicMock()
mock_response.status_code = 200
mock_get.return_value = mock_response

# Act
checker.validate_http_proxy()

# Assert
self.assertIn("http_proxy1", checker.fetch_proxies())
self.assertIn("http_proxy2", checker.fetch_proxies())
self.assertNotIn("https_proxy1", checker.fetch_proxies())

@patch("proxy_miner.proxy_checker.proxy_checker.requests.get")
def test_validate_http_proxy_failure(self, mock_get):
# Setup
proxies = [
(ProxyType.HTTP, ["http_proxy1"]),
]
url = "http://example.com"
timeout = 5.0
ssl_verify = False
checker = ProxyChecker(proxies, url, timeout, ssl_verify)

# Mock response for failed request
mock_get.side_effect = requests.exceptions.RequestException

# Act
checker.validate_http_proxy()

# Assert
self.assertNotIn("http_proxy1", checker.fetch_proxies())

@patch("proxy_miner.proxy_checker.proxy_checker.requests.get")
def test_validate_https_proxy_success(self, mock_get):
# Setup
proxies = [
(ProxyType.HTTPS, ["https_proxy1"]),
]
url = "https://example.com"
timeout = 5.0
ssl_verify = True
checker = ProxyChecker(proxies, url, timeout, ssl_verify)

# Mock response for successful request
mock_response = MagicMock()
mock_response.status_code = 200
mock_get.return_value = mock_response

# Act
checker.validate_https_proxy()

# Assert
self.assertIn("https_proxy1", checker.fetch_proxies())

@patch("proxy_miner.proxy_checker.proxy_checker.requests.get")
def test_validate_socks4_proxy_success(self, mock_get):
# Setup
proxies = [
(ProxyType.SOCKS4, ["socks4_proxy1"]),
]
url = "http://example.com"
timeout = 5.0
ssl_verify = False
checker = ProxyChecker(proxies, url, timeout, ssl_verify)

# Mock response for successful request
mock_response = MagicMock()
mock_response.status_code = 200
mock_get.return_value = mock_response

# Act
checker.validate_socks4_proxy()

# Assert
self.assertIn("socks4_proxy1", checker.fetch_proxies())

@patch("proxy_miner.proxy_checker.proxy_checker.requests.get")
def test_validate_socks5_proxy_success(self, mock_get):
# Setup
proxies = [
(ProxyType.SOCKS5, ["socks5_proxy1"]),
]
url = "http://example.com"
timeout = 5.0
ssl_verify = False
checker = ProxyChecker(proxies, url, timeout, ssl_verify)

# Mock response for successful request
mock_response = MagicMock()
mock_response.status_code = 200
mock_get.return_value = mock_response

# Act
checker.validate_socks5_proxy()

# Assert
self.assertIn("socks5_proxy1", checker.fetch_proxies())


if __name__ == "__main__":
unittest.main()

0 comments on commit 6696834

Please sign in to comment.