Skip to content

Commit

Permalink
tests: autoformat complex sql queries for easier debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 29, 2024
1 parent 7765573 commit 126b22f
Show file tree
Hide file tree
Showing 94 changed files with 2,753 additions and 165 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"phpstan/phpdoc-parser": "2.0.x-dev"
},
"require-dev": {
"doctrine/sql-formatter": "^1.5.1",
"nette/bootstrap": "~3.1",
"nette/di": "^3.1",
"nette/neon": "~3.0",
Expand Down
19 changes: 13 additions & 6 deletions tests/inc/QueryChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace NextrasTests\Orm;


use Doctrine\SqlFormatter\NullHighlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use Nette\Utils\FileSystem;
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\ILogger;
Expand All @@ -18,10 +20,13 @@ class QueryChecker implements ILogger
/** @var string */
private $sqls = '';

private SqlFormatter $formatter;


public function __construct(string $name)
{
$this->name = str_replace('\\', '/', $name);
$this->formatter = new SqlFormatter(new NullHighlighter());
}


Expand All @@ -31,12 +36,12 @@ public function assert(): void
$ci = getenv('GITHUB_ACTIONS') !== false;
if (!$ci) {
FileSystem::createDir(dirname($file));
FileSystem::write($file, $this->sqls);
FileSystem::write($file, trim($this->sqls) . "\n");
} else {
if (!file_exists($file)) {
throw new \Exception("Missing $this->name.sql file, run `composer tests` locally (with Postgres) to generate the expected SQL queries files.");
}
Assert::same(FileSystem::read($file), $this->sqls);
Assert::same(FileSystem::read($file), trim($this->sqls) . "\n");
}
}

Expand All @@ -53,14 +58,16 @@ public function onDisconnect(): void

public function onQuery(string $sqlQuery, float $timeTaken, ?Result $result): void
{
if (strpos($sqlQuery, 'pg_catalog.') !== false) return;
$this->sqls .= "$sqlQuery;\n";
if (str_contains($sqlQuery, 'pg_catalog.')) return;
$formattedSql = str_contains($sqlQuery, 'LEFT JOIN') ? $this->formatter->format($sqlQuery) . ";\n\n" : $sqlQuery . ";\n";
$this->sqls .= $formattedSql;
}


public function onQueryException(string $sqlQuery, float $timeTaken, ?DriverException $exception): void
{
if (strpos($sqlQuery, 'pg_catalog.') !== false) return;
$this->sqls .= "$sqlQuery;\n";
if (str_contains($sqlQuery, 'pg_catalog.')) return;
$formattedSql = str_contains($sqlQuery, 'LEFT JOIN') ? $this->formatter->format($sqlQuery) . ";\n\n" : $sqlQuery . ";\n";
$this->sqls .= $formattedSql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") WHERE ((("books_any"."title" = 'Book 1'))) GROUP BY "authors"."id";
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") WHERE ((("books_any"."title" = 'Book 1'))) GROUP BY "authors"."id") temp;
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") WHERE ((("books_any"."title" = 'Book 1'))) GROUP BY "authors"."id";
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") WHERE ((("books_any"."title" = 'Book 1'))) GROUP BY "authors"."id") temp;
SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
WHERE
(
(
("books_any"."title" = 'Book 1')
)
)
GROUP BY
"authors"."id";

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
WHERE
(
(
("books_any"."title" = 'Book 1')
)
)
GROUP BY
"authors"."id"
) temp;

SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
WHERE
(
(
("books_any"."title" = 'Book 1')
)
)
GROUP BY
"authors"."id";

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
WHERE
(
(
("books_any"."title" = 'Book 1')
)
)
GROUP BY
"authors"."id"
) temp;
Original file line number Diff line number Diff line change
@@ -1,4 +1,119 @@
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") LEFT JOIN "public"."authors" AS "books_translator_any" ON ("books_any"."translator_id" = "books_translator_any"."id") WHERE (("books_any"."title" = 'Book 1') AND ("books_translator_any"."id" IS NULL)) GROUP BY "authors"."id";
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_any" ON ("authors"."id" = "books_any"."author_id") LEFT JOIN "public"."authors" AS "books_translator_any" ON ("books_any"."translator_id" = "books_translator_any"."id") WHERE (("books_any"."title" = 'Book 1') AND ("books_translator_any"."id" IS NULL)) GROUP BY "authors"."id") temp;
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") OR (("authors"."id" = "books_count"."author_id") AND "books_count"."price" < 100)) LEFT JOIN "public"."authors" AS "books_translator_count" ON (("books_count"."translator_id" = "books_translator_count"."id") AND "books_translator_count"."id" IS NOT NULL) GROUP BY "authors"."id" HAVING ((COUNT("books_translator_count"."id") >= 1 AND COUNT("books_translator_count"."id") <= 1) OR (COUNT("books_count"."id") >= 1 AND COUNT("books_count"."id") <= 1));
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") OR (("authors"."id" = "books_count"."author_id") AND "books_count"."price" < 100)) LEFT JOIN "public"."authors" AS "books_translator_count" ON (("books_count"."translator_id" = "books_translator_count"."id") AND "books_translator_count"."id" IS NOT NULL) GROUP BY "authors"."id" HAVING ((COUNT("books_translator_count"."id") >= 1 AND COUNT("books_translator_count"."id") <= 1) OR (COUNT("books_count"."id") >= 1 AND COUNT("books_count"."id") <= 1))) temp;
SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
LEFT JOIN "public"."authors" AS "books_translator_any" ON (
"books_any"."translator_id" = "books_translator_any"."id"
)
WHERE
(
("books_any"."title" = 'Book 1')
AND (
"books_translator_any"."id" IS NULL
)
)
GROUP BY
"authors"."id";

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_any" ON (
"authors"."id" = "books_any"."author_id"
)
LEFT JOIN "public"."authors" AS "books_translator_any" ON (
"books_any"."translator_id" = "books_translator_any"."id"
)
WHERE
(
("books_any"."title" = 'Book 1')
AND (
"books_translator_any"."id" IS NULL
)
)
GROUP BY
"authors"."id"
) temp;

SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
OR (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" < 100
)
)
LEFT JOIN "public"."authors" AS "books_translator_count" ON (
(
"books_count"."translator_id" = "books_translator_count"."id"
)
AND "books_translator_count"."id" IS NOT NULL
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_translator_count"."id") >= 1
AND COUNT("books_translator_count"."id") <= 1
)
OR (
COUNT("books_count"."id") >= 1
AND COUNT("books_count"."id") <= 1
)
);

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
OR (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" < 100
)
)
LEFT JOIN "public"."authors" AS "books_translator_count" ON (
(
"books_count"."translator_id" = "books_translator_count"."id"
)
AND "books_translator_count"."id" IS NOT NULL
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_translator_count"."id") >= 1
AND COUNT("books_translator_count"."id") <= 1
)
OR (
COUNT("books_count"."id") >= 1
AND COUNT("books_count"."id") <= 1
)
)
) temp;
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") AND "books_count"."price" >= 50) GROUP BY "authors"."id" HAVING ((COUNT("books_count"."id") >= 2));
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") AND "books_count"."price" >= 50) GROUP BY "authors"."id" HAVING ((COUNT("books_count"."id") >= 2))) temp;
SELECT "authors".* FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") AND "books_count"."price" >= 51) GROUP BY "authors"."id" HAVING ((COUNT("books_count"."id") <= 1));
SELECT COUNT(*) AS count FROM (SELECT "authors"."id" FROM "public"."authors" AS "authors" LEFT JOIN "books" AS "books_count" ON (("authors"."id" = "books_count"."author_id") AND "books_count"."price" >= 51) GROUP BY "authors"."id" HAVING ((COUNT("books_count"."id") <= 1))) temp;
SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" >= 50
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_count"."id") >= 2
)
);

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" >= 50
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_count"."id") >= 2
)
)
) temp;

SELECT
"authors".*
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" >= 51
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_count"."id") <= 1
)
);

SELECT
COUNT(*) AS count
FROM
(
SELECT
"authors"."id"
FROM
"public"."authors" AS "authors"
LEFT JOIN "books" AS "books_count" ON (
(
"authors"."id" = "books_count"."author_id"
)
AND "books_count"."price" >= 51
)
GROUP BY
"authors"."id"
HAVING
(
(
COUNT("books_count"."id") <= 1
)
)
) temp;
Loading

0 comments on commit 126b22f

Please sign in to comment.