#!/usr/bin/env python3
"""
Weather API Tests
=================

Author: RSK World (https://rskworld.in)
Year: 2026

Description: Unit tests for weather API functionality
"""

import pytest
import os
import sys
from unittest.mock import patch, MagicMock

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from weather_api import get_weather

def test_get_weather_success():
    """Test successful weather API call."""
    mock_response = MagicMock()
    mock_response.json.return_value = {
        'name': 'London',
        'main': {'temp': 15, 'humidity': 60},
        'weather': [{'description': 'clear sky'}]
    }
    mock_response.raise_for_status = MagicMock()
    
    with patch('weather_api.requests.get', return_value=mock_response):
        with patch.dict(os.environ, {'OPENWEATHER_API_KEY': 'test_key'}):
            result = get_weather('London')
            assert 'name' in result
            assert result['name'] == 'London'

def test_get_weather_error():
    """Test weather API error handling."""
    with patch('weather_api.requests.get', side_effect=Exception('Network error')):
        with patch.dict(os.environ, {'OPENWEATHER_API_KEY': 'test_key'}):
            result = get_weather('London')
            assert 'error' in result

def test_get_weather_missing_key():
    """Test weather API with missing API key."""
    with patch.dict(os.environ, {}, clear=True):
        result = get_weather('London')
        # Should handle missing key gracefully
        assert result is not None
