-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_generator.py
More file actions
185 lines (148 loc) · 6.28 KB
/
Copy pathtest_generator.py
File metadata and controls
185 lines (148 loc) · 6.28 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from pathlib import Path
from src.Litestar.generator import LitestarGenerator
from src.models import Database, Framework, MemoryStore, ProjectConfig
def test_litestar_generator_context(tmp_path: Path) -> None:
"""Verify Litestar generator template context values with plugins and database enabled."""
config = ProjectConfig(
name="Test Project",
framework=Framework.LITESTAR,
database=Database.SQLITE,
memory_store=MemoryStore.NONE,
plugins=["advanced_alchemy"],
docker=True,
docker_infra=False,
)
generator = LitestarGenerator(config, tmp_path)
context = generator._get_template_context()
assert context["project_name"] == "Test Project"
assert context["advanced_alchemy"] is True
assert context["litestar_vite"] is False
assert context["has_database"] is True
assert context["docker"] is True
def test_litestar_generator_no_plugins(tmp_path: Path) -> None:
"""Verify Litestar generator template context values with no plugins or database."""
config = ProjectConfig(
name="Test Project",
framework=Framework.LITESTAR,
database=Database.NONE,
memory_store=MemoryStore.NONE,
plugins=[],
docker=False,
docker_infra=False,
)
generator = LitestarGenerator(config, tmp_path)
context = generator._get_template_context()
assert context["advanced_alchemy"] is False
assert context["has_database"] is False
def test_litestar_generator_plugins_rendering(tmp_path: Path) -> None:
"""Verify that plugin templates are correctly rendered into the output directory."""
config = ProjectConfig(
name="Plugin Test",
framework=Framework.LITESTAR,
database=Database.POSTGRESQL,
memory_store=MemoryStore.NONE,
plugins=["advanced_alchemy"],
docker=False,
docker_infra=True,
)
generator = LitestarGenerator(config, tmp_path)
generator.generate()
# Verify base files
assert (tmp_path / "pyproject.toml").exists()
assert (tmp_path / "src" / "backend" / "app.py").exists()
# Verify AdvancedAlchemy plugin files
# These are in src/Litestar/Plugins/AdvancedAlchemy/Templates/
# Should be rendered to src/backend of tmp_path
assert (tmp_path / "src" / "backend" / "models" / "users.py").exists()
assert (tmp_path / "src" / "backend" / "lib" / "dependencies.py").exists()
assert (tmp_path / "src" / "backend" / "lib" / "services.py").exists()
def test_litestar_generator_memory_store_context(tmp_path: Path) -> None:
"""Verify Litestar generator template context values with memory store enabled."""
config = ProjectConfig(
name="Store Test",
framework=Framework.LITESTAR,
database=Database.NONE,
memory_store=MemoryStore.REDIS,
plugins=[],
docker=True,
docker_infra=True,
)
generator = LitestarGenerator(config, tmp_path)
context = generator._get_template_context()
assert context["memory_store"] == MemoryStore.REDIS
assert context["has_store"] is True
assert context["store_config"].driver == "redis"
assert context["store_config"].docker_image == "redis:8.4.0-bookworm"
def test_litestar_generator_docker_compose_rendering(tmp_path: Path) -> None:
"""Verify that docker-compose.yml is correctly rendered with dependencies."""
config = ProjectConfig(
name="Docker Test",
framework=Framework.LITESTAR,
database=Database.POSTGRESQL,
memory_store=MemoryStore.REDIS,
plugins=[],
docker=True,
docker_infra=False,
)
generator = LitestarGenerator(config, tmp_path)
generator.generate()
docker_compose = tmp_path / "docker-compose.yml"
assert docker_compose.exists()
content = docker_compose.read_text()
# Check env var
assert "REDIS_URL=redis://docker_test_redis:6379/0" in content
assert "DATABASE_URL=postgresql+psycopg://myuser:mypassword@postgres:5432/mydb" in content
# Check depends_on structure
assert "depends_on:" in content
assert "postgres:" in content
assert "redis:" in content
# Check services
assert "image: postgres:17.7-alpine3.23" in content
assert "image: redis:8.4.0-bookworm" in content
def test_litestar_generator_saq_context(tmp_path: Path) -> None:
"""Verify Litestar generator template context values with SAQ plugin enabled."""
config = ProjectConfig(
name="SAQ Test",
framework=Framework.LITESTAR,
database=Database.NONE,
memory_store=MemoryStore.REDIS,
plugins=["litestar_saq"],
docker=False,
docker_infra=False,
)
generator = LitestarGenerator(config, tmp_path)
context = generator._get_template_context()
assert context["litestar_saq"] is True
assert context["has_store"] is True
assert context["memory_store"] == MemoryStore.REDIS
def test_litestar_generator_saq_rendering(tmp_path: Path) -> None:
"""Verify that SAQ plugin templates are correctly rendered into the output directory."""
config = ProjectConfig(
name="SAQ Plugin Test",
framework=Framework.LITESTAR,
database=Database.NONE,
memory_store=MemoryStore.REDIS,
plugins=["litestar_saq"],
docker=False,
docker_infra=False,
)
generator = LitestarGenerator(config, tmp_path)
generator.generate()
# Verify base files
assert (tmp_path / "pyproject.toml").exists()
assert (tmp_path / "src" / "backend" / "app.py").exists()
assert (tmp_path / "src" / "backend" / "config.py").exists()
# Verify SAQ plugin files
assert (tmp_path / "src" / "backend" / "lib" / "tasks.py").exists()
# Verify SAQ config in config.py
config_content = (tmp_path / "src" / "backend" / "config.py").read_text()
assert "from litestar_saq import QueueConfig, SAQConfig, SAQPlugin" in config_content
assert "saq = SAQPlugin(" in config_content
assert 'QueueConfig(name="default"' in config_content
# Verify SAQ plugin in app.py
app_content = (tmp_path / "src" / "backend" / "app.py").read_text()
assert "from .config import saq" in app_content
assert "saq," in app_content
# Verify SAQ dependency in pyproject.toml
pyproject_content = (tmp_path / "pyproject.toml").read_text()
assert "litestar-saq>=0.7.0" in pyproject_content