# AUTO-GENERATED FILE - DO NOT EDIT
# This file was automatically generated by the XDK build tool.
# Any manual changes will be overwritten on the next generation.
"""
Auto-generated contract tests for {"class_name": "General", "display_name": "general", "import_name": "general", "original": ["general"], "property_name": "general"} client.

This module contains tests that validate the request/response contracts
of the {"class_name": "General", "display_name": "general", "import_name": "general", "original": ["general"], "property_name": "general"} client against the OpenAPI specification.

Generated automatically - do not edit manually.
"""

import pytest
import json
import requests
from unittest.mock import Mock, patch
from xdk.general.client import GeneralClient
from xdk import Client


class TestGeneralContracts:
    """Test the API contracts of GeneralClient."""


    def setup_class(self):
        """Set up test fixtures."""
        self.client = Client(base_url="https://api.example.com")
        self.general_client = getattr(self.client, "general")


    def test_get_open_api_spec_request_structure(self):
        """Test get_open_api_spec request structure."""
        # Mock the session to capture request details
        with patch.object(self.client, "session") as mock_session:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_response.json.return_value = {}
            mock_response.raise_for_status.return_value = None
            mock_session.get.return_value = mock_response
            # Prepare test parameters
            kwargs = {}
            # Add required parameters
            # Add request body if required
            # Call the method
            try:
                method = getattr(self.general_client, "get_open_api_spec")
                # Check if this might be a streaming operation by inspecting return type
                import types
                import inspect
                sig = inspect.signature(method)
                return_annotation = str(sig.return_annotation)
                might_be_streaming = (
                    "Generator" in return_annotation or "Iterator" in return_annotation
                )
                # Set up streaming mock if it might be streaming (before calling method)
                if might_be_streaming:
                    mock_streaming_response = Mock()
                    mock_streaming_response.status_code = 200
                    mock_streaming_response.raise_for_status.return_value = None
                    # Make it a proper context manager
                    mock_streaming_response.__enter__ = Mock(
                        return_value=mock_streaming_response
                    )
                    mock_streaming_response.__exit__ = Mock(return_value=None)
                    # Set up iter_content to return an iterator that yields test data
                    # iter_content with decode_unicode=True returns strings, not bytes
                    test_data = '{"data": "test"}\n'
                    # iter_content is called as a method, so we need to make it return an iterator
                    mock_streaming_response.iter_content = Mock(
                        side_effect=lambda *args, **kwargs: iter([test_data])
                    )
                    # Make session.get return the context manager
                    mock_session.get.return_value = mock_streaming_response
                result = method(**kwargs)
                # Check if this is actually a streaming operation (returns Generator)
                is_streaming = isinstance(result, types.GeneratorType)
                if is_streaming:
                    # Consume the generator to trigger the HTTP request
                    # The HTTP request happens when entering the 'with' block inside the generator
                    # We need to actually iterate to trigger the request
                    try:
                        # Try to get first item - this will trigger the HTTP request
                        # The 'with' statement inside the generator will call session.get()
                        next(result)
                    except StopIteration:
                        # Generator exhausted immediately - request was still made
                        pass
                    except (
                        requests.exceptions.RequestException,
                        json.JSONDecodeError,
                        AttributeError,
                        ValueError,
                    ) as e:
                        # These exceptions can occur during streaming (request errors, JSON parsing, etc.)
                        # The request should still have been attempted
                        pass
                    # Don't catch other exceptions - if there's an error during setup (before the request),
                    # we want to know about it, and the request verification below will fail appropriately
                # Verify the request was made
                # For streaming operations, the request happens when entering the 'with' block
                # which occurs when we call next() on the generator
                mock_session.get.assert_called_once()
                # Verify request structure
                call_args = mock_session.get.call_args
                # Check URL structure
                called_url = (
                    call_args[0][0] if call_args[0] else call_args[1].get("url", "")
                )
                expected_path = "/2/openapi.json"
                assert expected_path.replace("{", "").replace(
                    "}", ""
                ) in called_url or any(
                    param in called_url for param in ["test_", "42"]
                ), f"URL should contain path template elements: {called_url}"
                # Verify response structure
                if is_streaming:
                    # For streaming, verify we got a generator
                    assert isinstance(
                        result, types.GeneratorType
                    ), "Streaming method should return a generator"
                else:
                    # For regular operations, verify we got a result
                    assert result is not None, "Method should return a result"
            except Exception as e:
                pytest.fail(f"Contract test failed for get_open_api_spec: {e}")


    def test_get_open_api_spec_required_parameters(self):
        """Test that get_open_api_spec handles parameters correctly."""
        method = getattr(self.general_client, "get_open_api_spec")
        # No required parameters, method should be callable without args
        with patch.object(self.client, "session") as mock_session:
            mock_response = Mock()
            mock_response.status_code = 200
            mock_response.json.return_value = {}
            mock_response.raise_for_status.return_value = None
            mock_session.get.return_value = mock_response
            try:
                method()
            except Exception as e:
                pytest.fail(f"Method with no required params should be callable: {e}")


    def test_get_open_api_spec_response_structure(self):
        """Test get_open_api_spec response structure validation."""
        with patch.object(self.client, "session") as mock_session:
            # Create mock response with expected structure
            mock_response_data = {}
            mock_response = Mock()
            mock_response.status_code = 200
            mock_response.json.return_value = mock_response_data
            mock_response.raise_for_status.return_value = None
            mock_session.get.return_value = mock_response
            # Prepare minimal valid parameters
            kwargs = {}
            # Add request body if required
            # Call method and verify response structure
            method = getattr(self.general_client, "get_open_api_spec")
            result = method(**kwargs)

            # Verify response object has expected attributes
