Skip to content

Commit a0bdbef

Browse files
committed
adding tests
1 parent ccfb275 commit a0bdbef

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/store/tests/Bridge/ChromaDb/StoreTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,108 @@ public function testQueryWithVariousFilterCombinations(
469469
$this->assertCount(1, $documents);
470470
}
471471

472+
public function testQueryReturnsMetadatasEmbeddingsDistanceWithoutInclude(): void
473+
{
474+
$queryVector = new Vector([0.15, 0.25, 0.35]);
475+
$queryResponse = new QueryItemsResponse(
476+
ids: [['01234567-89ab-cdef-0123-456789abcdef']],
477+
embeddings: [[[0.1, 0.2, 0.3]]],
478+
metadatas: [[['title' => 'Doc 1']]],
479+
documents: null,
480+
data: null,
481+
uris: null,
482+
distances: null
483+
);
484+
485+
$collection = $this->createMock(CollectionResource::class);
486+
$client = $this->createMock(Client::class);
487+
488+
$client->expects($this->once())
489+
->method('getOrCreateCollection')
490+
->with('test-collection')
491+
->willReturn($collection);
492+
493+
$collection->expects($this->once())
494+
->method('query')
495+
->willReturn($queryResponse);
496+
497+
$store = new Store($client, 'test-collection');
498+
$documents = iterator_to_array($store->query($queryVector));
499+
500+
$this->assertCount(1, $documents);
501+
$this->assertSame('01234567-89ab-cdef-0123-456789abcdef', (string) $documents[0]->id);
502+
$this->assertSame([0.1, 0.2, 0.3], $documents[0]->vector->getData());
503+
$this->assertSame(['title' => 'Doc 1'], $documents[0]->metadata->getArrayCopy());
504+
}
505+
506+
public function testQueryReturnsMetadatasEmbeddingsDistanceWithOnlyDocuments(): void
507+
{
508+
$queryVector = new Vector([0.15, 0.25, 0.35]);
509+
$queryResponse = new QueryItemsResponse(
510+
ids: [['01234567-89ab-cdef-0123-456789abcdef']],
511+
embeddings: [[[0.1, 0.2, 0.3]]],
512+
metadatas: [[['title' => 'Doc 1']]],
513+
documents: [['Document content here']],
514+
data: null,
515+
uris: null,
516+
distances: null
517+
);
518+
519+
$collection = $this->createMock(CollectionResource::class);
520+
$client = $this->createMock(Client::class);
521+
522+
$client->expects($this->once())
523+
->method('getOrCreateCollection')
524+
->with('test-collection')
525+
->willReturn($collection);
526+
527+
$collection->expects($this->once())
528+
->method('query')
529+
->willReturn($queryResponse);
530+
531+
$store = new Store($client, 'test-collection');
532+
$documents = iterator_to_array($store->query($queryVector, ['include' => ['documents']]));
533+
534+
$this->assertCount(1, $documents);
535+
$this->assertSame('01234567-89ab-cdef-0123-456789abcdef', (string) $documents[0]->id);
536+
$this->assertSame([0.1, 0.2, 0.3], $documents[0]->vector->getData());
537+
$this->assertSame(['title' => 'Doc 1', '_text' => 'Document content here'], $documents[0]->metadata->getArrayCopy());
538+
}
539+
540+
public function testQueryReturnsMetadatasEmbeddingsDistanceWithAll(): void
541+
{
542+
$queryVector = new Vector([0.15, 0.25, 0.35]);
543+
$queryResponse = new QueryItemsResponse(
544+
ids: [['01234567-89ab-cdef-0123-456789abcdef']],
545+
embeddings: [[[0.1, 0.2, 0.3]]],
546+
metadatas: [[['title' => 'Doc 1']]],
547+
documents: [['Document content here']],
548+
data: null,
549+
uris: null,
550+
distances: null
551+
);
552+
553+
$collection = $this->createMock(CollectionResource::class);
554+
$client = $this->createMock(Client::class);
555+
556+
$client->expects($this->once())
557+
->method('getOrCreateCollection')
558+
->with('test-collection')
559+
->willReturn($collection);
560+
561+
$collection->expects($this->once())
562+
->method('query')
563+
->willReturn($queryResponse);
564+
565+
$store = new Store($client, 'test-collection');
566+
$documents = iterator_to_array($store->query($queryVector, ['include' => ['embeddings', 'metadatas', 'distances', 'documents']]));
567+
568+
$this->assertCount(1, $documents);
569+
$this->assertSame('01234567-89ab-cdef-0123-456789abcdef', (string) $documents[0]->id);
570+
$this->assertSame([0.1, 0.2, 0.3], $documents[0]->vector->getData());
571+
$this->assertSame(['title' => 'Doc 1', '_text' => 'Document content here'], $documents[0]->metadata->getArrayCopy());
572+
}
573+
472574
/**
473575
* @return \Iterator<string, array{
474576
* options: array{where?: array<string, string>, whereDocument?: array<string, mixed>},

0 commit comments

Comments
 (0)