Skip to content

Fix tests, add whereIn, whereNotIn and queryCallback support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.phpunit.result.cache
composer.lock
.DS_Store
.idea
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
],
"require": {
"php": "^8.1|^8.2|^8.3|^8.4",
"illuminate/contracts": "^10|^11",
"illuminate/database": "^10|^11",
"illuminate/support": "^10|^11",
"laravel/scout": "^10|^11"
"illuminate/contracts": "^10|^11|^12",
"illuminate/database": "^10|^11|^12",
"illuminate/support": "^10|^11|^12",
"laravel/scout": "^10|^11|^12"
},
"require-dev": {
"laravel/pint": "^1.18",
Expand Down
29 changes: 23 additions & 6 deletions src/PostgresEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,34 @@ protected function performSearch(Builder $builder, ?int $perPage = 0, int $page
->selectRaw('COUNT(*) OVER () AS total_count')
->whereRaw("{$indexColumn} @@ \"tsquery\"");

// Apply query callback if set
if ($builder->queryCallback) {
call_user_func($builder->queryCallback, $builder);
}

// Apply where clauses that were set on the builder instance if any
foreach ($builder->wheres as $key => $value) {
if ($key == '__soft_deleted') {
if ($this->usesSoftDeletes($builder->model)) {
if ($value == 1) {
$query->whereNotNull($builder->model->getDeletedAtColumn());
} else {
$query->whereNull($builder->model->getDeletedAtColumn());
}
}
continue;
}
$query->where($key, $value);
}

// If parsed documents are being stored in the model's table
if (! $this->isExternalIndex($builder->model)) {
// and the model uses soft deletes we need to exclude trashed rows
if ($this->usesSoftDeletes($builder->model)) {
$query->whereNull($builder->model->getDeletedAtColumn());
}
// Apply whereIn clauses that were set on the builder instance if any
foreach ($builder->whereIns as $key => $value) {
$query->whereIn($key, $value);
}

// Apply whereNoIn clauses that were set on the builder instance if any
foreach ($builder->whereNotIns as $key => $value) {
$query->whereNotIn($key, $value);
}

// Apply order by clauses that were set on the builder instance if any
Expand Down
112 changes: 98 additions & 14 deletions tests/PostgresEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ public function update_adds_object_to_index()
[$engine, $db] = $this->getEngine();

$db->shouldReceive('query')
->andReturn($query = Mockery::mock('stdClass'));
->andReturn($query = Mockery::mock('stdClass'))->once();
$query->shouldReceive('selectRaw')
->with(
'to_tsvector(COALESCE(?, get_current_ts_config()), ?) || setweight(to_tsvector(COALESCE(?, get_current_ts_config()), ?), ?) AS tsvector',
[null, 'Foo', null, '', 'B']
)
->andReturnSelf();
->andReturnSelf()->once();
$query->shouldReceive('value')
->with('tsvector')
->andReturn('foo');
->andReturn('foo')->once();

$db->shouldReceive('table')
->andReturn($table = Mockery::mock('stdClass'));
$table->shouldReceive('where')
->with('id', '=', 1)
->andReturnSelf();
->andReturnSelf()->once();

$table->shouldReceive('update')
->with(['searchable' => 'foo']);
->with(['searchable' => 'foo'])->once();

$engine->update(Collection::make([new TestModel]));
}
Expand All @@ -62,7 +62,7 @@ public function update_do_nothing_if_index_maintenance_turned_off_globally()
{
[$engine] = $this->getEngine(['maintain_index' => false]);

$engine->update(Collection::make([new TestModel]));
$this->assertNull($engine->update(Collection::make([new TestModel])));
}

/**
Expand All @@ -73,12 +73,15 @@ public function delete_removes_object_from_index()
[$engine, $db] = $this->getEngine();

$db->shouldReceive('table')
->andReturn($table = Mockery::mock('stdClass'));
->andReturn($table = Mockery::mock('stdClass'))
->once();
$table->shouldReceive('whereIn')
->with('id', [1])
->andReturnSelf();
->andReturnSelf()
->once();
$table->shouldReceive('update')
->with(['searchable' => null]);
->with(['searchable' => null])
->once();

$engine->delete(Collection::make([new TestModel]));
}
Expand Down Expand Up @@ -142,7 +145,8 @@ public function search()
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);

$db->shouldReceive('select')
->with(null, $table->getBindings());
->with(null, $table->getBindings())
->once();

$builder = new Builder(new TestModel, 'foo');
$builder->where('bar', 1)
Expand All @@ -166,7 +170,8 @@ public function search_with_order_by()
->shouldReceive('getBindings')->andReturn([null, 'foo']);

$db->shouldReceive('select')
->with(null, $table->getBindings());
->with(null, $table->getBindings())
->once();

$builder = new Builder(new TestModel, 'foo');
$builder->orderBy('bar', 'desc')
Expand All @@ -175,6 +180,85 @@ public function search_with_order_by()
$engine->search($builder);
}

/**
* @test
*/
public function search_with_queryCallback()
{
[$engine, $db] = $this->getEngine();

$skip = 0;
$limit = 5;
$table = $this->setDbExpectations($db);

$table->shouldReceive('skip')->with($skip)->andReturnSelf()
->shouldReceive('limit')->with($limit)->andReturnSelf()
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
->shouldReceive('where')->with('baz', 'qux')
->shouldReceive('getBindings')->andReturn([null, 'foo', 1, 'qux']);

$db->shouldReceive('select')
->with(null, $table->getBindings())
->once();

$builder = new Builder(new TestModel, 'foo');
$builder->query(function ($q) {
$q->where('bar', 1)
->where('baz', 'qux')
->take(5);
});

$engine->search($builder);
}

/**
* @test
*/
public function search_with_whereIn()
{
[$engine, $db] = $this->getEngine();

$skip = 0;
$limit = 5;
$table = $this->setDbExpectations($db);

$table->shouldReceive('whereIn')->with('bar', [1])->andReturnSelf()
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);

$db->shouldReceive('select')
->with(null, $table->getBindings())
->once();

$builder = new Builder(new TestModel, 'foo');
$builder->whereIn('bar', [1]);

$engine->search($builder);
}

/**
* @test
*/
public function search_with_whereNotIn()
{
[$engine, $db] = $this->getEngine();

$skip = 0;
$limit = 5;
$table = $this->setDbExpectations($db);

$table->shouldReceive('whereNotIn')->with('bar', [1])->andReturnSelf()
->shouldReceive('getBindings')->andReturn([null, 'foo', [1]]);

$db->shouldReceive('select')
->with(null, $table->getBindings())
->once();

$builder = new Builder(new TestModel, 'foo');
$builder->whereNotIn('bar', [1]);

$engine->search($builder);
}

/**
* @test
*/
Expand All @@ -191,7 +275,7 @@ public function search_with_global_config()
->shouldReceive('where')->with('bar', 1)
->shouldReceive('getBindings')->andReturn(['simple', 'foo', 1]);

$db->shouldReceive('select')->with(null, $table->getBindings());
$db->shouldReceive('select')->with(null, $table->getBindings())->once();

$builder = new Builder(new TestModel, 'foo');
$builder->where('bar', 1)->take(5);
Expand All @@ -215,7 +299,7 @@ public function search_with_model_config()
->shouldReceive('where')->with('bar', 1)
->shouldReceive('getBindings')->andReturn(['english', 'foo', 1]);

$db->shouldReceive('select')->with(null, $table->getBindings());
$db->shouldReceive('select')->with(null, $table->getBindings())->once();

$model = new TestModel;
$model->searchableOptions['config'] = 'english';
Expand All @@ -241,7 +325,7 @@ public function search_with_soft_deletes()
->shouldReceive('whereNull')->with('deleted_at')
->shouldReceive('getBindings')->andReturn([null, 'foo', 1]);

$db->shouldReceive('select')->with(null, $table->getBindings());
$db->shouldReceive('select')->with(null, $table->getBindings())->once();

$builder = new Builder(new SoftDeletableTestModel, 'foo');
$builder->where('bar', 1)->take(5);
Expand Down