Skip to content

test : add unit tests for WorkflowScheduler._should_run datetime logic #1704

Description

@tmdeveloper007

Summary of What Needs to be Done:

Add unit tests for WorkflowScheduler._should_run in backend/secuscan/workflows.py. This method is a pure datetime-comparison function that determines whether a scheduled workflow should run based on the elapsed time since its last execution. It currently has no test coverage.

The method handles three cases:

  1. last_run_at is None (never run) → return True
  2. last_run_at is a naive datetime (SQLite returns no timezone suffix) → treat as UTC
  3. last_run_at is an aware datetime → compare with current UTC time
  4. elapsed >= schedule_seconds → return True, else False

Changes that Need to be Made:

Create testing/backend/unit/test_workflows_scheduler_should_run.py with the following test cases:

  • test_no_last_run_at_returns_true: when last_run_at=None, _should_run returns True regardless of schedule_seconds
  • test_elapsed_exceeds_schedule_returns_true: when enough time has passed, returns True
  • test_elapsed_less_than_schedule_returns_false: when not enough time has passed, returns False
  • test_naive_datetime_treated_as_utc: SQLite datetime format (no Z, no +00:00) is treated as UTC — a naive datetime 30s ago with schedule_seconds=60 should return False
  • test_aware_datetime_comparison_correct: an aware datetime 120s ago with schedule_seconds=60 returns True
  • test_zero_schedule_seconds: schedule_seconds=0 always returns True
  • test_exactly_at_boundary: elapsed == schedule_seconds returns True

Import the real WorkflowScheduler from backend.secuscan.workflows. Instantiate it directly (WorkflowScheduler()) — the _should_run method does not require a running scheduler or database.

Impact that it would Provide:

  • Reliability: ensures the scheduler respects timing constraints correctly
  • Regression safety: prevents silent scheduler overruns if datetime handling changes
  • Documentation: the test cases document the expected behavior for naive vs aware datetimes

Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions