Newer
Older
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
class RankingQueryTest extends PHPUnit_Framework_TestCase
{
/**
* @group Core
* @group RankingQuery
*/
public function testBasic()
{
$query = new Piwik_RankingQuery();
$query->addLabelColumn('label');
$query->addColumn('column');
$query->addColumn('columnSum', 'sum');
$query->setLimit(10);
$innerQuery = "SELECT label, column, columnSum FROM myTable";
$expected = "
SELECT
CASE
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
ELSE `label`
END AS `label`,
`column`,
sum(`columnSum`) AS `columnSum`
FROM (
SELECT
`label`,
CASE
WHEN @counter = 11 THEN 11
ELSE @counter:=@counter+1
END AS counter,
`column`,
`columnSum`
FROM
( SELECT @counter:=0 ) initCounter,
( SELECT label, column, columnSum FROM myTable ) actualQuery
) AS withCounter
GROUP BY counter
";
$this->checkQuery($query, $innerQuery, $expected);
}
/**
* @group Core
* @group RankingQuery
*/
public function testExcludeRows()
{
$query = new Piwik_RankingQuery(20);
$query->addLabelColumn('label');
$query->setColumnToMarkExcludedRows('exclude_marker');
$innerQuery = "SELECT label, 1 AS exclude_marker FROM myTable";
$expected = "
SELECT
CASE
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
ELSE `label`
END AS `label`,
`exclude_marker`
FROM (
SELECT
`label`,
CASE
WHEN exclude_marker != 0 THEN -1 * exclude_marker
WHEN @counter = 21 THEN 21
ELSE @counter:=@counter+1
END AS counter,
`exclude_marker`
FROM
( SELECT @counter:=0 ) initCounter,
( SELECT label, 1 AS exclude_marker FROM myTable ) actualQuery
) AS withCounter
GROUP BY counter
";
$this->checkQuery($query, $innerQuery, $expected);
}
/**
* @group Core
* @group RankingQuery
*/
public function testPartitionResult()
{
$query = new Piwik_RankingQuery(1000);
$query->addLabelColumn('label');
$query->partitionResultIntoMultipleGroups('partition', array(1, 2, 3));
$innerQuery = "SELECT label, partition FROM myTable";
$expected = "
SELECT
CASE
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
ELSE `label`
END AS `label`,
`partition`
FROM (
SELECT
`label`,
CASE
WHEN `partition` = 1 AND @counter1 = 1001 THEN 1001
WHEN `partition` = 1 THEN @counter1:=@counter1+1
WHEN `partition` = 2 AND @counter2 = 1001 THEN 1001
WHEN `partition` = 2 THEN @counter2:=@counter2+1
WHEN `partition` = 3 AND @counter3 = 1001 THEN 1001
WHEN `partition` = 3 THEN @counter3:=@counter3+1
ELSE 0
END AS counter,
`partition`
FROM
( SELECT @counter1:=0 ) initCounter1,
( SELECT @counter2:=0 ) initCounter2,
( SELECT @counter3:=0 ) initCounter3,
( SELECT label, partition FROM myTable ) actualQuery
) AS withCounter
GROUP BY counter, `partition`
";
$this->checkQuery($query, $innerQuery, $expected);
}
/**
* @param Piwik_RankingQuery $rankingQuery
* @param string $innerQuerySql
* @param string $expected
*/
private function checkQuery($rankingQuery, $innerQuerySql, $expected)
{
$query = $rankingQuery->generateQuery($innerQuerySql);
$queryNoWhitespace = preg_replace("/\s+/", "", $query);
$expectedNoWhitespace = preg_replace("/\s+/", "", $expected);
$message = 'Unexpected query: '.$query;
$this->assertEquals($queryNoWhitespace, $expectedNoWhitespace, $message);
}
}