-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
158 lines (123 loc) · 4.54 KB
/
test_models.py
File metadata and controls
158 lines (123 loc) · 4.54 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""Tests for the models module."""
from pathlib import Path
import pytest
from pydantic import ValidationError
from python_project_deployment.models import ProjectConfig
def test_project_config_valid(tmp_path: Path) -> None:
"""Test creating a valid ProjectConfig."""
test_dir = tmp_path / "test"
test_dir.mkdir()
config = ProjectConfig(
package_name="my_package",
target_dir=test_dir,
)
assert config.package_name == "my_package"
assert config.target_dir == test_dir
assert config.author_name == "Your Name"
assert str(config.author_email) == "your.email@example.com"
def test_project_config_invalid_package_name(tmp_path: Path) -> None:
"""Test that invalid package names raise ValidationError."""
with pytest.raises(ValidationError) as exc_info:
ProjectConfig(
package_name="123invalid", # Starts with number
target_dir=tmp_path / "test",
)
errors = exc_info.value.errors()
assert any("package_name" in str(e["loc"]) for e in errors)
def test_project_config_invalid_package_name_with_hyphen(tmp_path: Path) -> None:
"""Test that package names with hyphens raise ValidationError."""
with pytest.raises(ValidationError):
ProjectConfig(
package_name="my-package", # Contains hyphen
target_dir=tmp_path / "test",
)
def test_project_config_relative_path() -> None:
"""Test that relative paths raise ValidationError."""
with pytest.raises(ValidationError) as exc_info:
ProjectConfig(
package_name="my_package",
target_dir=Path("relative/path"),
)
errors = exc_info.value.errors()
assert any("target_dir" in str(e["loc"]) for e in errors)
def test_project_config_custom_values(tmp_path: Path) -> None:
"""Test ProjectConfig with custom values."""
test_dir = tmp_path / "projects"
test_dir.mkdir()
config = ProjectConfig(
package_name="awesome_pkg",
target_dir=test_dir,
author_name="John Doe",
author_email="john@example.com",
description="An awesome package",
license_type="Apache-2.0",
)
assert config.package_name == "awesome_pkg"
assert config.author_name == "John Doe"
assert str(config.author_email) == "john@example.com"
assert config.description == "An awesome package"
assert config.license_type == "Apache-2.0"
def test_destination_path(tmp_path: Path) -> None:
"""Test the destination_path property."""
test_dir = tmp_path / "test"
test_dir.mkdir()
config = ProjectConfig(
package_name="my_package",
target_dir=test_dir,
)
assert config.destination_path == test_dir / "my_package"
def test_to_template_context(tmp_path: Path) -> None:
"""Test conversion to template context."""
test_dir = tmp_path / "test"
test_dir.mkdir()
config = ProjectConfig(
package_name="my_package",
target_dir=test_dir,
author_name="Jane Doe",
author_email="jane@example.com",
description="Test package",
license_type="MIT",
)
context = config.to_template_context()
assert context["PKG"] == "my_package"
assert context["AUTHOR_NAME"] == "Jane Doe"
assert context["AUTHOR_EMAIL"] == "jane@example.com"
assert context["DESCRIPTION"] == "Test package"
assert context["LICENSE"] == "MIT"
assert context["GITHUB_URL"] == "https://github.com/your-username/my_package"
def test_valid_package_names(tmp_path: Path) -> None:
"""Test various valid package names."""
test_dir = tmp_path / "test"
test_dir.mkdir()
valid_names = [
"simple",
"with_underscore",
"MixedCase",
"_leading_underscore",
"name123",
"a",
]
for name in valid_names:
# Create separate dir for each to avoid "already exists" error
subdir = test_dir / name
subdir.mkdir()
config = ProjectConfig(
package_name=name,
target_dir=subdir,
)
assert config.package_name == name
def test_invalid_package_names(tmp_path: Path) -> None:
"""Test various invalid package names."""
invalid_names = [
"123start", # Starts with number
"with-hyphen", # Contains hyphen
"with space", # Contains space
"with.dot", # Contains dot
"", # Empty string
]
for name in invalid_names:
with pytest.raises(ValidationError):
ProjectConfig(
package_name=name,
target_dir=tmp_path / "test",
)