Fixing content type ordering when content_type is not defined.
[akelos.git] / test / unit / lib / AkActiveRecord / _AkActiveRecord_calculations.php
blobc1c7f67898e20c6282cc2445be4fb1dad12244fd
1 <?php
3 defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false);
4 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
6 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
8 class test_AkActiveRecord_calculations extends AkUnitTest
10 function test_start()
12 $this->installAndIncludeModels('Account', array('populate'=>true,'instantiate'=>true));
15 function test_should_sum_field()
17 $this->assertEqual(318, $this->Account->sum('credit_limit'));
20 function test_should_average_field()
22 $average = $this->Account->average('credit_limit');
23 $this->assertTrue(is_float($average));
24 $this->assertEqual(53.0, $average);
27 function test_should_get_maximum_of_field()
29 $this->assertEqual(60, $this->Account->maximum('credit_limit'));
32 function test_should_get_minimum_of_field()
34 $this->assertEqual(50, $this->Account->minimum('credit_limit'));
37 function test_should_group_by_field()
39 $credit = $this->Account->sum('credit_limit',array('group'=>'firm_id'));
40 foreach (array(1 => 50,2 => 60, 6 => 105, 9 => 53) as $k=>$v){
41 $this->assertTrue(array_key_exists($k,$credit));
42 $this->assertEqual($credit[$k], $v);
46 function test_should_order_by_grouped_field()
48 $credit = $this->Account->sum('credit_limit',array('group'=>'firm_id','order'=>'firm_id DESC'));
49 $this->assertEqual(join(array_diff(array_keys($credit),array(''))), join(array(9,6,2,1)));
52 function test_should_order_by_calculation()
54 $credit = $this->Account->sum('credit_limit',array('group'=>'firm_id','order'=>'sum_credit_limit desc, firm_id'));
55 $this->assertEqual(array_values($credit), array(105, 60, 53, 50, 50));
56 $this->assertEqual(join(array_keys($credit)), join(array(6,2,9,null,1)));
59 function test_should_limit_calculation()
61 $credit = $this->Account->sum('credit_limit',array('conditions' => "firm_id IS NOT NULL", 'group'=>'firm_id','order'=>'firm_id','limit'=>2));
62 $this->assertEqual(array_keys($credit), array(1,2));
65 function test_should_limit_calculation_with_offset()
67 $credit = $this->Account->sum('credit_limit',array('conditions' => "firm_id IS NOT NULL", 'group'=>'firm_id','order'=>'firm_id','limit'=>2, 'offset'=>1));
68 $this->assertEqual(array_keys($credit), array(2,6));
71 function test_should_group_by_summed_field_having_condition()
73 $credit = $this->Account->sum('credit_limit',array('conditions' => "firm_id IS NOT NULL", 'group'=>'firm_id','having' => 'sum(credit_limit) > 50'));
74 foreach (array(1 => null, 6 => 105, 2 => 60) as $k=>$v){
75 $this->assertEqual(@$credit[$k], $v);
79 function test_should_sum_field_with_conditions()
81 $this->assertEqual(105, $this->Account->sum('credit_limit',array('conditions' => "firm_id = 6")));
84 function test_should_group_by_summed_field_with_conditions()
86 $credit = $this->Account->sum('credit_limit',array('conditions' => "firm_id > 1", 'group'=>'firm_id'));
87 foreach (array(1 => null, 6 => 105, 2 => 60) as $k=>$v){
88 $this->assertEqual(@$credit[$k], $v);
92 function test_should_group_by_summed_field_with_conditions_and_having()
94 $credit = $this->Account->sum('credit_limit',array('conditions' => "firm_id > 1", 'group'=>'firm_id', 'having' => 'sum(credit_limit) > 60'));
95 foreach (array(1 => null, 6 => 105, 2 => null) as $k=>$v){
96 $this->assertEqual(@$credit[$k], $v);
100 function test_should_group_by_fields_with_table_alias()
102 $credit = $this->Account->sum('credit_limit',array('group'=>'accounts.firm_id'));
103 foreach (array(1 => 50, 6 => 105, 2 => 60) as $k=>$v){
104 $this->assertEqual($credit[$k], $v);
108 function test_should_calculate_with_invalid_field()
110 $this->assertEqual(6, $this->Account->calculate('count','*'));
111 $this->assertEqual(6, $this->Account->calculate('count','all'));
114 function test_should_calculate_grouped_with_invalid_field()
116 $credit = $this->Account->count('all',array('group'=>'accounts.firm_id'));
117 foreach (array(1 => 1, 6 => 2, 2 => 1) as $k=>$v){
118 $this->assertEqual($credit[$k], $v);
124 ak_test('test_AkActiveRecord_calculations',true);