Skip to content

Commit

Permalink
test: add tests for upsert data
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjorgedev committed Jan 17, 2025
1 parent 0232926 commit 4945d8b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
</include>
</source>
<php>
<!-- For testing dense index -->
<env name="DENSE_UPSTASH_VECTOR_REST_URL" value="https://vector.upstash.com" />
<env name="DENSE_UPSTASH_VECTOR_REST_TOKEN" value="test-token" />
<!-- For testing dense index with embedding -->
<env name="DENSE_EMBEDDING_UPSTASH_VECTOR_REST_URL" value="https://vector.upstash.com" />
<env name="DENSE_EMBEDDING_UPSTASH_VECTOR_REST_TOKEN" value="test-token" />
</php>
</phpunit>
33 changes: 33 additions & 0 deletions tests/Concerns/UsesDenseIndexWithEmbedding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Upstash\Vector\Tests\Concerns;

use Upstash\Vector\Contracts\IndexInterface;
use Upstash\Vector\Contracts\IndexNamespaceInterface;
use Upstash\Vector\Index;

trait UsesDenseIndexWithEmbedding
{
protected IndexInterface $index;

protected IndexNamespaceInterface $namespace;

public function setUp(): void
{
parent::setUp();

$this->index = new Index(
url: getenv('DENSE_EMBEDDING_UPSTASH_VECTOR_REST_URL'),
token: getenv('DENSE_EMBEDDING_UPSTASH_VECTOR_REST_TOKEN'),
);

$this->namespace = $this->index->namespace(bin2hex(random_bytes(32)));
}

public function tearDown(): void
{
$this->namespace->delete();

parent::tearDown();
}
}
43 changes: 43 additions & 0 deletions tests/Dense/Operations/UpsertDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Upstash\Vector\Tests\Dense\Operations;

use PHPUnit\Framework\TestCase;
use Upstash\Vector\DataUpsert;
use Upstash\Vector\Tests\Concerns\UsesDenseIndexWithEmbedding;
use Upstash\Vector\Tests\Concerns\WaitsForIndex;

class UpsertDataTest extends TestCase
{
use UsesDenseIndexWithEmbedding;
use WaitsForIndex;

public function test_upsert_data(): void
{
$this->namespace->upsertData(new DataUpsert(
id: '1',
data: 'The capital of Japan is Tokyo',
));

$this->waitForIndex($this->namespace);

$info = $this->namespace->getNamespaceInfo();

$this->assertSame(1, $info->vectorCount);
}

public function test_upsert_many_data(): void
{
$this->namespace->upsertDataMany([
new DataUpsert(id: '1', data: 'The capital of Japan is Tokyo'),
new DataUpsert(id: '2', data: 'The capital of France is Paris'),
new DataUpsert(id: '3', data: 'The capital of Germany is Berlin'),
]);

$this->waitForIndex($this->namespace);

$info = $this->namespace->getNamespaceInfo();

$this->assertSame(3, $info->vectorCount);
}
}

0 comments on commit 4945d8b

Please sign in to comment.