"""
Tests for PipelineContext.
"""

import pytest

from pycharter.etl_generator.context import PipelineContext


class TestPipelineContext:
    """Tests for PipelineContext."""

    def test_basic_creation(self):
        """Test basic context creation with variables."""
        context = PipelineContext(variables={"DATA_DIR": "/path/to/data"})
        assert context.variables["DATA_DIR"] == "/path/to/data"

    def test_resolve_simple_variable(self):
        """Test resolving a simple variable."""
        context = PipelineContext(variables={"DATA_DIR": "/data/pipeline"})
        result = context.resolve("${DATA_DIR}/mock_inputs.json")
        assert result == "/data/pipeline/mock_inputs.json"

    def test_resolve_with_default(self):
        """Test resolving with default value."""
        context = PipelineContext()
        result = context.resolve("${MISSING_VAR:-default_value}")
        assert result == "default_value"

    def test_resolve_from_env(self, monkeypatch):
        """Test resolving from environment."""
        monkeypatch.setenv("MY_TEST_VAR", "env_value")
        context = PipelineContext()
        result = context.resolve("${MY_TEST_VAR}")
        assert result == "env_value"

    def test_resolve_priority(self, monkeypatch):
        """Test variables take priority over env."""
        monkeypatch.setenv("MY_VAR", "env_value")
        context = PipelineContext(variables={"MY_VAR": "context_value"})
        result = context.resolve("${MY_VAR}")
        assert result == "context_value"

    def test_resolve_dict(self):
        """Test resolving variables in dictionary."""
        context = PipelineContext(variables={"DATA_DIR": "/data"})
        config = {
            "path": "${DATA_DIR}/file.json",
            "nested": {"url": "${DATA_DIR}/nested.json"},
        }
        result = context.resolve_dict(config)
        assert result["path"] == "/data/file.json"
        assert result["nested"]["url"] == "/data/nested.json"

    def test_to_dict(self):
        """Test converting to dictionary."""
        context = PipelineContext(
            variables={"DATA_DIR": "/data", "DB_URL": "postgresql://..."}
        )
        d = context.to_dict()
        assert d["DATA_DIR"] == "/data"
        assert d["DB_URL"] == "postgresql://..."

    def test_get(self):
        """Test get method."""
        context = PipelineContext(variables={"KEY": "value"})
        assert context.get("KEY") == "value"
        assert context.get("MISSING", "default") == "default"

    def test_set_and_update(self):
        """Test set and update methods."""
        context = PipelineContext(variables={"A": "1"})
        context.set("B", "2")
        context.update({"C": "3"})
        assert context.variables["A"] == "1"
        assert context.variables["B"] == "2"
        assert context.variables["C"] == "3"
