|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SingleStore\Laravel\Tests\Hybrid; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use SingleStore\Laravel\Schema\Blueprint; |
| 7 | +use SingleStore\Laravel\Tests\BaseTest; |
| 8 | + |
| 9 | +class UnionTest extends BaseTest |
| 10 | +{ |
| 11 | + use HybridTestHelpers; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + if ($this->runHybridIntegrations()) { |
| 18 | + $this->createTable(function (Blueprint $table) { |
| 19 | + $table->id(); |
| 20 | + }); |
| 21 | + |
| 22 | + DB::table('test')->insert([ |
| 23 | + ['id' => 1], |
| 24 | + ['id' => 2], |
| 25 | + ['id' => 3], |
| 26 | + ['id' => 4], |
| 27 | + ['id' => 100], |
| 28 | + ]); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + function union() { |
| 34 | + if (! $this->runHybridIntegrations()) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $first = DB::table('test')->where('id', '<', 3); |
| 39 | + $second = DB::table('test')->where('id', '>', 5); |
| 40 | + $res = $first->union($second)->get(); |
| 41 | + |
| 42 | + $indexes = array_map(function ($value): int { |
| 43 | + return $value->id; |
| 44 | + }, $res->toArray()); |
| 45 | + sort($indexes); |
| 46 | + |
| 47 | + $this->assertEquals([1, 2, 100], $indexes); |
| 48 | + } |
| 49 | + |
| 50 | + /** @test */ |
| 51 | + function unionAll() { |
| 52 | + if (! $this->runHybridIntegrations()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $first = DB::table('test')->where('id', '<', 4); |
| 57 | + $second = DB::table('test')->where('id', '>', 2); |
| 58 | + $res = $first->unionAll($second)->get(); |
| 59 | + |
| 60 | + $indexes = array_map(function ($value): int { |
| 61 | + return $value->id; |
| 62 | + }, $res->toArray()); |
| 63 | + sort($indexes); |
| 64 | + |
| 65 | + $this->assertEquals([1, 2, 3, 3, 4, 100], $indexes); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + function unionWithOrderByLimitAndOffset() { |
| 70 | + if (! $this->runHybridIntegrations()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + $first = DB::table('test')->where('id', '<', 3); |
| 75 | + $second = DB::table('test')->where('id', '>', 5); |
| 76 | + $res = $first->union($second)->orderBy('id')->limit(1)->offset(1)->get(); |
| 77 | + |
| 78 | + $indexes = array_map(function ($value): int { |
| 79 | + return $value->id; |
| 80 | + }, $res->toArray()); |
| 81 | + |
| 82 | + $this->assertEquals([2], $indexes); |
| 83 | + } |
| 84 | + |
| 85 | + /** @test */ |
| 86 | + function unionWithOrderBy() { |
| 87 | + if (! $this->runHybridIntegrations()) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $first = DB::table('test')->where('id', '<', 3); |
| 92 | + $second = DB::table('test')->where('id', '>', 5); |
| 93 | + $res = $first->union($second)->orderBy('id')->get(); |
| 94 | + |
| 95 | + $indexes = array_map(function ($value): int { |
| 96 | + return $value->id; |
| 97 | + }, $res->toArray()); |
| 98 | + |
| 99 | + $this->assertEquals([1, 2, 100], $indexes); |
| 100 | + } |
| 101 | + |
| 102 | + /** @test */ |
| 103 | + function unionWithLimit() { |
| 104 | + if (! $this->runHybridIntegrations()) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + $first = DB::table('test')->where('id', '<', 3); |
| 109 | + $second = DB::table('test')->where('id', '>', 5); |
| 110 | + $res = $first->union($second)->limit(2)->get(); |
| 111 | + |
| 112 | + $this->assertCount(2, $res); |
| 113 | + } |
| 114 | + |
| 115 | + /** @test */ |
| 116 | + function unionWithOffset() { |
| 117 | + if (! $this->runHybridIntegrations()) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + $first = DB::table('test')->where('id', '<', 3); |
| 122 | + $second = DB::table('test')->where('id', '>', 5); |
| 123 | + $res = $first->union($second)->offset(1)->get(); |
| 124 | + |
| 125 | + $this->assertCount(2, $res); |
| 126 | + } |
| 127 | + |
| 128 | + /** @test */ |
| 129 | + function unionWithInnerOffset() { |
| 130 | + if (! $this->runHybridIntegrations()) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + $first = DB::table('test')->where('id', '<', 3)->offset(1)->orderBy('id'); |
| 135 | + $second = DB::table('test')->where('id', '>', 5); |
| 136 | + $res = $first->union($second)->get(); |
| 137 | + |
| 138 | + $indexes = array_map(function ($value): int { |
| 139 | + return $value->id; |
| 140 | + }, $res->toArray()); |
| 141 | + sort($indexes); |
| 142 | + |
| 143 | + $this->assertEquals([2, 100], $indexes); |
| 144 | + } |
| 145 | +} |
0 commit comments