Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreign key constraints for all buildids #1657

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions app/cdash/tests/case/CDash/MultipleSubprojectsEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MultipleSubprojectsEmailTest extends CDashUseCaseTestCase
private static $tz;
private static $database;

private int $projectid = -1;
private static int $projectid = -1;

/** @var Database|PHPUnit_Framework_MockObject_MockObject $db */
private $db;
Expand Down Expand Up @@ -69,6 +69,13 @@ public static function tearDownAfterClass() : void
date_default_timezone_set(self::$tz);
Database::setInstance(Database::class, self::$database);

DB::delete('DELETE FROM project WHERE id = ?', [self::$projectid]);

// Clean up all of the placeholder builds we created
for ($i = 0; $i < 10; $i++) {
DB::table('build')->where('name', '=', 'TestBuild' . $i)->delete();
}

parent::tearDownAfterClass();
}

Expand Down Expand Up @@ -103,16 +110,20 @@ public function setUp() : void
$this->createApplication();
config(['app.url' => 'http://open.cdash.org']);

$this->projectid = DB::table('project')->insertGetId([
'name' => 'TestProject1',
]);
}

public function tearDown(): void
{
DB::delete('DELETE FROM project WHERE id = ?', [$this->projectid]);

parent::tearDown();
if (self::$projectid === -1) {
self::$projectid = DB::table('project')->insertGetId([
'name' => 'TestProject1',
]);

// A hack to make sure builds exist and can be referenced so we don't violate our foreign key constraints
for ($i = 0; $i < 10; $i++) {
DB::table('build')->insertOrIgnore([
'projectid' => self::$projectid,
'name' => 'TestBuild' . $i,
'uuid' => 'TestBuild' . $i,
]);
}
}
}

/**
Expand Down Expand Up @@ -167,7 +178,7 @@ private function getNotifications(array $subscribers)
public function testMultipleSubprojectsTestSubmission()
{
$this->useCase = UseCase::createBuilder($this, UseCase::TEST)
->setProjectId($this->projectid)
->setProjectId(self::$projectid)
->createSite([
'BuildName' => 'CTestTest-Linux-c++-Subprojects',
'BuildStamp' => '20160728-1932-Experimental',
Expand Down
15 changes: 15 additions & 0 deletions app/cdash/tests/case/CDash/TestUseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,28 @@ public function setUp(): void
'name' => 'TestProject1',
]);

// A hack to make sure builds exist and can be referenced so we don't violate our foreign key constraints
for ($i = 0; $i < 10; $i++) {
DB::table('build')->insert([
'id' => $i,
'projectid' => $this->projectid,
'name' => 'TestBuild' . $i,
'uuid' => 'TestBuild' . $i,
]);
}

parent::setUp();
}

public function tearDown(): void
{
DB::delete('DELETE FROM project WHERE id = ?', [$this->projectid]);

// Clean up all of the placeholder builds we created
for ($i = 0; $i < 10; $i++) {
DB::table('build')->where('name', '=', 'TestBuild' . $i)->delete();
}

parent::tearDown();
}

Expand Down
32 changes: 12 additions & 20 deletions app/cdash/tests/test_uniquediffs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// After including cdash_test_case.php, subsequent require_once calls are
// relative to the top of the CDash source tree
//
use CDash\Model\Build;
use Illuminate\Support\Facades\DB;

require_once dirname(__FILE__) . '/cdash_test_case.php';


Expand All @@ -19,27 +22,14 @@ public function __construct()

public function testUniqueDiffs()
{
// Find the highest buildid created so far and add one to it.
// This should give us a buildid that doesn't exist yet.
$pdo = \CDash\Database::getInstance();
$stmt = $pdo->query('SELECT id FROM build ORDER BY id DESC LIMIT 1');
$row = $stmt->fetch();
$buildid = $row['id'];
$buildid++;

// Verify that this buildid does not exist yet.
$safe_build = false;
while (!$safe_build) {
$stmt = $pdo->prepare('SELECT id FROM build WHERE id=?');
$stmt->execute([$buildid]);
$row = $stmt->fetch();
if (!$row) {
$this->BuildId = $buildid;
$safe_build = true;
} else {
$buildid++;
}
}

$build = new Build();
$build->ProjectId = 1;
$build->Name = 'uniquediffs_test_build';
$this->assertTrue($build->AddBuild());
$this->BuildId = $build->Id;


// Perform multiple INSERTs that violate the unique constraint
// and verify that only one row is recorded.
Expand Down Expand Up @@ -92,6 +82,8 @@ public function testUniqueDiffs()
$stmt->execute([$this->BuildId]);
$stmt = $pdo->prepare('DELETE FROM testdiff WHERE buildid=?');
$stmt->execute([$this->BuildId]);

DB::delete('DELETE FROM build WHERE id = ?', [$build->Id]);
}

public function testUniqueDiffsUpgrade()
Expand Down
Loading