Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
* improve diagnostics
* update feature detection
* ensure that index for beer-sample is loaded before the test run
  • Loading branch information
avsej committed Mar 17, 2024
1 parent 670a310 commit 71b73e8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 145 deletions.
6 changes: 3 additions & 3 deletions Couchbase/Management/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ public function setPlanParams(string|array|stdClass $planParams): SearchIndex
}

/**
* @internal
* @return array
*
* @internal
* @since 4.1.5
*/
public function jsonSerialize(): array
Expand Down Expand Up @@ -350,10 +350,10 @@ public function jsonSerialize(): array
}

/**
* @internal
* @param array|stdClass $searchIndex
* @return SearchIndex
*
* @internal
* @since 4.1.5
*/
public static function import(array|stdClass $searchIndex): SearchIndex
Expand Down Expand Up @@ -386,10 +386,10 @@ public static function import(array|stdClass $searchIndex): SearchIndex
}

/**
* @internal
* @param SearchIndex $index
* @return array
*
* @internal
* @since 4.1.5
*/
public static function export(SearchIndex $index): array
Expand Down
2 changes: 2 additions & 0 deletions Couchbase/Management/SearchIndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct($core)
* @param GetSearchIndexOptions|null $options
* @return SearchIndex
*
* @throws Couchbase\Exception\IndexNotFoundException
*
* @since 4.1.5
*/
public function getIndex(string $indexName, GetSearchIndexOptions $options = null): SearchIndex
Expand Down
2 changes: 2 additions & 0 deletions tests/BucketManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public function testCreateBucketNumReplicas()

public function testCreateBucketReplicaIndexes()
{
$this->skipIfReplicasAreNotConfigured();

$settings = new BucketSettings($this->bucketName);
$settings->setBucketType(BucketType::COUCHBASE)->enableReplicaIndexes(true);
$this->manager->createBucket($settings);
Expand Down
5 changes: 5 additions & 0 deletions tests/Helpers/ServerVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public function supportsScopeSearchIndexes(): bool
return ($this->major == 7 && $this->minor >= 6) || $this->major > 7;
}

public function supportsVectorSearch(): bool
{
return ($this->major == 7 && $this->minor >= 6) || $this->major > 7;
}

/**
* @return int
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryIndexManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function testQueryIndexesCrud()
$manager->createPrimaryIndex($bucketName, CreateQueryPrimaryIndexOptions::build()->ignoreIfExists(true));
break;
} catch (CouchbaseException $ex) {
printf("Error during primary index creation: %s, %s", $ex->getMessage(), var_export($ex->getContext(), true));
printf("Error during primary index creation for '%s': %s, %s", $bucketName, $ex->getMessage(), var_export($ex->getContext(), true));
if (time() > $deadline) {
$this->assertFalse("timed out waiting for create index to succeed");
$this->fail("timed out waiting for create index to succeed");
}
sleep(1);
}
Expand Down
167 changes: 27 additions & 140 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Couchbase\Exception\IndexNotFoundException;
use Couchbase\GeoBoundingBoxSearchQuery;
use Couchbase\GeoDistanceSearchQuery;
use Couchbase\Management\SearchIndex;
use Couchbase\Management\SearchIndexManager;
use Couchbase\MatchAllSearchQuery;
use Couchbase\MatchNoneSearchQuery;
use Couchbase\MatchPhraseSearchQuery;
Expand Down Expand Up @@ -61,12 +63,36 @@
class SearchTest extends Helpers\CouchbaseTestCase
{
private ClusterInterface $cluster;
private SearchIndexManager $indexManager;

public function setUp(): void
{
parent::setUp();

$this->cluster = $this->connectCluster();

if (self::env()->useCouchbase()) {
$this->indexManager = $this->cluster->searchIndexes();
try {
$this->indexManager->getIndex("beer-search");
} catch (IndexNotFoundException $ex) {
$indexDump = json_decode(file_get_contents(__DIR__ . "/beer-search.json"), true);
$index = SearchIndex::build("beer-search", "beer-sample");
$index->setParams($indexDump["params"]);
$this->indexManager->upsertIndex($index);
}
while (true) {
try {
$indexedDocuments = $this->indexManager->getIndexedDocumentsCount("beer-search");
fprintf(STDERR, "Indexing 'beer-search': %d docs\n", $indexedDocuments);
if ($indexedDocuments > 7000) {
break;
}
sleep(3);
} catch (\Couchbase\Exception\IndexNotReadyException $ex) {
}
}
}
}

public function tearDown(): void
Expand Down Expand Up @@ -495,6 +521,7 @@ public function testBooleanSearchQuery()
public function testVectorSearchThrowsIndexNotFound()
{
$this->skipIfCaves();
$this->skipIfUnsupported($this->version()->supportsVectorSearch());

$vectorQueryOne = VectorQuery::build("foo", [0.32, -0.536, 0.842])->boost(0.5)->numCandidates(4);
$vectorQueryTwo = VectorQuery::build("bar", [-0.00810353, 0.6433, 0.52364]);
Expand Down Expand Up @@ -546,143 +573,3 @@ public function testScopeSearchThrowsFeatureNotAvailable()
$this->openBucket()->defaultScope()->search("unknown-index", $searchRequest);
}
}

/*
curl -XPUT -H "Content-Type: application/json" \
-u Administrator:password http://localhost:8094/api/index/beer-search -d \
'{
"type": "fulltext-index",
"name": "beer-search",
"sourceType": "couchbase",
"sourceName": "beer-sample",
"planParams": {
"maxPartitionsPerPIndex": 171,
"indexPartitions": 6
},
"params": {
"doc_config": {
"docid_prefix_delim": "",
"docid_regexp": "",
"mode": "type_field",
"type_field": "type"
},
"mapping": {
"analysis": {},
"default_analyzer": "standard",
"default_datetime_parser": "dateTimeOptional",
"default_field": "_all",
"default_mapping": {
"dynamic": true,
"enabled": true
},
"default_type": "_default",
"docvalues_dynamic": true,
"index_dynamic": true,
"store_dynamic": false,
"type_field": "_type",
"types": {
"beer": {
"dynamic": true,
"enabled": true,
"properties": {
"abv": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "abv",
"store": true,
"type": "number"
}
]
},
"category": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "category",
"store": true,
"type": "text"
}
]
},
"description": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "description",
"store": true,
"type": "text"
}
]
},
"name": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "name",
"store": true,
"type": "text"
}
]
},
"style": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "style",
"store": true,
"type": "text"
}
]
},
"updated": {
"dynamic": false,
"enabled": true,
"fields": [
{
"docvalues": true,
"include_in_all": true,
"include_term_vectors": true,
"index": true,
"name": "updated",
"store": true,
"type": "datetime"
}
]
}
}
}
}
},
"store": {
"indexType": "scorch"
}
},
"sourceParams": {}
}'
*/
Loading

0 comments on commit 71b73e8

Please sign in to comment.