-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform custom variable filters as late as possible
fixes #865
- Loading branch information
Showing
6 changed files
with
214 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Module: icingadb | ||
Version: 1.1.1 | ||
Requires: | ||
Libraries: icinga-php-library (>=0.13.0), icinga-php-thirdparty (>=0.12.0) | ||
Libraries: icinga-php-library (>=0.13.2), icinga-php-thirdparty (>=0.12.0) | ||
Description: Icinga DB Web | ||
UI for Icinga DB – Provides a graphical interface to your Icinga monitoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
test/php/library/Icingadb/Model/Behavior/FlattenedObjectVarsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Tests\Icinga\Modules\Icingadb\Model\Behavior; | ||
|
||
use Icinga\Module\Icingadb\Model\Host; | ||
use ipl\Sql\Connection; | ||
use ipl\Sql\Test\SqlAssertions; | ||
use ipl\Sql\Test\TestConnection; | ||
use ipl\Stdlib\Filter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FlattenedObjectVarsTest extends TestCase | ||
{ | ||
use SqlAssertions; | ||
|
||
private const SINGLE_UNEQUAL_RESULT = <<<'SQL' | ||
SELECT host.id | ||
FROM host | ||
WHERE (host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id | ||
FROM customvar_flat sub_customvar_flat | ||
INNER JOIN host_customvar sub_customvar_flat_host_customvar | ||
ON sub_customvar_flat_host_customvar.customvar_id = | ||
sub_customvar_flat.customvar_id | ||
INNER JOIN host sub_customvar_flat_host | ||
ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id | ||
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) | ||
AND (sub_customvar_flat_host.id IS NOT NULL) | ||
GROUP BY sub_customvar_flat_host.id | ||
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL) | ||
ORDER BY host.id | ||
SQL; | ||
|
||
private const DOUBLE_UNEQUAL_RESULT = <<<'SQL' | ||
SELECT host.id | ||
FROM host | ||
WHERE (host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id | ||
FROM customvar_flat sub_customvar_flat | ||
INNER JOIN host_customvar sub_customvar_flat_host_customvar | ||
ON sub_customvar_flat_host_customvar.customvar_id = | ||
sub_customvar_flat.customvar_id | ||
INNER JOIN host sub_customvar_flat_host | ||
ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id | ||
WHERE (((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) OR | ||
((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))) | ||
AND (sub_customvar_flat_host.id IS NOT NULL) | ||
GROUP BY sub_customvar_flat_host.id | ||
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL) | ||
ORDER BY host.id | ||
SQL; | ||
|
||
private const EQUAL_UNEQUAL_RESULT = <<<'SQL' | ||
SELECT host.id | ||
FROM host | ||
WHERE ((host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id | ||
FROM customvar_flat sub_customvar_flat | ||
INNER JOIN host_customvar sub_customvar_flat_host_customvar | ||
ON sub_customvar_flat_host_customvar.customvar_id = | ||
sub_customvar_flat.customvar_id | ||
INNER JOIN host sub_customvar_flat_host | ||
ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id | ||
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) | ||
AND (sub_customvar_flat_host.id IS NOT NULL) | ||
GROUP BY sub_customvar_flat_host.id | ||
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL)) | ||
AND (host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id | ||
FROM customvar_flat sub_customvar_flat | ||
INNER JOIN host_customvar sub_customvar_flat_host_customvar | ||
ON sub_customvar_flat_host_customvar.customvar_id = | ||
sub_customvar_flat.customvar_id | ||
INNER JOIN host sub_customvar_flat_host | ||
ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id | ||
WHERE (sub_customvar_flat.flatname = ?) | ||
AND (sub_customvar_flat.flatvalue = ?) | ||
GROUP BY sub_customvar_flat_host.id | ||
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?))) | ||
ORDER BY host.id | ||
SQL; | ||
|
||
private const DOUBLE_EQUAL_RESULT = <<<'SQL' | ||
SELECT host.id | ||
FROM host | ||
WHERE host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id | ||
FROM customvar_flat sub_customvar_flat | ||
INNER JOIN host_customvar sub_customvar_flat_host_customvar | ||
ON sub_customvar_flat_host_customvar.customvar_id = | ||
sub_customvar_flat.customvar_id | ||
INNER JOIN host sub_customvar_flat_host | ||
ON sub_customvar_flat_host.id = sub_customvar_flat_host_customvar.host_id | ||
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) | ||
OR ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) | ||
GROUP BY sub_customvar_flat_host.id | ||
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) | ||
ORDER BY host.id | ||
SQL; | ||
|
||
/** @var Connection */ | ||
private $connection; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->connection = new TestConnection(); | ||
$this->setUpSqlAssertions(); | ||
} | ||
|
||
public function testSingleUnequalCondition() | ||
{ | ||
$query = Host::on($this->connection) | ||
->columns('host.id') | ||
->orderBy('host.id') | ||
->filter(Filter::unequal('host.vars.invalid', 'foo')); | ||
|
||
$this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['invalid', 'foo', 1]); | ||
} | ||
|
||
public function testDoubleUnequalCondition() | ||
{ | ||
$query = Host::on($this->connection) | ||
->columns('host.id') | ||
->orderBy('host.id') | ||
->filter(Filter::unequal('host.vars.invalid', 'foo')) | ||
->filter(Filter::unequal('host.vars.missing', 'bar')); | ||
|
||
$this->assertSql( | ||
self::DOUBLE_UNEQUAL_RESULT, | ||
$query->assembleSelect(), | ||
['invalid', 'foo', 'missing', 'bar', 1] | ||
); | ||
} | ||
|
||
public function testEqualAndUnequalCondition() | ||
{ | ||
$query = Host::on($this->connection) | ||
->columns('host.id') | ||
->orderBy('host.id') | ||
->filter(Filter::unequal('host.vars.invalid', 'bar')) | ||
->filter(Filter::equal('host.vars.env', 'foo')); | ||
|
||
$this->assertSql( | ||
self::EQUAL_UNEQUAL_RESULT, | ||
$query->assembleSelect(), | ||
['invalid', 'bar', 1, 'env', 'foo', 1] | ||
); | ||
} | ||
|
||
public function testDoubleEqualCondition() | ||
{ | ||
$query = Host::on($this->connection) | ||
->columns('host.id') | ||
->orderBy('host.id') | ||
->filter(Filter::equal('host.vars.env', 'foo')) | ||
->filter(Filter::equal('host.vars.os', 'bar')); | ||
|
||
$this->assertSql( | ||
self::DOUBLE_EQUAL_RESULT, | ||
$query->assembleSelect(), | ||
['env', 'foo', 'os', 'bar', 2] | ||
); | ||
} | ||
} |