package de.krisenvorrat.app.di

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import de.krisenvorrat.app.data.db.KrisenvorratDatabase
import org.junit.After
import org.junit.Assert.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
internal class DatabaseModuleTest {

    private lateinit var db: KrisenvorratDatabase

    @After
    fun tearDown() {
        if (::db.isInitialized) {
            db.close()
        }
    }

    @Test
    fun test_provideAllDaos_withInMemoryDb_allDaosAreNonNull() {
        // Given
        val context = ApplicationProvider.getApplicationContext<Context>()
        val module = DatabaseModule

        // When
        db = Room.inMemoryDatabaseBuilder(context, KrisenvorratDatabase::class.java)
            .allowMainThreadQueries()
            .build()
        val itemDao = module.provideItemDao(db)
        val categoryDao = module.provideCategoryDao(db)
        val locationDao = module.provideLocationDao(db)
        val settingsDao = module.provideSettingsDao(db)

        // Then
        assertNotNull(itemDao)
        assertNotNull(categoryDao)
        assertNotNull(locationDao)
        assertNotNull(settingsDao)
    }
}
