refactor: Extract UUID percentage testing logic into a separate method with tests

This commit is contained in:
Paul Gauthier (aider) 2024-11-20 17:54:16 -08:00
parent 75f52a1324
commit 82187f6a71
2 changed files with 50 additions and 3 deletions

View file

@ -78,11 +78,28 @@ class Analytics:
if not self.user_id: if not self.user_id:
return False return False
# Define percentage of users to ask
PERCENT = 1 PERCENT = 1
return self.is_uuid_in_percentage(self.user_id, PERCENT)
def is_uuid_in_percentage(self, uuid_str, percent):
"""Check if a UUID string falls within the first X percent of the UUID space.
Args:
uuid_str: UUID string to test
percent: Percentage threshold (0-100)
Returns:
bool: True if UUID falls within the first X percent
"""
if not (0 <= percent <= 100):
raise ValueError("Percentage must be between 0 and 100")
if not uuid_str:
return False
# Convert percentage to hex threshold (1% = "03", 10% = "1a", etc) # Convert percentage to hex threshold (1% = "03", 10% = "1a", etc)
threshold = format(int(256 * PERCENT / 100), "02x") threshold = format(int(256 * percent / 100), "02x")
return self.user_id < threshold return uuid_str < threshold
def get_data_file_path(self): def get_data_file_path(self):
data_file = Path.home() / ".aider" / "analytics.json" data_file = Path.home() / ".aider" / "analytics.json"

View file

@ -105,3 +105,33 @@ def test_need_to_ask(temp_data_dir):
analytics.permanently_disable = True analytics.permanently_disable = True
assert analytics.need_to_ask(True) is False assert analytics.need_to_ask(True) is False
def test_is_uuid_in_percentage():
analytics = Analytics()
# Test basic percentage thresholds
assert analytics.is_uuid_in_percentage("00", 1) is True
assert analytics.is_uuid_in_percentage("02", 1) is True
assert analytics.is_uuid_in_percentage("03", 1) is False
assert analytics.is_uuid_in_percentage("ff", 1) is False
assert analytics.is_uuid_in_percentage("00", 10) is True
assert analytics.is_uuid_in_percentage("19", 10) is True
assert analytics.is_uuid_in_percentage("1a", 10) is False
assert analytics.is_uuid_in_percentage("ff", 10) is False
# Test edge cases
assert analytics.is_uuid_in_percentage("00", 0) is False
assert analytics.is_uuid_in_percentage("00", 100) is True
assert analytics.is_uuid_in_percentage("ff", 100) is True
# Test invalid inputs
with pytest.raises(ValueError):
analytics.is_uuid_in_percentage("00", -1)
with pytest.raises(ValueError):
analytics.is_uuid_in_percentage("00", 101)
# Test empty/None UUID
assert analytics.is_uuid_in_percentage("", 50) is False
assert analytics.is_uuid_in_percentage(None, 50) is False