Skip to content

Commit

Permalink
Query By As String
Browse files Browse the repository at this point in the history
  • Loading branch information
karakhanyans committed Nov 10, 2023
1 parent 47727a8 commit a5e702a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
4 changes: 1 addition & 3 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@
// 'default_sorting_field' => 'created_at',
// ],
// 'search-parameters' => [
// 'query_by' => [
// 'name',
// ],
// 'query_by' => 'name, email'
// ],
// ],
],
Expand Down
6 changes: 1 addition & 5 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function buildSearchParameters(Builder $builder, int $page, int|null $per
{
$params = [
'q' => $builder->query,
'query_by' => implode(',', config('scout.typesense.model-settings.'.get_class($builder->model).'.search-parameters.query_by')) ?? '',
'query_by' => config('scout.typesense.model-settings.'.get_class($builder->model).'.search-parameters.query_by') ?? '',
'filter_by' => $this->filters($builder),
'per_page' => $perPage,
'page' => $page,
Expand All @@ -248,10 +248,6 @@ public function buildSearchParameters(Builder $builder, int $page, int|null $per

if (! empty($this->searchParameters)) {
$params = array_merge($params, $this->searchParameters);

if ($this->searchParameters['query_by']) {
$params['query_by'] = implode(',', $this->searchParameters['query_by']);
}
}

if (! empty($builder->orders)) {
Expand Down
98 changes: 64 additions & 34 deletions tests/Unit/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void
->getMock();
}

public function testUpdateMethod(): void
public function test_update_method(): void
{
// Mock models and their methods
$models = [
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testUpdateMethod(): void
$this->engine->update(collect($models));
}

public function testDeleteMethod(): void
public function test_delete_method(): void
{
// Mock models and their methods
$models = [
Expand All @@ -88,7 +88,7 @@ public function testDeleteMethod(): void
$this->engine->delete(collect($models));
}

public function testSearchMethod(): void
public function test_search_method(): void
{
// Mock the Builder
$builder = $this->createMock(Builder::class);
Expand All @@ -98,27 +98,27 @@ public function testSearchMethod(): void
->method('buildSearchParameters')
->with($builder, 1)
->willReturn([
'q' => $builder->query,
'query_by' => implode(',', ['id']),
'filter_by' => '',
'per_page' => 10,
'page' => 1,
'highlight_start_tag' => '<mark>',
'highlight_end_tag' => '</mark>',
'snippet_threshold' => 30,
'exhaustive_search' => false,
'use_cache' => false,
'cache_ttl' => 60,
'prioritize_exact_match' => true,
'enable_overrides' => true,
'q' => $builder->query,
'query_by' => 'id',
'filter_by' => '',
'per_page' => 10,
'page' => 1,
'highlight_start_tag' => '<mark>',
'highlight_end_tag' => '</mark>',
'snippet_threshold' => 30,
'exhaustive_search' => false,
'use_cache' => false,
'cache_ttl' => 60,
'prioritize_exact_match' => true,
'enable_overrides' => true,
'highlight_affix_num_tokens' => 4,
]);

// Call the search method
$this->engine->search($builder);
}

public function testPaginateMethod(): void
public function test_paginate_method(): void
{
// Mock the Builder
$builder = $this->createMock(Builder::class);
Expand All @@ -128,27 +128,27 @@ public function testPaginateMethod(): void
->method('buildSearchParameters')
->with($builder, 2, 10)
->willReturn([
'q' => $builder->query,
'query_by' => implode(',', ['id']),
'filter_by' => '',
'per_page' => 10,
'page' => 2,
'highlight_start_tag' => '<mark>',
'highlight_end_tag' => '</mark>',
'snippet_threshold' => 30,
'exhaustive_search' => false,
'use_cache' => false,
'cache_ttl' => 60,
'prioritize_exact_match' => true,
'enable_overrides' => true,
'q' => $builder->query,
'query_by' => 'id',
'filter_by' => '',
'per_page' => 10,
'page' => 2,
'highlight_start_tag' => '<mark>',
'highlight_end_tag' => '</mark>',
'snippet_threshold' => 30,
'exhaustive_search' => false,
'use_cache' => false,
'cache_ttl' => 60,
'prioritize_exact_match' => true,
'enable_overrides' => true,
'highlight_affix_num_tokens' => 4,
]);

// Call the paginate method
$this->engine->paginate($builder, 10, 2);
}

public function testMapIdsMethod(): void
public function test_map_ids_method(): void
{
// Sample search results
$results = [
Expand All @@ -169,7 +169,7 @@ public function testMapIdsMethod(): void
$this->assertEquals([1, 2, 3], $mappedIds->toArray());
}

public function testGetTotalCountMethod(): void
public function test_get_total_count_method(): void
{
// Sample search results with 'found' key
$resultsWithFound = ['found' => 5];
Expand All @@ -188,7 +188,7 @@ public function testGetTotalCountMethod(): void
$this->assertEquals(0, $totalCountWithoutFound);
}

public function testFlushMethod(): void
public function test_flush_method(): void
{
// Mock a model instance
$model = $this->createMock(Model::class);
Expand All @@ -208,7 +208,7 @@ public function testFlushMethod(): void
$this->engine->flush($model);
}

public function testCreateIndexMethodThrowsException(): void
public function test_create_index_method_throws_exception(): void
{
// Define the expected exception class and message
$expectedException = \Exception::class;
Expand All @@ -221,4 +221,34 @@ public function testCreateIndexMethodThrowsException(): void
// Call the createIndex method which should throw an exception
$this->engine->createIndex('test_index');
}

public function test_set_search_params_method(): void
{
// Mock the Builder
$builder = $this->createMock(Builder::class);

// Mock the buildSearchParameters method
$this->engine->expects($this->once())
->method('buildSearchParameters')
->with($builder, 1)
->willReturn([
'q' => $builder->query,
'query_by' => 'id',
'filter_by' => '',
'per_page' => 10,
'page' => 1,
'highlight_start_tag' => '<mark>',
'highlight_end_tag' => '</mark>',
'snippet_threshold' => 30,
'exhaustive_search' => false,
'use_cache' => false,
'cache_ttl' => 60,
'prioritize_exact_match' => true,
'enable_overrides' => true,
'highlight_affix_num_tokens' => 4,
]);

// Call the search method
$this->engine->setSearchParameters(['query_by' => 'id'])->search($builder);
}
}

0 comments on commit a5e702a

Please sign in to comment.