Auto-create account in both MySQL and PG

This commit is contained in:
P0nk
2024-09-26 07:59:27 +02:00
parent bf9c02bc16
commit 647e67f6e8
13 changed files with 158 additions and 49 deletions

View File

@@ -9,6 +9,7 @@ import database.PgDatabaseConfig;
import database.PgDatabaseConnection;
import database.migration.FlywayRunner;
import database.monsterbook.MonsterCardRepository;
import org.jdbi.v3.core.Handle;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -120,10 +121,12 @@ class CharacterSaverTest {
SELECT level
FROM chr
WHERE id = :id""";
return pgConnection.getHandle().createQuery(sql)
.bind("id", chrId)
.mapTo(Integer.class)
.one();
try (Handle handle = pgConnection.getHandle()) {
return handle.createQuery(sql)
.bind("id", chrId)
.mapTo(Integer.class)
.one();
}
}
}

View File

@@ -2,6 +2,8 @@ package testutil;
import client.CharacterStats;
import database.PgDatabaseConnection;
import database.account.Account;
import database.account.AccountRepository;
import database.character.CharacterRepository;
import org.jdbi.v3.core.Handle;
@@ -10,24 +12,20 @@ import java.time.LocalDate;
public class TestData {
public static GeneratedIds create(PgDatabaseConnection connection) {
int accountId = insertAccount(connection);
try (Handle handle = connection.getHandle()) {
int accountId = insertAccount(handle);
int chrId = insertChr(handle, accountId);
return new GeneratedIds(accountId, chrId);
}
}
private static int insertAccount(Handle handle) {
String sql = """
INSERT INTO account (name, password, birthday)
VALUES (:name, :password, :birthday)""";
return handle.createUpdate(sql)
.bind("name", "accountname")
.bind("password", "accountpassword")
.bind("birthday", LocalDate.of(2005, 5, 11))
.executeAndReturnGeneratedKeys()
.mapTo(Integer.class)
.one();
private static int insertAccount(PgDatabaseConnection connection) {
Account account = Account.builder()
.name("accountname")
.password("accountpassword")
.birthdate(LocalDate.now())
.build();
return new AccountRepository(connection).insert(account);
}
private static int insertChr(Handle handle, int accountId) {