Get database unit tests back into passing shape

This commit is contained in:
Dane Everitt 2020-06-24 20:47:52 -07:00
parent 756a21ff04
commit a5d9faf6b2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 20 additions and 73 deletions

View file

@ -10,6 +10,7 @@ use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;
class DeployServerDatabaseServiceTest extends TestCase
{
@ -51,16 +52,9 @@ class DeployServerDatabaseServiceTest extends TestCase
*/
public function testNonRandomFoundHost($limit, $count)
{
config()->set('pterodactyl.client_features.databases.allow_random', false);
$server = factory(Server::class)->make(['database_limit' => $limit]);
$model = factory(Database::class)->make();
$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn($count);
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
@ -68,7 +62,7 @@ class DeployServerDatabaseServiceTest extends TestCase
$this->managementService->shouldReceive('create')
->once()
->with($server->id, [
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
@ -83,25 +77,20 @@ class DeployServerDatabaseServiceTest extends TestCase
/**
* Test that an exception is thrown if in non-random mode and no host is found.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
*/
public function testNonRandomNoHost()
{
config()->set('pterodactyl.client_features.databases.allow_random', false);
$this->expectException(NoSuitableDatabaseHostException::class);
$server = factory(Server::class)->make(['database_limit' => 1]);
$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
$this->getService()->handle($server, []);
}
@ -113,11 +102,6 @@ class DeployServerDatabaseServiceTest extends TestCase
$server = factory(Server::class)->make(['database_limit' => 1]);
$model = factory(Database::class)->make();
$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
@ -129,7 +113,7 @@ class DeployServerDatabaseServiceTest extends TestCase
$this->managementService->shouldReceive('create')
->once()
->with($server->id, [
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
@ -144,60 +128,22 @@ class DeployServerDatabaseServiceTest extends TestCase
/**
* Test that an exception is thrown when no host is found and random is allowed.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
*/
public function testRandomNoHost()
{
$this->expectException(NoSuitableDatabaseHostException::class);
$server = factory(Server::class)->make(['database_limit' => 1]);
$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
$this->databaseHostRepository->expects('setColumns->findWhere')
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());
$this->databaseHostRepository->shouldReceive('setColumns->all')
->once()
->andReturn(collect());
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());
$this->getService()->handle($server, []);
}
/**
* Test that a server over the database limit throws an exception.
*
* @dataProvider databaseExceedingLimitDataProvider
* @expectedException \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
*/
public function testServerOverDatabaseLimit($limit, $count)
{
$server = factory(Server::class)->make(['database_limit' => $limit]);
$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn($count);
$this->getService()->handle($server, []);
}
/**
* Test that an exception is thrown if the feature is not enabled.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
*/
public function testFeatureNotEnabled()
{
config()->set('pterodactyl.client_features.databases.enabled', false);
$this->getService()->handle(factory(Server::class)->make(), []);
}
/**
* Provide limits and current database counts for testing.
*