Skip to content

Commit ace2256

Browse files
committed
Selection::insert(): added falling tests for insert into table without auto incerement PK
1 parent cc5d0e0 commit ace2256

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* @dataProvider? ../../databases.ini
4+
*/
5+
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../../connect.inc.php'; // create $connection
9+
10+
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../../files/{$driverName}-nette_test4.sql");
11+
12+
//Insert into table without auto_increament primary key
13+
test(function () use ($context) {
14+
15+
$inserted = $context->table('simple_pk')->insert([
16+
'id' => 8,
17+
'name' => 'Michal'
18+
]);
19+
20+
Assert::equal(8, $inserted->id);
21+
Assert::equal('Michal', $inserted->name);
22+
});
23+
24+
//Insert into table with composite primary key
25+
test(function () use ($context) {
26+
27+
$inserted = $context->table('composite_pk')->insert([
28+
'id1' => 8,
29+
'id2' => 10,
30+
'name' => 'Michal'
31+
]);
32+
33+
Assert::equal(8, $inserted->id1);
34+
Assert::equal(10, $inserted->id2);
35+
Assert::equal('Michal', $inserted->name);
36+
});
37+
38+
//Insert into table with composite primary key and one of them is auto_increment
39+
test(function () use ($context, $driverName) {
40+
41+
//Sqlite doesn't allow this type of table and sqlsrv's driver don't implement reflection
42+
if ($driverName == 'mysql' || $driverName == 'pgsql') {
43+
$inserted = $context->table('composite_pk_ai')->insert([
44+
'id2' => 10,
45+
'name' => 'Michal'
46+
]);
47+
48+
Assert::equal(10, $inserted->id2);
49+
Assert::equal('Michal', $inserted->name);
50+
}
51+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*!40102 SET storage_engine = InnoDB */;
2+
3+
DROP DATABASE IF EXISTS nette_test;
4+
CREATE DATABASE nette_test;
5+
USE nette_test;
6+
7+
8+
CREATE TABLE simple_pk (
9+
id int NOT NULL,
10+
name varchar(100),
11+
PRIMARY KEY (id)
12+
);
13+
14+
CREATE TABLE composite_pk (
15+
id1 int NOT NULL,
16+
id2 int NOT NULL,
17+
name varchar(100),
18+
PRIMARY KEY (id1, id2)
19+
);
20+
21+
CREATE TABLE composite_pk_ai (
22+
id1 int NOT NULL AUTO_INCREMENT,
23+
id2 int NOT NULL,
24+
name varchar(100),
25+
PRIMARY KEY (id1, id2)
26+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
DROP SCHEMA IF EXISTS public CASCADE;
2+
CREATE SCHEMA public;
3+
4+
CREATE TABLE simple_pk (
5+
id int NOT NULL,
6+
name varchar(100),
7+
PRIMARY KEY (id)
8+
);
9+
10+
CREATE TABLE composite_pk (
11+
id1 int NOT NULL,
12+
id2 int NOT NULL,
13+
name varchar(100),
14+
PRIMARY KEY (id1, id2)
15+
);
16+
17+
CREATE TABLE composite_pk_ai (
18+
id1 serial NOT NULL,
19+
id2 int NOT NULL,
20+
name varchar(100),
21+
PRIMARY KEY (id1, id2)
22+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DROP TABLE IF EXISTS simple_pk;
2+
DROP TABLE IF EXISTS composite_pk;
3+
4+
CREATE TABLE simple_pk (
5+
id INT NOT NULL,
6+
name TEXT,
7+
PRIMARY KEY (id)
8+
);
9+
10+
CREATE TABLE composite_pk (
11+
id1 int NOT NULL,
12+
id2 int NOT NULL,
13+
name TEXT,
14+
PRIMARY KEY (id1, id2)
15+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
IF OBJECT_ID('simple_pk', 'U') IS NOT NULL DROP TABLE simple_pk;
2+
IF OBJECT_ID('composite_pk', 'U') IS NOT NULL DROP TABLE composite_pk;
3+
IF OBJECT_ID('composite_pk_ai', 'U') IS NOT NULL DROP TABLE composite_pk_ai;
4+
5+
CREATE TABLE simple_pk (
6+
id INTEGER NOT NULL,
7+
name TEXT,
8+
PRIMARY KEY (id)
9+
);
10+
11+
CREATE TABLE composite_pk (
12+
id1 INTEGER NOT NULL,
13+
id2 INTEGER NOT NULL,
14+
name TEXT,
15+
PRIMARY KEY (id1, id2)
16+
);
17+
18+
CREATE TABLE composite_pk_ai (
19+
id1 INTEGER NOT NULL IDENTITY(1,1),
20+
id2 INTEGER NOT NULL,
21+
name TEXT,
22+
PRIMARY KEY (id1, id2)
23+
);

0 commit comments

Comments
 (0)