Skip to content

Commit f059335

Browse files
committed
Add tests for ModelNotFoundException in DockerModelRunner result converters
Tests verify that ModelNotFoundException is properly thrown when: - 404 response contains "model not found" message (case-insensitive) - Exception is not thrown for 404 responses without the specific message Uses TestWith data provider to simplify test cases and createMock for proper mock expectations.
1 parent c9fb496 commit f059335

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/platform/tests/Bridge/DockerModelRunner/Completions/ResultConverterTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313

1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\Attributes\TestWith;
1617
use PHPUnit\Framework\Attributes\UsesClass;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\AI\Platform\Bridge\DockerModelRunner\Completions;
1920
use Symfony\AI\Platform\Bridge\DockerModelRunner\Completions\ResultConverter;
2021
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter as OpenAiResultConverter;
22+
use Symfony\AI\Platform\Exception\ModelNotFoundException;
23+
use Symfony\AI\Platform\Exception\RuntimeException;
24+
use Symfony\AI\Platform\Result\RawHttpResult;
25+
use Symfony\Contracts\HttpClient\ResponseInterface;
2126

2227
#[CoversClass(ResultConverter::class)]
2328
#[UsesClass(Completions::class)]
@@ -31,4 +36,43 @@ public function testItSupportsCompletionsModel()
3136

3237
$this->assertTrue($converter->supports(new Completions('test-model')));
3338
}
39+
40+
#[TestWith(['Model not found'])]
41+
#[TestWith(['MODEL NOT FOUND'])]
42+
public function testItThrowsModelNotFoundExceptionWhen404WithModelNotFoundMessage(string $message)
43+
{
44+
$response = $this->createMock(ResponseInterface::class);
45+
$response
46+
->method('getStatusCode')
47+
->willReturn(404);
48+
$response
49+
->method('getContent')
50+
->with(false)
51+
->willReturn($message);
52+
53+
$this->expectException(ModelNotFoundException::class);
54+
$this->expectExceptionMessage($message);
55+
56+
(new ResultConverter())->convert(new RawHttpResult($response));
57+
}
58+
59+
public function testItDoesNotThrowModelNotFoundExceptionWhen404WithoutModelNotFoundMessage()
60+
{
61+
$response = $this->createMock(ResponseInterface::class);
62+
$response
63+
->method('getStatusCode')
64+
->willReturn(404);
65+
$response
66+
->method('getContent')
67+
->with(false)
68+
->willReturn('Not found');
69+
$response
70+
->method('toArray')
71+
->willReturn(['error' => 'some other error']);
72+
73+
$this->expectException(RuntimeException::class);
74+
$this->expectExceptionMessage('Response does not contain choices.');
75+
76+
(new ResultConverter())->convert(new RawHttpResult($response));
77+
}
3478
}

src/platform/tests/Bridge/DockerModelRunner/Embeddings/ResultConverterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use PHPUnit\Framework\Attributes\CoversClass;
1515
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\Attributes\TestWith;
1617
use PHPUnit\Framework\Attributes\UsesClass;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\AI\Platform\Bridge\DockerModelRunner\Embeddings;
1920
use Symfony\AI\Platform\Bridge\DockerModelRunner\Embeddings\ResultConverter;
21+
use Symfony\AI\Platform\Exception\ModelNotFoundException;
2022
use Symfony\AI\Platform\Exception\RuntimeException;
2123
use Symfony\AI\Platform\Result\RawHttpResult;
2224
use Symfony\AI\Platform\Result\VectorResult;
@@ -86,4 +88,43 @@ public function testItSupportsEmbeddingsModel()
8688

8789
$this->assertTrue($converter->supports(new Embeddings('test-model')));
8890
}
91+
92+
#[TestWith(['Model not found'])]
93+
#[TestWith(['MODEL NOT FOUND'])]
94+
public function testItThrowsModelNotFoundExceptionWhen404WithModelNotFoundMessage(string $message)
95+
{
96+
$response = $this->createMock(ResponseInterface::class);
97+
$response
98+
->method('getStatusCode')
99+
->willReturn(404);
100+
$response
101+
->method('getContent')
102+
->with(false)
103+
->willReturn($message);
104+
105+
$this->expectException(ModelNotFoundException::class);
106+
$this->expectExceptionMessage($message);
107+
108+
(new ResultConverter())->convert(new RawHttpResult($response));
109+
}
110+
111+
public function testItDoesNotThrowModelNotFoundExceptionWhen404WithoutModelNotFoundMessage()
112+
{
113+
$response = $this->createMock(ResponseInterface::class);
114+
$response
115+
->method('getStatusCode')
116+
->willReturn(404);
117+
$response
118+
->method('getContent')
119+
->with(false)
120+
->willReturn('Not found');
121+
$response
122+
->method('toArray')
123+
->willReturn(['error' => 'some other error']);
124+
125+
$this->expectException(RuntimeException::class);
126+
$this->expectExceptionMessage('Response does not contain data.');
127+
128+
(new ResultConverter())->convert(new RawHttpResult($response));
129+
}
89130
}

0 commit comments

Comments
 (0)