|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Database\Query\Expression; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Tpetry\QueryExpressions\Function\Time\TimestampBin; |
| 8 | + |
| 9 | +it('can bin the time of a column') |
| 10 | + ->expect(new TimestampBin('val', DateInterval::createFromDateString('5 minutes'))) |
| 11 | + ->toBeExecutable(['val timestamp']) |
| 12 | + ->toBeMysql('(from_unixtime(floor((unix_timestamp(`val`)-0)/300)*300+0))') |
| 13 | + ->toBePgsql('to_timestamp(floor((extract(epoch from "val")-0)/300)*300+0)') |
| 14 | + ->toBeSqlite('(datetime((strftime(\'%s\',"val")-0)/300*300+0,\'unixepoch\'))') |
| 15 | + ->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',[val])-0)/300*300+0,\'1970-01-01\')'); |
| 16 | + |
| 17 | +it('can bin the time of an expression') |
| 18 | + ->expect(new TimestampBin(new Expression('current_timestamp'), DateInterval::createFromDateString('1 minute'))) |
| 19 | + ->toBeExecutable(['val timestamp']) |
| 20 | + ->toBeMysql('(from_unixtime(floor((unix_timestamp(current_timestamp)-0)/60)*60+0))') |
| 21 | + ->toBePgsql('to_timestamp(floor((extract(epoch from current_timestamp)-0)/60)*60+0)') |
| 22 | + ->toBeSqlite('(datetime((strftime(\'%s\',current_timestamp)-0)/60*60+0,\'unixepoch\'))') |
| 23 | + ->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',current_timestamp)-0)/60*60+0,\'1970-01-01\')'); |
| 24 | + |
| 25 | +it('can bin the time of a column with an origin') |
| 26 | + ->expect(new TimestampBin('val', DateInterval::createFromDateString('90 seconds'), DateTime::createFromFormat('Y-m-d H:i:s', '2022-02-02 22:22:22'))) |
| 27 | + ->toBeExecutable(['val timestamp']) |
| 28 | + ->toBeMysql('(from_unixtime(floor((unix_timestamp(`val`)-1643840542)/90)*90+1643840542))') |
| 29 | + ->toBePgsql('to_timestamp(floor((extract(epoch from "val")-1643840542)/90)*90+1643840542)') |
| 30 | + ->toBeSqlite('(datetime((strftime(\'%s\',"val")-1643840542)/90*90+1643840542,\'unixepoch\'))') |
| 31 | + ->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',[val])-1643840542)/90*90+1643840542,\'1970-01-01\')'); |
| 32 | + |
| 33 | +it('can bin the time of an expression with an origin') |
| 34 | + ->expect(new TimestampBin(new Expression('current_timestamp'), DateInterval::createFromDateString('1 hour'), DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00'))) |
| 35 | + ->toBeExecutable(['val timestamp']) |
| 36 | + ->toBeMysql('(from_unixtime(floor((unix_timestamp(current_timestamp)-946684800)/3600)*3600+946684800))') |
| 37 | + ->toBePgsql('to_timestamp(floor((extract(epoch from current_timestamp)-946684800)/3600)*3600+946684800)') |
| 38 | + ->toBeSqlite('(datetime((strftime(\'%s\',current_timestamp)-946684800)/3600*3600+946684800,\'unixepoch\'))') |
| 39 | + ->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',current_timestamp)-946684800)/3600*3600+946684800,\'1970-01-01\')'); |
| 40 | + |
| 41 | +it('does not support millisecond steps', function () { |
| 42 | + $expression = new TimestampBin( |
| 43 | + expression: new Expression('current_timestamp'), |
| 44 | + step: DateInterval::createFromDateString('500 milliseconds'), |
| 45 | + ); |
| 46 | + |
| 47 | + $expression->getValue(DB::connection()->getQueryGrammar()); |
| 48 | +})->throws(RuntimeException::class, 'timestamp binning with millisecond resolution is not supported'); |
| 49 | + |
| 50 | +it('does not support origins before 1970-01-01', function () { |
| 51 | + $expression = new TimestampBin( |
| 52 | + expression: new Expression('current_timestamp'), |
| 53 | + step: DateInterval::createFromDateString('1 second'), |
| 54 | + origin: DateTime::createFromFormat('Y-m-d', '1900-01-01'), |
| 55 | + ); |
| 56 | + |
| 57 | + $expression->getValue(DB::connection()->getQueryGrammar()); |
| 58 | +})->throws(RuntimeException::class, 'timestamp binning with an origin before 1970-01-01 is not supported'); |
0 commit comments