Skip to content

Commit 2cb9b69

Browse files
committed
test improvement for VectorStruct and MultiVectorStruct
1 parent 0fbaf1b commit 2cb9b69

6 files changed

Lines changed: 134 additions & 15 deletions

File tree

src/Exception/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Qdrant\Response;
1212

13-
class InvalidArgumentException extends \Exception
13+
class InvalidArgumentException extends \InvalidArgumentException
1414
{
1515
protected Response $response;
1616

src/Models/MultiVectorStruct.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Qdrant\Models;
44

5-
use InvalidArgumentException;
5+
use Qdrant\Exception\InvalidArgumentException;
66

77
class MultiVectorStruct implements VectorStructInterface
88
{
@@ -51,7 +51,7 @@ public function toArray(): array
5151
return $this->vectors;
5252
}
5353

54-
public function count()
54+
public function count(): int
5555
{
5656
return count($this->vectors);
5757
}

src/Models/PointStruct.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class PointStruct
1515
{
1616
use ProtectedPropertyAccessor;
1717

18-
// TODO: we need a solution for point with uuid
1918
protected int|string $id;
2019
protected ?array $payload = null;
2120
protected VectorStructInterface $vector;
@@ -63,26 +62,17 @@ public function toArray(): array
6362
return $point;
6463
}
6564

66-
/**
67-
* @return int
68-
*/
6965
public function getId(): int|string
7066
{
7167
return $this->id;
7268
}
7369

74-
/**
75-
* @return array|null
76-
*/
7770
public function getPayload(): ?array
7871
{
7972
return $this->payload;
8073
}
8174

82-
/**
83-
* @return VectorStruct
84-
*/
85-
public function getVector(): VectorStruct
75+
public function getVector(): VectorStructInterface
8676
{
8777
return $this->vector;
8878
}

src/Models/VectorStruct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(array $vector, string $name = null)
2121
$this->name = $name;
2222
}
2323

24-
public function isNamed(): bool
24+
private function isNamed(): bool
2525
{
2626
return $this->name !== null;
2727
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* @since Aug 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Models;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Qdrant\Exception\InvalidArgumentException;
11+
use Qdrant\Models\MultiVectorStruct;
12+
use Qdrant\Models\VectorStruct;
13+
14+
class MultiVectorStructTest extends TestCase
15+
{
16+
public function testMultiVectorStruct(): void
17+
{
18+
$vector = new MultiVectorStruct([[1, 2, 3], [1, 2, 4]]);
19+
20+
$this->assertEquals(
21+
[[1, 2, 3], [1, 2, 4]],
22+
$vector->toArray()
23+
);
24+
}
25+
26+
public function testNamedMultiVectorStruct(): void
27+
{
28+
$vector = new MultiVectorStruct([
29+
'foo' => [1, 2, 3],
30+
'bar' => [1, 2, 4],
31+
]);
32+
33+
$this->assertEquals(
34+
[
35+
'foo' => [1, 2, 3],
36+
'bar' => [1, 2, 4],
37+
],
38+
$vector->toArray()
39+
);
40+
41+
$this->assertEquals(
42+
[
43+
'name' => 'foo',
44+
'vector' => [1, 2, 3]
45+
],
46+
$vector->toSearchArray('foo')
47+
);
48+
49+
$this->assertEquals(
50+
[
51+
'name' => 'bar',
52+
'vector' => [1, 2, 4]
53+
],
54+
$vector->toSearchArray('bar')
55+
);
56+
}
57+
58+
public function testNamedMultiVectorStructWithMissingName(): void
59+
{
60+
$this->expectException(InvalidArgumentException::class);
61+
$this->expectExceptionMessage("Vector with name uber not found");
62+
63+
$vector = new MultiVectorStruct([
64+
'foo' => [1, 2, 3],
65+
'bar' => [1, 2, 4],
66+
]);
67+
68+
$vector->toSearchArray('uber');
69+
}
70+
71+
public function testMultiVectorStructCount(): void
72+
{
73+
$vector = new MultiVectorStruct([
74+
'foo' => [1, 2, 3],
75+
'bar' => [1, 2, 4],
76+
]);
77+
78+
$this->assertEquals(2, $vector->count());
79+
}
80+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* @since Aug 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Models;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Qdrant\Models\VectorStruct;
11+
12+
class VectorStructTest extends TestCase
13+
{
14+
public function testVectorStruct(): void
15+
{
16+
$vector = new VectorStruct([1, 2, 3]);
17+
18+
$this->assertEquals(
19+
[1, 2, 3],
20+
$vector->toArray()
21+
);
22+
23+
24+
$this->assertEquals(
25+
[1, 2, 3],
26+
$vector->toSearchArray()
27+
);
28+
}
29+
30+
public function testNamedVectorStruct(): void
31+
{
32+
$vector = new VectorStruct([1, 2, 3], 'foo');
33+
34+
$this->assertEquals(
35+
[
36+
'foo' => [1, 2, 3]
37+
],
38+
$vector->toArray()
39+
);
40+
41+
$this->assertEquals(
42+
[
43+
'name' => 'foo',
44+
'vector' => [1, 2, 3]
45+
],
46+
$vector->toSearchArray()
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)