From 7c66a9dbb6347cf5fd8c4b80a1af275fb7aececf Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Sun, 22 Sep 2024 16:47:10 -0400 Subject: [PATCH] Merge tests --- tests/Fixtures/ModelStubWithQueryable.php | 10 +++ tests/ModelQueryableTest.php | 65 ++++++++++++++ tests/{Feature => }/ModelTest.php | 100 +++++++++++++++++++++- tests/Pest.php | 9 +- tests/QueryTest.php | 38 ++++++++ tests/Unit/ModelTest.php | 94 -------------------- 6 files changed, 219 insertions(+), 97 deletions(-) create mode 100644 tests/Fixtures/ModelStubWithQueryable.php create mode 100644 tests/ModelQueryableTest.php rename tests/{Feature => }/ModelTest.php (58%) create mode 100644 tests/QueryTest.php delete mode 100644 tests/Unit/ModelTest.php diff --git a/tests/Fixtures/ModelStubWithQueryable.php b/tests/Fixtures/ModelStubWithQueryable.php new file mode 100644 index 00000000..b20140ae --- /dev/null +++ b/tests/Fixtures/ModelStubWithQueryable.php @@ -0,0 +1,10 @@ + 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); +}); diff --git a/tests/Feature/ModelTest.php b/tests/ModelTest.php similarity index 58% rename from tests/Feature/ModelTest.php rename to tests/ModelTest.php index 633c626c..df187a3f 100644 --- a/tests/Feature/ModelTest.php +++ b/tests/ModelTest.php @@ -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(); @@ -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(); @@ -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 () { diff --git a/tests/Pest.php b/tests/Pest.php index 8364ba30..db01b231 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ in('Feature'); +uses(TestCase::class)->in(__DIR__); /* |-------------------------------------------------------------------------- @@ -31,3 +33,8 @@ function repository(): Repository { return app(Repository::class); } + +function query(Model $model): Query +{ + return new Query($model, repository()); +} diff --git a/tests/QueryTest.php b/tests/QueryTest.php new file mode 100644 index 00000000..df1d51f7 --- /dev/null +++ b/tests/QueryTest.php @@ -0,0 +1,38 @@ +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:*'); +}); diff --git a/tests/Unit/ModelTest.php b/tests/Unit/ModelTest.php deleted file mode 100644 index d7c79bdd..00000000 --- a/tests/Unit/ModelTest.php +++ /dev/null @@ -1,94 +0,0 @@ - '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)->getPrefix())->toBe('model_stubs'); -}); - -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'); -});