Refactor some unit tests

This commit is contained in:
Luck 2024-12-04 11:25:06 +00:00
parent a92e04f605
commit 2e67011f45
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
5 changed files with 45 additions and 17 deletions

View File

@ -81,6 +81,18 @@ public final class SchemaReader {
return queries;
}
public static String tableFromStatement(String statement) {
Matcher table = CREATE_TABLE_PATTERN.matcher(statement);
if (table.matches()) {
return table.group(1).toLowerCase(Locale.ROOT);
}
Matcher index = CREATE_INDEX_PATTERN.matcher(statement);
if (index.matches()) {
return index.group(1).toLowerCase(Locale.ROOT);
}
throw new IllegalArgumentException("Unknown statement type: " + statement);
}
/**
* Filters which statements should be executed based on the current list of tables in the database
*
@ -89,16 +101,8 @@ public final class SchemaReader {
* @return the filtered list of statements
*/
public static List<String> filterStatements(List<String> statements, List<String> currentTables) {
return statements.stream().filter(statement -> {
Matcher table = CREATE_TABLE_PATTERN.matcher(statement);
if (table.matches()) {
return !currentTables.contains(table.group(1).toLowerCase(Locale.ROOT));
}
Matcher index = CREATE_INDEX_PATTERN.matcher(statement);
if (index.matches()) {
return !currentTables.contains(index.group(1).toLowerCase(Locale.ROOT));
}
throw new IllegalArgumentException("Unknown statement type: " + statement);
}).collect(Collectors.toList());
return statements.stream()
.filter(statement -> !currentTables.contains(tableFromStatement(statement)))
.collect(Collectors.toList());
}
}

View File

@ -25,15 +25,18 @@
package me.lucko.luckperms.common.storage.implementation.sql;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SchemaReaderTest {
@ -111,6 +114,27 @@ public class SchemaReaderTest {
), readStatements("postgresql"));
}
@Test
public void testTableFromStatement() throws IOException {
Set<String> allowedTables = ImmutableSet.of(
"luckperms_user_permissions",
"luckperms_group_permissions",
"luckperms_players",
"luckperms_groups",
"luckperms_actions",
"luckperms_tracks"
);
for (String type : new String[]{"h2", "mariadb", "mysql", "postgresql", "sqlite"}) {
List<String> tables = readStatements(type).stream()
.map(s -> s.replace("{prefix}", "luckperms_"))
.map(SchemaReader::tableFromStatement)
.collect(Collectors.toList());
assertTrue(allowedTables.containsAll(tables));
}
}
@Test
public void testFilter() throws IOException {
StatementProcessor processor = s -> s.replace("{prefix}", "luckperms_");

View File

@ -25,6 +25,8 @@
package me.lucko.luckperms.standalone;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.types.Inheritance;
@ -36,8 +38,6 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.luckperms.api.event.log.LogNotifyEvent;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
import org.testcontainers.shaded.com.google.common.collect.ImmutableSet;
import java.nio.file.Path;
import java.util.HashMap;

View File

@ -25,6 +25,8 @@
package me.lucko.luckperms.standalone;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import me.lucko.luckperms.common.commands.misc.ExportCommand;
@ -38,8 +40,6 @@ import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
import org.testcontainers.shaded.com.google.common.collect.ImmutableSet;
import java.io.BufferedReader;
import java.io.IOException;

View File

@ -25,9 +25,9 @@
package me.lucko.luckperms.standalone.utils;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
import java.nio.file.Path;
import java.util.HashMap;