Skip to content

Test/add lambda client unit tests#1028

Merged
muddlebee merged 3 commits intoTracer-Cloud:mainfrom
gitsofaryan:test/add-lambda-client-unit-tests
Apr 28, 2026
Merged

Test/add lambda client unit tests#1028
muddlebee merged 3 commits intoTracer-Cloud:mainfrom
gitsofaryan:test/add-lambda-client-unit-tests

Conversation

@gitsofaryan
Copy link
Copy Markdown
Contributor

Fixes #887

Describe the changes you have made in this PR -

Added direct unit tests for the Lambda service client in app/services/lambda_client.py. These tests ensure that function inspection, code extraction, and log retrieval logic are directly protected by unit tests rather than just through tool-level tests.

Key coverage includes:

  1. Function Configuration: Verified field mapping and error handling for get_function_configuration.
  2. Code Extraction:
    • Mocked ZipFile in memory to verify extraction of text files.
    • Verified handling of binary files (UnicodeDecodeError).
    • Tested truncation logic for files exceeding max_file_size.
    • Verified that oversized deployment packages (>5MB) are not downloaded.
  3. Log Grouping:
    • Verified that get_recent_invocations correctly groups START, END, and REPORT log events into discrete invocation objects.
    • Ensured duration and memory metrics are correctly parsed from REPORT messages.
    • Tested handling of multiple/sequential invocations in the same log stream.
  4. Edge Cases:
    • Handled ResourceNotFoundException for missing log groups or functions.
    • Verified functionality of invoke_function and list_functions.

Demo/Screenshot for feature changes and bug fixes -

tests/services/test_lambda_client.py .............                       [100%]

============================= 13 passed in 1.39s ==============================

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 (Antigravity)

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:
The tests utilize unittest.mock to stub boto3 clients and requests.get to avoid any real AWS or network calls. A memory-resident ZipFile was used to simulate deployment package extraction, allowing for precise testing of text, binary, and size-limited file handling. The log parsing logic was tested against realistic CloudWatch Log event sequences to ensure robust grouping of Lambda invocations.


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

This PR adds 13 unit tests for app/services/lambda_client.py, covering function configuration, code extraction (text, binary, truncation, oversized packages), CloudWatch log parsing, and error paths. All tests use unittest.mock to avoid real AWS calls and pass cleanly.

Confidence Score: 5/5

Safe to merge; all remaining findings are P2 style/coverage suggestions with no blocking issues.

Both findings are P2: one is a test-coverage gap (malformed REPORT format means duration parsing isn't exercised in the multi-invocation test) and the other is a minor assertion-placement style issue. Neither breaks the test suite or production behavior.

tests/services/test_lambda_client.py — minor REPORT format and assertion-scope issues noted inline.

Important Files Changed

Filename Overview
tests/services/test_lambda_client.py New unit test file covering all 6 public functions of lambda_client.py (13 tests). Good overall coverage of success paths, error handling, zip extraction, and log parsing; two minor issues found: malformed REPORT format in the multi-invocation test silently skips duration-parsing coverage, and one assertion is placed outside its patch context manager.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[test_get_function_configuration_success] --> B[get_function_configuration]
    C[test_get_function_configuration_error] --> B
    D[test_get_function_code_success] --> E[get_function_code]
    F[test_get_function_code_no_extract] --> E
    G[test_get_function_code_too_large] --> E
    H[test_get_function_code_download_error] --> E
    I[test_get_function_code_corrupt_zip] --> E
    J[test_get_recent_invocations_success] --> K[get_recent_invocations]
    L[test_get_recent_invocations_resource_not_found] --> K
    M[test_get_recent_invocations_multiple] --> K
    N[test_get_invocation_logs_by_request_id_success] --> O[get_invocation_logs_by_request_id]
    P[test_invoke_function_success] --> Q[invoke_function]
    R[test_list_functions_success] --> S[list_functions]

    E -->|extract_files=True, size < 5MB| T{Download ZIP}
    T -->|200 OK| U[Parse files: text / binary / truncated]
    T -->|non-200| V[skip files key]
    T -->|bad ZIP| W[extract_error key]
    E -->|extract_files=False OR size >= 5MB| V

    K --> X{Parse CloudWatch events}
    X -->|START| Y[new invocation]
    X -->|END| Z[set end_time]
    X -->|REPORT| AA[parse duration + memory, flush invocation]
    X -->|other| BB[append to current logs]
Loading

Reviews (1): Last reviewed commit: "fix: resolve ruff formatting issues" | Re-trigger Greptile

Comment thread tests/services/test_lambda_client.py Outdated
Comment thread tests/services/test_lambda_client.py Outdated
@gitsofaryan
Copy link
Copy Markdown
Contributor Author

@muddlebee PTAL!

@muddlebee muddlebee merged commit 7e0aee3 into Tracer-Cloud:main Apr 28, 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/lambda_client.py

2 participants