Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/store/src/Bridge/Weaviate/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\VectorDocument;
use Symfony\AI\Store\Exception\InvalidArgumentException;
use Symfony\AI\Store\Exception\LogicException;
use Symfony\AI\Store\ManagedStoreInterface;
use Symfony\AI\Store\StoreInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand Down Expand Up @@ -61,7 +60,22 @@ public function add(VectorDocument|array $documents): void

public function remove(string|array $ids, array $options = []): void
{
throw new LogicException('Method not implemented yet.');
if (\is_string($ids)) {
$ids = [$ids];
}

if ([] === $ids) {
return;
}

$objects = array_map(fn (string $id) => [
'class' => $this->collection,
'id' => $id,
], $ids);
Comment on lines +71 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$this->request('DELETE', 'v1/batch/objects', [
'objects' => $objects,
]);
}

public function query(Vector $vector, array $options = []): iterable
Expand Down
81 changes: 81 additions & 0 deletions src/store/src/Bridge/Weaviate/Tests/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,85 @@ public function testStoreCanQuery()
$this->assertCount(2, $results);
$this->assertSame(1, $httpClient->getRequestsCount());
}

public function testStoreCanRemoveSingleId()
{
$httpClient = new MockHttpClient([
new JsonMockResponse([], [
'http_code' => 204,
]),
], 'http://127.0.0.1:8080');

$store = new Store(
$httpClient,
'http://127.0.0.1:8080',
'test',
'test',
);

$store->remove('test-id-1');

$this->assertSame(1, $httpClient->getRequestsCount());
}

public function testStoreCanRemoveMultipleIds()
{
$httpClient = new MockHttpClient([
new JsonMockResponse([], [
'http_code' => 204,
]),
], 'http://127.0.0.1:8080');

$store = new Store(
$httpClient,
'http://127.0.0.1:8080',
'test',
'test',
);

$store->remove(['test-id-1', 'test-id-2', 'test-id-3']);

$this->assertSame(1, $httpClient->getRequestsCount());
}

public function testStoreCanRemoveWithEmptyArray()
{
$httpClient = new MockHttpClient([], 'http://127.0.0.1:8080');

$store = new Store(
$httpClient,
'http://127.0.0.1:8080',
'test',
'test',
);

$store->remove([]);

$this->assertSame(0, $httpClient->getRequestsCount());
}

public function testStoreCannotRemoveOnInvalidResponse()
{
$httpClient = new MockHttpClient([
new JsonMockResponse([
'error' => [
'message' => 'Object not found',
],
], [
'http_code' => 404,
]),
], 'http://127.0.0.1:8080');

$store = new Store(
$httpClient,
'http://127.0.0.1:8080',
'test',
'test',
);

$this->expectException(ClientException::class);
$this->expectExceptionMessage('HTTP 404 returned for "http://127.0.0.1:8080/v1/batch/objects".');
$this->expectExceptionCode(404);
$store->remove('non-existent-id');
}
}