Skip to content

test: add unit tests for AWSBatchJobsMixin and fix timezone bug#1006

Merged
VaibhavUpreti merged 2 commits intoTracer-Cloud:mainfrom
kespineira:feat/aws-batch-jobs-unit-tests
Apr 27, 2026
Merged

test: add unit tests for AWSBatchJobsMixin and fix timezone bug#1006
VaibhavUpreti merged 2 commits intoTracer-Cloud:mainfrom
kespineira:feat/aws-batch-jobs-unit-tests

Conversation

@kespineira
Copy link
Copy Markdown
Contributor

@kespineira kespineira commented Apr 27, 2026

Fixes #892

Describe the changes you have made in this PR -

Two changes were made in app/services/tracer_client/aws_batch_jobs.py:

  1. Timezone fix: datetime.fromtimestamp() now uses tz=UTC explicitly. Without this, AWS Batch timestamp conversion depended on the local timezone, causing tests to fail in CI (which runs with TZ=UTC).

  2. 14 unit tests in tests/services/tracer_client/test_aws_batch_jobs.py:

    • TestNoTraceId (2 tests): behavior when no trace_id is passed, both in typed and raw dict modes.
    • TestStatusFilter (3 tests): default values (SUCCEEDED, FAILED, RUNNING), custom filters, and endpoint verification.
    • TestTypedReturnMode (9 tests): job parsing, extracted fields (vcpu, memory, gpu, exit_code, failure_reason), failed/succeeded counts, failure reason extraction from first failed job, timestamp conversion, and empty data or unsuccessful response cases.
    • TestRawReturnMode (1 test): return_dict=True returns the raw response without parsing.

Tests use _FakeBatchJobsClient and _FakeBatchJobsClientWithCapture which stub _get(), with no real HTTP calls.

Demo/Screenshot for feature changes and bug fixes -

# Local (CEST)
uv run python -m pytest tests/services/tracer_client/test_aws_batch_jobs.py -v
# 14 passed

# CI simulation (UTC)
env TZ=UTC uv run python -m pytest tests/services/tracer_client/test_aws_batch_jobs.py -v
# 14 passed

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

  • What problem does your code solve? Adds unit test coverage for AWSBatchJobsMixin.get_batch_jobs() which had none, and fixes a timezone bug in timestamp conversion that would cause tests to fail in CI.

  • What alternative approaches did you consider? I could have used pytest-mock or unittest.mock, but following the existing pattern in test_tracer_pipelines.py, a fake subclass that stubs _get() is simpler and more idiomatic.

  • Why did you choose this specific implementation? The fake subclass pattern is already established in the codebase (see test_tracer_pipelines.py). It keeps tests readable and avoids complex mock setup.

  • What are the key functions/components and what do they do?

    • _FakeBatchJobsClient — returns a configurable response from _get(), used for testing return value parsing.
    • _FakeBatchJobsClientWithCapture — captures the endpoint and params passed to _get(), used for verifying API calls.
    • TestNoTraceId — verifies early return behavior when no trace_id is provided (both typed and raw modes).
    • TestStatusFilter — verifies default and custom status filters are passed correctly to the API params.
    • TestTypedReturnMode — verifies AWSBatchJobResult parsing: job fields, failure reason extraction, timestamp conversion, counts.
    • TestRawReturnMode — verifies return_dict=True returns the raw API response unchanged.
    • Timezone fix in aws_batch_jobs.py — uses tz=UTC in datetime.fromtimestamp() so the conversion is deterministic regardless of the local timezone.

Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Note: Please check Allow edits from maintainers if you would like us to assist in the PR.

Add 14 unit tests covering no-trace-id behavior, status filters,
typed/raw return modes, failure reason extraction, and timestamp
conversion. Use fake subclasses that stub _get() to avoid HTTP calls.

Fix datetime.fromtimestamp() to use tz=UTC so timestamp conversion
is timezone-independent and tests pass in CI (TZ=UTC).
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 27, 2026

Greptile Summary

This PR adds 14 unit tests for AWSBatchJobsMixin.get_batch_jobs() and fixes a timezone bug by passing tz=UTC to datetime.fromtimestamp(), ensuring consistent UTC output regardless of the host's local timezone. The fix is correct: datetime.UTC is available on Python ≥3.11, which matches the project's requires-python = ">=3.11" constraint.

Confidence Score: 5/5

Safe to merge — the timezone fix is correct, Python version compatibility is satisfied, and tests are comprehensive.

No P0 or P1 findings. The tz=UTC fix is semantically correct and datetime.UTC is available on Python ≥3.11 as required. The 14 tests cover all meaningful code paths; the hardcoded expected timestamp "2024-04-24 12:00:00" is arithmetically correct for epoch 1713960000000 ms in UTC. All remaining observations are P2 or below.

No files require special attention.

Important Files Changed

Filename Overview
app/services/tracer_client/aws_batch_jobs.py One-line change: adds tz=UTC to datetime.fromtimestamp() to guarantee UTC output; import of UTC from datetime is valid for Python ≥3.11.
tests/services/tracer_client/test_aws_batch_jobs.py New test file with 14 tests across 4 classes; covers no-trace-id, status filters, typed/raw return modes, failure reason extraction, and timestamp conversion. Hardcoded expected timestamp "2024-04-24 12:00:00" correctly matches epoch 1713960000000 ms in UTC.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A([get_batch_jobs called]) --> B{trace_id provided?}
    B -- No --> C{return_dict?}
    C -- Yes --> D[Return hardcoded no-trace-id dict]
    C -- No --> E[Return AWSBatchJobResult found=False]
    B -- Yes --> F[Build params with traceId and orgId]
    F --> G{statuses arg?}
    G -- Provided --> H[Use provided statuses]
    G -- None --> I[Default to SUCCEEDED/FAILED/RUNNING]
    H & I --> J[_get /api/aws/batch/jobs/completed]
    J --> K{return_dict?}
    K -- Yes --> L[Return raw dict]
    K -- No --> M{success AND data non-empty?}
    M -- No --> E
    M -- Yes --> N[Parse each job row]
    N --> O{row status?}
    O -- FAILED --> P[Increment failed count and capture first failure reason]
    O -- SUCCEEDED --> Q[Increment succeeded count]
    O -- Other --> R[Count in total only]
    P & Q & R --> S{startedAt present?}
    S -- Yes --> T[fromtimestamp with tz=UTC then strftime]
    S -- No --> U[started_at = None]
    T & U --> V[Append job dict to list]
    V --> W[Return AWSBatchJobResult with found=True]
Loading

Reviews (1): Last reviewed commit: "style: fix formatting of datetime.fromti..." | Re-trigger Greptile

Copy link
Copy Markdown
Member

@VaibhavUpreti VaibhavUpreti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome @kespineira great job, welcome to the OpenSRE community!

@VaibhavUpreti VaibhavUpreti merged commit ff72d40 into Tracer-Cloud:main Apr 27, 2026
7 checks passed
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.

Add direct unit tests for app/services/tracer_client/aws_batch_jobs.py

2 participants