Skip to content

Add test coverage for cache.py and base.py#185

Merged
wuan merged 4 commits intomainfrom
test_coverage
Apr 22, 2026
Merged

Add test coverage for cache.py and base.py#185
wuan merged 4 commits intomainfrom
test_coverage

Conversation

@wuan
Copy link
Copy Markdown
Owner

@wuan wuan commented Apr 22, 2026

Add tests for EqualityAndHash class and additional Point tests in test_base.py. Add repr tests for CacheEntry in test_cache.py.

Increases test coverage:

  • base.py: 42% -> 100%
  • cache.py: 97% -> 99%

wuan and others added 2 commits April 22, 2026 20:59
Add tests for EqualityAndHash class and additional Point tests in test_base.py.
Add repr tests for CacheEntry in test_cache.py.

Increases test coverage:
- base.py: 42% -> 100%
- cache.py: 97% -> 99%

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add tests for db.py (parse_time, prepare_grid_if_applicable, parse_options)
and imprt.py (timestamp_is_newer_than, update_start_time).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds/extends unit tests to raise coverage for core utility modules and some CLI helpers.

Changes:

  • Add CacheEntry.__repr__ tests in tests/test_cache.py.
  • Add additional Point constructor/equality/hash tests and new EqualityAndHash tests in tests/test_base.py.
  • Add new CLI test modules for blitzortung.cli.db and blitzortung.cli.imprt.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
tests/test_cache.py Adds CacheEntry.__repr__ coverage.
tests/test_base.py Expands Point coverage and adds EqualityAndHash test coverage.
tests/cli/test_imprt.py Introduces tests for timestamp/update-time helper logic (currently implemented as local copies).
tests/cli/test_db.py Adds tests for CLI parsing and grid-prep helpers in blitzortung.cli.db.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_base.py Outdated
Comment thread tests/cli/test_imprt.py Outdated
Comment thread tests/cli/test_db.py
Comment thread tests/test_cache.py
Comment thread tests/test_cache.py Outdated
- test_base.py: Use set/dict keying to verify distinct objects
- test_imprt.py: Import actual production code using pytest's monkeypatch
- test_cache.py: Use deterministic expiry values instead of time.time()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_cache.py
Comment on lines +61 to +77
# Use deterministic large expiry time (100 > initial hit_count of 0)
self.cache_entry = CacheEntry("payload", 100)
result = repr(self.cache_entry)
assert_that(result).contains("cached<+")
assert_that(result).contains("payload")

def test_repr_with_hit_count(self):
"""Test string representation after retrieving payload (increases hit count)."""
# Use expiry time of 1, after 2 hits hit_count=2 > expiry_time=1
# This tests that invalid entries (expired) show "-" in repr
self.cache_entry = CacheEntry("payload", 1)
_ = self.cache_entry.get_payload()
_ = self.cache_entry.get_payload()
result = repr(self.cache_entry)
assert_that(result).contains("cached<-") # hit_count (2) > expiry_time (1)
assert_that(result).contains("payload")
assert_that(result).contains("2") # hit count
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

These repr tests treat expiry_time like a small counter (100/1) and rely on CacheEntry.__repr__ using hit_count as the current_time input to is_valid(). In actual usage expiry_time is an epoch timestamp (see ObjectCache.get()), so these tests encode behavior that’s inconsistent with the public semantics and will break if __repr__ is corrected to use real time. Consider asserting repr validity using expiry_time=time.time()+/-… and controlling time (or updating __repr__ to compute validity from wall clock) so the test matches intended TTL behavior.

Suggested change
# Use deterministic large expiry time (100 > initial hit_count of 0)
self.cache_entry = CacheEntry("payload", 100)
result = repr(self.cache_entry)
assert_that(result).contains("cached<+")
assert_that(result).contains("payload")
def test_repr_with_hit_count(self):
"""Test string representation after retrieving payload (increases hit count)."""
# Use expiry time of 1, after 2 hits hit_count=2 > expiry_time=1
# This tests that invalid entries (expired) show "-" in repr
self.cache_entry = CacheEntry("payload", 1)
_ = self.cache_entry.get_payload()
_ = self.cache_entry.get_payload()
result = repr(self.cache_entry)
assert_that(result).contains("cached<-") # hit_count (2) > expiry_time (1)
assert_that(result).contains("payload")
assert_that(result).contains("2") # hit count
self.cache_entry = CacheEntry("payload", time.time() + 10)
result = repr(self.cache_entry)
assert_that(result).contains("cached<+")
assert_that(result).contains("payload")
def test_repr_with_hit_count(self):
"""Test string representation for an expired cache entry with hits."""
self.cache_entry = CacheEntry("payload", time.time() - 10)
_ = self.cache_entry.get_payload()
_ = self.cache_entry.get_payload()
result = repr(self.cache_entry)
assert_that(result).contains("cached<-")
assert_that(result).contains("payload")
assert_that(result).contains("2")

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_imprt.py
Comment on lines +65 to +73
@pytest.fixture(autouse=True)
def mock_dependencies(self, monkeypatch):
"""Mock optional dependencies to allow importing the module."""
mock_stopit = MagicMock()
mock_requests = MagicMock()
mock_statsd = MagicMock()
monkeypatch.setitem(sys.modules, 'stopit', mock_stopit)
monkeypatch.setitem(sys.modules, 'requests', mock_requests)
monkeypatch.setitem(sys.modules, 'statsd', mock_statsd)
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

Same sys.modules mocking issue here: after the first import, blitzortung.cli.imprt will stay in sys.modules with mocked dependencies even after monkeypatch restores entries, which can leak into other tests. Consider removing the duplicate fixture and using a single session/function-scoped fixture that also unloads/reloads blitzortung.cli.imprt, or avoid mocking these modules entirely.

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_imprt.py

import datetime
import sys
from unittest.mock import MagicMock, patch
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

patch is imported but never used in this test module. Please remove it to keep the test file minimal and avoid confusion about what is being mocked.

Suggested change
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock

Copilot uses AI. Check for mistakes.
Comment thread tests/cli/test_db.py
Comment on lines +1 to +2
"""Tests for blitzortung.cli.db module."""

Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

PR description mentions adding coverage for cache.py and base.py, but this change set also introduces new test modules for blitzortung.cli.db and blitzortung.cli.imprt. Please update the PR description (or scope) so reviewers understand these additional areas are being covered/changed.

Copilot uses AI. Check for mistakes.
@wuan wuan marked this pull request as ready for review April 22, 2026 20:25
@wuan wuan merged commit 2068b94 into main Apr 22, 2026
6 checks passed
@wuan wuan deleted the test_coverage branch April 22, 2026 20:25
@sonarqubecloud
Copy link
Copy Markdown

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