Attempt to catch #956 with tests

This commit is contained in:
Rsl1122 2019-03-08 10:00:49 +02:00
parent 8a13f1d792
commit 4215939deb
4 changed files with 74 additions and 5 deletions

View File

@ -76,4 +76,14 @@ public class BaseUser {
public int hashCode() {
return Objects.hash(uuid, name, registered, timesKicked);
}
@Override
public String toString() {
return "BaseUser{" +
uuid +
", '" + name + '\'' +
", +" + registered +
", kick:" + timesKicked +
'}';
}
}

View File

@ -30,6 +30,7 @@ import com.djrapitops.plan.db.access.queries.objects.WorldTimesQueries;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.info.server.Server;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -65,7 +66,7 @@ public class ServerContainerQuery implements Query<ServerContainer> {
container.putRawData(ServerKeys.SERVER_UUID, serverUUID);
container.putRawData(ServerKeys.NAME, serverInfo.get().getName());
container.putCachingSupplier(ServerKeys.PLAYERS, () -> db.query(new ServerPlayerContainersQuery(serverUUID)));
container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getUnsafe(ServerKeys.PLAYERS).size());
container.putSupplier(ServerKeys.PLAYER_COUNT, () -> container.getValue(ServerKeys.PLAYERS).map(Collection::size).orElse(0));
container.putCachingSupplier(ServerKeys.TPS, () -> db.query(TPSQueries.fetchTPSDataOfServer(serverUUID)));
container.putCachingSupplier(ServerKeys.PING, () -> PlayersMutator.forContainer(container).pings());

View File

@ -32,7 +32,9 @@ import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Executable;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.queries.*;
import com.djrapitops.plan.db.access.queries.containers.AllPlayerContainersQuery;
import com.djrapitops.plan.db.access.queries.containers.ContainerFetchQueries;
import com.djrapitops.plan.db.access.queries.containers.ServerPlayerContainersQuery;
import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction;
import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction;
@ -60,10 +62,7 @@ import org.junit.rules.TemporaryFolder;
import org.junit.rules.Timeout;
import rules.ComponentMocker;
import rules.PluginComponentMocker;
import utilities.FieldFetcher;
import utilities.OptionalAssert;
import utilities.RandomData;
import utilities.TestConstants;
import utilities.*;
import java.io.File;
import java.lang.management.ManagementFactory;
@ -1020,4 +1019,54 @@ public abstract class CommonDBTest {
Map<UUID, Integer> result = db.query(ServerAggregateQueries.serverUserCounts());
assertEquals(expected, result);
}
@Test
public void baseUserQueryDoesNotReturnDuplicatePlayers() {
db.executeTransaction(TestData.storeServers());
db.executeTransaction(TestData.storePlayerOneData());
db.executeTransaction(TestData.storePlayerTwoData());
Collection<BaseUser> expected = Arrays.asList(TestData.getPlayerBaseUser(), TestData.getPlayer2BaseUser());
Collection<BaseUser> result = db.query(BaseUserQueries.fetchServerBaseUsers(TestConstants.SERVER_UUID));
assertEquals(expected, result);
result = db.query(BaseUserQueries.fetchServerBaseUsers(TestConstants.SERVER_TWO_UUID));
assertEquals(expected, result);
}
@Test
public void serverPlayerContainersQueryDoesNotReturnDuplicatePlayers() {
db.executeTransaction(TestData.storeServers());
db.executeTransaction(TestData.storePlayerOneData());
db.executeTransaction(TestData.storePlayerTwoData());
List<UUID> expected = Arrays.asList(playerUUID, player2UUID);
Collections.sort(expected);
Collection<UUID> result = db.query(new ServerPlayerContainersQuery(TestConstants.SERVER_UUID))
.stream().map(player -> player.getUnsafe(PlayerKeys.UUID))
.sorted()
.collect(Collectors.toList());
assertEquals(expected, result);
}
@Test
public void allPlayerContainersQueryDoesNotReturnDuplicatePlayers() {
db.executeTransaction(TestData.storeServers());
db.executeTransaction(TestData.storePlayerOneData());
db.executeTransaction(TestData.storePlayerTwoData());
List<UUID> expected = Arrays.asList(playerUUID, player2UUID);
Collections.sort(expected);
Collection<UUID> result = db.query(new AllPlayerContainersQuery())
.stream().map(player -> player.getUnsafe(PlayerKeys.UUID))
.sorted()
.collect(Collectors.toList());
assertEquals(expected, result);
}
}

View File

@ -16,6 +16,7 @@
*/
package utilities;
import com.djrapitops.plan.data.container.BaseUser;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.container.PlayerKill;
import com.djrapitops.plan.data.container.Session;
@ -174,4 +175,12 @@ public class TestData {
public static List<GeoInfo> getPlayerGeoInfo() {
return playerGeoInfo;
}
public static BaseUser getPlayerBaseUser() {
return new BaseUser(playerUUID, playerName, playerFirstJoin, 0);
}
public static BaseUser getPlayer2BaseUser() {
return new BaseUser(player2UUID, player2Name, playerFirstJoin, 0);
}
}