From 40cb8f905aa1ceb182eafb496170b7e96cbed021 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 18 Oct 2016 13:19:15 +0200 Subject: [PATCH] tests: added test for getInsertId() --- .../Connection.getInsertId().mysql.phpt | 77 +++++++++++++++++++ .../Connection.getInsertId().postgre.phpt | 61 +++++++++++++++ .../Connection.getInsertId().sqlite.phpt | 54 +++++++++++++ .../Connection.getInsertId().sqlsrv.phpt | 75 ++++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100644 tests/Database/Connection.getInsertId().mysql.phpt create mode 100644 tests/Database/Connection.getInsertId().postgre.phpt create mode 100644 tests/Database/Connection.getInsertId().sqlite.phpt create mode 100644 tests/Database/Connection.getInsertId().sqlsrv.phpt diff --git a/tests/Database/Connection.getInsertId().mysql.phpt b/tests/Database/Connection.getInsertId().mysql.phpt new file mode 100644 index 000000000..04a43d54d --- /dev/null +++ b/tests/Database/Connection.getInsertId().mysql.phpt @@ -0,0 +1,77 @@ +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()); diff --git a/tests/Database/Connection.getInsertId().postgre.phpt b/tests/Database/Connection.getInsertId().postgre.phpt new file mode 100644 index 000000000..c4c98816e --- /dev/null +++ b/tests/Database/Connection.getInsertId().postgre.phpt @@ -0,0 +1,61 @@ +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')); diff --git a/tests/Database/Connection.getInsertId().sqlite.phpt b/tests/Database/Connection.getInsertId().sqlite.phpt new file mode 100644 index 000000000..abcf92df5 --- /dev/null +++ b/tests/Database/Connection.getInsertId().sqlite.phpt @@ -0,0 +1,54 @@ +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()); diff --git a/tests/Database/Connection.getInsertId().sqlsrv.phpt b/tests/Database/Connection.getInsertId().sqlsrv.phpt new file mode 100644 index 000000000..4df3b4347 --- /dev/null +++ b/tests/Database/Connection.getInsertId().sqlsrv.phpt @@ -0,0 +1,75 @@ +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());