1414use Codewithkyrian \ChromaDB \Client ;
1515use Codewithkyrian \ChromaDB \Resources \CollectionResource ;
1616use PHPUnit \Framework \Attributes \CoversClass ;
17+ use PHPUnit \Framework \Attributes \DataProvider ;
1718use PHPUnit \Framework \TestCase ;
1819use Symfony \AI \Platform \Vector \Vector ;
1920use Symfony \AI \Store \Bridge \ChromaDb \Store ;
2425#[CoversClass(Store::class)]
2526final class StoreTest extends TestCase
2627{
27- public function testAddDocumentsSuccessfully ()
28- {
28+ /**
29+ * @param array<VectorDocument> $documents
30+ * @param array<string> $expectedIds
31+ * @param array<array<float>> $expectedVectors
32+ * @param array<array<string, mixed>> $expectedMetadata
33+ * @param array<string> $expectedOriginalDocuments
34+ */
35+ #[DataProvider('addDocumentsProvider ' )]
36+ public function testAddDocumentsSuccessfully (
37+ array $ documents ,
38+ array $ expectedIds ,
39+ array $ expectedVectors ,
40+ array $ expectedMetadata ,
41+ array $ expectedOriginalDocuments ,
42+ ): void {
2943 $ collection = $ this ->createMock (CollectionResource::class);
3044 $ client = $ this ->createMock (Client::class);
3145
@@ -34,49 +48,88 @@ public function testAddDocumentsSuccessfully()
3448 ->with ('test-collection ' )
3549 ->willReturn ($ collection );
3650
37- $ uuid1 = Uuid::v4 ();
38- $ uuid2 = Uuid::v4 ();
39-
4051 $ collection ->expects ($ this ->once ())
4152 ->method ('add ' )
42- ->with (
43- [(string ) $ uuid1 , (string ) $ uuid2 ],
44- [[0.1 , 0.2 , 0.3 ], [0.4 , 0.5 , 0.6 ]],
45- [[], ['title ' => 'Test Document ' ]],
46- );
53+ ->with ($ expectedIds , $ expectedVectors , $ expectedMetadata , $ expectedOriginalDocuments );
4754
4855 $ store = new Store ($ client , 'test-collection ' );
4956
50- $ document1 = new VectorDocument ($ uuid1 , new Vector ([0.1 , 0.2 , 0.3 ]));
51- $ document2 = new VectorDocument ($ uuid2 , new Vector ([0.4 , 0.5 , 0.6 ]), new Metadata (['title ' => 'Test Document ' ]));
52-
53- $ store ->add ($ document1 , $ document2 );
57+ $ store ->add (...$ documents );
5458 }
5559
56- public function testAddSingleDocument ()
60+ /**
61+ * @return \Iterator<string, array{
62+ * documents: array<VectorDocument>,
63+ * expectedIds: array<string>,
64+ * expectedVectors: array<array<float>>,
65+ * expectedMetadata: array<array<string, mixed>>,
66+ * expectedOriginalDocuments: array<string>
67+ * }>
68+ */
69+ public static function addDocumentsProvider (): \Iterator
5770 {
58- $ collection = $ this ->createMock (CollectionResource::class);
59- $ client = $ this ->createMock (Client::class);
60-
61- $ client ->expects ($ this ->once ())
62- ->method ('getOrCreateCollection ' )
63- ->with ('test-collection ' )
64- ->willReturn ($ collection );
65-
66- $ uuid = Uuid::v4 ();
67-
68- $ collection ->expects ($ this ->once ())
69- ->method ('add ' )
70- ->with (
71- [(string ) $ uuid ],
72- [[0.1 , 0.2 , 0.3 ]],
73- [['title ' => 'Test Document ' , 'category ' => 'test ' ]],
74- );
75-
76- $ store = new Store ($ client , 'test-collection ' );
77-
78- $ document = new VectorDocument ($ uuid , new Vector ([0.1 , 0.2 , 0.3 ]), new Metadata (['title ' => 'Test Document ' , 'category ' => 'test ' ]));
79-
80- $ store ->add ($ document );
71+ yield 'multiple documents with and without metadata ' => [
72+ 'documents ' => [
73+ new VectorDocument (
74+ Uuid::fromString ('01234567-89ab-cdef-0123-456789abcdef ' ),
75+ new Vector ([0.1 , 0.2 , 0.3 ]),
76+ ),
77+ new VectorDocument (
78+ Uuid::fromString ('fedcba98-7654-3210-fedc-ba9876543210 ' ),
79+ new Vector ([0.4 , 0.5 , 0.6 ]),
80+ new Metadata (['title ' => 'Test Document ' ]),
81+ ),
82+ ],
83+ 'expectedIds ' => ['01234567-89ab-cdef-0123-456789abcdef ' , 'fedcba98-7654-3210-fedc-ba9876543210 ' ],
84+ 'expectedVectors ' => [[0.1 , 0.2 , 0.3 ], [0.4 , 0.5 , 0.6 ]],
85+ 'expectedMetadata ' => [[], ['title ' => 'Test Document ' ]],
86+ 'expectedOriginalDocuments ' => ['' , '' ],
87+ ];
88+
89+ yield 'single document with metadata ' => [
90+ 'documents ' => [
91+ new VectorDocument (
92+ Uuid::fromString ('01234567-89ab-cdef-0123-456789abcdef ' ),
93+ new Vector ([0.1 , 0.2 , 0.3 ]),
94+ new Metadata (['title ' => 'Test Document ' , 'category ' => 'test ' ]),
95+ ),
96+ ],
97+ 'expectedIds ' => ['01234567-89ab-cdef-0123-456789abcdef ' ],
98+ 'expectedVectors ' => [[0.1 , 0.2 , 0.3 ]],
99+ 'expectedMetadata ' => [['title ' => 'Test Document ' , 'category ' => 'test ' ]],
100+ 'expectedOriginalDocuments ' => ['' ],
101+ ];
102+
103+ yield 'documents with text content ' => [
104+ 'documents ' => [
105+ new VectorDocument (
106+ Uuid::fromString ('01234567-89ab-cdef-0123-456789abcdef ' ),
107+ new Vector ([0.1 , 0.2 , 0.3 ]),
108+ new Metadata (['_text ' => 'This is the content of document 1 ' , 'title ' => 'Document 1 ' ])),
109+ new VectorDocument (
110+ Uuid::fromString ('fedcba98-7654-3210-fedc-ba9876543210 ' ),
111+ new Vector ([0.4 , 0.5 , 0.6 ]),
112+ new Metadata (['_text ' => 'This is the content of document 2 ' , 'title ' => 'Document 2 ' , 'category ' => 'test ' ]),
113+ ),
114+ ],
115+ 'expectedIds ' => ['01234567-89ab-cdef-0123-456789abcdef ' , 'fedcba98-7654-3210-fedc-ba9876543210 ' ],
116+ 'expectedVectors ' => [[0.1 , 0.2 , 0.3 ], [0.4 , 0.5 , 0.6 ]],
117+ 'expectedMetadata ' => [['title ' => 'Document 1 ' ], ['title ' => 'Document 2 ' , 'category ' => 'test ' ]],
118+ 'expectedOriginalDocuments ' => ['This is the content of document 1 ' , 'This is the content of document 2 ' ],
119+ ];
120+
121+ yield 'document with null text ' => [
122+ 'documents ' => [
123+ new VectorDocument (
124+ Uuid::fromString ('01234567-89ab-cdef-0123-456789abcdef ' ),
125+ new Vector ([0.1 , 0.2 , 0.3 ]),
126+ new Metadata (['_text ' => null , 'title ' => 'Test Document ' ]),
127+ ),
128+ ],
129+ 'expectedIds ' => ['01234567-89ab-cdef-0123-456789abcdef ' ],
130+ 'expectedVectors ' => [[0.1 , 0.2 , 0.3 ]],
131+ 'expectedMetadata ' => [['title ' => 'Test Document ' ]],
132+ 'expectedOriginalDocuments ' => ['' ],
133+ ];
81134 }
82135}
0 commit comments