14
14
use Codewithkyrian \ChromaDB \Client ;
15
15
use Codewithkyrian \ChromaDB \Resources \CollectionResource ;
16
16
use PHPUnit \Framework \Attributes \CoversClass ;
17
+ use PHPUnit \Framework \Attributes \DataProvider ;
17
18
use PHPUnit \Framework \TestCase ;
18
19
use Symfony \AI \Platform \Vector \Vector ;
19
20
use Symfony \AI \Store \Bridge \ChromaDb \Store ;
24
25
#[CoversClass(Store::class)]
25
26
final class StoreTest extends TestCase
26
27
{
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 {
29
43
$ collection = $ this ->createMock (CollectionResource::class);
30
44
$ client = $ this ->createMock (Client::class);
31
45
@@ -34,49 +48,88 @@ public function testAddDocumentsSuccessfully()
34
48
->with ('test-collection ' )
35
49
->willReturn ($ collection );
36
50
37
- $ uuid1 = Uuid::v4 ();
38
- $ uuid2 = Uuid::v4 ();
39
-
40
51
$ collection ->expects ($ this ->once ())
41
52
->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 );
47
54
48
55
$ store = new Store ($ client , 'test-collection ' );
49
56
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 );
54
58
}
55
59
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
57
70
{
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
+ ];
81
134
}
82
135
}
0 commit comments