[tests] put tests inside transactions

This commit is contained in:
Shish 2024-01-15 22:50:43 +00:00
parent de022564b3
commit 5d120cd0c8
5 changed files with 18 additions and 23 deletions

View file

@ -293,6 +293,8 @@ function create_tables(Database $db): void
if ($db->is_transaction_open()) {
$db->commit();
}
// Ensure that we end this code in a transaction (for testing)
$db->begin_transaction();
} catch (\PDOException $e) {
throw new InstallerException(
"PDO Error:",

View file

@ -17,8 +17,6 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) {
global $_tracer, $database;
$_tracer->begin(get_called_class());
$database->begin_transaction();
self::create_user(self::$admin_name);
self::create_user(self::$user_name);
parent::setUpBeforeClass();
}
@ -37,6 +35,7 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) {
}
// Set up a clean environment for each test
$database->execute("SAVEPOINT test_start");
self::log_out();
foreach ($database->get_col("SELECT id FROM images") as $image_id) {
send_event(new ImageDeletionEvent(Image::by_id((int)$image_id), true));
@ -48,7 +47,8 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) {
public function tearDown(): void
{
global $_tracer;
global $_tracer, $database;
$database->execute("ROLLBACK TO test_start");
$_tracer->end(); # test
$_tracer->end(); # $this->getName()
}
@ -63,16 +63,6 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) {
$_tracer->flush("data/test-trace.json");
}
protected static function create_user(string $name): void
{
if (is_null(User::by_name($name))) {
$userPage = new UserPage();
$userPage->onUserCreation(new UserCreationEvent($name, $name, $name, "$name@$name.com", false));
// @phpstan-ignore-next-line - ???
assert(!is_null(User::by_name($name)), "Creation of user $name failed");
}
}
private static function check_args(?array $args): array
{
if (!$args) {

View file

@ -30,11 +30,9 @@ class MimeSystem extends Extension
// adjustment needs to be made to the mime types.
if ($this->get_version(self::VERSION) < 1) {
if ($database->is_transaction_open()) {
// Each of these commands could hit a lot of data, combining
// them into one big transaction would not be a good idea.
$database->commit();
}
// Each of these commands could hit a lot of data, combining
// them into one big transaction would not be a good idea.
$database->commit();
$database->set_timeout(null); // These updates can take a little bit
$extensions = $database->get_col_iterable("SELECT DISTINCT ext FROM images");
@ -55,6 +53,7 @@ class MimeSystem extends Extension
}
$this->set_version(self::VERSION, 1);
$database->begin_transaction();
}
}

View file

@ -193,11 +193,9 @@ class Upgrade extends Extension
if ($this->get_version("db_version") < 21) {
log_info("upgrade", "Setting predictable media values for known file types");
if ($database->is_transaction_open()) {
// Each of these commands could hit a lot of data, combining
// them into one big transaction would not be a good idea.
$database->commit();
}
// Each of these commands could hit a lot of data, combining
// them into one big transaction would not be a good idea.
$database->commit();
$database->execute("UPDATE images SET lossless = :t, video = :t WHERE ext IN ('swf')", ["t" => true]);
$database->execute("UPDATE images SET lossless = :f, video = :f, audio = :t WHERE ext IN ('mp3')", ["t" => true, "f" => false]);
$database->execute("UPDATE images SET lossless = :f, video = :f, audio = :f WHERE ext IN ('jpg','jpeg')", ["f" => false]);
@ -206,6 +204,7 @@ class Upgrade extends Extension
$database->execute("UPDATE images SET audio = :f WHERE ext IN ('webp')", ["f" => false]);
$database->execute("UPDATE images SET lossless = :f, video = :t WHERE ext IN ('flv','mp4','m4v','ogv','webm')", ["t" => true, "f" => false]);
$this->set_version("db_version", 21);
$database->begin_transaction();
}
}

View file

@ -37,4 +37,9 @@ $config->set_bool("nice_urls", true);
send_event(new DatabaseUpgradeEvent());
send_event(new InitExtEvent());
$user = User::by_id($config->get_int("anon_id", 0));
$userPage = new UserPage();
$userPage->onUserCreation(new UserCreationEvent("demo", "demo", "demo", "demo@demo.com", false));
$userPage->onUserCreation(new UserCreationEvent("test", "test", "test", "test@test.com", false));
$database->commit();
$_tracer->end();