|
| 1 | +package net.discordjug.javabot.systems.help; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.sql.SQLException; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.boot.jdbc.DataSourceBuilder; |
| 14 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 15 | + |
| 16 | +import com.zaxxer.hikari.HikariDataSource; |
| 17 | + |
| 18 | +import net.discordjug.javabot.data.h2db.DbHelper; |
| 19 | +import net.discordjug.javabot.systems.help.dao.HelpAccountRepository; |
| 20 | +import net.discordjug.javabot.systems.help.model.HelpAccount; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests functionality of automated experience subtraction. |
| 24 | + */ |
| 25 | +public class HelpExperienceSubtractionTest { |
| 26 | + |
| 27 | + private HikariDataSource dataSource; |
| 28 | + private HelpAccountRepository repo; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setUp() throws IOException, SQLException { |
| 32 | + dataSource = DataSourceBuilder.create() |
| 33 | + .type(HikariDataSource.class) |
| 34 | + .url("jdbc:h2:mem:test") |
| 35 | + .username("test") |
| 36 | + .password("") |
| 37 | + .build(); |
| 38 | + |
| 39 | + DbHelper.initializeSchema(dataSource); |
| 40 | + |
| 41 | + JdbcTemplate template = new JdbcTemplate(dataSource); |
| 42 | + repo = new HelpAccountRepository(template); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + void cleanUp() { |
| 47 | + dataSource.close(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * If a user has less XP than the minimum experience subtraction, the user should lose all XP. |
| 52 | + */ |
| 53 | + @Test |
| 54 | + void testUserHasLessThanMinimum() { |
| 55 | + repo.insert(new HelpAccount(1, 1)); |
| 56 | + repo.removeExperienceFromAllAccounts(50, 2, 10); |
| 57 | + assertEquals(0, repo.getByUserId(1).get().getExperience()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * If the XP to subtract is less than the minimum, the minimum XP should be subtracted. |
| 62 | + */ |
| 63 | + @Test |
| 64 | + void testSubtractMinimum() { |
| 65 | + repo.insert(new HelpAccount(1, 6));//6XP |
| 66 | + //would remove 50% i.e. 3XP |
| 67 | + //the minimum is 4XP hence it should subtract 4XP |
| 68 | + repo.removeExperienceFromAllAccounts(50, 4, 10); |
| 69 | + assertEquals(2, repo.getByUserId(1).get().getExperience()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testSubtractMaximum() { |
| 74 | + repo.insert(new HelpAccount(1, 100)); |
| 75 | + //tries to subtract 50% which is 50XP |
| 76 | + //but maximum is 10XP hence it should subtract 10XP |
| 77 | + repo.removeExperienceFromAllAccounts(50, 1, 10); |
| 78 | + assertEquals(90, repo.getByUserId(1).get().getExperience()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testFraction() { |
| 83 | + repo.insert(new HelpAccount(1, 100)); |
| 84 | + //subtract 10% i.e. 10XP |
| 85 | + //should be inside [1,50] hence neither minimum nor maximum is active |
| 86 | + repo.removeExperienceFromAllAccounts(10, 1, 50); |
| 87 | + assertEquals(90, repo.getByUserId(1).get().getExperience()); |
| 88 | + //subtract 10% i.e. 9XP |
| 89 | + //should be inside [1,50] hence neither minimum nor maximum is active |
| 90 | + repo.removeExperienceFromAllAccounts(10, 1, 50); |
| 91 | + assertEquals(81, repo.getByUserId(1).get().getExperience()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testXPHalfLife() { |
| 96 | + int startXP = 1_000; |
| 97 | + int half = startXP/2; |
| 98 | + repo.insert(new HelpAccount(1, startXP)); |
| 99 | + for (int i = 0; i < 55; i++) { |
| 100 | + repo.removeExperienceFromAllAccounts(1.25, 0, 1_000); |
| 101 | + double actualXP = repo.getByUserId(1).get().getExperience(); |
| 102 | + assertTrue(actualXP > half, "In iteration "+i+", XP have decayed by more than 50%, user has "+actualXP+"XP after iteration"); |
| 103 | + } |
| 104 | + repo.removeExperienceFromAllAccounts(1.25, 0, 1_000); |
| 105 | + double actualXP = repo.getByUserId(1).get().getExperience(); |
| 106 | + assertFalse(actualXP > half, "After all iterations, XP have not decayed by more than 50%, user has "+actualXP+"XP at the end"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testMultipleUsers() { |
| 111 | + repo.insert(new HelpAccount(79, 79));//below min |
| 112 | + repo.insert(new HelpAccount(80, 80));//exactly min |
| 113 | + repo.insert(new HelpAccount(100, 100));//within bounds |
| 114 | + repo.insert(new HelpAccount(2_000, 2_000));//within bounds |
| 115 | + repo.insert(new HelpAccount(3_999, 3_999));//close to upper bound |
| 116 | + repo.insert(new HelpAccount(4_000, 4_000));//exactly upper bound |
| 117 | + repo.insert(new HelpAccount(4_001, 4_001));//exceeds upper bound |
| 118 | + repo.insert(new HelpAccount(10_000, 10_000));//significantly exceeds upper bound |
| 119 | + |
| 120 | + repo.removeExperienceFromAllAccounts(1.25, 1, 50); |
| 121 | + |
| 122 | + double delta = 0.0001;//required precision |
| 123 | + assertEquals(78, repo.getByUserId(79).get().getExperience(), delta); |
| 124 | + assertEquals(79, repo.getByUserId(80).get().getExperience(), delta); |
| 125 | + assertEquals(98.75, repo.getByUserId(100).get().getExperience(), delta); |
| 126 | + assertEquals(1975, repo.getByUserId(2_000).get().getExperience(), delta); |
| 127 | + assertEquals(3949.0125, repo.getByUserId(3_999).get().getExperience(), delta); |
| 128 | + assertEquals(3950, repo.getByUserId(4_000).get().getExperience(), delta); |
| 129 | + assertEquals(3951, repo.getByUserId(4_001).get().getExperience(), delta); |
| 130 | + assertEquals(9_950, repo.getByUserId(10_000).get().getExperience(), delta); |
| 131 | + } |
| 132 | +} |
0 commit comments