-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for createOrFirst/firstOrCreate
Added model crud tests
- Loading branch information
1 parent
7ff0c22
commit c302c4c
Showing
10 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Database\Seeder; | ||
use LaravelFreelancerNL\Aranguent\Auth\User; | ||
|
||
class UsersSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database Seeds. | ||
* | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
$users = '[ | ||
{ | ||
"_key":"LyannaStark", | ||
"username":"Lyanna Stark", | ||
"email":"[email protected]" | ||
} | ||
]'; | ||
|
||
$users = json_decode($users, JSON_OBJECT_AS_ARRAY); | ||
|
||
foreach ($users as $user) { | ||
User::insertOrIgnore($user); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelFreelancerNL\Aranguent\Exceptions; | ||
|
||
class UniqueConstraintViolationException extends QueryException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
use LaravelFreelancerNL\Aranguent\Testing\DatabaseTransactions; | ||
use TestSetup\Models\Character; | ||
use TestSetup\Models\User; | ||
use LaravelFreelancerNL\Aranguent\Exceptions\QueryException; | ||
|
||
uses( | ||
DatabaseTransactions::class, | ||
|
@@ -18,21 +20,49 @@ | |
expect($fresh->age)->toBe(($initialAge + 1)); | ||
}); | ||
|
||
test('update or create', function () { | ||
$character = Character::first(); | ||
$initialAge = $character->age; | ||
$newAge = ($initialAge + 1); | ||
test('updateOrCreate', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$character->updateOrCreate(['age' => $initialAge], ['age' => $newAge]); | ||
$user1 = User::updateOrCreate($userData); | ||
|
||
$fresh = $character->fresh(); | ||
$user2 = User::where("email", "[email protected]")->first(); | ||
|
||
expect($fresh->age)->toBe($newAge); | ||
expect($user1->_id)->toBe($user2->_id); | ||
}); | ||
|
||
test('upsert', function () { | ||
$this->skipTestOnArangoVersionsBefore('3.7'); | ||
test('updateOrCreate runs twice', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
$user1 = User::updateOrCreate($userData); | ||
|
||
$user2 = User::updateOrCreate($userData); | ||
|
||
expect($user1->_id)->toBe($user2->_id); | ||
}); | ||
|
||
test('updateOrCreate throws error on unique key if data is different', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
$user1 = User::updateOrCreate($userData); | ||
|
||
$userData = [ | ||
"username" => "Duncan the Tall", | ||
"email" => "[email protected]", | ||
]; | ||
$user2 = User::updateOrCreate($userData); | ||
|
||
expect($user1->_id)->toBe($user2->_id); | ||
})->throws(QueryException::class); | ||
|
||
|
||
test('upsert', function () { | ||
Character::upsert( | ||
[ | ||
[ | ||
|
@@ -63,6 +93,28 @@ | |
expect($jaime->alive)->toBeFalse(); | ||
}); | ||
|
||
test('upsert runs twice', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
User::upsert([$userData], ['email'], ['username']); | ||
|
||
$userData = [ | ||
"username" => "Duncan the Tall", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$result = User::upsert([$userData], ['email'], ['username']); | ||
|
||
$user = User::where("email", "[email protected]")->first(); | ||
|
||
expect($result)->toBe(1); | ||
expect($user->username)->toBe("Duncan the Tall"); | ||
}); | ||
|
||
|
||
test('delete model', function () { | ||
$character = Character::first(); | ||
|
||
|
@@ -131,3 +183,63 @@ | |
expect($ned->id)->toEqual('NedStarkIsDead'); | ||
expect($ned->_id)->toEqual('characters/NedStarkIsDead'); | ||
}); | ||
|
||
test('firstOrCreate', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$user = User::firstOrCreate($userData); | ||
|
||
$result = DB::table('users') | ||
->where('email', '[email protected]') | ||
->first(); | ||
|
||
expect($result->_id)->toBe($user->_id); | ||
}); | ||
|
||
test('firstOrCreate runs twice without error', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$user1 = User::firstOrCreate($userData); | ||
$user2 = User::firstOrCreate($userData); | ||
|
||
expect($user1->_id)->toBe($user2->_id); | ||
}); | ||
|
||
test('firstOrCreate throws error if data is different', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$user1 = User::firstOrCreate($userData); | ||
|
||
$userData = [ | ||
"username" => "Duncan the Tall", | ||
"email" => "[email protected]", | ||
]; | ||
$user2 = User::firstOrCreate($userData); | ||
|
||
expect($user1->_id)->toBe($user2->_id); | ||
})->throws(QueryException::class); | ||
|
||
|
||
test('createOrFirst', function () { | ||
$userData = [ | ||
"username" => "Dunk", | ||
"email" => "[email protected]", | ||
]; | ||
|
||
$user = User::createOrFirst($userData); | ||
|
||
$result = DB::table('users') | ||
->where('email', '[email protected]') | ||
->first(); | ||
|
||
expect($result->_id)->toBe($user->_id); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
expect($result)->toEqual(1); | ||
}); | ||
|
||
test('insert or ignore inserts data', function () { | ||
test('insertOrIgnore inserts data', function () { | ||
$characterData = [ | ||
"_key" => "LyannaStark", | ||
"name" => "Lyanna", | ||
|
@@ -58,7 +58,7 @@ | |
expect($result->count())->toBe(1); | ||
}); | ||
|
||
test('insert or ignore doesnt error on duplicates', function () { | ||
test('insertOrIgnore doesnt error on duplicates', function () { | ||
$characterData = [ | ||
"_key" => "LyannaStark", | ||
"name" => "Lyanna", | ||
|
@@ -78,6 +78,31 @@ | |
expect($result->count())->toBe(1); | ||
}); | ||
|
||
test('insertOrIgnore with unique index on non-primary fields', function () { | ||
$userData = [ | ||
"_key" => "LyannaStark", | ||
"username" => "Lyanna Stark", | ||
"email" => "[email protected]", | ||
]; | ||
DB::table('users')->insertOrIgnore($userData); | ||
|
||
$result = DB::table('users') | ||
->first(); | ||
|
||
expect($result->_id)->toBe('users/LyannaStark'); | ||
|
||
$userData = [ | ||
"username" => "Lya Stark", | ||
"email" => "[email protected]", | ||
]; | ||
DB::table('users')->insertOrIgnore($userData); | ||
|
||
$result = DB::table('users') | ||
->first(); | ||
|
||
expect($result->_id)->toBe('users/LyannaStark'); | ||
}); | ||
|
||
test('insert embedded empty array', function () { | ||
$characterData = [ | ||
"_key" => "LyannaStark", | ||
|
@@ -100,14 +125,14 @@ | |
expect($result->first()->tags)->toBeEmpty(); | ||
}); | ||
|
||
test('insert using', function () { | ||
test('insertUsing', function () { | ||
// Let's give Baelish a user, what could possibly go wrong? | ||
$baelishes = DB::table('characters') | ||
->where('surname', 'Baelish'); | ||
|
||
DB::table('users')->insertUsing(['name', 'surname'], $baelishes); | ||
|
||
$user = DB::table('users')->first(); | ||
$user = DB::table('users')->where("surname", "=", "Baelish")->first(); | ||
|
||
expect($user->surname)->toBe('Baelish'); | ||
}); |