Skip to content

Commit

Permalink
Merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Sep 22, 2024
1 parent d90ef51 commit 7c66a9d
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 97 deletions.
10 changes: 10 additions & 0 deletions tests/Fixtures/ModelStubWithQueryable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DirectoryTree\ActiveRedis\Tests\Fixtures;

use DirectoryTree\ActiveRedis\Model;

class ModelStubWithQueryable extends Model
{
protected array $queryable = ['user_id', 'company_id'];
}
65 changes: 65 additions & 0 deletions tests/ModelQueryableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithQueryable;
use Illuminate\Support\Facades\Redis;

beforeEach(fn () => Redis::flushall());

it('generates model hash with queryable attributes when present', function () {
$hash = (new ModelStubWithQueryable([
'user_id' => 1,
'company_id' => 2,
]))->getHashKey();

expect($hash)->toBe('model_stub_with_queryables:id:null:company_id:2:user_id:1');
});

it('generates model hash with null queryable attributes when missing', function () {
$hash = (new ModelStubWithQueryable)->getHashKey();

expect($hash)->toBe('model_stub_with_queryables:id:null:company_id:null:user_id:null');
});

it('creates model with null queryable values when missing', function () {
$model = ModelStubWithQueryable::create();

expect($model->getHashKey())->toBe(
"model_stub_with_queryables:id:{$model->getKey()}:company_id:null:user_id:null"
);

expect(ModelStubWithQueryable::first()->is($model))->toBeTrue();
});

it('creates model with queryable values when present', function () {
$model = ModelStubWithQueryable::create([
'user_id' => 1,
'company_id' => 2,
]);

expect($model->getHashKey())->toBe(
"model_stub_with_queryables:id:{$model->getKey()}:company_id:2:user_id:1"
);

expect(ModelStubWithQueryable::first()->is($model))->toBeTrue();
});

it('can query for queryable attributes', function () {
ModelStubWithQueryable::create([
'user_id' => 1,
'company_id' => 2,
]);

ModelStubWithQueryable::create([
'user_id' => 1,
'company_id' => 2,
]);

expect(ModelStubWithQueryable::exists())->toBeTrue();

$models = ModelStubWithQueryable::query()
->where('user_id', 1)
->where('company_id', 2)
->get();

expect($models->count())->toBe(2);
});
100 changes: 98 additions & 2 deletions tests/Feature/ModelTest.php → tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,106 @@
use DirectoryTree\ActiveRedis\ModelNotFoundException;
use DirectoryTree\ActiveRedis\Query;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey;
use Illuminate\Support\Facades\Redis;

beforeEach(fn () => Redis::flushall());

it('can be instantiated with attributes', function () {
$model = new ModelStub([
'name' => 'John',
'company' => 'Acme',
]);

expect($model->name)->toBe('John');
expect($model->company)->toBe('Acme');
});

it('can be filled with attributes', function () {
$model = (new ModelStub)->fill([
'name' => 'John',
'company' => 'Acme',
]);

expect($model->name)->toBe('John');
expect($model->company)->toBe('Acme');
});

it('can set attributes', function () {
$model = new ModelStub;

$model->name = 'John';

$model->setAttribute('company', 'Acme');

expect($model->name)->toBe('John');
expect($model->company)->toBe('Acme');
});

it('can set all attributes', function () {
$model = new ModelStub;

$model->setAttributes([
'name' => 'John',
'company' => 'Acme',
]);

expect($model->name)->toBe('John');
expect($model->company)->toBe('Acme');
});

it('can get dirty attributes', function () {
$model = new ModelStub;

$model->setAttributes([
'name' => 'John',
'company' => 'Acme',
]);

expect($model->isDirty())->toBeTrue();
expect($model->isDirty('name'))->toBeTrue();
expect($model->isDirty(['name', 'company']))->toBeTrue();
expect($model->isDirty(['name', 'invalid']))->toBeTrue();

expect($model->isDirty('invalid'))->toBeFalse();
expect($model->isDirty(['foo', 'bar']))->toBeFalse();
});

it('has date attributes', function () {
$model = new ModelStub;

expect($model->getDates())->toBe([
'created_at',
'updated_at',
]);
});

it('does not have dates by default', function () {
$model = new ModelStub;

expect($model->created_at)->toBeNull();
expect($model->updated_at)->toBeNull();
});

it('generates prefix off of class name', function () {
expect((new ModelStub)->getHashPrefix())->toBe('model_stubs');
});

it('generates hash from unsaved model null id', function () {
expect((new ModelStub)->getHashKey())->toBe('model_stubs:id:null');
expect((new ModelStubWithCustomKey)->getHashKey())->toBe('model_stub_with_custom_keys:custom:null');
});

it('generates base hash from model key', function () {
expect((new ModelStub)->getBaseHash())->toBe('model_stubs:id');
expect((new ModelStubWithCustomKey)->getBaseHash())->toBe('model_stub_with_custom_keys:custom');
});

it('generates original hash from unsaved model', function () {
expect((new ModelStub)->getBaseHash())->toBe('model_stubs:id');
expect((new ModelStubWithCustomKey)->getBaseHash())->toBe('model_stub_with_custom_keys:custom');
});

it('can be created without attributes', function () {
$model = ModelStub::create();

Expand All @@ -21,7 +117,7 @@
'id', 'created_at', 'updated_at',
]);

$hash = $model->getModelHash();
$hash = $model->getHashKey();

expect($hash)->toBe("model_stubs:id:{$model->id}");
expect(repository()->exists($hash))->toBeTrue();
Expand Down Expand Up @@ -100,7 +196,7 @@
$model->delete();

expect($model->exists)->toBeFalse();
expect(repository()->exists($model->getModelHash()))->toBeFalse();
expect(repository()->exists($model->getHashKey()))->toBeFalse();
});

it('can be found by its key', function () {
Expand Down
9 changes: 8 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use DirectoryTree\ActiveRedis\Model;
use DirectoryTree\ActiveRedis\Query;
use DirectoryTree\ActiveRedis\Repository;
use DirectoryTree\ActiveRedis\Tests\TestCase;

Expand All @@ -14,7 +16,7 @@
|
*/

uses(TestCase::class)->in('Feature');
uses(TestCase::class)->in(__DIR__);

/*
|--------------------------------------------------------------------------
Expand All @@ -31,3 +33,8 @@ function repository(): Repository
{
return app(Repository::class);
}

function query(Model $model): Query
{
return new Query($model, repository());
}
38 changes: 38 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use DirectoryTree\ActiveRedis\AttributeNotQueryableException;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithQueryable;

it('generates query using where clauses', function () {
$query = query(new ModelStubWithQueryable);

expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:*');

$query->where('user_id', '1');

expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:1');

$query->where('company_id', '2');

expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:2:user_id:1');
});

it('throws exception with where clause for attribute that is not queryable', function () {
$query = query(new ModelStub);

$query->where('foo', '1');
})->throws(AttributeNotQueryableException::class, 'The attribute [foo] is not queryable on the model [DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub].');

it('generates query using model prefix', function () {
expect(query(new ModelStub)->getQuery())->toBe('model_stubs:id:*');
});

it('generates query using custom key', function () {
expect(query(new ModelStubWithCustomKey)->getQuery())->toBe('model_stub_with_custom_keys:custom:*');
});

it('generates query using queryable', function () {
expect(query(new ModelStubWithQueryable)->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:*');
});
94 changes: 0 additions & 94 deletions tests/Unit/ModelTest.php

This file was deleted.

0 comments on commit 7c66a9d

Please sign in to comment.