|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +use Illuminate\Database\Eloquent\Factories\Sequence; |
5 | 6 | use Workbench\App\Models\Product; |
| 7 | +use Workbench\App\Models\UserLog; |
6 | 8 |
|
7 | 9 | test('retrieve distinct product statuses', function () { |
8 | 10 | Product::factory()->state(['status' => 1])->count(3)->create(); |
|
40 | 42 |
|
41 | 43 | expect($statuses->first()->status_count)->toEqual(5); |
42 | 44 | }); |
| 45 | + |
| 46 | +it('should perform aggregate sum on distinct and groupBy queries', function () { |
| 47 | + createCompanyData( |
| 48 | + logsPerUser: 50 |
| 49 | + ); |
| 50 | + |
| 51 | + $dist = UserLog::where('status', 9)->distinct()->sum('code'); |
| 52 | + $grp = UserLog::where('status', 9)->groupBy(['code'])->sum('code'); |
| 53 | + |
| 54 | + expect($dist)->toEqual($grp); |
| 55 | +}); |
| 56 | + |
| 57 | +it('should perform aggregate max on distinct and groupBy queries', function () { |
| 58 | + createCompanyData( |
| 59 | + logsPerUser: 50 |
| 60 | + ); |
| 61 | + |
| 62 | + $dist = UserLog::where('status', 9)->distinct()->max('code'); |
| 63 | + $grp = UserLog::where('status', 9)->groupBy(['code'])->max('code'); |
| 64 | + |
| 65 | + expect($dist)->toEqual($grp); |
| 66 | + |
| 67 | +}); |
| 68 | + |
| 69 | +it('should perform aggregate min on distinct and groupBy queries', function () { |
| 70 | + createCompanyData( |
| 71 | + logsPerUser: 50 |
| 72 | + ); |
| 73 | + |
| 74 | + $dist = UserLog::where('status', 9)->distinct()->min('code'); |
| 75 | + $grp = UserLog::where('status', 9)->groupBy(['code'])->min('code'); |
| 76 | + |
| 77 | + expect($dist)->toEqual($grp); |
| 78 | + |
| 79 | +}); |
| 80 | + |
| 81 | +it('should perform aggregate avg on distinct and groupBy queries', function () { |
| 82 | + createCompanyData( |
| 83 | + logsPerUser: 50 |
| 84 | + ); |
| 85 | + |
| 86 | + $dist = UserLog::where('status', 9)->distinct()->avg('code'); |
| 87 | + $grp = UserLog::where('status', 9)->groupBy(['code'])->avg('code'); |
| 88 | + |
| 89 | + expect($dist)->toEqual($grp); |
| 90 | + |
| 91 | +}); |
| 92 | + |
| 93 | +test('Count', function () { |
| 94 | + $products = Product::factory(10)->make(); |
| 95 | + Product::insert($products->toArray()); |
| 96 | + expect(Product::count())->toBe(10); |
| 97 | +}); |
| 98 | + |
| 99 | +test('Max', function () { |
| 100 | + $products = Product::factory(10) |
| 101 | + ->state(new Sequence( |
| 102 | + ['price' => 10.0], |
| 103 | + ['price' => 120.0], |
| 104 | + ))->make(); |
| 105 | + Product::insert($products->toArray()); |
| 106 | + expect(Product::max('price'))->toBe(120.0); |
| 107 | +}); |
| 108 | + |
| 109 | +test('Min', function () { |
| 110 | + $products = Product::factory(10) |
| 111 | + ->state(new Sequence( |
| 112 | + ['price' => 10.0], |
| 113 | + ['price' => 120.0], |
| 114 | + ))->make(); |
| 115 | + Product::insert($products->toArray()); |
| 116 | + expect(Product::min('price'))->toBe(10.0); |
| 117 | +}); |
| 118 | + |
| 119 | +test('Avg', function () { |
| 120 | + $products = Product::factory(10) |
| 121 | + ->state(new Sequence( |
| 122 | + ['price' => 10.0], |
| 123 | + ['price' => 120.0], |
| 124 | + ))->make(); |
| 125 | + Product::insert($products->toArray()); |
| 126 | + expect(Product::avg('price'))->toBe(65.0); |
| 127 | +}); |
| 128 | + |
| 129 | +test('Sum', function () { |
| 130 | + $products = Product::factory(10) |
| 131 | + ->state(['price' => 10.0])->make(); |
| 132 | + Product::insert($products->toArray()); |
| 133 | + expect(Product::sum('price'))->toBe(100.0); |
| 134 | +}); |
| 135 | + |
| 136 | +test('Multiple fields at once', function () { |
| 137 | + $products = Product::factory(10) |
| 138 | + ->state(new Sequence( |
| 139 | + ['price' => 10.0, 'discount_amount' => 5.0], |
| 140 | + ['price' => 120.0, 'discount_amount' => 7.0], |
| 141 | + ))->make(); |
| 142 | + Product::insert($products->toArray()); |
| 143 | + |
| 144 | + $max = Product::max(['price', 'discount_amount']); |
| 145 | + |
| 146 | + expect($max)->toBeArray() |
| 147 | + ->and($max['max_price'])->toBe(120.0) |
| 148 | + ->and($max['max_discount_amount'])->toBe(7.0); |
| 149 | +}); |
| 150 | + |
| 151 | +test('Multiple aggs', function () { |
| 152 | + $products = Product::factory(10) |
| 153 | + ->state(new Sequence( |
| 154 | + ['price' => 10.0, 'discount_amount' => 5.0], |
| 155 | + ['price' => 120.0, 'discount_amount' => 7.0], |
| 156 | + ))->make(); |
| 157 | + Product::insert($products->toArray()); |
| 158 | + |
| 159 | + $max = Product::agg(['count', 'avg', 'min', 'max', 'sum'], 'price'); |
| 160 | + |
| 161 | + expect($max)->toBeArray() |
| 162 | + ->and($max)->toHaveKeys(['max_price', 'min_price', 'count_price', 'avg_price', 'sum_price']); |
| 163 | +}); |
| 164 | + |
| 165 | +test('Matrix', function () { |
| 166 | + $products = Product::factory(100) |
| 167 | + ->state(new Sequence( |
| 168 | + ['color' => 'red'], |
| 169 | + ['color' => 'green'], |
| 170 | + ['color' => 'blue'], |
| 171 | + ['color' => 'yellow'], |
| 172 | + ))->make(); |
| 173 | + Product::insert($products->toArray()); |
| 174 | + |
| 175 | + $matrix = Product::whereNotIn('color', ['red', 'green'])->matrix('price'); |
| 176 | + |
| 177 | + expect($matrix)->toBeArray() |
| 178 | + ->and($matrix['fields'])->toBeArray(); |
| 179 | +}); |
0 commit comments