Spaces:
Sleeping
Sleeping
ping98k
commited on
Commit
·
aebdef3
1
Parent(s):
62eba67
Add unit tests for completion-based utils
Browse files
tests/test_tournament_utils.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, os, types
|
| 2 |
+
from unittest.mock import patch, MagicMock
|
| 3 |
+
|
| 4 |
+
# Ensure project root in path
|
| 5 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
| 6 |
+
|
| 7 |
+
# Provide dummy litellm module so import succeeds
|
| 8 |
+
fake_litellm = types.ModuleType('litellm')
|
| 9 |
+
fake_litellm.completion = MagicMock()
|
| 10 |
+
sys.modules.setdefault('litellm', fake_litellm)
|
| 11 |
+
|
| 12 |
+
import tournament_utils as tu
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def make_response(contents):
|
| 16 |
+
class Message:
|
| 17 |
+
def __init__(self, content):
|
| 18 |
+
self.content = content
|
| 19 |
+
class Choice:
|
| 20 |
+
def __init__(self, content):
|
| 21 |
+
self.message = Message(content)
|
| 22 |
+
return MagicMock(choices=[Choice(c) for c in contents])
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_generate_players():
|
| 26 |
+
resp = make_response([" player1 ", "player2\n"])
|
| 27 |
+
with patch('tournament_utils.completion', return_value=resp) as mock_comp:
|
| 28 |
+
players = tu.generate_players('instr', 2, model='m')
|
| 29 |
+
mock_comp.assert_called_once_with(model='m', messages=[{'role': 'user', 'content': 'instr'}], n=2)
|
| 30 |
+
assert players == ['player1', 'player2']
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_prompt_score():
|
| 34 |
+
resp = make_response([" {\"score\": [5]} "])
|
| 35 |
+
with patch('tournament_utils.completion', return_value=resp) as mock_comp:
|
| 36 |
+
result = tu.prompt_score('instr', ['c1'], 'block', 'pl', model='m')
|
| 37 |
+
mock_comp.assert_called_once()
|
| 38 |
+
assert result == '{"score": [5]}'
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_prompt_play():
|
| 42 |
+
resp = make_response([" {\"winner\": \"A\"} "])
|
| 43 |
+
with patch('tournament_utils.completion', return_value=resp) as mock_comp:
|
| 44 |
+
result = tu.prompt_play('instr', 'block', 'A text', 'B text', model='m')
|
| 45 |
+
mock_comp.assert_called_once()
|
| 46 |
+
assert result == '{"winner": "A"}'
|