Skip to content

Commit

Permalink
Merge pull request #3 from plasma-umass/coverup-thyself
Browse files Browse the repository at this point in the history
CoverUp, cover-up thyself.
  • Loading branch information
jaltmayerpizzorno authored Apr 22, 2024
2 parents fdb7582 + 9d47b7a commit cead10c
Show file tree
Hide file tree
Showing 28 changed files with 695 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_claude_coverup_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# file src/coverup/llm.py:47-51
# lines [48, 49, 51]
# branches ['48->49', '48->51']

import pytest
from unittest.mock import patch
from coverup.llm import token_rate_limit_for_model

@pytest.fixture
def mock_model_rate_limits():
with patch('coverup.llm.MODEL_RATE_LIMITS', {'model_a': {'token': (10, 20)}, 'model_b': {'token': (30, 40)}}):
yield

def test_token_rate_limit_for_model(mock_model_rate_limits):
# Test case when model_name is in MODEL_RATE_LIMITS
assert token_rate_limit_for_model('model_a') == (10, 20)
assert token_rate_limit_for_model('model_b') == (30, 40)

# Test case when model_name is not in MODEL_RATE_LIMITS
assert token_rate_limit_for_model('unknown_model') is None
14 changes: 14 additions & 0 deletions tests/test_claude_coverup_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# file src/coverup/utils.py:49-51
# lines [50, 51]
# branches ['50->exit', '50->51']

import pytest
from coverup.utils import format_branches

@pytest.fixture
def mock_branches():
return [(1, 2), (3, 0), (5, 6)]

def test_format_branches(mock_branches):
formatted_branches = list(format_branches(mock_branches))
assert formatted_branches == ['1->2', '3->exit', '5->6']
28 changes: 28 additions & 0 deletions tests/test_claude_coverup_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# file src/coverup/coverup.py:203-222
# lines [204, 206, 207, 208, 209, 211, 213, 214, 215, 216, 217, 219, 220, 222]
# branches ['213->214', '213->222', '214->215', '214->219', '215->213', '215->216', '216->215', '216->217', '219->213', '219->220']

import ast
import pytest
from coverup.coverup import find_imports

@pytest.fixture
def python_code_with_syntax_error():
return "invalid python code"

@pytest.fixture
def python_code_with_imports():
return """
import os
import sys
from pathlib import Path
import re
"""

def test_find_imports_with_syntax_error(python_code_with_syntax_error):
result = find_imports(python_code_with_syntax_error)
assert result == []

def test_find_imports_with_valid_code(python_code_with_imports):
result = find_imports(python_code_with_imports)
assert set(result) == {"os", "sys", "pathlib", "re"}
27 changes: 27 additions & 0 deletions tests/test_claude_coverup_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# file src/coverup/coverup.py:333-338
# lines [335, 336, 337, 338]
# branches ['336->exit', '336->337']

import pytest
from unittest.mock import Mock
from coverup.coverup import State, Progress

@pytest.fixture
def state():
return State(initial_coverage=0.0)

def test_set_progress_bar_with_bar(state):
mock_bar = Mock(spec=Progress)
state.usage = 'some_usage'
state.counters = {'key': 'value'}

state.set_progress_bar(mock_bar)

assert state.bar == mock_bar
mock_bar.update_usage.assert_called_once_with('some_usage')
mock_bar.update_counters.assert_called_once_with({'key': 'value'})

def test_set_progress_bar_without_bar(state):
state.set_progress_bar(None)

assert state.bar is None
23 changes: 23 additions & 0 deletions tests/test_claude_coverup_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# file src/coverup/coverup.py:590-594
# lines [591, 592, 593, 594]
# branches []

import sys
from pathlib import Path
from unittest.mock import patch
import os
from coverup import coverup

def test_add_to_pythonpath(tmp_path):
source_dir = tmp_path / "source"
source_dir.mkdir()

with patch.dict('os.environ', {'PYTHONPATH': '/existing/path'}):
coverup.add_to_pythonpath(source_dir)
assert str(source_dir.parent) in sys.path[0]
assert os.environ['PYTHONPATH'] == f"{str(source_dir.parent)}:/existing/path"

with patch.dict('os.environ', clear=True):
coverup.add_to_pythonpath(source_dir)
assert str(source_dir.parent) in sys.path[0]
assert os.environ['PYTHONPATH'] == str(source_dir.parent)
17 changes: 17 additions & 0 deletions tests/test_claude_coverup_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# file src/coverup/coverup.py:323-325
# lines [325]
# branches []

import pytest
from unittest.mock import Mock
from coverup.coverup import State

@pytest.fixture
def state():
initial_coverage = {"line1": True, "line2": False}
state = State(initial_coverage)
return state

def test_get_initial_coverage(state):
expected_coverage = {"line1": True, "line2": False}
assert state.get_initial_coverage() == expected_coverage
17 changes: 17 additions & 0 deletions tests/test_claude_coverup_8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# file src/coverup/coverup.py:328-330
# lines [330]
# branches []

import pytest
from unittest.mock import Mock
from coverup.coverup import State

@pytest.fixture
def state():
initial_coverage = Mock()
return State(initial_coverage)

def test_set_final_coverage(state):
expected_coverage = {'file1.py': 80, 'file2.py': 90}
state.set_final_coverage(expected_coverage)
assert state.final_coverage == expected_coverage
23 changes: 23 additions & 0 deletions tests/test_claude_coverup_9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# file src/coverup/coverup.py:254-256
# lines [256]
# branches []

import pytest
import typing as T
from unittest.mock import patch, MagicMock
from coverup.coverup import get_required_modules

@pytest.fixture
def mock_module_available():
return {
'module1': 1,
'module2': 0,
'module3': 1,
'module4': 0
}

def test_get_required_modules(mock_module_available):
with patch('coverup.coverup.module_available', new=mock_module_available):
result = get_required_modules()
expected = ['module2', 'module4']
assert sorted(result) == sorted(expected)
11 changes: 11 additions & 0 deletions tests/test_openai_coverup_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# file src/coverup/coverup.py:453-457
# lines []
# branches ['456->456']

import pytest
from coverup.coverup import extract_python

def test_extract_python_without_code_block():
with pytest.raises(RuntimeError) as exc_info:
extract_python("This is a response without a Python code block.")
assert "Unable to extract Python code from response" in str(exc_info.value)
23 changes: 23 additions & 0 deletions tests/test_openai_coverup_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# file src/coverup/coverup.py:341-347
# lines [343, 344, 346, 347]
# branches ['343->344', '343->346', '346->exit', '346->347']

import pytest
from unittest.mock import Mock
from src.coverup.coverup import State

@pytest.fixture
def state_with_bar():
initial_coverage = {'token1': 0, 'token2': 0}
state = State(initial_coverage)
state.usage = {'token1': 0, 'token2': 0}
state.bar = Mock()
return state

def test_add_usage_with_bar(state_with_bar):
additional_usage = {'token1': 1, 'token2': 2}
state_with_bar.add_usage(additional_usage)

assert state_with_bar.usage['token1'] == 1
assert state_with_bar.usage['token2'] == 2
state_with_bar.bar.update_usage.assert_called_once_with({'token1': 1, 'token2': 2})
25 changes: 25 additions & 0 deletions tests/test_openai_coverup_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# file src/coverup/delta.py:5-32
# lines [16, 18]
# branches ['12->18', '13->16']

import pytest
from pathlib import Path
from src.coverup.delta import _compact

def test_compact_with_non_path_objects(monkeypatch):
# Mocking Path to avoid filesystem dependency using monkeypatch
monkeypatch.setattr('src.coverup.delta.Path', Path)

# Create a test set with non-Path objects
test_set = {42, 'test_name', 3.14}

# Call the _compact function with the test set
result = _compact(test_set)

# Verify that the non-Path objects are converted to strings and included in the result
assert '42' in result
assert 'test_name' in result
assert '3.14' in result

# Verify that the result does not contain any range representation
assert '-' not in result
34 changes: 34 additions & 0 deletions tests/test_openai_coverup_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# file src/coverup/segment.py:66-67
# lines [67]
# branches []

import pytest
from src.coverup.segment import CodeSegment

@pytest.fixture
def code_segment():
segment = CodeSegment(
filename='test_file.py',
name='test_segment',
begin=1,
end=10,
lines_of_interest=set(range(1, 11)),
missing_lines=set(),
executed_lines=set(),
missing_branches=set(),
context={}
)
return segment

def test_missing_count_with_missing_lines_and_branches(code_segment):
# Setup: Add missing lines and branches to the segment
code_segment.missing_lines = {1, 2, 3}
code_segment.missing_branches = {4, 5}

# Exercise: Call the method under test
missing_count = code_segment.missing_count()

# Verify: Check if the missing count is correct
assert missing_count == 5, "The missing count should be the sum of missing lines and branches"

# Cleanup: No cleanup required as the fixture will provide a fresh instance for each test
29 changes: 29 additions & 0 deletions tests/test_openai_coverup_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# file src/coverup/segment.py:36-56
# lines [46, 47]
# branches ['45->46', '46->47', '46->56']

import pytest
from coverup.segment import CodeSegment
from unittest.mock import mock_open, patch

@pytest.fixture
def code_segment():
cs = CodeSegment(
filename="fake_file.py",
name="test_segment",
begin=1,
end=3,
lines_of_interest=set(),
missing_lines=set(),
executed_lines=set(),
missing_branches=set(),
context=[]
)
return cs

def test_get_excerpt_without_executed_lines(code_segment, monkeypatch):
mock_file_content = "line1\nline2\nline3\n"
monkeypatch.setattr("builtins.open", mock_open(read_data=mock_file_content))
excerpt = code_segment.get_excerpt()
expected_excerpt = " line1\n line2\n"
assert excerpt == expected_excerpt
32 changes: 32 additions & 0 deletions tests/test_openai_coverup_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# file src/coverup/coverup.py:254-256
# lines [256]
# branches []

import pytest
from unittest.mock import patch
from coverup.coverup import get_required_modules

# Assuming module_available is a global variable or accessible within the scope
# If it's not, you would need to mock or patch the appropriate object to provide it.

def test_get_required_modules():
# Mock the module_available dictionary to include modules with different statuses
mocked_module_available = {
'module1': 0, # Not available
'module2': 1, # Available
'module3': 0, # Not available
}

with patch('coverup.coverup.module_available', mocked_module_available):
# Call the function under test
result = get_required_modules()

# Verify that the result only includes the modules that were not available
assert 'module1' in result
assert 'module2' not in result
assert 'module3' in result

# Verify that the length of the result is correct
assert len(result) == 2

# No cleanup is necessary as the patch context manager will automatically undo the patch after the block
17 changes: 17 additions & 0 deletions tests/test_openai_coverup_15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# file src/coverup/utils.py:26-46
# lines [30, 31, 33, 34, 35, 36, 37, 39, 40, 42, 44, 46]
# branches ['34->exit', '34->35', '36->37', '36->39', '39->40', '39->42']

import pytest
from coverup.utils import format_ranges

def test_format_ranges_with_negative_ranges():
# Define the sets of lines and negative lines to pass to the function
lines = {1, 2, 4, 5}
negative = {3}

# Call the function with the test data
result = format_ranges(lines, negative)

# Verify the result is as expected
assert result == "1-2, 4-5", "The format_ranges function did not return the expected string"
Loading

0 comments on commit cead10c

Please sign in to comment.