128 lines
5.2 KiB
Python
128 lines
5.2 KiB
Python
import pytest
|
||
import sys
|
||
import requests
|
||
from pathlib import Path
|
||
import urllib3
|
||
|
||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||
|
||
from faction_utils import (
|
||
get_faction, get_all_factions, get_recent_events,
|
||
get_location, get_assets_in_location, get_factions_in_location
|
||
)
|
||
from vault_utils import BASE_URL, API_KEY, write_file
|
||
|
||
# ----------------------------------------------- Faction Integration Tests ------------------------------------------------------------
|
||
|
||
def test_obsidian_connection():
|
||
"""Проверяет наличие связи с плагином Obsidian Local REST API."""
|
||
headers = {
|
||
"Authorization": f"Bearer {API_KEY}",
|
||
"Content-Type": "application/json"
|
||
}
|
||
|
||
resp = requests.get(BASE_URL + "/", headers=headers, verify=False, proxies={"http": None, "https": None})
|
||
assert resp.status_code == 200, f"Expected 200, got {resp.status_code}. Response: {resp.text}"
|
||
|
||
def test_setup_test_factions():
|
||
"""Подготавливает тестовые файлы фракций, перезаписывая их содержимое для предсказуемости тестов."""
|
||
fm_a = {
|
||
"name": "Test_Faction_A",
|
||
"tags": ["faction"],
|
||
"force": 4,
|
||
"treasure": 10,
|
||
"assets": [{"name": "Asset1"}, {"name": "Asset2"}]
|
||
}
|
||
assert write_file("Фракции/Tests/Test_Faction_A.md", fm_a, "# Test Faction A\nФайл для автоматических тестов.") is True
|
||
|
||
fm_b = {
|
||
"name": "Test_Faction_B",
|
||
"tags": ["faction"],
|
||
"wealth": 4,
|
||
"cunning": 2
|
||
}
|
||
assert write_file("Фракции/Tests/Test_Faction_B.md", fm_b, "# Test Faction B\nФайл для автоматических тестов.") is True
|
||
|
||
def test_get_faction_integration():
|
||
"""Проверяет чтение конкретной тестовой фракции через vault_utils API."""
|
||
faction = get_faction("Фракции/Tests/Test_Faction_A.md")
|
||
assert faction is not None
|
||
assert faction.get("name") == "Test_Faction_A"
|
||
fm = faction.get("frontmatter", {})
|
||
assert fm.get("force") == 4
|
||
assert fm.get("treasure") == 10
|
||
assert len(fm.get("assets", [])) == 2
|
||
|
||
def test_get_all_factions_integration():
|
||
"""Проверяет загрузку списка тестовых фракций из директории Tests."""
|
||
factions = get_all_factions("Фракции/Tests")
|
||
assert len(factions) >= 2
|
||
names = [f.get("name") for f in factions]
|
||
assert "Test_Faction_A" in names
|
||
assert "Test_Faction_B" in names
|
||
|
||
faction_b = next(f for f in factions if f.get("name") == "Test_Faction_B")
|
||
fm_b = faction_b.get("frontmatter", {})
|
||
assert fm_b.get("wealth") == 4
|
||
assert fm_b.get("cunning") == 2
|
||
|
||
|
||
def test_setup_test_locations():
|
||
"""Подготавливает тестовые файлы локаций и событий для тестов."""
|
||
loc_a = {
|
||
"name": "Test_Location_A",
|
||
"travel-distance": {"[[Test_Location_B]]": 100}
|
||
}
|
||
write_file("TTRPG/Locations/Tests/Test_Location_A.md", loc_a, "# Location A")
|
||
|
||
loc_b = {
|
||
"name": "Test_Location_B",
|
||
"travel-distance": {"[[Test_Location_A]]": 100}
|
||
}
|
||
write_file("TTRPG/Locations/Tests/Test_Location_B.md", loc_b, "# Location B")
|
||
|
||
event = {
|
||
"name": "Test_Event_1",
|
||
"tags": ["event"]
|
||
}
|
||
write_file("События/Tests/Test_Event_1.md", event, "Event body")
|
||
|
||
def test_get_location_integration():
|
||
"""Проверяет получение локации и парсинг расстояний."""
|
||
loc = get_location("TTRPG/Locations/Tests/Test_Location_A.md")
|
||
assert loc is not None
|
||
assert loc.get("name") == "Test_Location_A"
|
||
assert loc.get("frontmatter", {}).get("travel-distance", {}).get("[[Test_Location_B]]") == 100
|
||
|
||
def test_get_recent_events_integration():
|
||
"""Проверяет получение списка недавних событий."""
|
||
events = get_recent_events("События/Tests", tags_filter=["event"], max_count=5)
|
||
assert len(events) >= 1
|
||
assert any(e.get("name") == "Test_Event_1" for e in events)
|
||
|
||
def test_assets_and_factions_in_location_integration():
|
||
"""Проверяет поиск активов и фракций по локациям."""
|
||
fm_c = {
|
||
"name": "Test_Faction_C",
|
||
"tags": ["faction"],
|
||
"locations": ["[[Test_Location_A]]"],
|
||
"assets": [
|
||
{"name": "Asset1", "location": "[[Test_Location_A]]"},
|
||
{"name": "Asset2", "location": "[[Test_Location_B]]"}
|
||
]
|
||
}
|
||
write_file("Фракции/Tests/Test_Faction_C.md", fm_c, "# Test Faction C")
|
||
|
||
faction = get_faction("Фракции/Tests/Test_Faction_C.md")
|
||
|
||
# Test get_assets_in_location
|
||
assets_a = get_assets_in_location(faction, ["[[Test_Location_A]]"])
|
||
assert len(assets_a) == 1
|
||
assert assets_a[0]["name"] == "Asset1"
|
||
|
||
# Test get_factions_in_location
|
||
factions = [faction]
|
||
factions_in_b = get_factions_in_location(factions, ["[[Test_Location_B]]"])
|
||
assert len(factions_in_b) == 1
|
||
assert factions_in_b[0].get("name") == "Test_Faction_C" |