Skip to content

test: add unit and integration tests for backend APIs and risk calculation#201

Open
Suhaskumard wants to merge 1 commit into
thetechguardians:mainfrom
Suhaskumard:feature/backend-api-risk-testing
Open

test: add unit and integration tests for backend APIs and risk calculation#201
Suhaskumard wants to merge 1 commit into
thetechguardians:mainfrom
Suhaskumard:feature/backend-api-risk-testing

Conversation

@Suhaskumard

Copy link
Copy Markdown

Add Comprehensive Backend Testing Suite and Risk Logic Refactoring

This PR introduces a comprehensive backend testing suite for Climate-Shield and refactors the weather risk calculation logic into a dedicated module to improve maintainability, testability, and code organization.

Closes #182

Type of Change

☑️ Feature
☑️ Backend
☑️ Testing
☑️ Refactoring
☑️ Documentation

Changes Made

  • Refactored weather risk calculation logic from alertsystem.py into a dedicated risk_calculator.py module

  • Improved separation of concerns by isolating business logic from API routes

  • Added centralized risk calculation utilities for:

    • Flood risk
    • Drought risk
    • Heat risk
    • Cyclone risk
    • Wildfire risk
  • Added Pytest-based testing infrastructure

  • Added reusable test fixtures through conftest.py

  • Added unit tests for risk calculation algorithms

  • Added integration tests for weather API endpoints

  • Added integration tests for chatbot API endpoints

  • Added validation for:

    • Normal weather conditions
    • Extreme heat scenarios
    • Extreme flood scenarios
    • Extreme drought scenarios
    • Extreme cold scenarios
  • Added API error handling tests for:

    • Missing request fields
    • Missing API keys
    • Invalid locations
    • Timeout exceptions
    • Connection failures
  • Added chatbot validation tests for:

    • Greetings
    • Knowledge base queries
    • Context-aware responses
    • Unknown topics
    • Empty payload handling
  • Added implementation walkthrough documentation

Files Added

File Purpose
backend/risk_calculator.py Centralized weather risk calculation logic
backend/conftest.py Shared Pytest fixtures and mocks
backend/test_risk_calculator.py Unit tests for risk calculation algorithms
backend/test_weather_api.py Integration tests for weather API endpoints
backend/test_chatbot_api.py Integration tests for chatbot API endpoints
walkthrough.md Testing suite implementation guide

Files Modified

File Purpose
backend/alertsystem.py Updated to use extracted risk calculation module
task.md Updated implementation tracking and completion notes

How Has This Been Tested?

  • Verified risk score calculations under normal weather conditions
  • Verified extreme heat risk calculations
  • Verified extreme flood risk calculations
  • Verified extreme drought risk calculations
  • Verified extreme cold weather handling
  • Verified risk values are properly bounded and validated
  • Verified chatbot endpoint responses for valid and invalid inputs
  • Verified chatbot fallback behavior for unsupported queries
  • Verified weather endpoint validation logic
  • Verified API behavior when OpenWeatherMap API key is missing
  • Verified location-not-found handling
  • Verified timeout exception handling
  • Verified connection error handling
  • Executed full backend test suite using Pytest

Test Results

============================= 22 passed in 0.11s =============================

Checklist

☑️ My code follows the existing code style of the project
☑️ I have performed a self-review of my own code
☑️ I have added/updated tests where necessary
☑️ All backend tests pass successfully
☑️ My changes do not introduce any new warnings or errors
☑️ I have linked the related issue

@Suhaskumard

Copy link
Copy Markdown
Author

Hi @thetechguardians,
I have solved the issue. If there are any changes, kindly let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEATURE:Unit & Integration Testing for Backend APIs and Risk Calculation Logic

1 participant