Skip to content

Commit 427c8ad

Browse files
committed
tests for XP subtraction
1 parent 8c66fb1 commit 427c8ad

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

src/main/java/net/discordjug/javabot/data/h2db/DbHelper.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ private static boolean shouldInitSchema(String jdbcUrl) {
9999
return shouldInitSchema;
100100
}
101101

102-
private static void initializeSchema(HikariDataSource dataSource) throws IOException, SQLException {
102+
/**
103+
* Initializes the schema of the database by running all SQL statements from the schema.sql script.
104+
* @param dataSource the {@link DataSource} to connect to the DB
105+
* @throws IOException if an error happened while loading the schema.sql
106+
* @throws SQLException if any SQL error happened
107+
*/
108+
public static void initializeSchema(DataSource dataSource) throws IOException, SQLException {
103109
try (InputStream is = DbHelper.class.getClassLoader().getResourceAsStream("database/schema.sql")) {
104110
if (is == null) throw new IOException("Could not load schema.sql.");
105111
List<String> queries = Arrays.stream(new String(is.readAllBytes()).split(";"))
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package net.discordjug.javabot.systems.help;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.sql.SQLException;
7+
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.boot.jdbc.DataSourceBuilder;
12+
import org.springframework.jdbc.core.JdbcTemplate;
13+
14+
import com.zaxxer.hikari.HikariDataSource;
15+
16+
import net.discordjug.javabot.data.h2db.DbHelper;
17+
import net.discordjug.javabot.systems.help.dao.HelpAccountRepository;
18+
import net.discordjug.javabot.systems.help.model.HelpAccount;
19+
20+
/**
21+
* Tests functionality of automated experience subtraction.
22+
*/
23+
public class HelpExperienceSubtractionTest {
24+
25+
private HikariDataSource dataSource;
26+
private HelpAccountRepository repo;
27+
28+
@BeforeEach
29+
void setUp() throws IOException, SQLException {
30+
dataSource = DataSourceBuilder.create()
31+
.type(HikariDataSource.class)
32+
.url("jdbc:h2:mem:test")
33+
.username("test")
34+
.password("")
35+
.build();
36+
37+
DbHelper.initializeSchema(dataSource);
38+
39+
JdbcTemplate template = new JdbcTemplate(dataSource);
40+
repo = new HelpAccountRepository(template);
41+
}
42+
43+
@AfterEach
44+
void cleanUp() {
45+
dataSource.close();
46+
}
47+
48+
/**
49+
* If a user has less XP than the minimum experience subtraction, the user should lose all XP.
50+
*/
51+
@Test
52+
void testUserHasLessThanMinimum() {
53+
repo.insert(new HelpAccount(1, 1));
54+
repo.removeExperienceFromAllAccounts(50, 2, 10);
55+
assertEquals(0, repo.getByUserId(1).get().getExperience());
56+
}
57+
58+
/**
59+
* If the XP to subtract is less than the minimum, the minimum XP should be subtracted.
60+
*/
61+
@Test
62+
void testSubtractMinimum() {
63+
repo.insert(new HelpAccount(1, 6));//6XP
64+
//would remove 50% i.e. 3XP
65+
//the minimum is 4XP hence it should subtract 4XP
66+
repo.removeExperienceFromAllAccounts(50, 4, 10);
67+
assertEquals(2, repo.getByUserId(1).get().getExperience());
68+
}
69+
70+
@Test
71+
void testSubtractMaximum() {
72+
repo.insert(new HelpAccount(1, 100));
73+
//tries to subtract 50% which is 50XP
74+
//but maximum is 10XP hence it should subtract 10XP
75+
repo.removeExperienceFromAllAccounts(50, 1, 10);
76+
assertEquals(90, repo.getByUserId(1).get().getExperience());
77+
}
78+
79+
@Test
80+
void testFraction() {
81+
repo.insert(new HelpAccount(1, 100));
82+
//subtract 10% i.e. 10XP
83+
//should be inside [1,50] hence neither minimum nor maximum is active
84+
repo.removeExperienceFromAllAccounts(10, 1, 50);
85+
assertEquals(90, repo.getByUserId(1).get().getExperience());
86+
//subtract 10% i.e. 9XP
87+
//should be inside [1,50] hence neither minimum nor maximum is active
88+
repo.removeExperienceFromAllAccounts(10, 1, 50);
89+
assertEquals(81, repo.getByUserId(1).get().getExperience());
90+
}
91+
92+
@Test
93+
void testMultipleUsers() {
94+
repo.insert(new HelpAccount(79, 79));//below min
95+
repo.insert(new HelpAccount(80, 80));//exactly min
96+
repo.insert(new HelpAccount(100, 100));//within bounds
97+
repo.insert(new HelpAccount(2_000, 2_000));//within bounds
98+
repo.insert(new HelpAccount(3_999, 3_999));//close to upper bound
99+
repo.insert(new HelpAccount(4_000, 4_000));//exactly upper bound
100+
repo.insert(new HelpAccount(4_001, 4_001));//exceeds upper bound
101+
repo.insert(new HelpAccount(10_000, 10_000));//significantly exceeds upper bound
102+
103+
repo.removeExperienceFromAllAccounts(1.25, 1, 50);
104+
105+
double delta = 0.0001;//required precision
106+
assertEquals(78, repo.getByUserId(79).get().getExperience(), delta);
107+
assertEquals(79, repo.getByUserId(80).get().getExperience(), delta);
108+
assertEquals(98.75, repo.getByUserId(100).get().getExperience(), delta);
109+
assertEquals(1975, repo.getByUserId(2_000).get().getExperience(), delta);
110+
assertEquals(3949.0125, repo.getByUserId(3_999).get().getExperience(), delta);
111+
assertEquals(3950, repo.getByUserId(4_000).get().getExperience(), delta);
112+
assertEquals(3951, repo.getByUserId(4_001).get().getExperience(), delta);
113+
assertEquals(9_950, repo.getByUserId(10_000).get().getExperience(), delta);
114+
}
115+
}

0 commit comments

Comments
 (0)