Skip to content

Commit

Permalink
test: add postgres date format test
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed May 9, 2024
1 parent ce4ea18 commit 567bc3f
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Database\Postgres;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -15,32 +16,33 @@ class DatabasePostgresConnectionTest extends PostgresTestCase
{
protected function afterRefreshingDatabase()
{
if (! Schema::hasTable('json_table')) {
Schema::create('json_table', function (Blueprint $table) {
if (! Schema::hasTable('pgsql_table')) {
Schema::create('pgsql_table', function (Blueprint $table) {
$table->json('json_col')->nullable();
$table->timestamptz('timestamptz', precision: 6)->nullable();
});
}
}

protected function destroyDatabaseMigrations()
{
Schema::drop('json_table');
Schema::drop('pgsql_table');
}

#[DataProvider('jsonWhereNullDataProvider')]
public function testJsonWhereNull($expected, $key, array $value = ['value' => 123])
{
DB::table('json_table')->insert(['json_col' => json_encode($value)]);
DB::table('pgsql_table')->insert(['json_col' => json_encode($value)]);

$this->assertSame($expected, DB::table('json_table')->whereNull("json_col->$key")->exists());
$this->assertSame($expected, DB::table('pgsql_table')->whereNull("json_col->$key")->exists());
}

#[DataProvider('jsonWhereNullDataProvider')]
public function testJsonWhereNotNull($expected, $key, array $value = ['value' => 123])
{
DB::table('json_table')->insert(['json_col' => json_encode($value)]);
DB::table('pgsql_table')->insert(['json_col' => json_encode($value)]);

$this->assertSame(! $expected, DB::table('json_table')->whereNotNull("json_col->$key")->exists());
$this->assertSame(! $expected, DB::table('pgsql_table')->whereNotNull("json_col->$key")->exists());
}

public static function jsonWhereNullDataProvider()
Expand Down Expand Up @@ -71,18 +73,18 @@ public static function jsonWhereNullDataProvider()

public function testJsonPathUpdate()
{
DB::table('json_table')->insert([
DB::table('pgsql_table')->insert([
['json_col' => '{"foo":["bar"]}'],
['json_col' => '{"foo":["baz"]}'],
['json_col' => '{"foo":[["array"]]}'],
]);

$updatedCount = DB::table('json_table')->where('json_col->foo[0]', 'baz')->update([
$updatedCount = DB::table('pgsql_table')->where('json_col->foo[0]', 'baz')->update([
'json_col->foo[0]' => 'updated',
]);
$this->assertSame(1, $updatedCount);

$updatedCount = DB::table('json_table')->where('json_col->foo[0][0]', 'array')->update([
$updatedCount = DB::table('pgsql_table')->where('json_col->foo[0][0]', 'array')->update([
'json_col->foo[0][0]' => 'updated',
]);
$this->assertSame(1, $updatedCount);
Expand All @@ -91,15 +93,27 @@ public function testJsonPathUpdate()
#[DataProvider('jsonContainsKeyDataProvider')]
public function testWhereJsonContainsKey($count, $column)
{
DB::table('json_table')->insert([
DB::table('pgsql_table')->insert([
['json_col' => '{"foo":{"bar":["baz"]}}'],
['json_col' => '{"foo":{"bar":false}}'],
['json_col' => '{"foo":{}}'],
['json_col' => '{"foo":[{"bar":"bar"},{"baz":"baz"}]}'],
['json_col' => '{"bar":null}'],
]);

$this->assertSame($count, DB::table('json_table')->whereJsonContainsKey($column)->count());
$this->assertSame($count, DB::table('pgsql_table')->whereJsonContainsKey($column)->count());
}

public function testDateTimeInterfacesAreNotTruncated()
{
$datetime = Carbon::parse('2021-01-01 12:34:56.123456', 'America/New_York');

DB::table('pgsql_table')->insert([['timestamptz' => $datetime]]);

$this->assertSame(
'2021-01-01 17:34:56.123456+00',
DB::table('pgsql_table')->pluck('timestamptz')->first(),
);
}

public static function jsonContainsKeyDataProvider()
Expand Down

0 comments on commit 567bc3f

Please sign in to comment.