forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelsCalculationsTest.php
153 lines (112 loc) · 5.53 KB
/
ModelsCalculationsTest.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
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
class ModelsCalculationsTest extends PHPUnit_Framework_TestCase {
public function testCalculations(){
$config = array(
'adapter' => 'Mysql',
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'name' => 'phalcon_test'
);
Phalcon_Db_Pool::setDefaultDescriptor($config);
$this->assertTrue(Phalcon_Db_Pool::hasDefaultDescriptor());
$manager = new Phalcon_Model_Manager();
$manager->setModelsDir('unit-tests/models/');
$success = $manager->load('Personnes');
$this->assertTrue($success);
//Count calculations
$rowcount = Personnes::count();
$this->assertEquals($rowcount, 2180);
$rowcount = Personnes::count(array('distinct' => 'estado'));
$this->assertEquals($rowcount, 2);
$rowcount = Personnes::count("estado='A'");
$this->assertEquals($rowcount, 2178);
$group = Personnes::count(array("group" => "estado"));
$this->assertEquals(2, count($group));
$group = Personnes::count(array("group" => "estado", "order" => "estado"));
$this->assertEquals(2, count($group));
$results = array('A' => 2178, 'I' => 2);
foreach($group as $row){
$this->assertEquals($results[$row->estado], $row->rowcount);
}
$this->assertEquals($group[0]->rowcount, 2178);
$this->assertEquals($group[1]->rowcount, 2);
$group = Personnes::count(array("group" => "estado"));
$this->assertEquals(2, count($group));
$group = Personnes::count(array("group" => "ciudad_id"));
$this->assertEquals(285, count($group));
$group = Personnes::count(array("group" => "ciudad_id", "order" => "rowcount DESC"));
$this->assertEquals($group[0]->rowcount, 727);
//Summatory
$total = Personnes::sum(array("column" => "cupo"));
$this->assertEquals(995066020.00, $total);
$total = Personnes::sum(array("column" => "cupo", "conditions" => "estado='I'"));
$this->assertEquals(567020.00, $total);
$group = Personnes::sum(array("column" => "cupo", "group" => "estado"));
$this->assertEquals(2, count($group));
$results = array('A' => 994499000.00, 'I' => 567020.00);
foreach($group as $row){
$this->assertEquals($results[$row->estado], $row->sumatory);
}
$group = Personnes::sum(array("column" => "cupo", "group" => "ciudad_id", "order" => "sumatory DESC"));
$this->assertEquals($group[0]->sumatory, 358467690.00);
//Average
$total = Personnes::average(array("column" => "cupo"));
$this->assertEquals(456452.302752, $total);
$total = Personnes::average(array("column" => "cupo", "conditions" => "estado='I'"));
$this->assertEquals(283510.00, $total);
$group = Personnes::average(array("column" => "cupo", "group" => "estado"));
$this->assertEquals(2, count($group));
$results = array('A' => 456611.111111, 'I' => 283510.00);
foreach($group as $row){
$this->assertEquals($results[$row->estado], $row->average);
}
$group = Personnes::average(array("column" => "cupo", "group" => "ciudad_id", "order" => "average DESC"));
$this->assertEquals($group[0]->average, 996200.00);
//Maximum
$max = Personnes::maximum(array("column" => "ciudad_id"));
$this->assertEquals($max, 302172);
$max = Personnes::maximum(array("column" => "ciudad_id", "conditions" => "estado='I'"));
$this->assertEquals($max, 127591);
$group = Personnes::maximum(array("column" => "ciudad_id", "group" => "estado"));
$this->assertEquals(2, count($group));
$results = array('A' => 302172, 'I' => 127591);
foreach($group as $row){
$this->assertEquals($results[$row->estado], $row->maximum);
}
$group = Personnes::maximum(array("column" => "ciudad_id", "group" => "estado", "order" => "maximum DESC"));
$this->assertEquals($group[0]->maximum, 302172);
//Minimum
$max = Personnes::minimum(array("column" => "ciudad_id"));
$this->assertEquals($max, 20404);
$max = Personnes::minimum(array("column" => "ciudad_id", "conditions" => "estado='I'"));
$this->assertEquals($max, 127591);
$group = Personnes::minimum(array("column" => "ciudad_id", "group" => "estado"));
$this->assertEquals(2, count($group));
$results = array('A' => 20404, 'I' => 127591);
foreach($group as $row){
$this->assertEquals($results[$row->estado], $row->minimum);
}
$group = Personnes::minimum(array("column" => "ciudad_id", "group" => "estado", "order" => "minimum DESC"));
$this->assertEquals($group[0]->minimum, 127591);
$group = Personnes::minimum(array("column" => "ciudad_id", "group" => "estado", "order" => "minimum ASC"));
$this->assertEquals($group[0]->minimum, 20404);
}
}