Skip to content

Commit 216f0a5

Browse files
committed
test: documents
1 parent fa53900 commit 216f0a5

1 file changed

Lines changed: 81 additions & 4 deletions

File tree

tests/Feature/DocumentsTest.php

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,102 @@
33
namespace Feature;
44

55
use Tests\TestCase;
6+
use Typesense\Documents;
67

78
class DocumentsTest extends TestCase
89
{
10+
private ?Documents $testDocuments = null;
11+
private $document = [
12+
"id" => "4",
13+
"title" => "Book 4",
14+
"description" => "Test...",
15+
"authors" => ["Hayden"],
16+
"isbn" => "1",
17+
"publisher" => "AwesomeBooks",
18+
"pages" => 9
19+
];
20+
921
protected function setUp(): void
1022
{
1123
parent::setUp();
12-
1324
$this->setUpCollection('books');
1425
$this->setUpDocuments('books');
26+
27+
$this->testDocuments = $this->client()->collections['books']->documents;
28+
}
29+
30+
public function testCanSearchForDocumentsInACollection(): void
31+
{
32+
$response = $this->testDocuments->search([
33+
'q' => 'book',
34+
'query_by' => 'title',
35+
]);
36+
$this->assertEquals('book', $response['request_params']['q']);
37+
$this->assertGreaterThan(0, $response['hits']);
38+
}
39+
40+
public function testCanCreateADocument(): void
41+
{
42+
$response = $this->testDocuments->create($this->document);
43+
$this->assertEquals($this->document, $response);
44+
}
45+
46+
public function testCanUpsertADocument(): void
47+
{
48+
$documentWithDifferentId = [...$this->document, 'id' => '1']; // id 1 already exists in the collection
49+
50+
$response = $this->testDocuments->upsert($documentWithDifferentId);
51+
$this->assertEquals($documentWithDifferentId, $response);
52+
}
53+
54+
public function testCanImportJsonlDocumentsWithQueryParameter(): void
55+
{
56+
$documentWithDifferentId = [...$this->document, 'id' => '1'];
57+
$jsonlDocuments = join(PHP_EOL, array_map(
58+
fn ($item) => json_encode($item),
59+
[$this->document, $documentWithDifferentId]
60+
));
61+
62+
$response = $this->testDocuments->import($jsonlDocuments, [
63+
'action' => 'upsert'
64+
]);
65+
$this->assertEquals("{\"success\":true}\n{\"success\":true}", $response);
1566
}
1667

17-
public function testCanUpdateDocumentsByFilter(): void
68+
public function testCanImportArrayOfDocuments(): void
69+
{
70+
$documentWithDifferentId = [...$this->document, 'id' => '5'];
71+
72+
$response = $this->testDocuments->import([$this->document, $documentWithDifferentId]);
73+
$this->assertEquals(1, $response[0]['success']);
74+
$this->assertEquals(1, $response[1]['success']);
75+
}
76+
77+
public function testCanExportDocumentsWithControlParameter(): void
78+
{
79+
$this->testDocuments->create($this->document);
80+
81+
$response = $this->testDocuments->export([
82+
"filter_by" => "id:=4"
83+
]);
84+
$this->assertEquals($this->document, json_decode($response, true));
85+
}
86+
87+
public function testCanUpdateDocumentsByQuery(): void
1888
{
1989
$document = ['publisher' => 'Renamed Publisher'];
2090

21-
$response = $this->client()->collections['books']->documents->update($document, [
91+
$response = $this->testDocuments->update($document, [
2292
'filter_by' => 'publisher:=AwesomeBooks'
2393
]);
24-
2594
$this->assertGreaterThan(0, $response['num_updated']);
2695
}
96+
97+
public function testCanDeleteDocumentsByQuery(): void
98+
{
99+
$response = $this->testDocuments->delete([
100+
'filter_by' => 'publisher:=AwesomeBooks'
101+
]);
102+
$this->assertGreaterThan(0, $response['num_deleted']);
103+
}
27104
}

0 commit comments

Comments
 (0)