Skip to content

Commit

Permalink
tests: added test for getInsertId()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 18, 2016
1 parent 961ef10 commit 40cb8f9
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tests/Database/Connection.getInsertId().mysql.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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 (3)');
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());

$res = $connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
Assert::equal('10', $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());

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

$res = $connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, 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'));

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

$res = $connection->query('INSERT INTO autoprimarykey (prim, col) VALUES (10, NULL)');
Assert::equal('10', $connection->getInsertId());
75 changes: 75 additions & 0 deletions tests/Database/Connection.getInsertId().sqlsrv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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("IF OBJECT_ID('noprimarykey', 'U') IS NOT NULL DROP TABLE noprimarykey");
$connection->query('
CREATE 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("IF OBJECT_ID('primarykey', 'U') IS NOT NULL DROP TABLE primarykey");
$connection->query('
CREATE 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("IF OBJECT_ID('autoprimarykey', 'U') IS NOT NULL DROP TABLE autoprimarykey");
$connection->query('
CREATE 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());

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


$connection->query("IF OBJECT_ID('multiautoprimarykey', 'U') IS NOT NULL DROP TABLE multiautoprimarykey");
$connection->query('
CREATE 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 40cb8f9

Please sign in to comment.