-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
WindowQueryTests.php
185 lines (162 loc) · 5.63 KB
/
WindowQueryTests.php
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Database\QueryTests;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\WindowExpression;
use Cake\Database\Query;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use RuntimeException;
/**
* Tests WindowExpression class
*/
class WindowQueryTests extends TestCase
{
protected $fixtures = [
'core.Comments',
];
public $autoFixtures = false;
/**
* @var \Cake\Database\Connection
*/
protected $connection = null;
protected $skipTests = false;
public function setUp(): void
{
parent::setUp();
$this->connection = ConnectionManager::get('test');
$driver = $this->connection->getDriver();
if (
$driver instanceof \Cake\Database\Driver\Mysql ||
$driver instanceof \Cake\Database\Driver\Sqlite
) {
$this->skipTests = !$this->connection->getDriver()->supportsWindowFunctions();
} else {
$this->skipTests = false;
}
}
public function tearDown(): void
{
parent::tearDown();
}
/**
* Tests window sql generation.
*
* @return void
*/
public function testWindowSql()
{
$query = new Query($this->connection);
$sql = $query
->select('*')
->window('name', new WindowExpression())
->sql();
$this->assertEqualsSql('SELECT * WINDOW name AS ()', $sql);
$sql = $query
->window('name2', new WindowExpression('name'))
->sql();
$this->assertEqualsSql('SELECT * WINDOW name AS (), name2 AS (name)', $sql);
$sql = $query
->window('name', function ($window, $query) {
return $window->name('name3');
}, true)
->sql();
$this->assertEqualsSql('SELECT * WINDOW name AS (name3)', $sql);
}
public function testMissingWindow()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('You must return a `WindowExpression`');
(new Query($this->connection))->window('name', function () {
return new QueryExpression();
});
}
public function testPartitions()
{
$this->skipIf($this->skipTests);
$this->loadFixtures('Comments');
$query = new Query($this->connection);
$result = $query
->select(['num_rows' => $query->func()->count('*')->over()])
->from('comments')
->execute()
->fetchAll();
$this->assertCount(6, $result);
$query = new Query($this->connection);
$result = $query
->select(['num_rows' => $query->func()->count('*')->partition('article_id')])
->from('comments')
->order(['article_id'])
->execute()
->fetchAll('assoc');
$this->assertEquals(4, $result[0]['num_rows']);
$query = new Query($this->connection);
$result = $query
->select(['num_rows' => $query->func()->count('*')->partition('article_id')->order('updated')])
->from('comments')
->order(['updated'])
->execute()
->fetchAll('assoc');
$this->assertEquals(1, $result[0]['num_rows']);
$this->assertEquals(4, $result[3]['num_rows']);
$this->assertEquals(1, $result[4]['num_rows']);
}
/**
* Tests adding named windows to the query.
*
* @return void
*/
public function testNamedWindow()
{
$skip = $this->skipTests;
if (!$skip) {
$skip = $this->connection->getDriver() instanceof \Cake\Database\Driver\Sqlserver;
}
$this->skipIf($skip);
$this->loadFixtures('Comments');
$query = new Query($this->connection);
$result = $query
->select(['num_rows' => $query->func()->count('*')->over('window1')])
->from('comments')
->window('window1', (new WindowExpression())->partition('article_id'))
->order(['article_id'])
->execute()
->fetchAll('assoc');
$this->assertEquals(4, $result[0]['num_rows']);
}
public function testWindowChaining()
{
$skip = $this->skipTests;
if (!$skip) {
$driver = $this->connection->getDriver();
$skip = $driver instanceof \Cake\Database\Driver\Sqlserver;
if ($driver instanceof \Cake\Database\Driver\Sqlite) {
$skip = version_compare($driver->getVersion(), '3.28.0', '<');
}
}
$this->skipIf($skip);
$this->loadFixtures('Comments');
$query = new Query($this->connection);
$result = $query
->select(['num_rows' => $query->func()->count('*')->over('window2')])
->from('comments')
->window('window1', (new WindowExpression())->partition('article_id'))
->window('window2', new WindowExpression('window1'))
->order(['article_id'])
->execute()
->fetchAll('assoc');
$this->assertEquals(4, $result[0]['num_rows']);
}
}