-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
β
Added tests for repository classes
- Loading branch information
1 parent
87061c9
commit d4a9ef8
Showing
5 changed files
with
357 additions
and
34 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
.../com/lorenzovainigli/foodexpirationdates/model/repository/ExpirationDateRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.lorenzovainigli.foodexpirationdates.model.repository | ||
|
||
import androidx.room.Room.inMemoryDatabaseBuilder | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.lorenzovainigli.foodexpirationdates.model.AppDatabase | ||
import com.lorenzovainigli.foodexpirationdates.model.dao.ExpirationDatesDao | ||
import com.lorenzovainigli.foodexpirationdates.model.entity.ExpirationDate | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class ExpirationDateRepositoryTest { | ||
|
||
private lateinit var dao: ExpirationDatesDao | ||
private lateinit var database: AppDatabase | ||
private lateinit var repository: ExpirationDateRepository | ||
|
||
@Before | ||
fun setup() { | ||
// Set up the in-memory Room database for testing | ||
database = inMemoryDatabaseBuilder( | ||
InstrumentationRegistry.getInstrumentation().context, | ||
AppDatabase::class.java | ||
).build() | ||
dao = database.expirationDatesDao | ||
repository = ExpirationDatesRepositoryImpl(dao) | ||
} | ||
|
||
@After | ||
fun teardown() { | ||
// Close the Room database after testing | ||
database.close() | ||
} | ||
|
||
@Test | ||
fun insertExpirationDateTest() { | ||
val foodName = "Cheese" | ||
val expirationDate = System.currentTimeMillis() | ||
val insertedItem = ExpirationDate( | ||
id = 0, | ||
foodName = foodName, | ||
expirationDate = expirationDate | ||
) | ||
val list: MutableList<ExpirationDate> = mutableListOf() | ||
runBlocking { | ||
repository.addExpirationDate(insertedItem) | ||
val retrievedItem = dao.getAll().firstOrNull() | ||
retrievedItem?.forEach { | ||
list.add(it) | ||
} | ||
} | ||
assertEquals(list.size, 1) | ||
assertEquals(list[0].foodName, foodName) | ||
assertEquals(list[0].expirationDate, expirationDate) | ||
} | ||
|
||
@Test | ||
fun updateExpirationDateTest() { | ||
var foodName = "Cheese" | ||
val expirationDate = System.currentTimeMillis() | ||
val insertedItem = ExpirationDate( | ||
id = 0, | ||
foodName = foodName, | ||
expirationDate = expirationDate | ||
) | ||
dao.insert(insertedItem) | ||
var insertedId = 0 | ||
runBlocking { | ||
val item = dao.getAll().firstOrNull() | ||
item?.forEach { | ||
insertedId = it.id | ||
} | ||
} | ||
insertedItem.id = insertedId | ||
foodName = "Tomato" | ||
insertedItem.foodName = foodName | ||
insertedItem.expirationDate = expirationDate + 1 | ||
val list: MutableList<ExpirationDate> = mutableListOf() | ||
runBlocking { | ||
repository.addExpirationDate(insertedItem) | ||
val retrievedItem = dao.getAll().firstOrNull() | ||
retrievedItem?.forEach { | ||
list.add(it) | ||
} | ||
} | ||
assertEquals(list.size, 1) | ||
assertEquals(list[0].foodName, foodName) | ||
assertEquals(list[0].expirationDate, expirationDate + 1) | ||
} | ||
|
||
@Test | ||
fun deleteExpirationDateTest() { | ||
val expirationDate = System.currentTimeMillis() | ||
val insertedItem = ExpirationDate( | ||
id = 0, | ||
foodName = "Cheese", | ||
expirationDate = expirationDate | ||
) | ||
dao.insert(insertedItem) | ||
var insertedId = 0 | ||
runBlocking { | ||
val item = dao.getAll().firstOrNull() | ||
item?.forEach { | ||
insertedId = it.id | ||
} | ||
} | ||
insertedItem.id = insertedId | ||
dao.delete(insertedItem) | ||
val list: MutableList<ExpirationDate> = mutableListOf() | ||
runBlocking { | ||
repository.deleteExpirationDate(insertedItem) | ||
val retrievedItem = dao.getAll().firstOrNull() | ||
retrievedItem?.forEach { | ||
list.add(it) | ||
} | ||
} | ||
assertEquals(list.size, 0) | ||
val retrievedItem = dao.getOne(id = insertedId) | ||
assertNull(retrievedItem) | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
...ava/com/lorenzovainigli/foodexpirationdates/model/repository/PreferencesRepositoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package com.lorenzovainigli.foodexpirationdates.model.repository | ||
|
||
import android.content.Context | ||
import androidx.activity.ComponentActivity | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
internal class PreferencesRepositoryTest { | ||
|
||
private val testSharedPrefsName = "test_shared_prefs" | ||
private lateinit var context: Context | ||
|
||
@Before | ||
fun setUp() { | ||
context = InstrumentationRegistry.getInstrumentation().context | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
context.deleteSharedPreferences(testSharedPrefsName) | ||
} | ||
|
||
@Test | ||
fun userDateFormatTest() { | ||
val sharedPrefs = | ||
context.getSharedPreferences(testSharedPrefsName, ComponentActivity.MODE_PRIVATE) | ||
// Test the default value | ||
assertEquals( | ||
sharedPrefs.getString( | ||
PreferencesRepository.keyDateFormat, | ||
PreferencesRepository.getAvailOtherDateFormats()[0] | ||
), | ||
PreferencesRepository.getAvailOtherDateFormats()[0] | ||
) | ||
// Test the setter | ||
PreferencesRepository.setUserDateFormat( | ||
context, | ||
testSharedPrefsName, | ||
PreferencesRepository.getAvailOtherDateFormats()[1] | ||
) | ||
assertEquals( | ||
sharedPrefs.getString( | ||
PreferencesRepository.keyDateFormat, | ||
PreferencesRepository.getAvailOtherDateFormats()[0] | ||
), | ||
PreferencesRepository.getAvailOtherDateFormats()[1] | ||
) | ||
// Test the getter | ||
assertEquals( | ||
PreferencesRepository.getUserDateFormat(context, testSharedPrefsName), | ||
PreferencesRepository.getAvailOtherDateFormats()[1] | ||
) | ||
} | ||
|
||
@Test | ||
fun userNotificationTimeHourTest() { | ||
val sharedPrefs = | ||
context.getSharedPreferences(testSharedPrefsName, ComponentActivity.MODE_PRIVATE) | ||
// Test the default value | ||
assertEquals(sharedPrefs.getInt(PreferencesRepository.keyNotificationTimeHour, 11), 11) | ||
// Test the setter | ||
PreferencesRepository.setUserNotificationTime(context, testSharedPrefsName, 12, 30) | ||
assertEquals(sharedPrefs.getInt(PreferencesRepository.keyNotificationTimeHour, 11), 12) | ||
assertEquals(sharedPrefs.getInt(PreferencesRepository.keyNotificationTimeMinute, 11), 30) | ||
// Test the getter | ||
assertEquals( | ||
PreferencesRepository.getUserNotificationTimeHour( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName | ||
), 12 | ||
) | ||
assertEquals( | ||
PreferencesRepository.getUserNotificationTimeMinute( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName | ||
), 30 | ||
) | ||
} | ||
|
||
@Test | ||
fun userThemeMode() { | ||
val sharedPrefs = | ||
context.getSharedPreferences(testSharedPrefsName, ComponentActivity.MODE_PRIVATE) | ||
// Test the default value | ||
assertEquals( | ||
sharedPrefs.getInt( | ||
PreferencesRepository.keyThemeMode, | ||
PreferencesRepository.Companion.ThemeMode.SYSTEM.ordinal | ||
), | ||
PreferencesRepository.Companion.ThemeMode.SYSTEM.ordinal | ||
) | ||
// Test the setter | ||
PreferencesRepository.setThemeMode( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName, | ||
themeMode = PreferencesRepository.Companion.ThemeMode.DARK | ||
) | ||
assertEquals( | ||
sharedPrefs.getInt( | ||
PreferencesRepository.keyThemeMode, | ||
PreferencesRepository.Companion.ThemeMode.SYSTEM.ordinal | ||
), | ||
PreferencesRepository.Companion.ThemeMode.DARK.ordinal | ||
) | ||
// Test the getter | ||
assertEquals( | ||
PreferencesRepository.getThemeMode( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName | ||
), | ||
PreferencesRepository.Companion.ThemeMode.DARK.ordinal | ||
) | ||
} | ||
|
||
@Test | ||
fun userDynamicColors() { | ||
val sharedPrefs = | ||
context.getSharedPreferences(testSharedPrefsName, ComponentActivity.MODE_PRIVATE) | ||
// Test the default value | ||
assertEquals( | ||
sharedPrefs.getBoolean( | ||
PreferencesRepository.keyDynamicColors, | ||
false | ||
), | ||
false | ||
) | ||
// Test the setter | ||
PreferencesRepository.setDynamicColors( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName, | ||
dynamicColorsEnabled = true | ||
) | ||
assertEquals( | ||
sharedPrefs.getBoolean( | ||
PreferencesRepository.keyDynamicColors, | ||
false | ||
), | ||
true | ||
) | ||
// Test the getter | ||
assertEquals( | ||
PreferencesRepository.getDynamicColors( | ||
context = context, | ||
sharedPrefs = testSharedPrefsName | ||
), | ||
true | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.