Skip to content

Commit

Permalink
feat: introduce tests to cover the new jsonapi functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolGoose committed Dec 6, 2024
1 parent cc074ba commit 1caa985
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
120 changes: 120 additions & 0 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
expect($query)->toEqual($expected);
});

it('can fetch specific string columns jsonApi Format', function () {
config(['query-builder.convert_field_names_to_snake_case' => true]);
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);

$query = createQueryFromFieldRequest('firstName,id')
->allowedFields(['firstName', 'id'])
->toSql();

$expected = TestModel::query()
->select("{$this->modelTableName}.first_name", "{$this->modelTableName}.id")
->toSql();

expect($query)->toEqual($expected);
});

it('wont fetch a specific array column if its not allowed', function () {
$query = createQueryFromFieldRequest(['test_models' => 'random-column'])->toSql();

Expand Down Expand Up @@ -200,6 +215,81 @@
$this->assertQueryLogContains('select `name` from `related_models`');
});

it('can fetch only requested string columns from an included model jsonApi format', function () {
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);
RelatedModel::create([
'test_model_id' => $this->model->id,
'name' => 'related',
]);

$request = new Request([
'fields' => 'id,relatedModels.name',
'include' => ['relatedModels'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedFields('relatedModels.name', 'id')
->allowedIncludes('relatedModels');

DB::enableQueryLog();

$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$this->assertQueryLogContains('select `name` from `related_models`');
});

it('can fetch only requested string columns from an included model jsonApi format with field conversion', function () {
config(['query-builder.convert_field_names_to_snake_case' => true]);
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);

RelatedModel::create([
'test_model_id' => $this->model->id,
'name' => 'related',
]);

$request = new Request([
'fields' => 'id,relatedModels.fullName',
'include' => ['relatedModels'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedFields('relatedModels.fullName', 'id')
->allowedIncludes('relatedModels');

DB::enableQueryLog();

$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$this->assertQueryLogContains('select `full_name` from `related_models`');
});

it('can fetch only requested string columns from an included model through pivot jsonApi format', function () {
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);

$this->model->relatedThroughPivotModels()->create([
'id' => $this->model->id + 1,
'name' => 'Test',
]);

$request = new Request([
'fields' => 'id,relatedThroughPivotModels.name',
'include' => ['relatedThroughPivotModels'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedFields('relatedThroughPivotModels.name', 'id')
->allowedIncludes('relatedThroughPivotModels');

DB::enableQueryLog();

$queryBuilder->first()->relatedThroughPivotModels;

$this->assertQueryLogContains('select `test_models`.`id` from `test_models`');
$this->assertQueryLogContains('select `name`, `pivot_models`.`test_model_id` as `pivot_test_model_id`, `pivot_models`.`related_through_pivot_model_id` as `pivot_related_through_pivot_model_id` from `related_through_pivot_models`');
});

it('can fetch requested array columns from included models up to two levels deep', function () {
RelatedModel::create([
'test_model_id' => $this->model->id,
Expand All @@ -224,6 +314,36 @@
expect($result->relatedModels->first()->testModel->toArray())->toEqual(['id' => $this->model->id]);
});

it('can fetch requested array columns from included models up to two levels deep jsonApi mapper', function () {
config(['query-builder.convert_field_names_to_snake_case' => true]);
config(['query-builder.convert_relation_table_name_strategy' => 'camelCase']);

$relatedModel = RelatedModel::create([
'test_model_id' => $this->model->id,
'name' => 'related',
]);

$relatedModel->nestedRelatedModels()->create([
'name' => 'nested related',
]);

$request = new Request([
'fields' => 'id,name,relatedModels.id,relatedModels.name,nestedRelatedModels.id,nestedRelatedModels.name',
'include' => ['nestedRelatedModels', 'relatedModels'],
]);


$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedFields('id', 'name', 'relatedModels.id', 'relatedModels.name', 'nestedRelatedModels.id', 'nestedRelatedModels.name')
->allowedIncludes('relatedModels', 'nestedRelatedModels');

DB::enableQueryLog();
$queryBuilder->first();

$this->assertQueryLogContains('select `test_models`.`id`, `test_models`.`name` from `test_models`');
$this->assertQueryLogContains('select `nested_related_models`.`id`, `nested_related_models`.`name`, `related_models`.`test_model_id` as `laravel_through_key` from `nested_related_models`');
});

it('can fetch requested string columns from included models up to two levels deep', function () {
RelatedModel::create([
'test_model_id' => $this->model->id,
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function setUpDatabase(Application $app)
$table->increments('id');
$table->timestamps();
$table->string('name')->nullable();
$table->string('full_name')->nullable();
$table->double('salary')->nullable();
$table->boolean('is_visible')->default(true);
});
Expand All @@ -62,6 +63,7 @@ protected function setUpDatabase(Application $app)
$table->increments('id');
$table->integer('test_model_id');
$table->string('name');
$table->string('full_name')->nullable();
});

$app['db']->connection()->getSchemaBuilder()->create('nested_related_models', function (Blueprint $table) {
Expand Down Expand Up @@ -92,7 +94,7 @@ protected function setUpDatabase(Application $app)
protected function getPackageProviders($app)
{
return [
RayServiceProvider::class,
// RayServiceProvider::class,
QueryBuilderServiceProvider::class,
];
}
Expand Down
13 changes: 13 additions & 0 deletions tests/TestClasses/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Carbon;

Expand All @@ -27,6 +28,18 @@ public function relatedModel(): BelongsTo
return $this->belongsTo(RelatedModel::class);
}

public function nestedRelatedModels(): HasManyThrough
{
return $this->hasManyThrough(
NestedRelatedModel::class, // Target model
RelatedModel::class, // Intermediate model
'test_model_id', // Foreign key on RelatedModel
'related_model_id', // Foreign key on NestedRelatedModel
'id', // Local key on TestModel
'id' // Local key on RelatedModel
);
}

public function otherRelatedModels(): HasMany
{
return $this->hasMany(RelatedModel::class);
Expand Down

0 comments on commit 1caa985

Please sign in to comment.