#!/usr/bin/env python3
"""
#exonware/xwdata/tests/0.core/test_core_query_integration.py
Core tests for XWData + XWQuery integration.
Company: eXonware.com
Author: eXonware Backend Team
Email: connect@exonware.com
Version: 0.0.1.3
Generation Date: 26-Oct-2025
"""

import pytest
from exonware.xwdata import XWData
@pytest.mark.xwdata_core

class TestQueryIntegration:
    """Core tests for XWData query integration (Plan 1)."""

    def test_as_xwnode_returns_node(self):
        """Test as_xwnode() returns valid XWNode."""
        from exonware.xwnode import XWNode
        data = XWData.from_native({'users': [{'name': 'Alice', 'age': 30}]})
        node = data.as_xwnode()
        # Should be XWNode instance
        assert isinstance(node, XWNode)
        # Should have data
        native = node.to_native()
        assert 'users' in native
        assert len(native['users']) == 1

    def test_as_xwnode_fails_without_node(self):
        """Test as_xwnode() raises error when no node available."""
        # Create XWData with None (edge case)
        data = XWData.__new__(XWData)
        data._node = None
        with pytest.raises(ValueError, match="No XWNode available"):
            data.as_xwnode()
    @pytest.mark.asyncio

    async def test_query_method_sql(self):
        """Test query() method with SQL."""
        data = XWData.from_native({
            'users': [
                {'name': 'Alice', 'age': 30},
                {'name': 'Bob', 'age': 25},
                {'name': 'Charlie', 'age': 35}
            ]
        })
        # Execute SQL query
        result = await data.query("SELECT * FROM users WHERE age > 28")
        # Should return filtered results
        assert result is not None
@pytest.mark.xwdata_core

class TestDetectionMetadata:
    """Core tests for format detection metadata (Plan 3)."""
    @pytest.mark.asyncio

    async def test_detection_metadata_from_file(self, tmp_path):
        """Test detection metadata is stored when loading files."""
        # Create test file
        test_file = tmp_path / "test.json"
        test_file.write_text('{"name": "Alice", "age": 30}')
        # Load file
        data = await XWData.load(str(test_file))
        # Check detection metadata
        detected = data.get_detected_format()
        confidence = data.get_detection_confidence()
        info = data.get_detection_info()
        assert detected is not None
        assert detected in ['JSON', 'json']  # Case may vary
        assert confidence is not None
        assert confidence > 0.8  # Should be confident
        assert info['detected_format'] is not None
        assert info['detection_method'] in ['extension', 'content', 'hint']

    def test_detection_metadata_not_set_for_native(self):
        """Test detection metadata not set for from_native()."""
        data = XWData.from_native({'key': 'value'})
        # Should not have detection metadata (data wasn't loaded from file)
        detected = data.get_detected_format()
        assert detected is None or detected == ''
    @pytest.mark.asyncio

    async def test_detection_info_complete(self, tmp_path):
        """Test detection info returns all fields."""
        # Create YAML file
        yaml_file = tmp_path / "config.yaml"
        yaml_file.write_text("name: Alice\nage: 30")
        data = await XWData.load(str(yaml_file))
        info = data.get_detection_info()
        # Check all fields present
        assert 'detected_format' in info
        assert 'detection_confidence' in info
        assert 'detection_method' in info
        assert 'format_candidates' in info
