mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-03-02 11:31:13 +01:00
Refactor some unit tests
This commit is contained in:
parent
a92e04f605
commit
2e67011f45
@ -81,6 +81,18 @@ public final class SchemaReader {
|
|||||||
return queries;
|
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
|
* 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
|
* @return the filtered list of statements
|
||||||
*/
|
*/
|
||||||
public static List<String> filterStatements(List<String> statements, List<String> currentTables) {
|
public static List<String> filterStatements(List<String> statements, List<String> currentTables) {
|
||||||
return statements.stream().filter(statement -> {
|
return statements.stream()
|
||||||
Matcher table = CREATE_TABLE_PATTERN.matcher(statement);
|
.filter(statement -> !currentTables.contains(tableFromStatement(statement)))
|
||||||
if (table.matches()) {
|
.collect(Collectors.toList());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,15 +25,18 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.storage.implementation.sql;
|
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.junit.jupiter.api.Test;
|
||||||
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class SchemaReaderTest {
|
public class SchemaReaderTest {
|
||||||
|
|
||||||
@ -111,6 +114,27 @@ public class SchemaReaderTest {
|
|||||||
), readStatements("postgresql"));
|
), 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
|
@Test
|
||||||
public void testFilter() throws IOException {
|
public void testFilter() throws IOException {
|
||||||
StatementProcessor processor = s -> s.replace("{prefix}", "luckperms_");
|
StatementProcessor processor = s -> s.replace("{prefix}", "luckperms_");
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.standalone;
|
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.Group;
|
||||||
import me.lucko.luckperms.common.model.User;
|
import me.lucko.luckperms.common.model.User;
|
||||||
import me.lucko.luckperms.common.node.types.Inheritance;
|
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 net.luckperms.api.event.log.LogNotifyEvent;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
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.nio.file.Path;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.standalone;
|
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.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import me.lucko.luckperms.common.commands.misc.ExportCommand;
|
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 me.lucko.luckperms.standalone.utils.TestPluginProvider;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
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.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -25,9 +25,9 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.standalone.utils;
|
package me.lucko.luckperms.standalone.utils;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
|
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
|
||||||
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin;
|
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin;
|
||||||
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
|
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
Loading…
Reference in New Issue
Block a user