Squashed 'ai-context/trustgraph-templates/' content from commit 42a5fd1b

git-subtree-dir: ai-context/trustgraph-templates
git-subtree-split: 42a5fd1b678f32be378062e30451e2052ccb95dd
This commit is contained in:
elpresidank 2026-04-05 21:09:49 -05:00
commit 74cc8a4685
1216 changed files with 116347 additions and 0 deletions

0
tests/unit/__init__.py Normal file
View file

38
tests/unit/test_api.py Normal file
View file

@ -0,0 +1,38 @@
"""
Unit tests for Index class.
"""
import pytest
from trustgraph_configurator import Index
@pytest.mark.unit
class TestAPI:
"""Tests for the Index class."""
def test_get_templates_returns_list(self):
"""Test that get_templates returns a list."""
templates = Index.get_templates()
assert isinstance(templates, list)
assert len(templates) > 0
def test_templates_have_required_fields(self):
"""Test that templates have name and version fields."""
templates = Index.get_templates()
for template in templates:
assert hasattr(template, 'name')
assert hasattr(template, 'version')
def test_get_latest_returns_template(self):
"""Test that get_latest returns a template."""
latest = Index.get_latest()
assert latest is not None
assert hasattr(latest, 'name')
assert hasattr(latest, 'version')
def test_get_latest_stable_returns_template(self):
"""Test that get_latest_stable returns a template."""
latest_stable = Index.get_latest_stable()
assert latest_stable is not None
assert hasattr(latest_stable, 'name')
assert hasattr(latest_stable, 'version')

View file

@ -0,0 +1,101 @@
"""
Unit tests for Generator class.
"""
import pytest
import json
from trustgraph_configurator.generator import Generator
@pytest.mark.unit
class TestGenerator:
"""Tests for the Generator class."""
def test_simple_jsonnet(self):
"""Test processing simple jsonnet."""
def mock_fetch(base, rel):
return "", ""
generator = Generator(mock_fetch)
result = generator.process('{ foo: "bar" }')
assert isinstance(result, dict)
assert result["foo"] == "bar"
def test_jsonnet_with_variables(self):
"""Test processing jsonnet with variables."""
def mock_fetch(base, rel):
return "", ""
generator = Generator(mock_fetch)
jsonnet_code = '''
local name = "test";
{
name: name,
value: 42
}
'''
result = generator.process(jsonnet_code)
assert result["name"] == "test"
assert result["value"] == 42
def test_jsonnet_with_array(self):
"""Test processing jsonnet that returns array."""
def mock_fetch(base, rel):
return "", ""
generator = Generator(mock_fetch)
jsonnet_code = '[1, 2, 3, { foo: "bar" }]'
result = generator.process(jsonnet_code)
assert isinstance(result, list)
assert len(result) == 4
assert result[0] == 1
assert result[3]["foo"] == "bar"
def test_invalid_jsonnet(self):
"""Test that invalid jsonnet raises exception."""
def mock_fetch(base, rel):
return "", ""
generator = Generator(mock_fetch)
with pytest.raises(Exception):
generator.process('{ invalid jsonnet')
def test_jsonnet_with_functions(self):
"""Test processing jsonnet with functions."""
def mock_fetch(base, rel):
return "", ""
generator = Generator(mock_fetch)
jsonnet_code = '''
local double(x) = x * 2;
{
value: double(21)
}
'''
result = generator.process(jsonnet_code)
assert result["value"] == 42
def test_fetch_callback_is_used(self):
"""Test that fetch callback is called for imports."""
fetch_called = []
def mock_fetch(base, rel):
fetch_called.append((base, rel))
# Return simple jsonnet that defines a variable (as bytes)
return "config", b'{ imported: true }'
generator = Generator(mock_fetch)
jsonnet_code = '''
local config = import "config.jsonnet";
config
'''
result = generator.process(jsonnet_code)
assert len(fetch_called) > 0
assert result["imported"] is True

View file

@ -0,0 +1,60 @@
"""
Unit tests for Packager class.
"""
import pytest
from trustgraph_configurator.packager import Packager
@pytest.mark.unit
class TestPackager:
"""Tests for the Packager class."""
def test_init_with_latest_stable(self):
"""Test initialization with latest_stable flag."""
packager = Packager(
version=None,
template=None,
platform="docker-compose",
latest=False,
latest_stable=True
)
assert packager.version is not None
assert packager.template is not None
def test_init_with_template(self):
"""Test initialization with specific template."""
packager = Packager(
version="1.8.12",
template="1.8",
platform="docker-compose",
latest=False,
latest_stable=False
)
assert packager.version == "1.8.12"
assert packager.template == "1.8"
assert packager.platform == "docker-compose"
def test_invalid_platform_raises_error(self):
"""Test that invalid platform raises error during generation."""
packager = Packager(
version="1.8.12",
template="1.8",
platform="invalid-platform",
latest=False,
latest_stable=False
)
with pytest.raises(RuntimeError, match="Bad platform"):
packager.generate('[{"name": "test", "parameters": {}}]')
def test_init_without_template_raises_error(self):
"""Test that initialization without template/latest raises error."""
with pytest.raises(RuntimeError, match="You must"):
Packager(
version=None,
template=None,
platform="docker-compose",
latest=False,
latest_stable=False
)

62
tests/unit/test_run.py Normal file
View file

@ -0,0 +1,62 @@
"""
Unit tests for run module (CLI entry point).
"""
import pytest
import sys
@pytest.mark.unit
class TestRun:
"""Tests for the run module."""
def test_run_without_args_fails(self, run_configurator):
"""Test that running without required args fails."""
stdout, stderr, code = run_configurator([])
assert code != 0
def test_run_with_help_succeeds(self, run_configurator):
"""Test that help flag works."""
stdout, stderr, code = run_configurator(['-h'])
assert code == 0
def test_run_with_invalid_platform_fails(self, run_configurator, test_config_dir, primary_version):
"""Test that invalid platform fails during resource generation."""
config_file = str(test_config_dir / "minimal.json")
stdout, stderr, code = run_configurator([
'-t', primary_version,
'-p', 'invalid-platform',
'-i', config_file,
'--latest-stable',
'-R' # Use -R to trigger platform-specific generation
])
assert code == 1
def test_run_with_nonexistent_config_fails(self, run_configurator, primary_version):
"""Test that nonexistent config file fails."""
stdout, stderr, code = run_configurator([
'-t', primary_version,
'-p', 'docker-compose',
'-i', '/nonexistent/config.json',
'--latest-stable',
'-O'
])
assert code == 1
def test_exit_code_propagates(self, monkeypatch):
"""Test that exit codes are properly set."""
from trustgraph_configurator import run
# Test successful exit (no exception)
# This would require a valid config, so we'll just test the error path
# Test error exit
monkeypatch.setattr(sys, 'argv', [
'tg-build-deployment',
'-i', '/nonexistent/config.json'
])
with pytest.raises(SystemExit) as exc_info:
run() # run is already the function
assert exc_info.value.code == 1