Skip to content

Commit

Permalink
To prevent the creation of a new record in the database, correct the …
Browse files Browse the repository at this point in the history
…hash key. (#15)

* fix hash key creation

* fix model static fn insert

* Add unit tests to ensure prevention of duplicate records during model updates and for filtering queried results.

---------

Co-authored-by: Châu Đình Châu Ái <36218616+alvin0@users.noreply.github.com>
  • Loading branch information
agrism and alvin0 committed Nov 3, 2023
1 parent 89ee3ee commit e8277bd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/Builder.php
Expand Up @@ -360,8 +360,7 @@ public function compileHashByFields(array $attributes)

foreach ($listKey as $key) {
$attributeValue = $attributes[$key] ?? '*';
$attributeValue = is_string($attributeValue) ? $attributeValue : json_encode($attributeValue);
$stringKey .= $key . ':' . $attributeValue . ':';
$stringKey .= $key . ':' . ($attributeValue === '*' ? '*' : $this->model->castAttributeBeforeSave($key, $attributeValue)) . ':';
}

return $this->model->getTable() . ":" . rtrim($stringKey, ':');
Expand Down
4 changes: 2 additions & 2 deletions src/Model.php
Expand Up @@ -535,7 +535,7 @@ protected function performUpdate(Builder $build)
*
* @return mixed The
*/
protected function castAttributeBeforeSave($key, $value) {
public function castAttributeBeforeSave($key, $value) {
// Cast the attribute if necessary
$value = $this->hasCast($key) ? $this->castAttribute($key, $value) : $value;

Expand Down Expand Up @@ -672,7 +672,7 @@ public static function insert(array $dataInsert, Redis $hasTransaction = null)
}

$inserts[$key] = collect($attributes)->map(function ($item, $key) use ($model) {
return (string) $this->castAttributeBeforeSave($key, $item);
return (string) $model->castAttributeBeforeSave($key, $item);
})->toArray();
} else {
return false;
Expand Down
104 changes: 96 additions & 8 deletions tests/ModelTest.php
Expand Up @@ -9,7 +9,6 @@

it('a user can be created without id', function ($userInput, $expect) {
$user = User::create($userInput);

expect($user->name)->toEqual($expect['name']);
expect($user->email)->toEqual($expect['email']);
})->with([
Expand All @@ -28,6 +27,7 @@
]);

it('can insert multiple users without id', function ($data) {

User::insert($data);

$users = User::get();
Expand Down Expand Up @@ -118,7 +118,7 @@ function () {
],
]);

it('can retrieve all users', function ($setup) {
it('can retrieve all users', function () {
expect(User::all()->count())->toBeGreaterThan(0);
})->with([
[
Expand All @@ -140,10 +140,9 @@ function () {
],
]);

it('can retrieve a single user by ID', function ($createData, $expect) {
$createData();

it('can retrieve a single user by ID', function ($expect) {
$user = User::find(1);

expect($user->name)->toEqual($expect['name']);
expect($user->email)->toEqual($expect['email']);
})->with([
Expand All @@ -157,9 +156,7 @@ function () {
],
]);

it('can retrieve users matching a given criteria', function ($createData, $expect) {
$createData();

it('can retrieve users matching a given criteria', function ($expect) {
$users = User::where('name', 'Nuno*')->get();
expect($users->count())->toBeGreaterThan(0);

Expand Down Expand Up @@ -282,3 +279,94 @@ function () {
return $data;
}
]);

it('can retrieve users by email', function () {
$users = User::query()->where('email', 'nuno_naduro@example.com')->get();

expect(2)->toEqual($users->count());
})->with([
[
fn() => User::insert([
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.com'],
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.com'],
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.net'],
]),
],
[
fn() => User::insert([
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.net'],
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.com'],
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.com'],
]),
]
]);

it('can create user assigning model property values', function ($userData, $expected) {
$user = User::query()->where('email', 'nuno_naduro@example.com')->first();

expect($expected['name'])->toEqual($user->name);
expect($expected['email'])->toEqual($user->email);
})->with([
[
function () {
$user = new User;
$user->name = 'Nuno Maduro';
$user->email = 'nuno_naduro@example.com';
$user->save();
},
['name' => 'Nuno Maduro', 'email' => 'nuno_naduro@example.com'],
]
]);

it('can update user subKey without duplication', function () {
expect(1)->toEqual(User::query()->count());
})->with([
[
function () {
$user = new User;
$user->name = 'Nuno Maduro';
$user->email = 'nuno_naduro@example.com';
$user->save();
$user->email = 'nuno_naduro@example.net';
$user->save();
},
],
[
function () {
$user = new User;
$user->name = 'Nuno Maduro';
$user->email = 'nuno_naduro@example.com';
$user->save();
$user->name = 'Nuno';
$user->email = 'nuno_naduro@example.net';
$user->save();
},
]
]);

it('can update user primaryKey without duplication', function () {
expect(1)->toEqual(User::query()->count());
})->with([
[
function () {
$user = new User;
$user->id = '1';
$user->name = 'Nuno Maduro';
$user->email = 'nuno_naduro@example.com';
$user->save();
$user->id = 2;
$user->save();
},
],
[
function () {
$user = new User;
$user->id = 1;
$user->name = 'Nuno Maduro';
$user->email = 'nuno_naduro@example.com';
$user->save();
$user->id = 2;
$user->save();
},
]
]);
5 changes: 5 additions & 0 deletions tests/Models/User.php
Expand Up @@ -3,6 +3,11 @@

use Alvin0\RedisModel\Model;

/**
* @property $id
* @property $email
* @property $name
*/
class User extends Model
{
/**
Expand Down

0 comments on commit e8277bd

Please sign in to comment.