Skip to content

Commit

Permalink
tests: added test for getInsertId() WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 18, 2016
1 parent efe7ff2 commit fa2defa
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/Database/Connection.getInsertId().mysql.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Test: Nette\Database\Connection::getInsertId()
* @dataProvider? databases.ini mysql
*/

use Tester\Assert;
use Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection

$connection->query('CREATE DATABASE IF NOT EXISTS nette_test');
$connection->query('USE nette_test');

$connection->query('
CREATE TEMPORARY TABLE noprimarykey (
col int
) ENGINE=InnoDB
');

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
Assert::equal('0', $connection->getInsertId());

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
Assert::equal('0', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE primarykey (
prim int NOT NULL,
PRIMARY KEY(prim)
) ENGINE=InnoDB
');

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)');
Assert::equal('0', $connection->getInsertId());

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)');
Assert::equal('0', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE autoprimarykey (
prim int NOT NULL AUTO_INCREMENT,
col int,
PRIMARY KEY(prim)
) ENGINE=InnoDB
');

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('2', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE multiautoprimarykey (
prim1 int NOT NULL AUTO_INCREMENT,
prim2 int NOT NULL,
PRIMARY KEY(prim1, prim2)
) ENGINE=InnoDB
');

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('2', $connection->getInsertId());
55 changes: 55 additions & 0 deletions tests/Database/Connection.getInsertId().postgre.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Test: Nette\Database\Connection::getInsertId()
* @dataProvider? databases.ini postgresql
*/

use Tester\Assert;
use Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection


$connection->query('
CREATE TEMPORARY TABLE "primarykey" (
prim int NOT NULL,
PRIMARY KEY(prim)
)
');

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)');
Assert::equal(PHP_VERSION_ID < 70011 ? FALSE : '0', $connection->getInsertId());

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)');
Assert::equal(PHP_VERSION_ID < 70011 ? FALSE : '0', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE "autoprimarykey" (
prim serial NOT NULL,
col int,
PRIMARY KEY(prim)
)
');

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId('autoprimarykey_prim_seq'));

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('2', $connection->getInsertId('autoprimarykey_prim_seq'));


$connection->query('
CREATE TEMPORARY TABLE "multiautoprimarykey" (
prim1 serial NOT NULL,
prim2 int NOT NULL,
PRIMARY KEY(prim1, prim2)
);
');

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('1', $connection->getInsertId('multiautoprimarykey_prim1_seq'));

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('2', $connection->getInsertId('multiautoprimarykey_prim1_seq'));
51 changes: 51 additions & 0 deletions tests/Database/Connection.getInsertId().sqlite.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Test: Nette\Database\Connection::getInsertId()
* @dataProvider? databases.ini sqlite
*/

use Tester\Assert;
use Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection


$connection->query('
CREATE TABLE [noprimarykey] (
[col] INTEGER
)
');

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (3)');
Assert::equal('2', $connection->getInsertId());


$connection->query('
CREATE TABLE [primarykey] (
[prim] INTEGER PRIMARY KEY NOT NULL
)
');

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)');
Assert::equal('5', $connection->getInsertId());

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)');
Assert::equal('6', $connection->getInsertId());


$connection->query('
CREATE TABLE [autoprimarykey] (
[prim] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[col] INTEGER
)
');

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('2', $connection->getInsertId());
68 changes: 68 additions & 0 deletions tests/Database/Connection.getInsertId().sqlsrv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* Test: Nette\Database\Connection::getInsertId()
* @dataProvider? databases.ini sqlsrv
*/

use Tester\Assert;
use Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection


$connection->query('
CREATE TEMPORARY TABLE [noprimarykey] (
col int
)
');

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO noprimarykey (col) VALUES (NULL)');
Assert::equal('2', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE [primarykey] (
prim int NOT NULL,
PRIMARY KEY(prim)
)
');

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (5)');
Assert::equal('5', $connection->getInsertId());

$res = $connection->query('INSERT INTO primarykey (prim) VALUES (6)');
Assert::equal('6', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE [autoprimarykey] (
prim int NOT NULL IDENTITY(1,1),
col int,
PRIMARY KEY(prim)
)
');

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO autoprimarykey (col) VALUES (NULL)');
Assert::equal('2', $connection->getInsertId());


$connection->query('
CREATE TEMPORARY TABLE [multiautoprimarykey] (
prim1 int NOT NULL IDENTITY(1,1),
prim2 int NOT NULL,
PRIMARY KEY(prim1, prim2)
)
');

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('1', $connection->getInsertId());

$res = $connection->query('INSERT INTO multiautoprimarykey (prim2) VALUES (3)');
Assert::equal('2', $connection->getInsertId());

0 comments on commit fa2defa

Please sign in to comment.