-
Notifications
You must be signed in to change notification settings - Fork 0
Fix DB already exists #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e567f9f
81c1c9e
3fa60ae
9fbd0fc
3da35bc
8cd3830
1275eac
6ea5817
8ac6d7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,8 @@ | ||
| package me.axeno.hommr.managers; | ||
|
|
||
| import com.j256.ormlite.dao.Dao; | ||
| import com.j256.ormlite.support.ConnectionSource; | ||
| import me.axeno.hommr.Hommr; | ||
| import me.axeno.hommr.models.Home; | ||
| import org.bukkit.configuration.file.FileConfiguration; | ||
| import org.bukkit.plugin.PluginDescriptionFile; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -15,7 +12,6 @@ | |
| import org.mockito.MockedStatic; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Path; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
|
|
@@ -45,16 +41,17 @@ class DatabaseManagerTest { | |
| private MockedStatic<Hommr> hommrMockedStatic; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| void setUp() throws SQLException { | ||
| // Use SQLite in-memory database for testing | ||
| databaseManager = new DatabaseManager(); | ||
| databaseManager.init("jdbc:sqlite::memory:", "", ""); | ||
|
Comment on lines
+44
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find the DatabaseManager class
fd "DatabaseManager\.java" --type fRepository: AxenoDev/Hommr Length of output: 116 🏁 Script executed: # Check project Java version
fd "pom\.xml|build\.gradle" --type fRepository: AxenoDev/Hommr Length of output: 70 🏁 Script executed: # Look at the test file to understand the changes
cat -n src/test/java/me/axeno/hommr/managers/DatabaseManagerTest.javaRepository: AxenoDev/Hommr Length of output: 5782 🏁 Script executed: # Check DatabaseManager init method
sed -n '1,100p' src/main/java/me/axeno/hommr/managers/DatabaseManager.javaRepository: AxenoDev/Hommr Length of output: 3918 🏁 Script executed: # Check Java version in build.gradle
cat build.gradleRepository: AxenoDev/Hommr Length of output: 5748 Replace in-memory SQLite with a file-based database to avoid flaky tests with connection pooling.
Suggested fix void setUp() throws SQLException {
// Use SQLite in-memory database for testing
databaseManager = new DatabaseManager();
- databaseManager.init("jdbc:sqlite::memory:", "", "");
+ Path dbFile = tempDir.resolve("test.db");
+ databaseManager.init("jdbc:sqlite:" + dbFile.toString(), "", "");🤖 Prompt for AI Agents |
||
|
|
||
| hommrMockedStatic = mockStatic(Hommr.class); | ||
| hommrMockedStatic.when(Hommr::getInstance).thenReturn(mockPlugin); | ||
|
|
||
| lenient().when(mockPlugin.getDataFolder()).thenReturn(tempDir.toFile()); | ||
| lenient().when(mockPlugin.getConfig()).thenReturn(mockConfig); | ||
| lenient().when(mockPlugin.getLogger()).thenReturn(mockLogger); | ||
| lenient().when(mockConfig.getString("database.type", "sqlite")).thenReturn("sqlite"); | ||
| } | ||
|
|
||
| @AfterEach | ||
|
|
@@ -69,17 +66,11 @@ void tearDown() { | |
|
|
||
| @Test | ||
| void testInitWithSQLiteCreatesDatabase() { | ||
| databaseManager.init(); | ||
|
|
||
| assertNotNull(databaseManager.getHomeDao()); | ||
| File dbFile = new File(tempDir.toFile(), "homes.db"); | ||
| assertTrue(dbFile.exists()); | ||
| } | ||
|
|
||
| @Test | ||
| void testSaveAndGetAllHomes() throws SQLException { | ||
| databaseManager.init(); | ||
|
|
||
| List<Home> homesToSave = new ArrayList<>(); | ||
| homesToSave.add(createTestHome(UUID.randomUUID(), "home1")); | ||
| homesToSave.add(createTestHome(UUID.randomUUID(), "home2")); | ||
|
|
@@ -94,8 +85,6 @@ void testSaveAndGetAllHomes() throws SQLException { | |
|
|
||
| @Test | ||
| void testSaveAllHomesClearsExistingHomes() throws SQLException { | ||
| databaseManager.init(); | ||
|
|
||
| List<Home> firstBatch = new ArrayList<>(); | ||
| firstBatch.add(createTestHome(UUID.randomUUID(), "home1")); | ||
| firstBatch.add(createTestHome(UUID.randomUUID(), "home2")); | ||
|
|
@@ -108,13 +97,11 @@ void testSaveAllHomesClearsExistingHomes() throws SQLException { | |
| List<Home> retrievedHomes = databaseManager.getAllHomes(); | ||
|
|
||
| assertEquals(1, retrievedHomes.size()); | ||
| assertEquals("home3", retrievedHomes.get(0).getName()); | ||
| assertEquals("home3", retrievedHomes.getFirst().getName()); | ||
| } | ||
|
|
||
| @Test | ||
| void testSaveAllHomesPreservesHomeData() throws SQLException { | ||
| databaseManager.init(); | ||
|
|
||
| UUID ownerId = UUID.randomUUID(); | ||
| Home originalHome = new Home( | ||
| 0, | ||
|
|
@@ -133,7 +120,7 @@ void testSaveAllHomesPreservesHomeData() throws SQLException { | |
| List<Home> retrievedHomes = databaseManager.getAllHomes(); | ||
|
|
||
| assertEquals(1, retrievedHomes.size()); | ||
| Home retrievedHome = retrievedHomes.get(0); | ||
| Home retrievedHome = retrievedHomes.getFirst(); | ||
| assertEquals(ownerId, retrievedHome.getOwner()); | ||
| assertEquals("testHome", retrievedHome.getName()); | ||
| assertEquals("world", retrievedHome.getWorld()); | ||
|
|
@@ -146,8 +133,6 @@ void testSaveAllHomesPreservesHomeData() throws SQLException { | |
|
|
||
| @Test | ||
| void testCloseDoesNotThrowException() { | ||
| databaseManager.init(); | ||
|
|
||
| assertDoesNotThrow(() -> databaseManager.close()); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.