Skip to content

Commit 6fb495c

Browse files
committed
test: Add baseline tests for package.
1 parent b7bbde8 commit 6fb495c

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import pytest
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def setup_and_teardown_per_test():
7+
"""Setup and teardown logic for each test run."""
8+
with open("py_ddns.ini", "w") as f:
9+
f.write("""[Client_settings]\nlogging_level=info\n""")
10+
11+
yield # This allows each test to run before cleanup
12+
13+
files = ["py_ddns.ini", "py_ddns.db", "py_ddns.log"]
14+
for file in files:
15+
try:
16+
if os.path.exists(file):
17+
os.remove(file)
18+
except Exception as e:
19+
print(f"Error occurred while trying to remove {file}: {e}")

tests/test_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from pyddns.client import DDNSClient
3+
4+
5+
class TestDDNSClient(DDNSClient):
6+
def update_dns(self, ip_address: str, record_name: str) -> None:
7+
pass # Mock implementation for testing
8+
9+
10+
def test_ddns_client_ipv4():
11+
client = TestDDNSClient()
12+
ip = client.get_ipv4()
13+
assert ip is not None, "Failed to retrieve IPv4 address!"
14+
assert isinstance(ip, str), "Returned IP is not a string!"
15+
16+
17+
@pytest.mark.parametrize(
18+
"ip_address, record_name",
19+
[
20+
("127.0.0.1", "test.example.com"),
21+
("192.168.0.1", "local.example.com"),
22+
],
23+
)
24+
def test_ddns_client_update_dns(ip_address, record_name):
25+
client = TestDDNSClient()
26+
try:
27+
client.update_dns(ip_address, record_name)
28+
except Exception as e:
29+
pytest.fail(f"update_dns raised an exception: {e}")

tests/test_cloudflare_service.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
from pyddns.services.cloudflare_service import CloudflareDNS
4+
5+
6+
def test_cloudflare_dns_initialization():
7+
with pytest.raises(KeyError):
8+
CloudflareDNS(api_token=None, zone_id=None)
9+
10+
client = CloudflareDNS(api_token="test_token", zone_id="test_zone")
11+
assert client is not None, "Failed to initialize CloudflareDNS!"
12+
13+
14+
def test_cloudflare_dns_update():
15+
client = CloudflareDNS(api_token="test_token", zone_id="test_zone")
16+
client.cf_client = MagicMock()
17+
18+
try:
19+
client.update_dns("127.0.0.1", "test.example.com")
20+
except Exception as e:
21+
pytest.fail(f"update_dns raised an exception: {e}")
22+
23+
24+
def test_cloudflare_dns_obtain_record():
25+
client = CloudflareDNS(api_token="test_token", zone_id="test_zone")
26+
client.cf_client = MagicMock()
27+
client.cf_client.dns.records.list = MagicMock(
28+
return_value=MagicMock(result=[])
29+
)
30+
31+
record = client._obtain_record("test.example.com")
32+
assert (
33+
record is None
34+
), "_obtain_record should return None if no records are found!"

tests/test_config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
from pyddns.config import Config
3+
4+
5+
def test_config_singleton():
6+
config1 = Config(config_file="py_ddns.ini")
7+
config2 = Config(config_file="py_ddns.ini")
8+
assert config1 is config2, "Config is not a singleton!"
9+
10+
11+
def test_config_get():
12+
config = Config(config_file="py_ddns.ini")
13+
value = config.get("Client_settings", "logging_level")
14+
assert value == "info", "Config did not return the correct value!"
15+
16+
17+
def test_config_file_not_found():
18+
with pytest.raises(FileNotFoundError):
19+
Config(config_file="non_existent.ini")

tests/test_duckdns_service.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
from unittest.mock import MagicMock
3+
from pyddns.services.duckdns_service import DuckDNS
4+
5+
6+
def test_duckdns_initialization():
7+
with pytest.raises(KeyError):
8+
DuckDNS(token=None)
9+
10+
client = DuckDNS(token="test_token")
11+
assert client is not None, "Failed to initialize DuckDNS!"
12+
13+
14+
def test_duckdns_update():
15+
client = DuckDNS(token="test_token")
16+
client.storage = MagicMock()
17+
client.storage.update_ip = MagicMock()
18+
19+
client._parse_api_response = MagicMock(
20+
return_value=("OK", "127.0.0.1", None, "UPDATED")
21+
)
22+
23+
try:
24+
client.update_dns("127.0.0.1", "test.example.com")
25+
except Exception as e:
26+
pytest.fail(f"update_dns raised an exception: {e}")
27+
28+
client.storage.update_ip.assert_called_once()
29+
30+
31+
def test_duckdns_obtain_record():
32+
client = DuckDNS(token="test_token")
33+
client.storage = MagicMock()
34+
client.storage.retrieve_record = MagicMock(return_value="test")
35+
36+
record = client._obtain_record("test.example.com")
37+
assert record == "test", "_obtain_record should return the correct record!"

tests/test_storage.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from pyddns.storage import Storage
2+
3+
4+
def test_storage_singleton():
5+
storage1 = Storage(filename="py_ddns.db")
6+
storage2 = Storage(filename="py_ddns.db")
7+
assert storage1 is storage2, "Storage is not a singleton!"
8+
9+
10+
def test_storage_create_tables():
11+
storage = Storage(filename="py_ddns.db")
12+
assert storage.cursor is not None, "Cursor is not initialized!"
13+
14+
15+
def test_storage_add_and_retrieve():
16+
storage = Storage(filename="py_ddns.db")
17+
storage.add_service("TestService", "test.example.com", "127.0.0.1")
18+
record = storage.retrieve_record("test.example.com")
19+
assert record is not None, "Failed to retrieve the record!"
20+
assert record[0] == "127.0.0.1", "Retrieved IP does not match!"
21+
22+
23+
def test_storage_update_ip():
24+
storage = Storage(filename="py_ddns.db")
25+
storage.add_service("TestService2", "test2.example.com", "127.0.0.1")
26+
storage.update_ip("TestService2", "test2.example.com", "127.0.0.2")
27+
record = storage.retrieve_record("test2.example.com")
28+
assert record[0] == "127.0.0.2", "IP address was not updated!"

0 commit comments

Comments
 (0)