Skip to content

Commit

Permalink
test: add getByCategory, getBetween to dao test
Browse files Browse the repository at this point in the history
  • Loading branch information
ksw4015 committed Nov 21, 2024
1 parent fd7b8aa commit 57b95df
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import kr.ksw.mybudget.R
import kr.ksw.mybudget.data.local.dao.SpendingDao
import kr.ksw.mybudget.data.local.databases.MyBudgetDatabase
import kr.ksw.mybudget.data.local.entity.SpendingEntity
import kr.ksw.mybudget.data.local.mock.MAJOR_CATEGORY_FOOD
import kr.ksw.mybudget.data.local.mock.MAJOR_CATEGORY_LIFE_STYLE
import kr.ksw.mybudget.data.local.mock.spendingList
import kr.ksw.mybudget.data.local.mock.spendingListForBetween
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.util.Date
import kotlin.jvm.Throws
import java.time.LocalDate

@RunWith(AndroidJUnit4::class)
class SpendingDaoTest {
Expand Down Expand Up @@ -50,7 +53,7 @@ class SpendingDaoTest {
val context = ApplicationProvider.getApplicationContext<Context>()
dao.upsertSpendingEntity(SpendingEntity(
title = "스타벅스",
date = Date(),
date = LocalDate.now(),
majorCategory = context.resources.getInteger(R.integer.category_food),
subCategory = context.resources.getInteger(R.integer.category_cafe),
price = 5_000
Expand All @@ -65,7 +68,7 @@ class SpendingDaoTest {
val spending = SpendingEntity(
id = 1,
title = "스타벅스",
date = Date(),
date = LocalDate.now(),
majorCategory = context.resources.getInteger(R.integer.category_food),
subCategory = context.resources.getInteger(R.integer.category_cafe),
price = 5_000
Expand All @@ -76,5 +79,95 @@ class SpendingDaoTest {
assert(dao.getAllSpendingEntities().first().isEmpty())
}

@Test
fun `getSpendingEntitiesByMajorCategory Test Food Result Count is 2`() = runTest {
spendingList.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
assert(dao.getAllSpendingEntities().first().isNotEmpty())

val result = dao.getSpendingEntitiesByMajorCategory(MAJOR_CATEGORY_FOOD)
assert(result.isNotEmpty())
assert(result.size == 2)
}

@Test
fun `getSpendingEntitiesBySubCategory Test LifeStyle And Transportation Category`() = runTest {
spendingList.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
assert(dao.getAllSpendingEntities().first().isNotEmpty())

val resultLifeStyle = dao.getSpendingEntitiesByMajorCategory(MAJOR_CATEGORY_LIFE_STYLE)
assert(resultLifeStyle.size == 2)

val categoryTransportation = 31
val resultTransportation = dao.getSpendingEntitiesBySubCategory(categoryTransportation)
assert(resultTransportation.size == 1)
}

@Test
fun `getSpendingEntitiesBetween Today`() = runTest {
spendingList.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
val from = LocalDate.now()
val to = LocalDate.now()
val result = dao.getSpendingEntitiesBetween(from, to)
assert(result.isNotEmpty())
assert(result.size == spendingList.size)
}

@Test
fun `getSpendingEntitiesBetween Week`() = runTest {
val now = LocalDate.now()
val from = now.plusDays(-((now.dayOfWeek.value - 1).toLong()))
val to = now.plusDays((7 - now.dayOfWeek.value).toLong())
spendingListForBetween.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
val result = dao.getSpendingEntitiesBetween(from, to)
assert(result.isNotEmpty())
assert(result.size == 1)
}

@Test
fun `getSpendingEntitiesBetween Month`() = runTest {
val now = LocalDate.now()
val from = now.withDayOfMonth(1)
val to = now.withDayOfMonth(now.lengthOfMonth())
spendingListForBetween.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
val result = dao.getSpendingEntitiesBetween(from, to)
assert(result.isNotEmpty())
assert(result.size == 2)
}

@Test
fun `getSpendingEntitiesBetween One Month Before`() = runTest {
val month = LocalDate.now().plusMonths(-1L)
val from = month.withDayOfMonth(1)
val to = month.withDayOfMonth(month.lengthOfMonth())
println("$from, $to")
spendingListForBetween.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
val result = dao.getSpendingEntitiesBetween(from, to)
assert(result.isNotEmpty())
assert(result.size == 1)
}

@Test
fun `getSpendingEntitiesBetween Year`() = runTest {
val now = LocalDate.now()
val from = now.withDayOfYear(1)
println("$from, $now")
spendingListForBetween.forEach { spending ->
dao.upsertSpendingEntity(spending)
}
val result = dao.getSpendingEntitiesBetween(from, now)
assert(result.isNotEmpty())
assert(result.size == spendingListForBetween.size)
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,77 @@
package kr.ksw.mybudget.data.local.mock

import kr.ksw.mybudget.data.local.entity.SpendingEntity
import java.util.Date
import java.time.LocalDate

const val MAJOR_CATEGORY_FOOD = 1
const val MAJOR_CATEGORY_CULTURE = 2
const val MAJOR_CATEGORY_LIFE_STYLE = 3
const val MAJOR_CATEGORY_CASH = 4

val spendingList = listOf(
SpendingEntity(
title = "스타벅스",
date = Date(),
majorCategory = 1,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_FOOD,
subCategory = 12,
price = 6_000
),
SpendingEntity(
title = "할리스",
date = Date(),
majorCategory = 1,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_FOOD,
subCategory = 12,
price = 5_000
),
SpendingEntity(
title = "영화",
date = Date(),
majorCategory = 2,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_CULTURE,
subCategory = 22,
price = 15_000
),
SpendingEntity(
title = "교통비",
date = Date(),
majorCategory = 3,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_LIFE_STYLE,
subCategory = 31,
price = 100_000
),
SpendingEntity(
title = "통신비",
date = Date(),
majorCategory = 3,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_LIFE_STYLE,
subCategory = 32,
price = 50_000
),
SpendingEntity(
title = "송금",
date = Date(),
majorCategory = 4,
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_CASH,
subCategory = 41,
price = 200_000
),
)

val spendingListForBetween = listOf(
SpendingEntity(
title = "송금",
date = LocalDate.now().plusWeeks(-1L),
majorCategory = MAJOR_CATEGORY_CASH,
subCategory = 41,
price = 200_000
),
SpendingEntity(
title = "송금",
date = LocalDate.now().plusMonths(-1L),
majorCategory = MAJOR_CATEGORY_CASH,
subCategory = 41,
price = 200_000
),
SpendingEntity(
title = "송금",
date = LocalDate.now(),
majorCategory = MAJOR_CATEGORY_CASH,
subCategory = 41,
price = 200_000
),
Expand Down

0 comments on commit 57b95df

Please sign in to comment.