Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tests: use schema builder for columns setup #20

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion tests/Function/Aggregate/AvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\Avg;

it('can aggregate a column by AVG')
->expect(new Avg('val'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('avg(`val`)')
->toBePgsql('avg("val")')
->toBeSqlite('avg("val")')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/Aggregate/CountFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\CountFilter;

it('can aggregate a column by COUNT with a filter')
->expect(new CountFilter(new Expression('val = 1')))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('sum(val = 1)')
->toBePgsql('count(*) filter (where val = 1)')
->toBeSqlite('count(*) filter (where val = 1)')
Expand Down
9 changes: 7 additions & 2 deletions tests/Function/Aggregate/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\Count;

it('can aggregate a column by COUNT')
->expect(new Count('val'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('count(`val`)')
->toBePgsql('count("val")')
->toBeSqlite('count("val")')
->toBeSqlsrv('count([val])');

it('can aggregate a column by COUNT with DISTINCT')
->expect(new Count('val', true))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('count(distinct `val`)')
->toBePgsql('count(distinct "val")')
->toBeSqlite('count(distinct "val")')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/Aggregate/MaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\Max;

it('can aggregate a column by MAX')
->expect(new Max('val'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('max(`val`)')
->toBePgsql('max("val")')
->toBeSqlite('max("val")')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/Aggregate/MinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\Min;

it('can aggregate a column by MIN')
->expect(new Min('val'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('min(`val`)')
->toBePgsql('min("val")')
->toBeSqlite('min("val")')
Expand Down
10 changes: 8 additions & 2 deletions tests/Function/Aggregate/SumFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\SumFilter;

it('can aggregate a column by SUM with a filter')
->expect(new SumFilter('val1', new Expression('val2 = 1')))
->toBeExecutable(['val1 int', 'val2 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
})
->toBeMysql('sum(case when val2 = 1 then `val1` else 0 end)')
->toBePgsql('sum("val1") filter (where val2 = 1)')
->toBeSqlite('sum("val1") filter (where val2 = 1)')
->toBeSqlsrv('sum(case when val2 = 1 then [val1] else 0 end)');

it('can aggregate an expression by SUM with a filter')
->expect(new SumFilter(new Expression(1), new Expression('val = 1')))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('sum(case when val = 1 then 1 else 0 end)')
->toBePgsql('sum(1) filter (where val = 1)')
->toBeSqlite('sum(1) filter (where val = 1)')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/Aggregate/SumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Aggregate\Sum;

it('can aggregate a column by SUM')
->expect(new Sum('val'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('sum(`val`)')
->toBePgsql('sum("val")')
->toBeSqlite('sum("val")')
Expand Down
14 changes: 11 additions & 3 deletions tests/Function/Comparison/StrListContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Comparison\StrListContains;

it('can check for existence of a column within a column string list')
->expect(new StrListContains('haystack', 'needle'))
->toBeExecutable(['haystack varchar(255)', 'needle varchar(255)'], options: [
->toBeExecutable(function (Blueprint $table) {
$table->string('haystack');
$table->string('needle');
}, options: [
'sqlsrv' => ['position' => 'where'],
])
->toBeMysql('FIND_IN_SET(`needle`, `haystack`) > 0')
Expand All @@ -27,7 +31,9 @@

it('can check for existence of a column within an expression string list')
->expect(new StrListContains(new Expression("'a,b,c'"), 'needle'))
->toBeExecutable(['needle varchar(255)'], options: [
->toBeExecutable(function (Blueprint $table) {
$table->string('needle');
}, options: [
'sqlsrv' => ['position' => 'where'],
])
->toBeMysql("FIND_IN_SET(`needle`, 'a,b,c') > 0")
Expand All @@ -37,7 +43,9 @@

it('can check for existence of an expression within a column string list')
->expect(new StrListContains('haystack', new Expression("'a'")))
->toBeExecutable(['haystack varchar(255)'], options: [
->toBeExecutable(function (Blueprint $table) {
$table->string('haystack');
}, options: [
'sqlsrv' => ['position' => 'where'],
])
->toBeMysql("FIND_IN_SET('a', `haystack`) > 0")
Expand Down
12 changes: 10 additions & 2 deletions tests/Function/Conditional/CoalesceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Conditional\Coalesce;

it('can combine multiple columns')
->expect(new Coalesce(['val1', 'val2', 'val3']))
->toBeExecutable(['val1 int', 'val2 int', 'val3 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
$table->integer('val3');
})
->toBeMysql('coalesce(`val1`, `val2`, `val3`)')
->toBePgsql('coalesce("val1", "val2", "val3")')
->toBeSqlite('coalesce("val1", "val2", "val3")')
Expand All @@ -23,7 +28,10 @@

it('can combine multiple columns and expressions')
->expect(new Coalesce(['val1', 'val2', new Expression(3)]))
->toBeExecutable(['val1 int', 'val2 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
})
->toBeMysql('coalesce(`val1`, `val2`, 3)')
->toBePgsql('coalesce("val1", "val2", 3)')
->toBeSqlite('coalesce("val1", "val2", 3)')
Expand Down
12 changes: 10 additions & 2 deletions tests/Function/Conditional/GreatestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Conditional\Greatest;

it('can combine multiple columns')
->expect(new Greatest(['val1', 'val2', 'val3']))
->toBeExecutable(['val1 int', 'val2 int', 'val3 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
$table->integer('val3');
})
->toBeMysql('greatest(`val1`, `val2`, `val3`)')
->toBePgsql('greatest("val1", "val2", "val3")')
->toBeSqlite('max("val1", "val2", "val3")')
Expand All @@ -23,7 +28,10 @@

it('can combine multiple columns and expressions')
->expect(new Greatest(['val1', 'val2', new Expression(3)]))
->toBeExecutable(['val1 int', 'val2 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
})
->toBeMysql('greatest(`val1`, `val2`, 3)')
->toBePgsql('greatest("val1", "val2", 3)')
->toBeSqlite('max("val1", "val2", 3)')
Expand Down
12 changes: 10 additions & 2 deletions tests/Function/Conditional/LeastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\Conditional\Least;

it('can combine multiple columns')
->expect(new Least(['val1', 'val2', 'val3']))
->toBeExecutable(['val1 int', 'val2 int', 'val3 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
$table->integer('val3');
})
->toBeMysql('least(`val1`, `val2`, `val3`)')
->toBePgsql('least("val1", "val2", "val3")')
->toBeSqlite('min("val1", "val2", "val3")')
Expand All @@ -23,7 +28,10 @@

it('can combine multiple columns and expressions')
->expect(new Least(['val1', 'val2', new Expression(3)]))
->toBeExecutable(['val1 int', 'val2 int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val1');
$table->integer('val2');
})
->toBeMysql('least(`val1`, `val2`, 3)')
->toBePgsql('least("val1", "val2", 3)')
->toBeSqlite('min("val1", "val2", 3)')
Expand Down
12 changes: 10 additions & 2 deletions tests/Function/String/ConcatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\String\Concat;

it('can concat multiple columns')
->expect(new Concat(['val1', 'val2', 'val3']))
->toBeExecutable(['val1 varchar(255)', 'val2 varchar(255)', 'val3 varchar(255)'])
->toBeExecutable(function (Blueprint $table) {
$table->string('val1');
$table->string('val2');
$table->string('val3');
})
->toBeMysql('(concat(`val1`,`val2`,`val3`))')
->toBePgsql('("val1"||"val2"||"val3")')
->toBeSqlite('("val1"||"val2"||"val3")')
Expand All @@ -23,7 +28,10 @@

it('can concat multiple columns and expressions')
->expect(new Concat(['val1', 'val2', new Expression("'c'")]))
->toBeExecutable(['val1 varchar(255)', 'val2 varchar(255)'])
->toBeExecutable(function (Blueprint $table) {
$table->string('val1');
$table->string('val2');
})
->toBeMysql("(concat(`val1`,`val2`,'c'))")
->toBePgsql('("val1"||"val2"||\'c\')')
->toBeSqlite('("val1"||"val2"||\'c\')')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/String/LowerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\String\Lower;

it('can lowercase a column')
->expect(new Lower('val'))
->toBeExecutable(['val varchar(255)'])
->toBeExecutable(function (Blueprint $table) {
$table->string('val');
})
->toBeMysql('(lower(`val`))')
->toBePgsql('lower("val")')
->toBeSqlite('(lower("val"))')
Expand Down
5 changes: 4 additions & 1 deletion tests/Function/String/UpperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Function\String\Upper;

it('can uppercase a column')
->expect(new Upper('val'))
->toBeExecutable(['val varchar(255)'])
->toBeExecutable(function (Blueprint $table) {
$table->string('val');
})
->toBeMysql('(upper(`val`))')
->toBePgsql('upper("val")')
->toBeSqlite('(upper("val"))')
Expand Down
17 changes: 13 additions & 4 deletions tests/Function/Time/TimestampBinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,45 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Tpetry\QueryExpressions\Function\Time\TimestampBin;

it('can bin the time of a column')
->expect(new TimestampBin('val', DateInterval::createFromDateString('5 minutes')))
->toBeExecutable(['val timestamp'])
->toBeExecutable(function (Blueprint $table) {
$table->timestamp('val');
})
->toBeMysql('(from_unixtime(floor((unix_timestamp(`val`)-0)/300)*300+0))')
->toBePgsql('to_timestamp(floor((extract(epoch from "val")-0)/300)*300+0)')
->toBeSqlite('(datetime((strftime(\'%s\',"val")-0)/300*300+0,\'unixepoch\'))')
->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',[val])-0)/300*300+0,\'1970-01-01\')');

it('can bin the time of an expression')
->expect(new TimestampBin(new Expression('current_timestamp'), DateInterval::createFromDateString('1 minute')))
->toBeExecutable(['val timestamp'])
->toBeExecutable(function (Blueprint $table) {
$table->timestamp('val');
})
->toBeMysql('(from_unixtime(floor((unix_timestamp(current_timestamp)-0)/60)*60+0))')
->toBePgsql('to_timestamp(floor((extract(epoch from current_timestamp)-0)/60)*60+0)')
->toBeSqlite('(datetime((strftime(\'%s\',current_timestamp)-0)/60*60+0,\'unixepoch\'))')
->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',current_timestamp)-0)/60*60+0,\'1970-01-01\')');

it('can bin the time of a column with an origin')
->expect(new TimestampBin('val', DateInterval::createFromDateString('90 seconds'), DateTime::createFromFormat('Y-m-d H:i:s', '2022-02-02 22:22:22')))
->toBeExecutable(['val timestamp'])
->toBeExecutable(function (Blueprint $table) {
$table->timestamp('val');
})
->toBeMysql('(from_unixtime(floor((unix_timestamp(`val`)-1643840542)/90)*90+1643840542))')
->toBePgsql('to_timestamp(floor((extract(epoch from "val")-1643840542)/90)*90+1643840542)')
->toBeSqlite('(datetime((strftime(\'%s\',"val")-1643840542)/90*90+1643840542,\'unixepoch\'))')
->toBeSqlsrv('dateadd(s,(datediff(s,\'1970-01-01\',[val])-1643840542)/90*90+1643840542,\'1970-01-01\')');

it('can bin the time of an expression with an origin')
->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')))
->toBeExecutable(['val timestamp'])
->toBeExecutable(function (Blueprint $table) {
$table->timestamp('val');
})
->toBeMysql('(from_unixtime(floor((unix_timestamp(current_timestamp)-946684800)/3600)*3600+946684800))')
->toBePgsql('to_timestamp(floor((extract(epoch from current_timestamp)-946684800)/3600)*3600+946684800)')
->toBeSqlite('(datetime((strftime(\'%s\',current_timestamp)-946684800)/3600*3600+946684800,\'unixepoch\'))')
Expand Down
5 changes: 4 additions & 1 deletion tests/Language/AliasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Tpetry\QueryExpressions\Language\Alias;

it('can alias a column')
->expect(new Alias('val', 'value'))
->toBeExecutable(['val int'])
->toBeExecutable(function (Blueprint $table) {
$table->integer('val');
})
->toBeMysql('`val` as `value`')
->toBePgsql('"val" as "value"')
->toBeSqlite('"val" as "value"')
Expand Down
Loading
Loading