test: add unit tests for AWSBatchJobsMixin and fix timezone bug#1006
Conversation
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 SummaryThis PR adds 14 unit tests for Confidence Score: 5/5Safe to merge — the timezone fix is correct, Python version compatibility is satisfied, and tests are comprehensive. No P0 or P1 findings. The No files require special attention. Important Files Changed
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]
Reviews (1): Last reviewed commit: "style: fix formatting of datetime.fromti..." | Re-trigger Greptile |
VaibhavUpreti
left a comment
There was a problem hiding this comment.
awesome @kespineira great job, welcome to the OpenSRE community!
Fixes #892
Describe the changes you have made in this PR -
Two changes were made in
app/services/tracer_client/aws_batch_jobs.py:Timezone fix:
datetime.fromtimestamp()now usestz=UTCexplicitly. Without this, AWS Batch timestamp conversion depended on the local timezone, causing tests to fail in CI (which runs withTZ=UTC).14 unit tests in
tests/services/tracer_client/test_aws_batch_jobs.py:TestNoTraceId(2 tests): behavior when notrace_idis 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=Truereturns the raw response without parsing.Tests use
_FakeBatchJobsClientand_FakeBatchJobsClientWithCapturewhich stub_get(), with no real HTTP calls.Demo/Screenshot for feature changes and bug fixes -
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
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-mockorunittest.mock, but following the existing pattern intest_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— verifiesAWSBatchJobResultparsing: job fields, failure reason extraction, timestamp conversion, counts.TestRawReturnMode— verifiesreturn_dict=Truereturns the raw API response unchanged.aws_batch_jobs.py— usestz=UTCindatetime.fromtimestamp()so the conversion is deterministic regardless of the local timezone.Checklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.