Skip to content

Commit

Permalink
-finished all the Unit Tests
Browse files Browse the repository at this point in the history
-Fixed the CompanyInfoViewModel (i guess doing Unit Testing is important lmao)
  • Loading branch information
LeonelZalegas committed Jul 26, 2024
1 parent 5ceb72c commit 37e17cd
Show file tree
Hide file tree
Showing 9 changed files with 725 additions and 36 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,30 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.junit.ktx)
testImplementation(libs.junit)
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.jupiter)
testImplementation(libs.jupiter.junit.jupiter)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

//
testImplementation(libs.jupiter.junit.jupiter.api)
testRuntimeOnly(libs.jupiter.junit.jupiter.engine)

// AndroidX Test Core
testImplementation(libs.androidx.core)

// Kotlin testing
testImplementation(libs.jetbrains.kotlin.test)

// MockK
testImplementation(libs.mockk.mockk)

Expand Down Expand Up @@ -152,6 +164,7 @@ dependencies {
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
testImplementation(libs.androidx.room.testing)
testImplementation(libs.robolectric.robolectric)

// Retrofit
implementation(libs.retrofit2.retrofit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun CompanyInfoScreen(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 5.dp),
.padding(start = 10.dp),
)
Spacer(modifier = Modifier.height(8.dp))
Text(
Expand All @@ -65,7 +65,7 @@ fun CompanyInfoScreen(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 5.dp),
.padding(start = 10.dp),
)
Spacer(modifier = Modifier.height(8.dp))
HorizontalDivider(
Expand All @@ -80,7 +80,7 @@ fun CompanyInfoScreen(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 5.dp),
.padding(start = 10.dp),
overflow = TextOverflow.Ellipsis,
)
Spacer(modifier = Modifier.height(8.dp))
Expand All @@ -90,7 +90,7 @@ fun CompanyInfoScreen(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 5.dp),
.padding(start = 10.dp),
overflow = TextOverflow.Ellipsis,
)
Spacer(modifier = Modifier.height(8.dp))
Expand All @@ -106,7 +106,7 @@ fun CompanyInfoScreen(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 5.dp),
.padding(start = 10.dp),
)
if (state.stockIntradayInfos.isNotEmpty()) {
Spacer(modifier = Modifier.height(16.dp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,33 @@ class CompanyInfoViewModel
state = state.copy(isLoading = true)
val companyInfoResult = async { repository.getCompanyInfo(symbol) }
val intradayInfoResult = async { repository.getIntradayInfo(symbol) }
when (val result = companyInfoResult.await()) {
is Resource.Success -> {
state =
state.copy(
company = result.data,
isLoading = false,
error = null,
)
}
is Resource.Error -> {
state =

val companyInfo = companyInfoResult.await()
val intradayInfo = intradayInfoResult.await()

state =
when {
companyInfo is Resource.Error ->
state.copy(
isLoading = false,
error = result.message,
error = companyInfo.message,
company = null,
)
}
else -> Unit
}
when (val result = intradayInfoResult.await()) {
is Resource.Success -> {
state =
intradayInfo is Resource.Error ->
state.copy(
stockIntradayInfos = result.data ?: emptyList(),
isLoading = false,
error = null,
error = intradayInfo.message,
company = (companyInfo as? Resource.Success)?.data,
)
}
is Resource.Error -> {
state =
companyInfo is Resource.Success && intradayInfo is Resource.Success ->
state.copy(
isLoading = false,
error = result.message,
company = null,
company = companyInfo.data,
stockIntradayInfos = intradayInfo.data ?: emptyList(),
error = null,
)
else -> state.copy(isLoading = false, error = "An unexpected error occurred")
}
else -> Unit
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ fun IntradayInfoChart(
val xAxisData =
AxisData.Builder()
.axisStepSize(50.dp)
.steps(infos.size - 1)
.steps(infos.size)
.labelData { i ->
if (i != 0) {
infos.getOrNull(i)?.date?.format(DateTimeFormatter.ofPattern("HH:mm")) ?: ""
infos.getOrNull(i)?.date?.minusHours(0)?.format(DateTimeFormatter.ofPattern("HH:mm")) ?: ""
} else {
infos.getOrNull(i)?.date?.format(DateTimeFormatter.ofPattern("HH")) ?: ""
infos.getOrNull(i)?.date?.minusHours(0)?.format(DateTimeFormatter.ofPattern("HH")) ?: ""
}
}
.labelAndAxisLinePadding(15.dp)
Expand Down
Loading

0 comments on commit 37e17cd

Please sign in to comment.