-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert to table with multi primary or without autoincrement primary (#…
- Loading branch information
Showing
10 changed files
with
327 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/Database/Table/Selection.insert().primaryKeys.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Database\Table\Selection: Different setup for primary keys | ||
* @dataProvider? ../databases.ini | ||
*/ | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../connect.inc.php'; // create $connection | ||
|
||
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverName}-nette_test4.sql"); | ||
|
||
// Insert into table with simple primary index (autoincrement) | ||
test(function() use ($context) { | ||
$simplePkAutoincrementResult = $context->table('simple_pk_autoincrement')->insert([ | ||
'note' => 'Some note here' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $simplePkAutoincrementResult); | ||
Assert::equal(1, $simplePkAutoincrementResult->identifier1); | ||
Assert::equal('Some note here', $simplePkAutoincrementResult->note); | ||
|
||
$simplePkAutoincrementResult2 = $context->table('simple_pk_autoincrement')->insert([ | ||
'note' => 'Some note here 2' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $simplePkAutoincrementResult2); | ||
Assert::equal(2, $simplePkAutoincrementResult2->identifier1); | ||
Assert::equal('Some note here 2', $simplePkAutoincrementResult2->note); | ||
}); | ||
|
||
// Insert into table with simple primary index (no autoincrement) | ||
test(function() use ($context) { | ||
$simplePkNoAutoincrementResult = $context->table('simple_pk_no_autoincrement')->insert([ | ||
'identifier1' => 100, | ||
'note' => 'Some note here' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $simplePkNoAutoincrementResult); | ||
Assert::equal(100, $simplePkNoAutoincrementResult->identifier1); | ||
Assert::equal('Some note here', $simplePkNoAutoincrementResult->note); | ||
|
||
$simplePkNoAutoincrementResult2 = $context->table('simple_pk_no_autoincrement')->insert([ | ||
'identifier1' => 200, | ||
'note' => 'Some note here 2' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $simplePkNoAutoincrementResult2); | ||
Assert::equal(200, $simplePkNoAutoincrementResult2->identifier1); | ||
Assert::equal('Some note here 2', $simplePkNoAutoincrementResult2->note); | ||
}); | ||
|
||
// Insert into table with multi column primary index (no autoincrement) | ||
test(function() use ($context) { | ||
$multiPkNoAutoincrementResult = $context->table('multi_pk_no_autoincrement')->insert([ | ||
'identifier1' => 5, | ||
'identifier2' => 10, | ||
'note' => 'Some note here' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $multiPkNoAutoincrementResult); | ||
Assert::equal(5, $multiPkNoAutoincrementResult->identifier1); | ||
Assert::equal(10, $multiPkNoAutoincrementResult->identifier2); | ||
Assert::equal('Some note here', $multiPkNoAutoincrementResult->note); | ||
|
||
$multiPkNoAutoincrementResult2 = $context->table('multi_pk_no_autoincrement')->insert([ | ||
'identifier1' => 5, | ||
'identifier2' => 100, | ||
'note' => 'Some note here 2' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $multiPkNoAutoincrementResult2); | ||
Assert::equal(5, $multiPkNoAutoincrementResult2->identifier1); | ||
Assert::equal(100, $multiPkNoAutoincrementResult2->identifier2); | ||
Assert::equal('Some note here 2', $multiPkNoAutoincrementResult2->note); | ||
}); | ||
|
||
// Insert into table with multi column primary index (autoincrement) | ||
test(function() use ($driverName, $context) { | ||
if (in_array($driverName, ['mysql', 'pgsql'])) { | ||
$multiPkAutoincrementResult = $context->table('multi_pk_autoincrement')->insert([ | ||
'identifier2' => 999, | ||
'note' => 'Some note here' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $multiPkAutoincrementResult); | ||
Assert::equal(1, $multiPkAutoincrementResult->identifier1); | ||
Assert::equal(999, $multiPkAutoincrementResult->identifier2); | ||
Assert::equal('Some note here', $multiPkAutoincrementResult->note); | ||
|
||
$multiPkAutoincrementResult2 = $context->table('multi_pk_autoincrement')->insert([ | ||
'identifier2' => 999, | ||
'note' => 'Some note here 2' | ||
]); | ||
|
||
Assert::type(Nette\Database\Table\ActiveRow::class, $multiPkAutoincrementResult2); | ||
Assert::equal(2, $multiPkAutoincrementResult2->identifier1); | ||
Assert::equal(999, $multiPkAutoincrementResult2->identifier2); | ||
Assert::equal('Some note here 2', $multiPkAutoincrementResult2->note); | ||
} | ||
}); | ||
|
||
// Insert into table without primary key | ||
test(function() use ($context) { | ||
$noPkResult1 = $context->table('no_pk')->insert([ | ||
'note' => 'Some note here', | ||
]); | ||
Assert::equal(1, $noPkResult1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
DROP DATABASE IF EXISTS nette_test; | ||
CREATE DATABASE nette_test; | ||
USE nette_test; | ||
|
||
CREATE TABLE simple_pk_autoincrement ( | ||
identifier1 int NOT NULL AUTO_INCREMENT, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1) | ||
) ENGINE=InnoDB; | ||
|
||
CREATE TABLE simple_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1) | ||
) ENGINE=InnoDB; | ||
|
||
CREATE TABLE multi_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
identifier2 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1, identifier2) | ||
) ENGINE=InnoDB; | ||
|
||
CREATE TABLE multi_pk_autoincrement( | ||
identifier1 int NOT NULL AUTO_INCREMENT, | ||
identifier2 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1, identifier2) | ||
) ENGINE=InnoDB; | ||
|
||
CREATE TABLE no_pk ( | ||
note varchar(100) | ||
) ENGINE=InnoDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
DROP SCHEMA IF EXISTS public CASCADE; | ||
CREATE SCHEMA public; | ||
|
||
CREATE TABLE simple_pk_autoincrement ( | ||
identifier1 serial NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1) | ||
); | ||
|
||
CREATE TABLE simple_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1) | ||
); | ||
|
||
CREATE TABLE multi_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
identifier2 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1, identifier2) | ||
); | ||
|
||
CREATE TABLE multi_pk_autoincrement( | ||
identifier1 serial NOT NULL, | ||
identifier2 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1, identifier2) | ||
); | ||
|
||
CREATE TABLE no_pk ( | ||
note varchar(100) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
DROP TABLE IF EXISTS simple_pk_autoincrement; | ||
DROP TABLE IF EXISTS simple_pk_no_autoincrement; | ||
DROP TABLE IF EXISTS multi_pk_no_autoincrement; | ||
DROP TABLE IF EXISTS multi_pk_autoincrement; | ||
DROP TABLE IF EXISTS no_pk; | ||
|
||
CREATE TABLE simple_pk_autoincrement ( | ||
identifier1 integer PRIMARY KEY AUTOINCREMENT, | ||
note varchar(100) | ||
); | ||
|
||
CREATE TABLE simple_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1) | ||
); | ||
|
||
CREATE TABLE multi_pk_no_autoincrement ( | ||
identifier1 int NOT NULL, | ||
identifier2 int NOT NULL, | ||
note varchar(100), | ||
PRIMARY KEY (identifier1, identifier2) | ||
); | ||
|
||
CREATE TABLE no_pk ( | ||
note varchar(100) | ||
); |
Oops, something went wrong.