Big-Link-Man/tests/unit/test_job_config_extensions.py

207 lines
6.3 KiB
Python

"""
Unit tests for job config extensions (Story 3.1)
"""
import pytest
import json
import tempfile
from pathlib import Path
from src.generation.job_config import JobConfig
class TestJobConfigExtensions:
"""Tests for new job config fields"""
def test_parse_tier1_preferred_sites(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"tier1_preferred_sites": ["www.site1.com", "www.site2.com"]
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
config = JobConfig(temp_path)
job = config.get_jobs()[0]
assert job.tier1_preferred_sites == ["www.site1.com", "www.site2.com"]
finally:
Path(temp_path).unlink()
def test_parse_auto_create_sites(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"auto_create_sites": True
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
config = JobConfig(temp_path)
job = config.get_jobs()[0]
assert job.auto_create_sites is True
finally:
Path(temp_path).unlink()
def test_auto_create_sites_defaults_to_false(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
}
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
config = JobConfig(temp_path)
job = config.get_jobs()[0]
assert job.auto_create_sites is False
finally:
Path(temp_path).unlink()
def test_parse_create_sites_for_keywords(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"create_sites_for_keywords": [
{"keyword": "engine repair", "count": 3},
{"keyword": "car maintenance", "count": 2}
]
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
config = JobConfig(temp_path)
job = config.get_jobs()[0]
assert len(job.create_sites_for_keywords) == 2
assert job.create_sites_for_keywords[0]["keyword"] == "engine repair"
assert job.create_sites_for_keywords[0]["count"] == 3
finally:
Path(temp_path).unlink()
def test_invalid_tier1_preferred_sites_type(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"tier1_preferred_sites": "not-an-array"
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
with pytest.raises(ValueError, match="tier1_preferred_sites.*must be an array"):
JobConfig(temp_path)
finally:
Path(temp_path).unlink()
def test_invalid_auto_create_sites_type(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"auto_create_sites": "yes"
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
with pytest.raises(ValueError, match="auto_create_sites.*must be a boolean"):
JobConfig(temp_path)
finally:
Path(temp_path).unlink()
def test_invalid_create_sites_for_keywords_missing_fields(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 5}
},
"create_sites_for_keywords": [
{"keyword": "engine repair"}
]
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
with pytest.raises(ValueError, match="must have 'keyword' and 'count'"):
JobConfig(temp_path)
finally:
Path(temp_path).unlink()
def test_all_new_fields_together(self):
config_data = {
"jobs": [{
"project_id": 1,
"tiers": {
"tier1": {"count": 10}
},
"deployment_targets": ["www.primary.com"],
"tier1_preferred_sites": ["www.site1.com", "www.site2.com"],
"auto_create_sites": True,
"create_sites_for_keywords": [
{"keyword": "engine", "count": 5}
]
}]
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(config_data, f)
temp_path = f.name
try:
config = JobConfig(temp_path)
job = config.get_jobs()[0]
assert job.deployment_targets == ["www.primary.com"]
assert job.tier1_preferred_sites == ["www.site1.com", "www.site2.com"]
assert job.auto_create_sites is True
assert len(job.create_sites_for_keywords) == 1
finally:
Path(temp_path).unlink()