1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
use \Codeception\Lib\Driver\Db;
use \Codeception\Test\Unit;
/**
* @group appveyor
* @group db
*/
class PostgresTest extends Unit
{
protected static $config = [
'dsn' => 'pgsql:host=localhost;dbname=codeception_test',
'user' => 'postgres',
'password' => null,
];
protected static $sql;
protected $postgres;
public static function setUpBeforeClass()
{
if (!function_exists('pg_connect')) {
return;
}
if (getenv('APPVEYOR')) {
self::$config['password'] = 'Password12!';
}
$dumpFile = 'dumps/postgres.sql';
if (defined('HHVM_VERSION')) {
$dumpFile = 'dumps/postgres-hhvm.sql';
}
$sql = file_get_contents(codecept_data_dir($dumpFile));
$sql = preg_replace('%/\*(?:(?!\*/).)*\*/%s', '', $sql);
self::$sql = explode("\n", $sql);
}
public function setUp()
{
try {
$this->postgres = Db::create(self::$config['dsn'], self::$config['user'], self::$config['password']);
$this->postgres->cleanup();
} catch (\Exception $e) {
$this->markTestSkipped('Coudn\'t establish connection to database: ' . $e->getMessage());
}
$this->postgres->load(self::$sql);
}
public function tearDown()
{
if (isset($this->postgres)) {
$this->postgres->cleanup();
}
}
public function testCleanupDatabase()
{
$this->assertNotEmpty(
$this->postgres->getDbh()->query("SELECT * FROM pg_tables where schemaname = 'public'")->fetchAll()
);
$this->postgres->cleanup();
$this->assertEmpty(
$this->postgres->getDbh()->query("SELECT * FROM pg_tables where schemaname = 'public'")->fetchAll()
);
}
public function testCleanupDatabaseDeletesTypes()
{
$customTypes = ['composite_type', 'enum_type', 'range_type', 'base_type'];
foreach ($customTypes as $customType) {
$this->assertNotEmpty(
$this->postgres->getDbh()
->query("SELECT 1 FROM pg_type WHERE typname = '" . $customType . "';")
->fetchAll()
);
}
$this->postgres->cleanup();
foreach ($customTypes as $customType) {
$this->assertEmpty(
$this->postgres->getDbh()
->query("SELECT 1 FROM pg_type WHERE typname = '" . $customType . "';")
->fetchAll()
);
}
}
public function testLoadDump()
{
$res = $this->postgres->getDbh()->query("select * from users where name = 'davert'");
$this->assertNotEquals(false, $res);
$this->assertGreaterThan(0, $res->rowCount());
$res = $this->postgres->getDbh()->query("select * from groups where name = 'coders'");
$this->assertNotEquals(false, $res);
$this->assertGreaterThan(0, $res->rowCount());
$res = $this->postgres->getDbh()->query("select * from users where email = 'user2@example.org'");
$this->assertNotEquals(false, $res);
$this->assertGreaterThan(0, $res->rowCount());
$res = $this->postgres->getDbh()
->query("select * from anotherschema.users where email = 'schemauser@example.org'");
$this->assertEquals(1, $res->rowCount());
}
public function testSelectWithEmptyCriteria()
{
$emptyCriteria = [];
$generatedSql = $this->postgres->select('test_column', 'test_table', $emptyCriteria);
$this->assertNotContains('where', $generatedSql);
}
public function testGetSingleColumnPrimaryKey()
{
$this->assertEquals(['id'], $this->postgres->getPrimaryKey('order'));
}
public function testGetCompositePrimaryKey()
{
$this->assertEquals(['group_id', 'id'], $this->postgres->getPrimaryKey('composite_pk'));
}
public function testGetEmptyArrayIfTableHasNoPrimaryKey()
{
$this->assertEquals([], $this->postgres->getPrimaryKey('no_pk'));
}
public function testLastInsertIdReturnsSequenceValueWhenNonStandardSequenceNameIsUsed()
{
$this->postgres->executeQuery('INSERT INTO seqnames(name) VALUES(?)',['test']);
$this->assertEquals(1, $this->postgres->lastInsertId('seqnames'));
}
public function testGetPrimaryColumnOfTableUsingReservedWordAsTableName()
{
$this->assertEquals('id', $this->postgres->getPrimaryColumn('order'));
}
public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey()
{
$this->setExpectedException(
'\Exception',
'getPrimaryColumn method does not support composite primary keys, use getPrimaryKey instead'
);
$this->postgres->getPrimaryColumn('composite_pk');
}
/**
* @issue https://github.com/Codeception/Codeception/issues/4059
*/
public function testLoadDumpEndingWithoutDelimiter()
{
$newDriver = new \Codeception\Lib\Driver\PostgreSql(self::$config['dsn'], self::$config['user'], self::$config['password']);
$newDriver->load(['INSERT INTO empty_table VALUES(1, \'test\')']);
$res = $newDriver->getDbh()->query("select * from empty_table where field = 'test'");
$this->assertNotEquals(false, $res);
$this->assertNotEmpty($res->fetchAll());
}
}