help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
weather-chatbot
/
tests
RSK World
weather-chatbot
Weather Chatbot - Python + Flask + OpenWeatherMap + OpenAI + Weather Forecast + Weather Alerts + Natural Language Processing
tests
  • __init__.py110 B
  • conftest.py1.3 KB
  • test_app.py3.3 KB
  • test_utils.py1.8 KB
  • test_weather_api.py1.6 KB
conftest.pytest_app.py
tests/conftest.py
Raw Download
Find: Go to:
#!/usr/bin/env python3
"""
Pytest Configuration File
==========================

Author: RSK World (https://rskworld.in)
Year: 2026

Description: Pytest configuration and fixtures
"""

import pytest
import os
import sys
from unittest.mock import MagicMock, patch

# Add parent directory to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

@pytest.fixture
def mock_openweather_api():
    """Mock OpenWeatherMap API response."""
    return {
        'name': 'London',
        'sys': {'country': 'GB'},
        'main': {
            'temp': 15.5,
            'feels_like': 14.2,
            'humidity': 65,
            'pressure': 1013
        },
        'weather': [{'description': 'clear sky', 'icon': '01d'}],
        'wind': {'speed': 3.5},
        'visibility': 10000
    }

@pytest.fixture
def mock_env_vars(monkeypatch):
    """Mock environment variables."""
    monkeypatch.setenv('OPENWEATHER_API_KEY', 'test_openweather_key')
    monkeypatch.setenv('OPENAI_API_KEY', 'test_openai_key')
    monkeypatch.setenv('SECRET_KEY', 'test_secret_key')

@pytest.fixture(scope='session')
def test_db_path(tmp_path_factory):
    """Create a temporary database path for testing."""
    db_path = tmp_path_factory.mktemp('test_db') / 'test_weather_chatbot.db'
    return str(db_path)
49 lines•1.3 KB
python
tests/test_app.py
Raw Download
Find: Go to:
#!/usr/bin/env python3
"""
Weather Chatbot Application Tests
==================================

Author: RSK World (https://rskworld.in)
Year: 2026

Description: Unit tests for the Weather Chatbot Flask application
"""

import pytest
import os
import sys
from unittest.mock import patch, MagicMock

# Add parent directory to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app import app, chatbot

@pytest.fixture
def client():
    """Create a test client for the Flask application."""
    app.config['TESTING'] = True
    app.config['SECRET_KEY'] = 'test-secret-key'
    with app.test_client() as client:
        yield client

def test_index_route(client):
    """Test the index route."""
    response = client.get('/')
    assert response.status_code == 200
    assert b'Weather Chatbot' in response.data

def test_health_check(client):
    """Test the health check endpoint."""
    response = client.get('/health')
    assert response.status_code == 200
    data = response.get_json()
    assert data['status'] == 'healthy'
    assert 'timestamp' in data

def test_chat_route_empty_message(client):
    """Test chat route with empty message."""
    response = client.post('/chat', data={'message': ''})
    assert response.status_code == 200
    data = response.get_json()
    assert 'error' in data

def test_chat_route_with_message(client):
    """Test chat route with a message."""
    with patch.object(chatbot, 'chat', return_value={'city': 'London', 'temperature': 15}):
        response = client.post('/chat', data={'message': 'What is the weather in London?'})
        assert response.status_code == 200
        data = response.get_json()
        assert 'city' in data or 'error' in data

def test_weather_route(client):
    """Test weather endpoint."""
    with patch.object(chatbot, 'get_weather_by_city', return_value={'city': 'London', 'temperature': 15}):
        response = client.get('/weather/London')
        assert response.status_code == 200
        data = response.get_json()
        assert 'city' in data or 'error' in data

def test_forecast_route(client):
    """Test forecast endpoint."""
    with patch.object(chatbot, 'get_forecast_by_city', return_value={'city': 'London', 'forecasts': []}):
        response = client.get('/forecast/London')
        assert response.status_code == 200
        data = response.get_json()
        assert 'city' in data or 'error' in data

def test_alerts_route(client):
    """Test alerts endpoint."""
    with patch.object(chatbot, 'get_weather_alerts', return_value={'city': 'London', 'alerts': []}):
        response = client.get('/alerts/London')
        assert response.status_code == 200
        data = response.get_json()
        assert 'city' in data or 'error' in data

def test_404_error(client):
    """Test 404 error handling."""
    response = client.get('/nonexistent')
    assert response.status_code == 404

def test_500_error_handling(client):
    """Test 500 error handling."""
    with patch.object(chatbot, 'chat', side_effect=Exception('Test error')):
        response = client.post('/chat', data={'message': 'test'})
        assert response.status_code == 200
        data = response.get_json()
        assert 'error' in data
95 lines•3.3 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer