Skip to content

Commit

Permalink
tests: added/improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 28, 2024
1 parent 451b33e commit 8e9a427
Show file tree
Hide file tree
Showing 34 changed files with 531 additions and 365 deletions.
62 changes: 62 additions & 0 deletions tests/Database.Tracy/ConnectionPanel.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Test: ConnectionPanel
*/

declare(strict_types=1);

use Nette\Bridges\DatabaseTracy\ConnectionPanel;
use Nette\Database\Connection;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('Tracy Bar', function () {
$connection = new Connection('sqlite::memory:');
$panel = ConnectionPanel::initialize($connection, addBarPanel: true, name: 'foo');

$connection->beginTransaction();
$connection->query('SELECT 1');
$connection->commit();
try {
$connection->query('SELECT');
} catch (Throwable) {
}

Assert::matchFile(__DIR__ . '/tab.html', $panel->getTab());
Assert::matchFile(__DIR__ . '/panel.html', $panel->getPanel());
});

test('Bluescreen Panel', function () {
$connection = new Connection('sqlite::memory:');
try {
$connection->query('SELECT');
} catch (Throwable $e) {
}

Assert::same(
[
'tab' => 'SQL',
'panel' => "<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong></pre>\n",
],
ConnectionPanel::renderException($e),
);
});

test('deprecated initialization', function () {
$connection = new Connection('sqlite::memory:');
$panel = Nette\Database\Helpers::initializeTracy($connection, addBarPanel: true, name: 'foo');

$connection->beginTransaction();
$connection->query('SELECT 1');
$connection->commit();
try {
$connection->query('SELECT');
} catch (Throwable) {
}

Assert::matchFile(__DIR__ . '/tab.html', $panel->getTab());
Assert::matchFile(__DIR__ . '/panel.html', $panel->getPanel());
});
36 changes: 36 additions & 0 deletions tests/Database.Tracy/panel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%A%
<h1 title="sqlite::memory:">Queries: 4, time: %a% ms, foo</h1>

<div class="tracy-inner">
<div class="tracy-inner-container">
<table class="tracy-sortable">
%A%
<td %a%>%A%</td>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump">::beginTransaction</pre>
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
<td></td>
</tr>
<tr>
<td %a%>%A%</td>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-select"><pre class="dump"><strong style="color:blue">SELECT</strong> 1</pre>
<table class="tracy-collapsed nette-DbConnectionPanel-explain">%A%</table>
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
<td>0</td>
</tr>
<tr>
<td %a%>%A%</td>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump">::commit</pre>
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%Connection.php:%d%" class="tracy-editor">%a%<b>Connection.php</b>:%d%</a> </td>
<td></td>
</tr>
<tr>
<td %a%>
<span title="SQLSTATE[HY000]: %a%">ERROR</span>
</td>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-"><pre class="dump"><strong style="color:blue">SELECT</strong></pre>
<a class="nette-DbConnectionPanel-source" href="%a%" title="%a%SqliteDriver.php:%d%" class="tracy-editor">%a%<b>SqliteDriver.php</b>:%d%</a> </td>
<td></td>
</tr>
</table>
</div>
</div>
3 changes: 3 additions & 0 deletions tests/Database.Tracy/tab.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span title="Nette\Database foo">
<svg %A%</svg><span class="tracy-label">%a% ms / 4</span>
</span>
88 changes: 72 additions & 16 deletions tests/Database/Connection.exceptions.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,80 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

$connection = connectToDB()->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");

$options = Tester\Environment::loadData();
$e = Assert::exception(
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
Nette\Database\ConnectionException::class,
'%a% Access denied for user %a%',
);
test('Exception thrown for invalid database credentials', function () {
$options = Tester\Environment::loadData();
$e = Assert::exception(
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
Nette\Database\ConnectionException::class,
'%a% Access denied for user %a%',
);

Assert::same(1045, $e->getDriverCode());
Assert::contains($e->getSqlState(), ['HY000', '28000']);
Assert::same($e->getCode(), $e->getSqlState());
Assert::same(1045, $e->getDriverCode());
Assert::contains($e->getSqlState(), ['HY000', '28000']);
Assert::same($e->getCode(), $e->getSqlState());
});


$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);
Assert::same(null, $e->getDriverCode());
});

Assert::same(null, $e->getDriverCode());

test('Exception thrown for syntax error in SQL query', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('SELECT'),
Nette\Database\DriverException::class,
'%a% Syntax error %a%',
'42000',
);

Assert::same(1064, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for unique constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
Nette\Database\UniqueConstraintViolationException::class,
'%a% Integrity constraint violation: %a%',
'23000',
);

Assert::same(1062, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for not null constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
Nette\Database\NotNullConstraintViolationException::class,
'%a% Integrity constraint violation: %a%',
'23000',
);

Assert::same(1048, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for foreign key constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")'),
Nette\Database\ForeignKeyConstraintViolationException::class,
'%a% a foreign key constraint fails %a%',
'23000',
);

Assert::same(1452, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});
90 changes: 74 additions & 16 deletions tests/Database/Connection.exceptions.postgre.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,82 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

$connection = connectToDB()->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");

$options = Tester\Environment::loadData();
$e = Assert::exception(
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
Nette\Database\ConnectionException::class,
null,
'08006',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
test('Exception thrown for invalid database credentials', function () {
$options = Tester\Environment::loadData();
$e = Assert::exception(
fn() => new Nette\Database\Connection($options['dsn'], 'unknown', 'unknown'),
Nette\Database\ConnectionException::class,
null,
'08006',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});

$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);

Assert::same(null, $e->getDriverCode());
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);

Assert::same(null, $e->getDriverCode());
});


test('Exception thrown for syntax error in SQL query', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('SELECT INTO'),
Nette\Database\DriverException::class,
'%a% syntax error %A%',
'42601',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for unique constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query("INSERT INTO author (id, name, web, born) VALUES (11, '', '', NULL)"),
Nette\Database\UniqueConstraintViolationException::class,
'%a% Unique violation: %A%',
'23505',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for not null constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query("INSERT INTO author (name, web, born) VALUES (NULL, '', NULL)"),
Nette\Database\NotNullConstraintViolationException::class,
'%a% Not null violation: %A%',
'23502',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for foreign key constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query("INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, '')"),
Nette\Database\ForeignKeyConstraintViolationException::class,
'%a% Foreign key violation: %A%',
'23503',
);

Assert::same(7, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});
86 changes: 71 additions & 15 deletions tests/Database/Connection.exceptions.sqlite.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,79 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';

$connection = connectToDB()->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName}-nette_test1.sql");

$e = Assert::exception(
fn() => new Nette\Database\Connection('sqlite:.'),
Nette\Database\ConnectionException::class,
'SQLSTATE[HY000] [14] unable to open database file',
'HY000',
);

Assert::same(14, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
test('Exception thrown for unable to open database file', function () {
$e = Assert::exception(
fn() => new Nette\Database\Connection('sqlite:.'),
Nette\Database\ConnectionException::class,
'SQLSTATE[HY000] [14] unable to open database file',
'HY000',
);

Assert::same(14, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});

$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);

Assert::same(null, $e->getDriverCode());
test('Exception thrown when calling rollback with no active transaction', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->rollback(),
Nette\Database\DriverException::class,
'There is no active transaction',
0,
);

Assert::same(null, $e->getDriverCode());
});


test('Exception thrown for error in SQL query', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('SELECT'),
Nette\Database\DriverException::class,
'%a% error%a%',
'HY000',
);

Assert::same(1, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for unique constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('INSERT INTO author (id, name, web, born) VALUES (11, "", "", NULL)'),
Nette\Database\UniqueConstraintViolationException::class,
'%a% Integrity constraint violation: %a%',
'23000',
);

Assert::same(19, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for not null constraint violation', function () use ($connection) {
$e = Assert::exception(
fn() => $connection->query('INSERT INTO author (name, web, born) VALUES (NULL, "", NULL)'),
Nette\Database\NotNullConstraintViolationException::class,
'%a% Integrity constraint violation: %a%',
'23000',
);

Assert::same(19, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});


test('Exception thrown for foreign key constraint violation', function () use ($connection) {
$e = Assert::exception(function () use ($connection) {
$connection->query('PRAGMA foreign_keys=true');
$connection->query('INSERT INTO book (author_id, translator_id, title) VALUES (999, 12, "")');
}, Nette\Database\ForeignKeyConstraintViolationException::class, '%a% Integrity constraint violation: %a%', '23000');

Assert::same(19, $e->getDriverCode());
Assert::same($e->getCode(), $e->getSqlState());
});
Loading

0 comments on commit 8e9a427

Please sign in to comment.