Skip to content

Commit

Permalink
test added
Browse files Browse the repository at this point in the history
search query adjusted
  • Loading branch information
eddierusinskas committed Apr 26, 2023
1 parent 2b92415 commit 35da95f
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 13 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
}
},
"require-dev": {
"laravel/pint": "^1.7"
"laravel/pint": "^1.7",
"pestphp/pest": "^2.5"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
17 changes: 5 additions & 12 deletions src/Services/OpenSearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function deleteIndex(string $index): void
public function bulkUpdate(string $index, $models): callable|array
{
$data = [];

$models->each(function ($model) use ($index, &$data) {

$data[] = [
'index' => [
'_index' => $index,
Expand Down Expand Up @@ -67,18 +67,11 @@ public function search(string $index, string $query, array $options = [])
return $this->client->search(array_merge([
'index' => $index,
'body' => [
'size' => 1000,
'size' => 1000,
'query' => [
'bool' => [
'must' => [
[
'simple_query_string' => [
'query' => "$query*",
'default_operator' => 'and',
'analyzer' => 'simple'
],
],
],
'simple_query_string' => [
'query' => "$query* | \"$query\"",
'default_operator' => 'and'
],
],
],
Expand Down
93 changes: 93 additions & 0 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use ByteXR\LaravelScoutOpenSearch\Engines\OpenSearchEngine;
use ByteXR\LaravelScoutOpenSearch\Services\OpenSearchClient;

beforeEach(function () {
$this->index = "opensearch_engine_test";

$this->openSearch = new OpenSearchClient(
(new \OpenSearch\ClientBuilder())
->setHosts(["https://127.0.0.1:9200"])
->setSSLVerification(false)
->setBasicAuthentication("admin", "admin")
->build()
);
$this->engine = new OpenSearchEngine($this->openSearch);

$data = collect(json_decode(file_get_contents("tests/data/documents.json"), true));

$this->openSearch->bulkUpdate($this->index, $data);
});

it('can search full keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "Greenwood paper Factory"));

expect($count)->toBe(1);
});

it('can search partial (prefix) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "greenwood"));

expect($count)->toBe(1);
});

it('can search partial (middle) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "paper"));

expect($count)->toBe(2);
});

it('can search email keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "[email protected]"));

expect($count)->toBe(1);
});

it('can search partial email (prefix) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "greenwood+email"));

expect($count)->toBe(1);
});

it('can search partial email (suffix) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "@example.com"));

expect($count)->toBe(2);
});

it('can search postcode keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "AX1 BT2"));

expect($count)->toBe(1);
});

it('can search partial postcode (prefix) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "AX1"));

expect($count)->toBe(1);
});

it('can search partial postcode (suffix) keyword', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "XC3"));

expect($count)->toBe(1);
});

it('can search keyword with "of" without pulling all results with "of"', function (){
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "Isle of Green"));

expect($count)->toBe(2);
});

it('can search keyword with "of" without pulling all results with "of" partial (prefix)', function (){
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "Isle of"));

expect($count)->toBe(3);
});

it('can search by house number', function () {
$count = $this->engine->getTotalCount($this->openSearch->search($this->index, "456"));

expect($count)->toBe(1);
});
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
}
}
5 changes: 5 additions & 0 deletions tests/Unit/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
});
7 changes: 7 additions & 0 deletions tests/data/documents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{ "objectID": 1, "name": "Greenwood Paper Factory", "address_line_1": "123 Shore road", "address_postcode": "AX1 BT2", "email": "[email protected]" },
{ "objectID": 2, "name": "Blackwood Paper Factory", "address_line_1": "456 London street", "address_postcode": "TY8 XC3", "email": "[email protected]" },
{ "objectID": 3, "name": "Isle of Green Company", "address_line_1": "756 Brussels avenue", "address_postcode": "HY7 TJ8", "email": "[email protected]" },
{ "objectID": 4, "name": "Isle of Purple Company", "address_line_1": "7 Purple avenue", "address_postcode": "XV1 KI9", "email": "[email protected]" },
{ "objectID": 5, "name": "True storage solutions", "address_line_1": "Isle of green", "address_postcode": "UI8 GH5", "email": "[email protected]" }
]

0 comments on commit 35da95f

Please sign in to comment.