1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<?php
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */
namespace Tests\Icinga\Modules\Icingadb\Model\Behavior;
use Icinga\Module\Icingadb\Common\Backend;
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_t_host_customvar
ON sub_customvar_flat_t_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_t_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_t_host_customvar
ON sub_customvar_flat_t_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_t_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_t_host_customvar
ON sub_customvar_flat_t_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_t_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_t_host_customvar
ON sub_customvar_flat_t_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_t_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_t_host_customvar
ON sub_customvar_flat_t_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_t_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();
Backend::setDb($this->connection);
$this->setUpSqlAssertions();
}
public function testSingleUnequalCondition()
{
$query = Host::on($this->connection)
->columns('host.id')
->orderBy('host.id')
->filter(Filter::unequal('host.vars.in.valid', 'foo'));
$this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['in.valid', 'foo', 1]);
}
public function testDoubleUnequalCondition()
{
$query = Host::on($this->connection)
->columns('host.id')
->orderBy('host.id')
->filter(Filter::unequal('host.vars.in.valid', 'foo'))
->filter(Filter::unequal('host.vars.missing', 'bar'));
$this->assertSql(
self::DOUBLE_UNEQUAL_RESULT,
$query->assembleSelect(),
['in.valid', 'foo', 'missing', 'bar', 1]
);
}
public function testEqualAndUnequalCondition()
{
$query = Host::on($this->connection)
->columns('host.id')
->orderBy('host.id')
->filter(Filter::unequal('host.vars.in.valid', 'bar'))
->filter(Filter::equal('host.vars.env', 'foo'));
$this->assertSql(
self::EQUAL_UNEQUAL_RESULT,
$query->assembleSelect(),
['in.valid', '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]
);
}
}
|