Skip to content

test : added unit tests for CacheClient helpers#1080

Open
tmdeveloper007 wants to merge 4 commits into
utksh1:mainfrom
tmdeveloper007:#1075
Open

test : added unit tests for CacheClient helpers#1080
tmdeveloper007 wants to merge 4 commits into
utksh1:mainfrom
tmdeveloper007:#1075

Conversation

@tmdeveloper007

Copy link
Copy Markdown
Contributor

Closes #1075.

Summary of What Has Been Done:
Added testing/backend/unit/test_cache.py covering the CacheClient class in backend.secuscan.cache. The class implements an in-memory TTL cache with LRU eviction. 15 tests, all passing.

Changes Made:

  • Created testing/backend/unit/test_cache.py
  • _sweep_expired: removes expired keys, preserves non-expired keys
  • _evict_lru: evicts LRU entries when over capacity, no-op when under capacity
  • get_json: returns None for missing/expired keys, updates access order, returns stored value
  • set_json: stores value with correct TTL, uses settings.cache_ttl_seconds as default, triggers eviction when at capacity with a new key
  • delete_prefix: removes all keys matching a given prefix, handles empty results
  • size property: returns entry count
  • stats property: reports eviction_count, sweep_count, size, max_entries

Impact it Made:
Zero test coverage for the cache module. CacheClient is used throughout the API for response caching. Untested, TTL bugs could cause stale data to persist indefinitely and LRU eviction bugs could cause unbounded memory growth.

Note: This task is being handled by tmdeveloper007 — please assign to that account when picking it up.

@utksh1 utksh1 added level:beginner 20 pts difficulty label for small beginner-friendly PRs type:testing Testing work category bonus label area:backend Backend API, database, or service work labels Jun 20, 2026

@utksh1 utksh1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Requesting changes. The tests mutate the global cache settings object directly and do not restore it, which can leak state into later tests. Please use monkeypatch or a local Settings override and keep the cache tests isolated.

Use MagicMock() to completely replace cache_module.settings instead of
mutating the global settings singleton. The fixture now restores the
original reference after each test instead of relying on monkeypatch
to restore a mutated attribute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Backend API, database, or service work level:beginner 20 pts difficulty label for small beginner-friendly PRs type:testing Testing work category bonus label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test : add unit tests for CacheClient helpers

2 participants