Skip to content

Commit c978b59

Browse files
committed
minor #190 [Store][MariaDB] Add unit tests for query method (OskarStark)
This PR was merged into the main branch. Discussion ---------- [Store][MariaDB] Add unit tests for query method | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | Refs #189 | License | MIT This commit adds comprehensive unit tests for the MariaDB Store query method, specifically testing the fix from PR #189 where sprintf placeholders weren't being properly replaced in the WHERE clause when using minScore parameter. Tests cover: - Query with minScore parameter (validates the bug fix) - Query without minScore parameter - Query with custom limit option Commits ------- 5540a11 Add unit tests for MariaDB Store query method
2 parents 60968eb + 5540a11 commit c978b59

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Store\Tests\Bridge\MariaDB;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\AI\Platform\Vector\Vector;
17+
use Symfony\AI\Store\Bridge\MariaDB\Store;
18+
use Symfony\AI\Store\Document\VectorDocument;
19+
use Symfony\Component\Uid\Uuid;
20+
21+
#[CoversClass(Store::class)]
22+
final class StoreTest extends TestCase
23+
{
24+
public function testQueryWithMinScore(): void
25+
{
26+
$pdo = $this->createMock(\PDO::class);
27+
$statement = $this->createMock(\PDOStatement::class);
28+
29+
$store = new Store($pdo, 'embeddings_table', 'embedding_index', 'embedding');
30+
31+
// Expected SQL query with minScore
32+
$expectedQuery = <<<'SQL'
33+
SELECT id, VEC_ToText(embedding) embedding, metadata, VEC_DISTANCE_EUCLIDEAN(embedding, VEC_FromText(:embedding)) AS score
34+
FROM embeddings_table
35+
WHERE VEC_DISTANCE_EUCLIDEAN(embedding, VEC_FromText(:embedding)) >= :minScore
36+
ORDER BY score ASC
37+
LIMIT 5
38+
SQL;
39+
40+
$pdo->expects($this->once())
41+
->method('prepare')
42+
->with($expectedQuery)
43+
->willReturn($statement);
44+
45+
$uuid = Uuid::v4();
46+
$vectorData = [0.1, 0.2, 0.3];
47+
$minScore = 0.8;
48+
49+
$statement->expects($this->once())
50+
->method('execute')
51+
->with([
52+
'embedding' => json_encode($vectorData),
53+
'minScore' => $minScore,
54+
]);
55+
56+
$statement->expects($this->once())
57+
->method('fetchAll')
58+
->with(\PDO::FETCH_ASSOC)
59+
->willReturn([
60+
[
61+
'id' => $uuid->toBinary(),
62+
'embedding' => json_encode($vectorData),
63+
'metadata' => json_encode(['title' => 'Test Document']),
64+
'score' => 0.85,
65+
],
66+
]);
67+
68+
$results = $store->query(new Vector($vectorData), [], $minScore);
69+
70+
$this->assertCount(1, $results);
71+
$this->assertInstanceOf(VectorDocument::class, $results[0]);
72+
$this->assertSame(0.85, $results[0]->score);
73+
$this->assertSame(['title' => 'Test Document'], $results[0]->metadata->getArrayCopy());
74+
}
75+
76+
public function testQueryWithoutMinScore(): void
77+
{
78+
$pdo = $this->createMock(\PDO::class);
79+
$statement = $this->createMock(\PDOStatement::class);
80+
81+
$store = new Store($pdo, 'embeddings_table', 'embedding_index', 'embedding');
82+
83+
// Expected SQL query without minScore
84+
$expectedQuery = <<<'SQL'
85+
SELECT id, VEC_ToText(embedding) embedding, metadata, VEC_DISTANCE_EUCLIDEAN(embedding, VEC_FromText(:embedding)) AS score
86+
FROM embeddings_table
87+
88+
ORDER BY score ASC
89+
LIMIT 5
90+
SQL;
91+
92+
$pdo->expects($this->once())
93+
->method('prepare')
94+
->with($expectedQuery)
95+
->willReturn($statement);
96+
97+
$uuid = Uuid::v4();
98+
$vectorData = [0.1, 0.2, 0.3];
99+
100+
$statement->expects($this->once())
101+
->method('execute')
102+
->with(['embedding' => json_encode($vectorData)]);
103+
104+
$statement->expects($this->once())
105+
->method('fetchAll')
106+
->with(\PDO::FETCH_ASSOC)
107+
->willReturn([
108+
[
109+
'id' => $uuid->toBinary(),
110+
'embedding' => json_encode($vectorData),
111+
'metadata' => json_encode(['title' => 'Test Document']),
112+
'score' => 0.95,
113+
],
114+
]);
115+
116+
$results = $store->query(new Vector($vectorData));
117+
118+
$this->assertCount(1, $results);
119+
$this->assertInstanceOf(VectorDocument::class, $results[0]);
120+
$this->assertSame(0.95, $results[0]->score);
121+
}
122+
123+
public function testQueryWithCustomLimit(): void
124+
{
125+
$pdo = $this->createMock(\PDO::class);
126+
$statement = $this->createMock(\PDOStatement::class);
127+
128+
$store = new Store($pdo, 'embeddings_table', 'embedding_index', 'embedding');
129+
130+
// Expected SQL query with custom limit
131+
$expectedQuery = <<<'SQL'
132+
SELECT id, VEC_ToText(embedding) embedding, metadata, VEC_DISTANCE_EUCLIDEAN(embedding, VEC_FromText(:embedding)) AS score
133+
FROM embeddings_table
134+
135+
ORDER BY score ASC
136+
LIMIT 10
137+
SQL;
138+
139+
$pdo->expects($this->once())
140+
->method('prepare')
141+
->with($expectedQuery)
142+
->willReturn($statement);
143+
144+
$vectorData = [0.1, 0.2, 0.3];
145+
146+
$statement->expects($this->once())
147+
->method('execute')
148+
->with(['embedding' => json_encode($vectorData)]);
149+
150+
$statement->expects($this->once())
151+
->method('fetchAll')
152+
->with(\PDO::FETCH_ASSOC)
153+
->willReturn([]);
154+
155+
$results = $store->query(new Vector($vectorData), ['limit' => 10]);
156+
157+
$this->assertCount(0, $results);
158+
}
159+
}

0 commit comments

Comments
 (0)