forked from pi-node/pi-node
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_utils.py
More file actions
82 lines (68 loc) · 2.72 KB
/
test_utils.py
File metadata and controls
82 lines (68 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pytest
import os
import json
import logging
from src.utils import setup_logging, AppException, load_config, validate_input
# Test logging setup
def test_setup_logging(caplog):
"""Test logging setup."""
setup_logging("test.log")
logging.info("Test log message.")
# Check if the log message is captured
assert "Test log message." in caplog.text
# Test AppException
def test_app_exception():
"""Test the AppException class."""
exception = AppException("This is a test error", status_code=400)
assert exception.message == "This is a test error"
assert exception.status_code == 400
assert exception.to_dict() == {"detail": "This is a test error", "status_code": 400}
# Test load_config with a valid file
def test_load_config_valid():
"""Test loading configuration from a valid JSON file."""
config_data = {
"key1": "value1",
"key2": "value2"
}
with open("test_config.json", "w") as f:
json.dump(config_data, f)
config = load_config("test_config.json")
assert config == config_data
# Clean up
os.remove("test_config.json")
# Test load_config with a missing file
def test_load_config_missing_file():
"""Test loading configuration from a missing file."""
with pytest.raises(AppException) as excinfo:
load_config("missing_config.json")
assert excinfo.value.message == "Configuration file 'missing_config.json' not found."
assert excinfo.value.status_code == 404
# Test validate_input with valid data
def test_validate_input_valid():
"""Test input validation with valid data."""
data = {"field1": "value1", "field2": "value2"}
required_fields = ["field1", "field2"]
try:
validate_input(data, required_fields)
except AppException:
pytest.fail("validate_input raised AppException unexpectedly!")
# Test validate_input with missing fields
def test_validate_input_missing_field():
"""Test input validation with missing required fields."""
data = {"field1": "value1"}
required_fields = ["field1", "field2"]
with pytest.raises(AppException) as excinfo:
validate_input(data, required_fields)
assert excinfo.value.message == "Missing required field: field2"
assert excinfo.value.status_code == 400
# Test validate_input with empty fields
def test_validate_input_empty_field():
"""Test input validation with empty required fields."""
data = {"field1": "", "field2": "value2"}
required_fields = ["field1", "field2"]
with pytest.raises(AppException) as excinfo:
validate_input(data, required_fields)
assert excinfo.value.message == "Field 'field1' cannot be empty."
assert excinfo.value.status_code == 400
if __name__ == "__main__":
pytest.main()