Skip to content

Commit bbfb747

Browse files
committed
Fix TypeError: 'Mock' object is not iterable
1 parent a65ab87 commit bbfb747

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tests/test_outline.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
from unittest.mock import Mock, patch
1+
from unittest.mock import MagicMock, patch
22

33
import pytest
44
from pydantic import BaseModel
55

66
from outlines import Outline
77

88

9+
class IterableMock(MagicMock):
10+
def __getattr__(self, name):
11+
result = MagicMock()
12+
result.__iter__.return_value = iter([])
13+
return result
14+
15+
916
class OutputModel(BaseModel):
1017
result: int
1118

@@ -15,8 +22,8 @@ def template(a: int) -> str:
1522

1623

1724
def test_outline():
18-
mock_model = Mock()
19-
mock_generator = Mock()
25+
mock_model = IterableMock()
26+
mock_generator = MagicMock()
2027
mock_generator.return_value = '{"result": 6}'
2128
with patch("outlines.generate.json", return_value=mock_generator):
2229
outline_instance = Outline(mock_model, template, OutputModel)
@@ -26,8 +33,8 @@ def test_outline():
2633

2734

2835
def test_outline_with_json_schema():
29-
mock_model = Mock()
30-
mock_generator = Mock()
36+
mock_model = IterableMock()
37+
mock_generator = MagicMock()
3138
mock_generator.return_value = '{"result": 6}'
3239
with patch("outlines.generate.json", return_value=mock_generator):
3340
outline_instance = Outline(
@@ -40,14 +47,14 @@ def test_outline_with_json_schema():
4047

4148

4249
def test_invalid_output_type():
43-
mock_model = Mock()
50+
mock_model = IterableMock()
4451
with pytest.raises(TypeError):
4552
Outline(mock_model, template, int)
4653

4754

4855
def test_invalid_json_response():
49-
mock_model = Mock()
50-
mock_generator = Mock()
56+
mock_model = IterableMock()
57+
mock_generator = MagicMock()
5158
mock_generator.return_value = "invalid json"
5259
with patch("outlines.generate.json", return_value=mock_generator):
5360
outline_instance = Outline(mock_model, template, OutputModel)
@@ -56,7 +63,7 @@ def test_invalid_json_response():
5663

5764

5865
def test_invalid_json_schema():
59-
mock_model = Mock()
66+
mock_model = IterableMock()
6067
invalid_json_schema = (
6168
'{"type": "object", "properties": {"result": {"type": "invalid_type"}}}'
6269
)

0 commit comments

Comments
 (0)