Skip to content

Commit 5b7bffa

Browse files
committed
Tests for field level boosting.
1 parent 334e6c7 commit 5b7bffa

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

src/Nqxcode/LuceneSearch/Model/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function fields(Model $model)
152152
$field = $value;
153153

154154
if (is_array($value)) {
155-
$boost = array_get($value, "boost");
155+
$boost = array_get($value, 'boost', 1);
156156
$field = $key;
157157
}
158158

@@ -199,8 +199,8 @@ function ($i) use ($field) {
199199
$boost = 1;
200200

201201
if (is_array($value)) {
202-
$boost = array_get($value, "boost");
203-
$value = array_get($value, "value");
202+
$boost = array_get($value, 'boost', 1);
203+
$value = array_get($value, 'value');
204204
}
205205

206206
return ['boost' => $boost, 'value' => $value];

tests/functional/BaseTestCase.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public function setUp()
1414
{
1515
parent::setUp();
1616
$this->configure();
17+
18+
$artisan = $this->app->make('artisan');
19+
20+
// Call migrations specific to our tests, e.g. to seed the db.
21+
$artisan->call('migrate', ['--database' => 'testbench', '--path' => '../tests/migrations']);
22+
23+
// Call rebuild search index.
24+
$artisan->call('search:rebuild');
1725
}
1826

1927
protected function configure()
@@ -24,20 +32,12 @@ protected function configure()
2432
[
2533
'tests\models\Product' => [
2634
'fields' => [
27-
'name' => ['boost' => 1],
28-
'description' => ['boost' => 0.2],
35+
'name',
36+
'description',
2937
],
3038
'optional_attributes' => true
3139
]
3240
]
3341
);
34-
35-
$artisan = $this->app->make('artisan');
36-
37-
// Call migrations specific to our tests, e.g. to seed the db.
38-
$artisan->call('migrate', ['--database' => 'testbench', '--path' => '../tests/migrations']);
39-
40-
// Call rebuild search index.
41-
$artisan->call('search:rebuild');
4242
}
4343
}

tests/functional/SearchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public function testSearchRawQuery()
7979

8080
public function testSearchByOptionalAttributes()
8181
{
82-
$query = Search::query('optional_value1', ['optional_attribute1']);
83-
$this->assertEquals(6, $query->count());
82+
$query = Search::query('some custom text', ['custom_text']);
83+
$this->assertEquals(9, $query->count());
8484
}
8585

8686
public function testSearchHighlightResults()

tests/migrations/2014_07_13_200151_create_products_table.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,29 @@ private function seed()
9292
'updated_at' => $now,
9393
'publish' => 1,
9494
));
95+
96+
DB::table('products')->insert(array(
97+
'name' => 'noname pointer',
98+
'description' => 'laser pointer',
99+
'created_at' => $now,
100+
'updated_at' => $now,
101+
'publish' => 1,
102+
));
103+
104+
DB::table('products')->insert(array(
105+
'name' => 'broken pointer',
106+
'description' => 'high power laser pointer',
107+
'created_at' => $now,
108+
'updated_at' => $now,
109+
'publish' => 1,
110+
));
111+
112+
DB::table('products')->insert(array(
113+
'name' => 'laser pointer',
114+
'description' => 'high power led pointer',
115+
'created_at' => $now,
116+
'updated_at' => $now,
117+
'publish' => 1,
118+
));
95119
}
96120
}

tests/models/Product.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function isSearchable()
2727

2828
public function getOptionalAttributesAttribute()
2929
{
30-
return ['optional_attribute1' => "optional_value1"];
30+
return [
31+
'custom_text' => 'some custom text',
32+
'boosted_name' => ['boost' => 0.9, 'value' => $this->name],
33+
];
3134
}
3235
}

0 commit comments

Comments
 (0)