Skip to content

🧪 QA: Add unit test for log_event function#326

Open
daggerstuff wants to merge 9 commits intostagingfrom
qa/log-event-test-10604391926475469914
Open

🧪 QA: Add unit test for log_event function#326
daggerstuff wants to merge 9 commits intostagingfrom
qa/log-event-test-10604391926475469914

Conversation

@daggerstuff
Copy link
Copy Markdown
Owner

@daggerstuff daggerstuff commented Mar 31, 2026

🎯 What: Added unit test coverage for log_event in workflow/worker.py
📊 Coverage: Validates event generation, timestamp addition, and file writing logic.
Result: Improves test coverage for the workflow module and prevents future regressions.


PR created automatically by Jules for task 10604391926475469914 started by @daggerstuff

Summary by Sourcery

Tests:

  • Add a unit test verifying that log_event writes a single JSON line with event_id, message, and timestamp to the configured log path.

Summary by cubic

Add a unit test for worker.log_event that writes a single JSON line to a temporary LOG_PATH and restores the original. Asserts event_id, message, and a timestamp to improve coverage and prevent regressions.

Written for commit 0c2810e. Summary will update on new commits.

Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 31, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pixelated Ready Ready Preview, Comment Apr 1, 2026 2:14am

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 31, 2026

Reviewer's Guide

Adds a new unit test module for worker.log_event that uses a temporary log file to validate JSON log line contents and timestamp presence without modifying existing production code.

File-Level Changes

Change Details Files
Add unit test for log_event that verifies log file output and JSON fields using a temporary log path.
  • Introduce TestWorker unittest.TestCase with setUp/tearDown to override worker.LOG_PATH with a temporary file
  • Invoke worker.log_event with a sample event_id and message, then read back the log file contents
  • Assert that exactly one log line is written and that its JSON includes expected event_id, message, and a timestamp field
workflow/test_worker.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

Warning

Rate limit exceeded

@daggerstuff has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 26 minutes and 15 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 26 minutes and 15 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e58eeb83-b981-4b2f-894d-c129756388cd

📥 Commits

Reviewing files that changed from the base of the PR and between 8396cd9 and 0c2810e.

📒 Files selected for processing (1)
  • workflow/test_worker.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch qa/log-event-test-10604391926475469914

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Overriding the global worker.LOG_PATH in setUp may cause interference if tests are run in parallel; consider using unittest.mock.patch (e.g., @patch('worker.LOG_PATH', new_callable=...)) or a context manager to isolate this change per test.
  • The explicit if __name__ == "__main__": unittest.main() block is unnecessary for typical test runners and can be removed to keep the test module minimal.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Overriding the global `worker.LOG_PATH` in `setUp` may cause interference if tests are run in parallel; consider using `unittest.mock.patch` (e.g., `@patch('worker.LOG_PATH', new_callable=...)`) or a context manager to isolate this change per test.
- The explicit `if __name__ == "__main__": unittest.main()` block is unnecessary for typical test runners and can be removed to keep the test module minimal.

Fix all in Cursor


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

The test is a good start, but it has two reliability hazards: importing worker without a package-qualified path can break depending on test invocation, and leaving the mkstemp() file descriptor open can cause write failures on Windows due to file locking. Consider scoping LOG_PATH overrides with unittest.mock.patch to reduce global state leakage.

Additional notes (2)
  • Maintainability | workflow/test_worker.py:10-16
    Mutating worker.LOG_PATH as a global test side effect is workable, but it’s easy to leak state if additional tests are added or if failures happen before tearDown runs (or if tests are parallelized). A scoped patch is more robust.

  • Maintainability | workflow/test_worker.py:13-16
    os.remove(self.temp_path) will raise if the file is already removed (or if something went wrong before it was created). Making cleanup resilient reduces test brittleness and prevents masking the real failure with a teardown error.

Summary of changes

Summary

  • Added a new unittest module at workflow/test_worker.py to cover worker.log_event().
  • Test writes to a temporary file by overriding worker.LOG_PATH, then asserts:
    • exactly one JSON line is written
    • event_id and message fields match
    • a timestamp field is present
  • Includes setUp/tearDown for temp file creation and cleanup, plus __main__ execution via unittest.main().

import os
import tempfile
import json
import worker
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

workflow/test_worker.py importing worker directly is fragile: depending on how tests are executed (repo root vs workflow/ as CWD), this can import the wrong module or fail. It’s safer to import via the package/module path used by the application (e.g., from workflow import worker or a relative import).

Suggestion

Prefer an absolute/relative package import so the test runs reliably under CI runners invoked from the repo root.

from workflow import worker
# or, if `workflow` is a package:
from . import worker

Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this change.

Comment on lines +9 to +12
self.fd, self.temp_path = tempfile.mkstemp()
self.original_log_path = worker.LOG_PATH
worker.LOG_PATH = self.temp_path

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tempfile.mkstemp() returns an open file descriptor. Keeping it open while worker.log_event() opens/writes the same path can fail on Windows due to file locking/sharing semantics. Close the FD immediately after creating the path (or use NamedTemporaryFile(delete=False) and close it).

Suggestion

Close the mkstemp() descriptor right away (and set fd=None so tearDown doesn’t double-close), or switch to NamedTemporaryFile.

def setUp(self):
    self.fd, self.temp_path = tempfile.mkstemp()
    os.close(self.fd)
    self.fd = None
    self.original_log_path = worker.LOG_PATH
    worker.LOG_PATH = self.temp_path

def tearDown(self):
    worker.LOG_PATH = self.original_log_path
    if self.fd is not None:
        os.close(self.fd)
    os.remove(self.temp_path)

Reply with "@CharlieHelps yes please" if you’d like me to add a commit with this suggestion.

@charliecreates charliecreates bot removed the request for review from CharlieHelps March 31, 2026 20:00
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="workflow/test_worker.py">

<violation number="1" location="workflow/test_worker.py:1">
P2: This test file is outside the configured pytest testpaths ("tests" and "ai/core/tests"), so it won’t be collected or run in CI. Move it under the tests/ hierarchy or update pytest testpaths to include workflow/ so the coverage actually runs.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@@ -0,0 +1,29 @@
import unittest
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Mar 31, 2026

Choose a reason for hiding this comment

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

P2: This test file is outside the configured pytest testpaths ("tests" and "ai/core/tests"), so it won’t be collected or run in CI. Move it under the tests/ hierarchy or update pytest testpaths to include workflow/ so the coverage actually runs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At workflow/test_worker.py, line 1:

<comment>This test file is outside the configured pytest testpaths ("tests" and "ai/core/tests"), so it won’t be collected or run in CI. Move it under the tests/ hierarchy or update pytest testpaths to include workflow/ so the coverage actually runs.</comment>

<file context>
@@ -0,0 +1,29 @@
+import unittest
+import os
+import tempfile
</file context>
Fix with Cubic

Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
Co-authored-by: daggerstuff <261005129+daggerstuff@users.noreply.github.com>
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.

1 participant