Skip to content

feat: setup super basic python testing and GHA to run them#58

Closed
westonplatter wants to merge 4 commits intoMaciek-roboblog:mainfrom
westonplatter:main
Closed

feat: setup super basic python testing and GHA to run them#58
westonplatter wants to merge 4 commits intoMaciek-roboblog:mainfrom
westonplatter:main

Conversation

@westonplatter
Copy link
Collaborator

@westonplatter westonplatter commented Jun 27, 2025

Changes

  • added some basic tests around the calculation logic (since that seems core to the package)
  • added github action entry to run the tests on pull requests. See this GHA run to see results (tests pass 🥳 )

Summary by CodeRabbit

  • Chores

    • Added automated testing workflow to run tests on pull requests.
    • Updated development dependencies to include pytest for testing.
  • Tests

    • Introduced unit tests for burn rate calculations, ensuring correct handling of input/output tokens and exclusion of cache tokens.

@coderabbitai
Copy link

coderabbitai bot commented Jun 27, 2025

Walkthrough

A continuous integration workflow was added to run tests on pull requests using GitHub Actions. The pyproject.toml was updated to include pytest as a development dependency. A new test file was introduced with unit tests for the BurnRateCalculator.calculate_burn_rate method, verifying correct token and cost calculations.

Changes

File(s) Change Summary
.github/workflows/tests.yml Added a CI workflow to install dependencies and run tests with pytest on pull requests.
pyproject.toml Added pytest as a development dependency in two locations: dev extras and a new [dependency-groups].
tests/test_calculator.py Added unit tests for BurnRateCalculator.calculate_burn_rate, including cache token handling checks.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant GitHub
    participant CI Runner

    Developer->>GitHub: Open Pull Request
    GitHub->>CI Runner: Trigger CI workflow (tests.yml)
    CI Runner->>CI Runner: Checkout code
    CI Runner->>CI Runner: Install dependencies (uv sync)
    CI Runner->>CI Runner: Run tests (pytest)
    CI Runner->>GitHub: Report test results
Loading

Poem

In the warren where code bunnies dwell,
New tests and CI cast their spell.
With pytest in tow and workflows anew,
Burn rates are checked, as only bunnies do.
Each merge now hops with confidence bright—
For every test passes, day and night!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
.github/workflows/tests.yml (1)

3-4: Fix trailing whitespace formatting issue.

Static analysis detected trailing spaces that should be removed for clean formatting.

-on: 
+on:
tests/test_calculator.py (2)

15-30: Consider refactoring helper method to reduce argument count.

The helper method has 6 parameters, which exceeds the typical limit of 5. Consider using a builder pattern or dataclass for better maintainability.

+from dataclasses import dataclass
+
+@dataclass
+class SessionBlockParams:
+    input_tokens: int
+    output_tokens: int
+    duration_minutes: int
+    cost_usd: float = 1.0
+    is_active: bool = True
+
-    def create_session_block(self, input_tokens, output_tokens, duration_minutes, cost_usd=1.0, is_active=True):
+    def create_session_block(self, params: SessionBlockParams):
         """Create test session block. Cache tokens are always ignored in calculations."""
         return SessionBlock(
             id="test-session",
             start_time=self.base_time,
             end_time=self.base_time + timedelta(hours=5),
-            actual_end_time=self.base_time + timedelta(minutes=duration_minutes),
-            is_active=is_active,
+            actual_end_time=self.base_time + timedelta(minutes=params.duration_minutes),
+            is_active=params.is_active,
             token_counts=TokenCounts(
-                input_tokens=input_tokens,
-                output_tokens=output_tokens,
+                input_tokens=params.input_tokens,
+                output_tokens=params.output_tokens,
                 cache_creation_tokens=999,  # Should be ignored
                 cache_read_tokens=999       # Should be ignored
             ),
-            cost_usd=cost_usd
+            cost_usd=params.cost_usd
         )

1-58: Clean up formatting issues throughout the file.

Static analysis detected multiple whitespace and trailing space issues that should be addressed for code quality.

The following lines need formatting cleanup:

  • Remove whitespace from blank lines: 10, 14, 31, 40, 42, 45, 54, 56
  • Remove trailing whitespace: 35, 36, 37, 49, 50

Apply these fixes to maintain clean formatting standards.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between edc5a1c and fce3c88.

📒 Files selected for processing (3)
  • .github/workflows/tests.yml (1 hunks)
  • pyproject.toml (2 hunks)
  • tests/test_calculator.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/test_calculator.py (2)
usage_analyzer/core/calculator.py (2)
  • BurnRateCalculator (13-66)
  • calculate_burn_rate (16-33)
usage_analyzer/models/data_structures.py (3)
  • SessionBlock (53-84)
  • TokenCounts (29-47)
  • duration_minutes (77-84)
🪛 YAMLlint (1.37.1)
.github/workflows/tests.yml

[error] 3-3: trailing spaces

(trailing-spaces)

🪛 Ruff (0.11.9)
tests/test_calculator.py

10-10: Blank line contains whitespace

Remove whitespace from blank line

(W293)


14-14: Blank line contains whitespace

Remove whitespace from blank line

(W293)


31-31: Blank line contains whitespace

Remove whitespace from blank line

(W293)


35-35: Trailing whitespace

Remove trailing whitespace

(W291)


36-36: Trailing whitespace

Remove trailing whitespace

(W291)


37-37: Trailing whitespace

Remove trailing whitespace

(W291)


40-40: Blank line contains whitespace

Remove whitespace from blank line

(W293)


42-42: Blank line contains whitespace

Remove whitespace from blank line

(W293)


45-45: Blank line contains whitespace

Remove whitespace from blank line

(W293)


49-49: Trailing whitespace

Remove trailing whitespace

(W291)


50-50: Trailing whitespace

Remove trailing whitespace

(W291)


54-54: Blank line contains whitespace

Remove whitespace from blank line

(W293)


56-56: Blank line contains whitespace

Remove whitespace from blank line

(W293)

🪛 Pylint (3.3.7)
tests/test_calculator.py

[refactor] 15-15: Too many arguments (6/5)

(R0913)


[refactor] 15-15: Too many positional arguments (6/5)

(R0917)

🔇 Additional comments (3)
.github/workflows/tests.yml (1)

1-22: LGTM! Well-structured CI workflow.

The workflow follows modern best practices using uv for fast dependency management and includes appropriate caching. The job structure is clean and efficient.

tests/test_calculator.py (2)

32-44: LGTM! Test logic is mathematically correct.

The test correctly verifies:

  • Token calculation: (1000 + 500) / 30 = 50 tokens/minute ✓
  • Cost calculation: 1.50 / 30 × 60 = 3.0 $/hour ✓

This aligns with the implementation in usage_analyzer/core/calculator.py lines 20-26.


46-57: LGTM! Correctly verifies cache token exclusion.

The test properly confirms that cache tokens (set to 999 each) are ignored in burn rate calculations, using only input + output tokens as expected from the implementation.

"ruff>=0.8.0",
"pre-commit>=2.20.0; python_version<'3.8'",
"pre-commit>=3.0.0; python_version>='3.8'",
"pytest>=6.0.0",
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consolidate redundant pytest dependencies to avoid version conflicts.

The pytest dependency is declared in two places with different version requirements:

  • Line 45: pytest>=6.0.0 in [project.optional-dependencies].dev
  • Lines 77-80: pytest>=8.3.5 in [dependency-groups].dev

This redundancy could lead to confusion about which version requirement applies.

Choose one approach and remove the other. Since [dependency-groups] is the newer PEP 735 standard, consider keeping that and removing from the optional-dependencies:

-    "pytest>=6.0.0",

Or alternatively, remove the newer [dependency-groups] section if you prefer the traditional approach.

Also applies to: 77-80

🤖 Prompt for AI Agents
In pyproject.toml at lines 45 and 77-80, there are redundant pytest dependency
declarations with conflicting versions. To fix this, choose either the
traditional `[project.optional-dependencies].dev` section or the newer
`[dependency-groups].dev` section to declare pytest, and remove the other to
avoid confusion and version conflicts. Preferably keep the
`[dependency-groups].dev` declaration with `pytest>=8.3.5` and remove the
`pytest>=6.0.0` entry from `[project.optional-dependencies].dev`.

@Maciek-roboblog
Copy link
Owner

@westonplatter check new version :D

@westonplatter
Copy link
Collaborator Author

@Maciek-roboblog great, I'll take a look!

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.

2 participants