From ed17ebd4024adafb342cef636cadd800bc98a14d Mon Sep 17 00:00:00 2001
From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com>
Date: Sun, 9 Jan 2022 12:37:29 +0200
Subject: [PATCH] Fix top category queries
- Moved group by before order by
- Fixed off by one error in the offset
Affects issues:
- Fixed #2206
---
.../queries/analysis/TopListQueries.java | 8 +--
.../plan/storage/database/MySQLTest.java | 17 ++---
.../plan/storage/database/SQLiteTest.java | 16 +----
.../queries/QueriesTestAggregate.java | 35 +++++++++++
.../queries/analysis/TopListQueriesTest.java | 63 +++++++++++++++++++
5 files changed, 109 insertions(+), 30 deletions(-)
create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/QueriesTestAggregate.java
create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java
diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java
index 92b1a0a9a..85da37bc2 100644
--- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java
+++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueries.java
@@ -43,8 +43,8 @@ public class TopListQueries {
WHERE + SessionsTable.SERVER_UUID + "=?" +
AND + SessionsTable.SESSION_START + ">?" +
AND + SessionsTable.SESSION_END + "" +
- ORDER_BY + "playtime DESC" +
GROUP_BY + "name" +
+ ORDER_BY + "playtime DESC" +
LIMIT + "10" +
OFFSET + "?";
@@ -54,7 +54,7 @@ public class TopListQueries {
statement.setString(1, serverUUID.toString());
statement.setLong(2, after);
statement.setLong(3, before);
- statement.setInt(4, n);
+ statement.setInt(4, n - 1);
}
@Override
@@ -76,8 +76,8 @@ public class TopListQueries {
WHERE + SessionsTable.SERVER_UUID + "=?" +
AND + SessionsTable.SESSION_START + ">?" +
AND + SessionsTable.SESSION_END + "" +
- ORDER_BY + "active_playtime DESC" +
GROUP_BY + "name" +
+ ORDER_BY + "active_playtime DESC" +
LIMIT + "10" +
OFFSET + "?";
@@ -87,7 +87,7 @@ public class TopListQueries {
statement.setString(1, serverUUID.toString());
statement.setLong(2, after);
statement.setLong(3, before);
- statement.setInt(4, n);
+ statement.setInt(4, n - 1);
}
@Override
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/MySQLTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/MySQLTest.java
index 0780ab476..817031cd3 100644
--- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/MySQLTest.java
+++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/MySQLTest.java
@@ -23,7 +23,8 @@ import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.config.PlanConfig;
-import com.djrapitops.plan.storage.database.queries.*;
+import com.djrapitops.plan.storage.database.queries.ExtensionsDatabaseTest;
+import com.djrapitops.plan.storage.database.queries.QueriesTestAggregate;
import com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction;
import com.djrapitops.plan.storage.database.transactions.init.CreateTablesTransaction;
@@ -59,18 +60,7 @@ import static org.mockito.Mockito.when;
* @see utilities.CIProperties for assumed MySQL setup.
*/
@ExtendWith(MockitoExtension.class)
-class MySQLTest implements DatabaseTest,
- DatabaseBackupTest,
- ExtensionsDatabaseTest,
- ActivityIndexQueriesTest,
- GeolocationQueriesTest,
- NicknameQueriesTest,
- PingQueriesTest,
- SessionQueriesTest,
- ServerQueriesTest,
- TPSQueriesTest,
- UserInfoQueriesTest,
- WebUserQueriesTest {
+class MySQLTest implements DatabaseTest, QueriesTestAggregate {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
@@ -113,6 +103,7 @@ class MySQLTest implements DatabaseTest,
db().executeTransaction(new StoreServerInformationTransaction(new Server(serverUUID(), TestConstants.SERVER_NAME, "")));
assertEquals(serverUUID(), ((SQLDB) db()).getServerUUIDSupplier().get());
}
+
@AfterAll
static void disableSystem() {
if (database != null) database.close();
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/SQLiteTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/SQLiteTest.java
index f335e0329..af062ca21 100644
--- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/SQLiteTest.java
+++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/SQLiteTest.java
@@ -23,7 +23,8 @@ import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.config.PlanConfig;
-import com.djrapitops.plan.storage.database.queries.*;
+import com.djrapitops.plan.storage.database.queries.ExtensionsDatabaseTest;
+import com.djrapitops.plan.storage.database.queries.QueriesTestAggregate;
import com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction;
import com.djrapitops.plan.storage.database.transactions.init.CreateTablesTransaction;
@@ -52,18 +53,7 @@ import static org.mockito.Mockito.when;
* @see ExtensionsDatabaseTest
*/
@ExtendWith(MockitoExtension.class)
-public class SQLiteTest implements DatabaseTest,
- DatabaseBackupTest,
- ExtensionsDatabaseTest,
- ActivityIndexQueriesTest,
- GeolocationQueriesTest,
- NicknameQueriesTest,
- PingQueriesTest,
- SessionQueriesTest,
- ServerQueriesTest,
- TPSQueriesTest,
- UserInfoQueriesTest,
- WebUserQueriesTest {
+public class SQLiteTest implements DatabaseTest, QueriesTestAggregate {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/QueriesTestAggregate.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/QueriesTestAggregate.java
new file mode 100644
index 000000000..e94d9185f
--- /dev/null
+++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/QueriesTestAggregate.java
@@ -0,0 +1,35 @@
+/*
+ * This file is part of Player Analytics (Plan).
+ *
+ * Plan is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License v3 as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Plan is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Plan. If not, see .
+ */
+package com.djrapitops.plan.storage.database.queries;
+
+import com.djrapitops.plan.storage.database.queries.analysis.TopListQueriesTest;
+
+public interface QueriesTestAggregate extends
+ ActivityIndexQueriesTest,
+ DatabaseBackupTest,
+ ExtensionsDatabaseTest,
+ GeolocationQueriesTest,
+ NicknameQueriesTest,
+ PingQueriesTest,
+ ServerQueriesTest,
+ SessionQueriesTest,
+ TopListQueriesTest,
+ TPSQueriesTest,
+ UserInfoQueriesTest,
+ WebUserQueriesTest {
+ /* Collects all query tests together so its easier to implement database tests */
+}
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java
new file mode 100644
index 000000000..ee49818db
--- /dev/null
+++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/analysis/TopListQueriesTest.java
@@ -0,0 +1,63 @@
+/*
+ * This file is part of Player Analytics (Plan).
+ *
+ * Plan is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License v3 as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Plan is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Plan. If not, see .
+ */
+package com.djrapitops.plan.storage.database.queries.analysis;
+
+import com.djrapitops.plan.gathering.domain.FinishedSession;
+import com.djrapitops.plan.storage.database.DatabaseTestPreparer;
+import com.djrapitops.plan.storage.database.queries.DataStoreQueries;
+import com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction;
+import com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction;
+import org.junit.jupiter.api.Test;
+import utilities.RandomData;
+import utilities.TestConstants;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public interface TopListQueriesTest extends DatabaseTestPreparer {
+
+ private void storeSessionForTopListQueries() {
+ db().executeTransaction(new WorldNameStoreTransaction(serverUUID(), worlds[0]));
+ db().executeTransaction(new WorldNameStoreTransaction(serverUUID(), worlds[1]));
+ db().executeTransaction(new PlayerServerRegisterTransaction(playerUUID, RandomData::randomTime,
+ TestConstants.PLAYER_ONE_NAME, serverUUID(), TestConstants.GET_PLAYER_HOSTNAME));
+ db().executeTransaction(new PlayerServerRegisterTransaction(player2UUID, RandomData::randomTime,
+ TestConstants.PLAYER_TWO_NAME, serverUUID(), TestConstants.GET_PLAYER_HOSTNAME));
+ FinishedSession session = RandomData.randomSession(serverUUID(), worlds, playerUUID, player2UUID);
+ execute(DataStoreQueries.storeSession(session));
+ }
+
+ @Test
+ default void topActivePlaytimeListQueryReturnsSinglePlayer() {
+ storeSessionForTopListQueries();
+
+ String expected = TestConstants.PLAYER_ONE_NAME;
+ String result = db().query(TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(serverUUID(), 1, 0, System.currentTimeMillis()))
+ .orElseThrow(AssertionError::new);
+ assertEquals(expected, result);
+ }
+
+ @Test
+ default void topPlaytimeListQueryReturnsSinglePlayer() {
+ storeSessionForTopListQueries();
+
+ String expected = TestConstants.PLAYER_ONE_NAME;
+ String result = db().query(TopListQueries.fetchNthTop10ActivePlaytimePlayerOn(serverUUID(), 1, 0, System.currentTimeMillis()))
+ .orElseThrow(AssertionError::new);
+ assertEquals(expected, result);
+ }
+
+}
\ No newline at end of file