Skip to content

test(tracer): add unit tests for get_logs parameter construction#983

Merged
VaibhavUpreti merged 1 commit intoTracer-Cloud:mainfrom
Piyushtiwari919:fix/tracer-logs-tests
Apr 27, 2026
Merged

test(tracer): add unit tests for get_logs parameter construction#983
VaibhavUpreti merged 1 commit intoTracer-Cloud:mainfrom
Piyushtiwari919:fix/tracer-logs-tests

Conversation

@Piyushtiwari919
Copy link
Copy Markdown
Contributor

@Piyushtiwari919 Piyushtiwari919 commented Apr 26, 2026

Introduces a DummyTracerClient with a mocked _get method to isolate and test the TracerLogsMixin without making real HTTP requests. Coverage includes default parameter construction (size=100), trace_id precedence, and run_id fallback behavior.

Fixes #890

Describe the changes you have made in this PR -

  • Added tests/services/tracer_client/test_tracer_logs.py
  • Created a DummyTracerClient class to attach the mixin and mock the internal _get network call using MagicMock.
  • Added 3 focused unit tests to verify:
    1. Default parameter construction (size=100, orgId).
    2. Precedence logic (ensuring trace_id overrides run_id when both are passed).
    3. Fallback logic (ensuring run_id is used when trace_id is missing).
  • Ensured code passes all local ruff linting and pytest coverage requirements.

Demo/Screenshot for feature changes and bug fixes -

Screenshot 2026-04-26 230203

Code Understanding and AI Usage

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

  • No, I wrote all the code myself
  • 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? It allows us to safely test the parameter-building logic of TracerLogsMixin without triggering real HTTP requests to an OpenSearch database, which would make the tests flaky and dependent on network status.
  • Alternative approaches: The alternative would be an integration test hitting a real database or a mocked local server, which is too heavy for testing simple dictionary construction.
  • Why this implementation? Using Python's MagicMock acts as a spy, allowing us to intercept the _get call and assert exactly what parameters were passed. Creating a DummyTracerClient is the cleanest way to test a standalone mixin.
  • Key functions: DummyTracerClient initializes the spy. The three test functions handle the empty state, the collision state (both IDs), and the fallback state.

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

Introduces a \DummyTracerClient\ with a mocked \_get\ method to isolate and test the \TracerLogsMixin\ without making real HTTP requests. Coverage includes default parameter construction (size=100), \	race_id\ precedence, and \
un_id\ fallback behavior.

Closes Tracer-Cloud#890
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 26, 2026

Greptile Summary

Adds three focused unit tests for TracerLogsMixin.get_logs using a lightweight DummyTracerClient fixture with a MagicMock spy on _get. The tests correctly validate default parameter construction, trace_id precedence over run_id, and the run_id fallback path. All findings are P2 style/coverage suggestions.

Confidence Score: 5/5

Safe to merge; test logic is correct and all findings are minor P2 style/coverage suggestions.

All three assertions match the actual implementation behaviour. No P0/P1 defects were found. The remaining comments are about future-proofing the dummy fixture and adding a missing size parameter test case — neither blocks correctness today.

No files require special attention.

Important Files Changed

Filename Overview
tests/services/tracer_client/test_tracer_logs.py Adds three unit tests for TracerLogsMixin.get_logs using a DummyTracerClient that mocks _get; correctly verifies default params, trace_id precedence, and run_id fallback, but skips super().__init__() and lacks a custom-size test.

Sequence Diagram

sequenceDiagram
    participant T as Test
    participant D as DummyTracerClient
    participant M as TracerLogsMixin.get_logs()
    participant G as _get (MagicMock)

    T->>D: DummyTracerClient()
    Note over D: org_id = "test_org_123"<br/>_get = MagicMock

    T->>M: get_logs(trace_id?, run_id?, size=100)
    M->>M: build params {orgId, size}
    alt trace_id provided
        M->>M: params[runId] = trace_id
    else run_id provided
        M->>M: params[runId] = run_id
    end
    M->>G: _get("/api/opensearch/logs", params)
    G-->>M: {success: True, data: []}
    M-->>T: returns dict

    T->>G: assert_called_once_with(path, expected_params)
Loading

Reviews (1): Last reviewed commit: "test(tracer): add unit tests for get_log..." | Re-trigger Greptile

Comment on lines +29 to +36

client._get.assert_called_once_with(
"/api/opensearch/logs",
{"orgId": "test_org_123", "size": 100, "runId": "trace_abc"}
)


def test_get_logs_run_id_fallback():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Misleading assertion key in precedence test

The test name test_get_logs_trace_id_precedence asserts "runId": "trace_abc", which is technically correct given the implementation maps trace_idparams["runId"]. However the test name and docstring say "precedence" but the assertion doesn't actually verify that run_id="run_xyz" was ignored — it only checks that the final dict value equals the trace_id value. Adding an explicit assertNotIn or checking the value isn't "run_xyz" would make the intent clearer.

More importantly, the assertion key "runId" for a value that came from the trace_id argument mirrors a potential naming confusion in the underlying implementation. If this mapping is intentional, a brief comment documenting why trace_id feeds the runId field would help future readers.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +6 to +11
class DummyTracerClient(TracerLogsMixin):
"""Dummy client to isolate and test the TracerLogsMixin."""

def __init__(self):
self.org_id = "test_org_123"
self._get = MagicMock(return_value={"success": True, "data": []})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 super().__init__() skipped silently

DummyTracerClient.__init__ manually sets self.org_id and self._get without calling super().__init__(). TracerClientBase.__init__ initialises self.base_url, self.organization_slug, and an httpx.Client. While get_logs only relies on org_id and _get, any future mixin that touches self.base_url or self.organization_slug will raise an AttributeError silently with this pattern. Consider using unittest.mock.patch or a @pytest.fixture with spec=TracerClientBase to prevent this drift.

Comment on lines +13 to +21

def test_get_logs_default_size():
"""Cover default size."""
client = DummyTracerClient()
client.get_logs()

client._get.assert_called_once_with(
"/api/opensearch/logs",
{"orgId": "test_org_123", "size": 100}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 No coverage for custom size parameter

The suite tests the default size=100 but never passes an explicit size (e.g. client.get_logs(size=50)). The current three tests would still pass even if the size argument were completely ignored by the implementation. Adding one test for a custom size would close this gap.

from app.services.tracer_client.tracer_logs import TracerLogsMixin


class DummyTracerClient(TracerLogsMixin):
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 @Piyushtiwari919 , merging this for velocity, I'll fix the changes on main

@VaibhavUpreti VaibhavUpreti merged commit 07ce809 into Tracer-Cloud:main Apr 27, 2026
5 of 7 checks passed
@Piyushtiwari919
Copy link
Copy Markdown
Contributor Author

Thanks @VaibhavUpreti , sounds great. Thanks for stepping in to handle the final linter adjustments. Have a great week!

@Piyushtiwari919 Piyushtiwari919 deleted the fix/tracer-logs-tests branch April 27, 2026 15:53
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/tracer_logs.py

3 participants