Skip to content

Commit 09d78fa

Browse files
committed
test(vectore-stores): search resource and response
1 parent cbcdb3f commit 09d78fa

File tree

7 files changed

+171
-28
lines changed

7 files changed

+171
-28
lines changed

src/Responses/VectorStores/Search/VectorStoreSearchResponse.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,4 @@ public function toArray(): array
7777
'next_page' => $this->nextPage,
7878
];
7979
}
80-
81-
/**
82-
* Returns whether there are more results available.
83-
*/
84-
public function hasNextPage(): bool
85-
{
86-
return $this->hasMore;
87-
}
88-
89-
/**
90-
* Returns the identifier to use to retrieve the next page of results.
91-
*/
92-
public function nextPageId(): ?string
93-
{
94-
return $this->nextPage;
95-
}
96-
97-
public function __get(string $name): mixed
98-
{
99-
return match ($name) {
100-
'object' => $this->object,
101-
'search_query' => $this->searchQuery,
102-
'data' => $this->data,
103-
'has_more' => $this->hasMore,
104-
'next_page' => $this->nextPage,
105-
default => null,
106-
};
107-
}
10880
}

src/Testing/Resources/VectorStoresTestResource.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use OpenAI\Contracts\Resources\VectorStoresFileBatchesContract;
77
use OpenAI\Contracts\Resources\VectorStoresFilesContract;
88
use OpenAI\Resources\VectorStores;
9+
use OpenAI\Responses\VectorStores\Search\VectorStoreSearchResponse;
910
use OpenAI\Responses\VectorStores\VectorStoreDeleteResponse;
1011
use OpenAI\Responses\VectorStores\VectorStoreListResponse;
1112
use OpenAI\Responses\VectorStores\VectorStoreResponse;
@@ -45,6 +46,14 @@ public function list(array $parameters = []): VectorStoreListResponse
4546
return $this->record(__FUNCTION__, func_get_args());
4647
}
4748

49+
/**
50+
* @param array<string, mixed> $parameters
51+
*/
52+
public function search(string $vectorStoreId, array $parameters = []): VectorStoreSearchResponse
53+
{
54+
return $this->record(__FUNCTION__, func_get_args());
55+
}
56+
4857
public function files(): VectorStoresFilesContract
4958
{
5059
return new VectorStoresFilesTestResource($this->fake);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace OpenAI\Testing\Responses\Fixtures\VectorStores\Search;
4+
5+
final class VectorStoreSearchResponseContentFixture
6+
{
7+
public const ATTRIBUTES = [
8+
'type' => 'text',
9+
'text' => 'Sample text content from the vector store.',
10+
];
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace OpenAI\Testing\Responses\Fixtures\VectorStores\Search;
4+
5+
final class VectorStoreSearchResponseFileFixture
6+
{
7+
public const ATTRIBUTES = [
8+
'file_id' => 'file_abc123',
9+
'filename' => 'document.pdf',
10+
'score' => 0.95,
11+
'attributes' => [
12+
'author' => 'John Doe',
13+
'date' => '2023-01-01',
14+
],
15+
'content' => [
16+
VectorStoreSearchResponseContentFixture::ATTRIBUTES,
17+
],
18+
];
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace OpenAI\Testing\Responses\Fixtures\VectorStores\Search;
4+
5+
final class VectorStoreSearchResponseFixture
6+
{
7+
public const ATTRIBUTES = [
8+
'object' => 'vector_store.search_results.page',
9+
'search_query' => 'What is the return policy?',
10+
'data' => [
11+
VectorStoreSearchResponseFileFixture::ATTRIBUTES,
12+
[
13+
'file_id' => 'file_xyz789',
14+
'filename' => 'notes.txt',
15+
'score' => 0.89,
16+
'attributes' => [
17+
'author' => 'Jane Smith',
18+
'date' => '2023-01-02',
19+
],
20+
'content' => [
21+
[
22+
'type' => 'text',
23+
'text' => 'Sample text content from the vector store.',
24+
],
25+
],
26+
],
27+
],
28+
'has_more' => false,
29+
'next_page' => null,
30+
];
31+
}

tests/Fixtures/VectorStoreSearch.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* @return array<string, mixed>
5+
*/
6+
function vectorStoreSearchResource(): array
7+
{
8+
return [
9+
'object' => 'vector_store.search_results.page',
10+
'search_query' => 'What is the return policy?',
11+
'data' => [
12+
[
13+
'file_id' => 'file_abc123',
14+
'filename' => 'policy.pdf',
15+
'score' => 0.95,
16+
'attributes' => [
17+
'author' => 'John Doe',
18+
'date' => '2023-01-01',
19+
],
20+
'content' => [
21+
[
22+
'type' => 'text',
23+
'text' => 'Our return policy allows for returns within 30 days of purchase.',
24+
],
25+
],
26+
],
27+
[
28+
'file_id' => 'file_xyz789',
29+
'filename' => 'notes.txt',
30+
'score' => 0.89,
31+
'attributes' => [
32+
'author' => 'Jane Smith',
33+
'date' => '2023-01-02',
34+
],
35+
'content' => [
36+
[
37+
'type' => 'text',
38+
'text' => 'Sample text content from the vector store.',
39+
],
40+
],
41+
],
42+
],
43+
'has_more' => false,
44+
'next_page' => null,
45+
];
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use OpenAI\Responses\VectorStores\Search\VectorStoreSearchResponse;
4+
use OpenAI\Responses\VectorStores\Search\VectorStoreSearchResponseFile;
5+
6+
test('from', function () {
7+
$result = VectorStoreSearchResponse::from(vectorStoreSearchResource(), meta());
8+
9+
expect($result)
10+
->object->toBe('vector_store.search_results.page')
11+
->searchQuery->toBe('What is the return policy?')
12+
->data->toBeArray()->toHaveCount(2)
13+
->data->{0}->toBeInstanceOf(VectorStoreSearchResponseFile::class)
14+
->hasMore->toBe(false)
15+
->nextPage->toBe(null);
16+
});
17+
18+
test('as array accessible', function () {
19+
$result = VectorStoreSearchResponse::from(vectorStoreSearchResource(), meta());
20+
21+
expect($result['search_query'])
22+
->toBe('What is the return policy?');
23+
});
24+
25+
test('to array', function () {
26+
$result = VectorStoreSearchResponse::from(vectorStoreSearchResource(), meta());
27+
28+
expect($result->toArray())
29+
->toBe(vectorStoreSearchResource());
30+
});
31+
32+
test('fake', function () {
33+
$response = VectorStoreSearchResponse::fake();
34+
35+
expect($response)
36+
->object->toBe('vector_store.search_results.page')
37+
->searchQuery->toBe('What is the return policy?')
38+
->hasMore->toBe(false)
39+
->nextPage->toBe(null);
40+
});
41+
42+
test('fake with override', function () {
43+
$response = VectorStoreSearchResponse::fake([
44+
'object' => 'custom_object',
45+
'search_query' => 'Custom query',
46+
'has_more' => false,
47+
'next_page' => null,
48+
]);
49+
50+
expect($response)
51+
->object->toBe('custom_object')
52+
->searchQuery->toBe('Custom query')
53+
->hasMore->toBe(false)
54+
->nextPage->toBeNull();
55+
});

0 commit comments

Comments
 (0)