Skip to content

test(services): add direct unit tests for cloudwatch_client#1032

Merged
muddlebee merged 3 commits intoTracer-Cloud:mainfrom
GoDiao:issue/885-cloudwatch-client-tests
Apr 28, 2026
Merged

test(services): add direct unit tests for cloudwatch_client#1032
muddlebee merged 3 commits intoTracer-Cloud:mainfrom
GoDiao:issue/885-cloudwatch-client-tests

Conversation

@GoDiao
Copy link
Copy Markdown
Contributor

@GoDiao GoDiao commented Apr 28, 2026

Fixes #885

Describe the changes you have done in this PR -

Added tests/services/test_cloudwatch_client.py with 18 direct unit tests for the three service functions in app/services/cloudwatch_client.py:

  • TestGetMetricStatistics (6 tests): success path, dimensions/statistics passthrough, default statistics, ClientError handling, no-client fallback, credentials missing
  • TestFilterLogEvents (6 tests): success path, optional params passthrough, omitted optional params, ClientError handling, no-client fallback, credentials missing
  • TestGetLogEvents (6 tests): success path, optional params passthrough, omitted optional params, ClientError handling, no-client fallback, credentials missing

All tests mock make_boto3_client() and require_aws_credentials() as specified in the issue. Tool-registration assertions are intentionally excluded.


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:
Each service function (get_metric_statistics, filter_log_events, get_log_events) follows the same pattern: get a boto3 client → check credentials → call AWS API → return success/error. Tests mirror this pattern with three scenarios per function: happy path with correct parameter forwarding, error handling (ClientError / no client / no credentials), and optional parameter handling. I used unittest.mock.patch on the internal _get_cloudwatch_client and _get_cloudwatch_logs_client helpers to keep mocking minimal and focused.


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

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 28, 2026

Greptile Summary

Adds 18 direct unit tests for get_metric_statistics, filter_log_events, and get_log_events in app/services/cloudwatch_client.py, covering success paths, optional-parameter forwarding/omission, and error handling. Previous review concerns (module-level ClientError import, .kwargs access, and missing ClientError error-message assertions) are all addressed in this revision.

Confidence Score: 4/5

Safe to merge once the credentials tests are tightened; all previous review concerns have been addressed.

All prior P1/P2 findings (module-level import, .kwargs access, missing ClientError error-message assertions) are resolved. One remaining P2 concern: the three credentials tests don't mock the client helper, so the assertion success is False alone cannot distinguish between the "credentials missing" and "boto3 not available" code paths.

tests/services/test_cloudwatch_client.py — credentials test methods in all three classes

Important Files Changed

Filename Overview
tests/services/test_cloudwatch_client.py Adds 18 unit tests across three test classes covering happy-path, optional-parameter passthrough, and error cases; credentials tests lack the client mock fixture, leaving the tested code path unverifiable by assertion alone.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Test calls service function] --> B{client fixture\nrequested?}
    B -->|Yes| C[_get_cloudwatch_client returns MagicMock]
    B -->|No - credentials tests| D[_get_cloudwatch_client called for real]
    C --> E[require_aws_credentials checked]
    D --> E
    E -->|test overrides patch| F[Returns error dict]
    E -->|autouse patch returns None| G[Proceeds to boto3 call]
    F --> H[Assert success is False\nWARNING: no error-message assertion]
    G --> I{Simulated boto3 call}
    I -->|ClientError| J[Returns error dict\nAssert success + error message]
    I -->|return_value| K[Returns success dict\nAssert success + data]
Loading

Reviews (2): Last reviewed commit: "Address Greptile review: hoist imports, ..." | Re-trigger Greptile

Comment thread tests/services/test_cloudwatch_client.py Outdated
Comment thread tests/services/test_cloudwatch_client.py Outdated
Comment thread tests/services/test_cloudwatch_client.py
@muddlebee
Copy link
Copy Markdown
Collaborator

@GoDiao fix ci and review

@muddlebee
Copy link
Copy Markdown
Collaborator

@GoDiao greptile reviews not yet fixed. Could you pls handle them. Pls check carefully

@muddlebee muddlebee merged commit 5a53bed into Tracer-Cloud:main Apr 28, 2026
7 checks passed
@muddlebee
Copy link
Copy Markdown
Collaborator

@GoDiao thank you 🌟

@GoDiao
Copy link
Copy Markdown
Contributor Author

GoDiao commented Apr 28, 2026

@muddlebee Sorry for the back-and-forth! I'm new to open source contributions and still learning the ropes — didn't mean to waste your time with the lint issues and Greptile review items. All three Greptile suggestions should be addressed now (hoisted the ClientError import, switched to call_args.kwargs, and added consistent error-message assertions). Also fixed the ruff lint CI failure. Thanks for your patience!

cc @greptile-apps

@muddlebee
Copy link
Copy Markdown
Collaborator

@GoDiao no issues, nicely done. Hope you are having good time contributing :)

@muddlebee
Copy link
Copy Markdown
Collaborator

feel free to join our discord if you haven't already https://discord.com/invite/opensre

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/cloudwatch_client.py

2 participants