Skip to content

Commit f5c914c

Browse files
committed
minor #446 [Store] Add comprehensive unit tests for VectorDocument (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Store] Add comprehensive unit tests for `VectorDocument` | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Commits ------- 4b8e460 [Store] Add comprehensive unit tests for `VectorDocument`
2 parents 8259ae4 + 4b8e460 commit f5c914c

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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\Document;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\DataProvider;
16+
use PHPUnit\Framework\Attributes\TestDox;
17+
use PHPUnit\Framework\Attributes\TestWith;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\AI\Platform\Vector\Vector;
20+
use Symfony\AI\Store\Document\Metadata;
21+
use Symfony\AI\Store\Document\VectorDocument;
22+
use Symfony\Component\Uid\Uuid;
23+
24+
/**
25+
* @author Oskar Stark <[email protected]>
26+
*/
27+
#[CoversClass(VectorDocument::class)]
28+
final class VectorDocumentTest extends TestCase
29+
{
30+
#[TestDox('Creates document with required parameters only')]
31+
public function testConstructorWithRequiredParameters()
32+
{
33+
$id = Uuid::v4();
34+
$vector = new Vector([0.1, 0.2, 0.3]);
35+
36+
$document = new VectorDocument($id, $vector);
37+
38+
$this->assertSame($id, $document->id);
39+
$this->assertSame($vector, $document->vector);
40+
$this->assertInstanceOf(Metadata::class, $document->metadata);
41+
$this->assertCount(0, $document->metadata);
42+
$this->assertNull($document->score);
43+
}
44+
45+
#[TestDox('Creates document with metadata')]
46+
public function testConstructorWithMetadata()
47+
{
48+
$id = Uuid::v4();
49+
$vector = new Vector([0.1, 0.2, 0.3]);
50+
$metadata = new Metadata(['source' => 'test.txt', 'author' => 'John Doe']);
51+
52+
$document = new VectorDocument($id, $vector, $metadata);
53+
54+
$this->assertSame($id, $document->id);
55+
$this->assertSame($vector, $document->vector);
56+
$this->assertSame($metadata, $document->metadata);
57+
$this->assertSame(['source' => 'test.txt', 'author' => 'John Doe'], $document->metadata->getArrayCopy());
58+
}
59+
60+
#[TestDox('Creates document with all parameters including score')]
61+
public function testConstructorWithAllParameters()
62+
{
63+
$id = Uuid::v4();
64+
$vector = new Vector([0.1, 0.2, 0.3]);
65+
$metadata = new Metadata(['title' => 'Test Document']);
66+
$score = 0.95;
67+
68+
$document = new VectorDocument($id, $vector, $metadata, $score);
69+
70+
$this->assertSame($id, $document->id);
71+
$this->assertSame($vector, $document->vector);
72+
$this->assertSame($metadata, $document->metadata);
73+
$this->assertSame($score, $document->score);
74+
}
75+
76+
#[TestWith([null])]
77+
#[TestWith([0.0])]
78+
#[TestWith([0.75])]
79+
#[TestWith([-0.25])]
80+
#[TestWith([1.0])]
81+
#[TestWith([0.000001])]
82+
#[TestWith([999999.99])]
83+
#[TestDox('Handles different score values: $score')]
84+
public function testConstructorWithDifferentScores(?float $score)
85+
{
86+
$id = Uuid::v4();
87+
$vector = new Vector([0.1, 0.2, 0.3]);
88+
89+
$document = new VectorDocument($id, $vector, new Metadata(), $score);
90+
91+
$this->assertSame($score, $document->score);
92+
}
93+
94+
#[TestDox('Ensures all properties are readonly')]
95+
public function testReadonlyProperties()
96+
{
97+
$id = Uuid::v4();
98+
$vector = new Vector([0.1, 0.2, 0.3]);
99+
$metadata = new Metadata(['key' => 'value']);
100+
$score = 0.85;
101+
102+
$document = new VectorDocument($id, $vector, $metadata, $score);
103+
104+
// Verify all properties are accessible
105+
$this->assertSame($id, $document->id);
106+
$this->assertSame($vector, $document->vector);
107+
$this->assertSame($metadata, $document->metadata);
108+
$this->assertSame($score, $document->score);
109+
110+
// Verify the class is marked as readonly
111+
$reflection = new \ReflectionClass(VectorDocument::class);
112+
$this->assertTrue($reflection->isReadOnly());
113+
}
114+
115+
#[TestDox('Handles metadata with special keys')]
116+
public function testMetadataWithSpecialKeys()
117+
{
118+
$id = Uuid::v4();
119+
$vector = new Vector([0.1, 0.2, 0.3]);
120+
$metadata = new Metadata([
121+
Metadata::KEY_PARENT_ID => 'parent-123',
122+
Metadata::KEY_TEXT => 'Additional text information',
123+
Metadata::KEY_SOURCE => 'document.pdf',
124+
'custom_field' => 'custom_value',
125+
]);
126+
127+
$document = new VectorDocument($id, $vector, $metadata);
128+
129+
$this->assertSame($metadata, $document->metadata);
130+
$this->assertTrue($document->metadata->hasParentId());
131+
$this->assertSame('parent-123', $document->metadata->getParentId());
132+
$this->assertTrue($document->metadata->hasText());
133+
$this->assertSame('Additional text information', $document->metadata->getText());
134+
$this->assertTrue($document->metadata->hasSource());
135+
$this->assertSame('document.pdf', $document->metadata->getSource());
136+
$this->assertSame('custom_value', $document->metadata['custom_field']);
137+
}
138+
139+
#[DataProvider('uuidProvider')]
140+
#[TestDox('Accepts different UUID versions')]
141+
public function testWithDifferentUuidVersions(Uuid $uuid)
142+
{
143+
$vector = new Vector([0.1, 0.2, 0.3]);
144+
145+
$document = new VectorDocument($uuid, $vector);
146+
147+
$this->assertSame($uuid, $document->id);
148+
}
149+
150+
/**
151+
* @return \Iterator<string, array{uuid: Uuid}>
152+
*/
153+
public static function uuidProvider(): \Iterator
154+
{
155+
yield 'UUID v1' => ['uuid' => Uuid::v1()];
156+
yield 'UUID v4' => ['uuid' => Uuid::v4()];
157+
yield 'UUID v6' => ['uuid' => Uuid::v6()];
158+
yield 'UUID v7' => ['uuid' => Uuid::v7()];
159+
}
160+
161+
#[TestDox('Handles complex nested metadata structures')]
162+
public function testWithComplexMetadata()
163+
{
164+
$id = Uuid::v4();
165+
$vector = new Vector([0.1, 0.2, 0.3]);
166+
$metadata = new Metadata([
167+
'title' => 'Complex Document',
168+
'tags' => ['ai', 'ml', 'vectors'],
169+
'nested' => [
170+
'level1' => [
171+
'level2' => 'deep value',
172+
],
173+
],
174+
'timestamp' => 1234567890,
175+
'active' => true,
176+
]);
177+
178+
$document = new VectorDocument($id, $vector, $metadata);
179+
180+
$this->assertSame($metadata, $document->metadata);
181+
$this->assertSame('Complex Document', $document->metadata['title']);
182+
$this->assertSame(['ai', 'ml', 'vectors'], $document->metadata['tags']);
183+
$this->assertSame(['level1' => ['level2' => 'deep value']], $document->metadata['nested']);
184+
$this->assertSame(1234567890, $document->metadata['timestamp']);
185+
$this->assertTrue($document->metadata['active']);
186+
}
187+
188+
#[TestDox('Verifies vector interface methods are accessible')]
189+
public function testVectorInterfaceInteraction()
190+
{
191+
$id = Uuid::v4();
192+
$vector = new Vector([0.1, 0.2, 0.3]);
193+
194+
$document = new VectorDocument($id, $vector);
195+
196+
$this->assertSame([0.1, 0.2, 0.3], $document->vector->getData());
197+
$this->assertSame(3, $document->vector->getDimensions());
198+
}
199+
200+
#[TestDox('Multiple documents can share the same metadata instance')]
201+
public function testMultipleDocumentsWithSameMetadataInstance()
202+
{
203+
$metadata = new Metadata(['shared' => 'value']);
204+
$vector1 = new Vector([0.1, 0.2, 0.3]);
205+
$vector2 = new Vector([0.4, 0.5, 0.6]);
206+
207+
$document1 = new VectorDocument(Uuid::v4(), $vector1, $metadata);
208+
$document2 = new VectorDocument(Uuid::v4(), $vector2, $metadata);
209+
210+
// Both documents share the same metadata instance
211+
$this->assertSame($metadata, $document1->metadata);
212+
$this->assertSame($metadata, $document2->metadata);
213+
$this->assertSame($document1->metadata, $document2->metadata);
214+
}
215+
216+
#[TestDox('Documents with same values are equal but not identical')]
217+
public function testDocumentEquality()
218+
{
219+
$id = Uuid::v4();
220+
$vector = new Vector([0.1, 0.2, 0.3]);
221+
$metadata = new Metadata(['key' => 'value']);
222+
$score = 0.75;
223+
224+
$document1 = new VectorDocument($id, $vector, $metadata, $score);
225+
$document2 = new VectorDocument($id, $vector, $metadata, $score);
226+
227+
// Same values but different instances
228+
$this->assertNotSame($document1, $document2);
229+
230+
// But properties should be the same
231+
$this->assertSame($document1->id, $document2->id);
232+
$this->assertSame($document1->vector, $document2->vector);
233+
$this->assertSame($document1->metadata, $document2->metadata);
234+
$this->assertSame($document1->score, $document2->score);
235+
}
236+
}

0 commit comments

Comments
 (0)