Skip to content

Commit

Permalink
WIP: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 10, 2023
1 parent 0575905 commit bc40271
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static function from(array $attributes): self
public function toArray(): array
{
return [
'description' => $this->description,
'name' => $this->name,
'description' => $this->description,
'parameters' => $this->parameters,
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace OpenAI\Testing\Responses\Fixtures\Assistants;

final class AssistantDeleteResponseFixture
{
public const ATTRIBUTES = [
'id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'object' => 'assistant.deleted',
'deleted' => true,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OpenAI\Testing\Responses\Fixtures\Assistants;

final class AssistantListResponseFixture
{
public const ATTRIBUTES = [
'object' => 'list',
'data' => [
[
'id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'object' => 'assistant',
'created_at' => 1_699_619_403,
'name' => 'Math Tutor',
'description' => null,
'model' => 'gpt-4',
'instructions' => 'You are a personal math tutor.',
'tools' => [],
'file_ids' => [],
'metadata' => [],
],
],
'first_id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'last_id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'has_more' => false,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OpenAI\Testing\Responses\Fixtures\Assistants;

final class AssistantResponseFixture
{
public const ATTRIBUTES = [
'id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'object' => 'assistant',
'created_at' => 1_699_619_403,
'name' => 'Math Tutor',
'description' => null,
'model' => 'gpt-4',
'instructions' => 'You are a personal math tutor.',
'tools' => [],
'file_ids' => [],
'metadata' => [],
];
}
115 changes: 114 additions & 1 deletion tests/Fixtures/Assistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function assistantResource(): array
'model' => 'gpt-4',
'instructions' => 'You are a personal math tutor.',
'tools' => [
0 => [
[
'type' => 'code_interpreter',
],
],
Expand All @@ -23,6 +23,119 @@ function assistantResource(): array
];
}

/**
* @return array<string, mixed>
*/
function assistantWithFunctionToolResource(): array
{
return [
'id' => 'asst_reHHtAM0jKLDIxanM6gP6DaR',
'object' => 'assistant',
'created_at' => 1699642651,
'name' => 'Math Tutor',
'description' => null,
'model' => 'gpt-4',
'instructions' => 'You are a personal math tutor. When asked a question, write and run Python code to answer the question.',
'tools' => [
[
'type' => 'function',
'function' => [
'name' => 'add',
'description' => 'Returns the sum of two numbers',
'parameters' => [
'type' => 'object',
'properties' => [
'a' => [
'type' => 'number',
],
'b' => [
'type' => 'number',
],
],
'required' => [
'a',
'b',
],
],
],
],
],
'file_ids' => [],
'metadata' => [],
];
}

/**
* @return array<string, mixed>
*/
function assistantWithRetrievalToolResource(): array
{
return [
'id' => 'asst_3jHvDyRbElRz2yig9RrPT9cX',
'object' => 'assistant',
'created_at' => 1699642972,
'name' => 'Math Tutor',
'description' => null,
'model' => 'gpt-4-1106-preview',
'instructions' => 'You are a personal math tutor. When asked a question, write and run Python code to answer the question.',
'tools' => [
[
'type' => 'retrieval',
],
],
'file_ids' => [],
'metadata' => [],
];
}

/**
* @return array<string, mixed>
*/
function assistantWithAllToolsResource(): array
{
return [
'id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
'object' => 'assistant',
'created_at' => 1699619403,
'name' => 'Math Tutor',
'description' => null,
'model' => 'gpt-4',
'instructions' => 'You are a personal math tutor.',
'tools' => [
[
'type' => 'code_interpreter',
],
[
'type' => 'retrieval',
],
[
'type' => 'function',
'function' => [
'name' => 'add',
'description' => 'Returns the sum of two numbers',
'parameters' => [
'type' => 'object',
'properties' => [
'a' => [
'type' => 'number',
],
'b' => [
'type' => 'number',
],
],
'required' => [
'a',
'b',
],
],
],
],
],
'file_ids' => [],
'metadata' => [],
];
}

/**
* @return array<string, mixed>
*/
Expand Down
47 changes: 47 additions & 0 deletions tests/Responses/Assistants/AssistantDeleteResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use OpenAI\Responses\Assistants\AssistantDeleteResponse;
use OpenAI\Responses\Meta\MetaInformation;

test('from', function () {
$result = AssistantDeleteResponse::from(assistantDeleteResource(), meta());

expect($result)
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc')
->object->toBe('assistant.deleted')
->deleted->toBe(true)
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('as array accessible', function () {
$result = AssistantDeleteResponse::from(assistantDeleteResource(), meta());

expect($result['id'])
->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc');
});

test('to array', function () {
$result = AssistantDeleteResponse::from(assistantDeleteResource(), meta());

expect($result->toArray())
->toBe(assistantDeleteResource());
});

test('fake', function () {
$response = AssistantDeleteResponse::fake();

expect($response)
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc')
->deleted->toBe(true);
});

test('fake with override', function () {
$response = AssistantDeleteResponse::fake([
'id' => 'asst_1234',
'deleted' => false,
]);

expect($response)
->id->toBe('asst_1234')
->deleted->toBe(false);
});
50 changes: 50 additions & 0 deletions tests/Responses/Assistants/AssistantListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use OpenAI\Responses\Assistants\AssistantListResponse;
use OpenAI\Responses\Assistants\AssistantResponse;
use OpenAI\Responses\Meta\MetaInformation;

test('from', function () {
$response = AssistantListResponse::from(assistantListResource(), meta());

expect($response)
->toBeInstanceOf(AssistantListResponse::class)
->object->toBe('list')
->data->toBeArray()->toHaveCount(2)
->data->each->toBeInstanceOf(AssistantResponse::class)
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('as array accessible', function () {
$response = AssistantListResponse::from(assistantListResource(), meta());

expect($response['object'])->toBe('list');
});

test('to array', function () {
$response = AssistantListResponse::from(assistantListResource(), meta());

expect($response->toArray())
->toBeArray()
->toBe(assistantListResource());
});

test('fake', function () {
$response = AssistantListResponse::fake();

expect($response['data'][0])
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc');
});

test('fake with override', function () {
$response = AssistantListResponse::fake([
'data' => [
[
'id' => 'asst_1234',
],
],
]);

expect($response['data'][0])
->id->toBe('asst_1234');
});
46 changes: 46 additions & 0 deletions tests/Responses/Assistants/AssistantResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use OpenAI\Responses\Assistants\AssistantResponse;
use OpenAI\Responses\Assistants\AssistantResponseToolCodeInterpreter;
use OpenAI\Responses\Meta\MetaInformation;

test('from', function () {
$result = AssistantResponse::from(assistantWithAllToolsResource(), meta());

expect($result)
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc')
->object->toBe('assistant')
->createdAt->toBe(1699619403)
->name->toBe('Math Tutor')
->description->toBeNull()
->model->toBe('gpt-4')
->instructions->toBe('You are a personal math tutor.')
->tools->toBeArray()
->tools->{0}->toBeInstanceOf(AssistantResponseToolCodeInterpreter::class)
->fileIds->toBeArray()
->metadata->toBeArray()
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('to array', function () {
$result = AssistantResponse::from(assistantWithAllToolsResource(), meta());

expect($result->toArray())
->toBe(assistantWithAllToolsResource());
});

test('fake', function () {
$response = AssistantResponse::fake();

expect($response)
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc');
});

test('fake with override', function () {
$response = AssistantResponse::fake([
'id' => 'asst_1234',
]);

expect($response)
->id->toBe('asst_1234');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use OpenAI\Responses\Assistants\AssistantResponseToolCodeInterpreter;

test('from', function () {
$result = AssistantResponseToolCodeInterpreter::from(assistantResource()['tools'][0]);

expect($result)
->type->toBe('code_interpreter');
});

test('to array', function () {
$result = AssistantResponseToolCodeInterpreter::from(assistantResource()['tools'][0]);

expect($result->toArray())
->toBe(assistantResource()['tools'][0]);
});
19 changes: 19 additions & 0 deletions tests/Responses/Assistants/AssistantResponseToolFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use OpenAI\Responses\Assistants\AssistantResponseToolFunction;
use OpenAI\Responses\Assistants\AssistantResponseToolFunctionFunction;

test('from', function () {
$result = AssistantResponseToolFunction::from(assistantWithFunctionToolResource()['tools'][0]);

expect($result)
->type->toBe('function')
->function->toBeInstanceOf(AssistantResponseToolFunctionFunction::class);
});

test('to array', function () {
$result = AssistantResponseToolFunction::from(assistantWithFunctionToolResource()['tools'][0]);

expect($result->toArray())
->toBe(assistantWithFunctionToolResource()['tools'][0]);
});
Loading

0 comments on commit bc40271

Please sign in to comment.