Skip to content

Commit 6eced84

Browse files
committed
test(curation): add test suite for curation sets
1 parent afa1476 commit 6eced84

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Tests\TestCase;
6+
use Typesense\Exceptions\ObjectNotFound;
7+
use Exception;
8+
9+
class CurationSetItemsTest extends TestCase
10+
{
11+
private $curationSets = null;
12+
private $curationSetData = [
13+
'items' => [
14+
[
15+
'id' => 'rule-1',
16+
'rule' => [
17+
'query' => 'test',
18+
'match' => 'exact',
19+
],
20+
'includes' => [
21+
[
22+
'id' => '123',
23+
'position' => 1,
24+
],
25+
],
26+
],
27+
],
28+
];
29+
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
if (!$this->isV30OrAbove()) {
35+
$this->markTestSkipped('CurationSetItems is only supported in Typesense v30+');
36+
}
37+
38+
$this->curationSets = $this->client()->curationSets;
39+
$this->curationSets->upsert('test-curation-set-items', $this->curationSetData);
40+
}
41+
42+
protected function tearDown(): void
43+
{
44+
try {
45+
$this->curationSets['test-curation-set-items']->delete();
46+
} catch (Exception $e) {
47+
// Ignore cleanup errors
48+
}
49+
parent::tearDown();
50+
}
51+
52+
public function testCanListItemsInACurationSet(): void
53+
{
54+
$items = $this->curationSets['test-curation-set-items']->getItems()->retrieve();
55+
56+
$this->assertIsArray($items);
57+
$this->assertGreaterThan(0, count($items));
58+
$this->assertEquals('123', $items[0]['includes'][0]['id']);
59+
}
60+
61+
public function testCanUpsertRetrieveAndDeleteAnItem(): void
62+
{
63+
$upserted = $this->curationSets['test-curation-set-items']->getItems()['rule-1']->upsert([
64+
'id' => 'rule-1',
65+
'rule' => [
66+
'query' => 'test',
67+
'match' => 'exact',
68+
],
69+
'includes' => [
70+
[
71+
'id' => '999',
72+
'position' => 1,
73+
],
74+
],
75+
]);
76+
77+
$this->assertEquals('rule-1', $upserted['id']);
78+
79+
$fetched = $this->curationSets['test-curation-set-items']->getItems()['rule-1']->retrieve();
80+
$this->assertEquals('999', $fetched['includes'][0]['id']);
81+
82+
$deletion = $this->curationSets['test-curation-set-items']->getItems()['rule-1']->delete();
83+
$this->assertEquals('rule-1', $deletion['id']);
84+
}
85+
}

tests/Feature/CurationSetsTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Tests\TestCase;
6+
use Typesense\Exceptions\ObjectNotFound;
7+
use Exception;
8+
9+
class CurationSetsTest extends TestCase
10+
{
11+
private $upsertResponse = null;
12+
private $curationSets = null;
13+
private $curationSetData = [
14+
'items' => [
15+
[
16+
'id' => 'rule-1',
17+
'rule' => [
18+
'query' => 'test',
19+
'match' => 'exact',
20+
],
21+
'includes' => [
22+
[
23+
'id' => '123',
24+
'position' => 1,
25+
],
26+
],
27+
],
28+
],
29+
];
30+
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
if (!$this->isV30OrAbove()) {
36+
$this->markTestSkipped('CurationSets is only supported in Typesense v30+');
37+
}
38+
39+
$this->curationSets = $this->client()->curationSets;
40+
$this->upsertResponse = $this->curationSets->upsert('test-curation-set', $this->curationSetData);
41+
}
42+
43+
protected function tearDown(): void
44+
{
45+
try {
46+
$this->curationSets['test-curation-set']->delete();
47+
} catch (Exception $e) {
48+
// Ignore cleanup errors
49+
}
50+
parent::tearDown();
51+
}
52+
53+
public function testCanUpsertACurationSet(): void
54+
{
55+
$this->assertEquals($this->curationSetData['items'][0]['id'], $this->upsertResponse['items'][0]['id']);
56+
$this->assertEquals($this->curationSetData['items'][0]['rule'], $this->upsertResponse['items'][0]['rule']);
57+
$this->assertEquals($this->curationSetData['items'][0]['includes'], $this->upsertResponse['items'][0]['includes']);
58+
}
59+
60+
public function testCanRetrieveAllCurationSets(): void
61+
{
62+
$returnData = $this->curationSets->retrieve();
63+
$this->assertIsArray($returnData);
64+
$this->assertGreaterThan(0, count($returnData));
65+
66+
$created = null;
67+
foreach ($returnData as $curationSet) {
68+
if ($curationSet['name'] === 'test-curation-set') {
69+
$created = $curationSet;
70+
break;
71+
}
72+
}
73+
$this->assertNotNull($created);
74+
}
75+
76+
public function testCanRetrieveASpecificCurationSet(): void
77+
{
78+
$returnData = $this->curationSets['test-curation-set']->retrieve();
79+
$this->assertEquals($this->curationSetData['items'][0]['id'], $returnData['items'][0]['id']);
80+
$this->assertEquals($this->curationSetData['items'][0]['rule'], $returnData['items'][0]['rule']);
81+
$this->assertEquals($this->curationSetData['items'][0]['includes'], $returnData['items'][0]['includes']);
82+
}
83+
84+
public function testCanDeleteACurationSet(): void
85+
{
86+
$returnData = $this->curationSets['test-curation-set']->delete();
87+
$this->assertEquals('test-curation-set', $returnData['name']);
88+
89+
$this->expectException(ObjectNotFound::class);
90+
$this->curationSets['test-curation-set']->retrieve();
91+
}
92+
}

0 commit comments

Comments
 (0)