-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for lateral joins
- Loading branch information
Showing
3 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\MySql; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @requires extension pdo_mysql | ||
* @requires OS Linux|Darwin | ||
*/ | ||
class JoinLateralTest extends MySqlTestCase | ||
{ | ||
protected function afterRefreshingDatabase() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('name'); | ||
}); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('title'); | ||
$table->integer('rating'); | ||
$table->unsignedBigInteger('user_id'); | ||
}); | ||
} | ||
|
||
protected function destroyDatabaseMigrations() | ||
{ | ||
Schema::drop('posts'); | ||
Schema::drop('users'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::table('users')->insert([ | ||
['name' => Str::random()], | ||
['name' => Str::random()], | ||
]); | ||
|
||
DB::table('posts')->insert([ | ||
['title' => Str::random(), 'rating' => 1, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 3, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 7, 'user_id' => 1], | ||
]); | ||
} | ||
|
||
public function testJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNull($userWithoutPosts); | ||
} | ||
|
||
public function testLeftJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithoutPosts); | ||
$this->assertNull($userWithoutPosts->best_post_title); | ||
$this->assertNull($userWithoutPosts->best_post_rating); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
tests/Integration/Database/Postgres/JoinLateralTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\Postgres; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @requires extension pdo_pgsql | ||
* @requires OS Linux|Darwin | ||
*/ | ||
class JoinLateralTest extends PostgresTestCase | ||
{ | ||
protected function afterRefreshingDatabase() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('name'); | ||
}); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('title'); | ||
$table->integer('rating'); | ||
$table->unsignedBigInteger('user_id'); | ||
}); | ||
} | ||
|
||
protected function destroyDatabaseMigrations() | ||
{ | ||
Schema::drop('posts'); | ||
Schema::drop('users'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::table('users')->insert([ | ||
['name' => Str::random()], | ||
['name' => Str::random()], | ||
]); | ||
|
||
DB::table('posts')->insert([ | ||
['title' => Str::random(), 'rating' => 1, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 3, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 7, 'user_id' => 1], | ||
]); | ||
} | ||
|
||
public function testJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNull($userWithoutPosts); | ||
} | ||
|
||
public function testLeftJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithoutPosts); | ||
$this->assertNull($userWithoutPosts->best_post_title); | ||
$this->assertNull($userWithoutPosts->best_post_rating); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\SqlServer; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class JoinLateralTest extends SqlServerTestCase | ||
{ | ||
protected function afterRefreshingDatabase() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('name'); | ||
}); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id('id'); | ||
$table->string('title'); | ||
$table->integer('rating'); | ||
$table->unsignedBigInteger('user_id'); | ||
}); | ||
} | ||
|
||
protected function destroyDatabaseMigrations() | ||
{ | ||
Schema::drop('posts'); | ||
Schema::drop('users'); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::table('users')->insert([ | ||
['name' => Str::random()], | ||
['name' => Str::random()], | ||
]); | ||
|
||
DB::table('posts')->insert([ | ||
['title' => Str::random(), 'rating' => 1, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 3, 'user_id' => 1], | ||
['title' => Str::random(), 'rating' => 7, 'user_id' => 1], | ||
]); | ||
} | ||
|
||
public function testJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, (int) $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->joinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNull($userWithoutPosts); | ||
} | ||
|
||
public function testLeftJoinLateral() | ||
{ | ||
$subquery = DB::table('posts') | ||
->select('title as best_post_title', 'rating as best_post_rating') | ||
->whereColumn('user_id', 'users.id') | ||
->orderBy('rating', 'desc') | ||
->limit(1); | ||
|
||
$userWithPosts = DB::table('users') | ||
->where('id', 1) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithPosts); | ||
$this->assertEquals(7, (int) $userWithPosts->best_post_rating); | ||
|
||
$userWithoutPosts = DB::table('users') | ||
->where('id', 2) | ||
->leftJoinLateral($subquery, 'best_post') | ||
->first(); | ||
|
||
$this->assertNotNull($userWithoutPosts); | ||
$this->assertNull($userWithoutPosts->best_post_title); | ||
$this->assertNull($userWithoutPosts->best_post_rating); | ||
} | ||
} |