Skip to content

Commit

Permalink
feat: Add unit tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ittuann authored Sep 8, 2024
1 parent 027d667 commit 1c9601f
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: per-commit hooks
run: make pre-commit

- name: Unit Test
run: make unit-tests

- name: Auto Commit
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
run: |
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ __pycache__/

/.ruff_cache
/.mypy_cache
/.pytest_cache
/.coverage
/htmlcov
/coverage.xml
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MAKEFLAGS = --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

.PHONY: run install lint pre-commi
.PHONY: run install lint pre-commi unit-tests

.DEFAULT_GOAL := run

Expand All @@ -20,3 +20,6 @@ lint:

pre-commit:
poetry run pre-commit run --verbose --all-files

unit-tests:
poetry run pytest
167 changes: 166 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ types-requests = "^2.32.0.20240712"
autoflake = "^2.3.1"
pyupgrade = "^3.16.0"
pre-commit = "^3.8.0"
pytest = "^8.3.2"
pytest-cov = "^5.0.0"
pytest-mock = "^3.14.0"

[tool.black]
line-length = 120
Expand Down Expand Up @@ -63,6 +66,9 @@ ignore = ["F401", "UP009", "D415"]
[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.pytest.ini_options]
addopts = "-ra --cov=./scripts --cov-report=term --cov-report=xml --cov-report=html"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
7 changes: 7 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""__init__.py
Note:
File : __init__.py
Author : ittuann <ittuann@outlook.com>
License: MIT License.
"""
36 changes: 36 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Code Coverage Test.
Note:
File : test_functions.py
Author : ittuann <ittuann@outlook.com>
License: MIT License.
"""

from scripts.constants import DNS_API_CLOUDFLARE, DNS_API_GOOGLE, GITHUB_URLS
from scripts.get_ip import DNSRecord, URLIPMapping


def test_dnsrecord_class():
"""Test DNSRecord class."""
record = DNSRecord(name="example.com", type=1, TTL=300, data="1.2.3.4")
assert record.name == "example.com"
assert record.type == 1
assert record.ttl == 300
assert record.data == "1.2.3.4"


def test_urlipmapping_class():
"""Test URLIPMapping class."""
mapping = URLIPMapping(url="example.com", ip="1.2.3.4")
assert mapping.url == "example.com"
assert mapping.ip == "1.2.3.4"
assert mapping.to_dict() == {"example.com": "1.2.3.4"}
assert str(mapping) == "URLIPMapping(url='example.com', ip='1.2.3.4')"


def test_constants():
"""Test constants"""
assert isinstance(DNS_API_GOOGLE, str)
assert isinstance(DNS_API_CLOUDFLARE, str)
assert isinstance(GITHUB_URLS, list)
assert all(isinstance(url, str) for url in GITHUB_URLS)

0 comments on commit 1c9601f

Please sign in to comment.