Skip to content

Commit 1cbd1fe

Browse files
committed
tests: add more unit tests
1 parent f45ddb3 commit 1cbd1fe

File tree

11 files changed

+1191
-10
lines changed

11 files changed

+1191
-10
lines changed

data/src/main/java/com/klee/sapio/data/api/RetrofitClient.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ interface EvaluationApi {
8585
): StrapiAnswer
8686
}
8787

88-
class EvaluationService @Inject constructor(
88+
open class EvaluationService @Inject constructor(
8989
@ApplicationContext private val context: Context
9090
) {
9191
companion object {
@@ -115,7 +115,7 @@ class EvaluationService @Inject constructor(
115115
evaluationsApi = retrofit.create(EvaluationApi::class.java)
116116
}
117117

118-
suspend fun listLatestEvaluations(pageNumber: Int): Result<List<Evaluation>> =
118+
open suspend fun listLatestEvaluations(pageNumber: Int): Result<List<Evaluation>> =
119119
runCatching {
120120
val strapiAnswer = evaluationsApi.listLatestEvaluationsAsync(
121121
settings.getRootConfigurationLevel(),
@@ -130,7 +130,7 @@ class EvaluationService @Inject constructor(
130130
}
131131
}
132132

133-
suspend fun searchEvaluation(pattern: String): Result<List<Evaluation>> =
133+
open suspend fun searchEvaluation(pattern: String): Result<List<Evaluation>> =
134134
runCatching {
135135
val strapiAnswer = evaluationsApi.searchAsync(
136136
pattern,
@@ -146,25 +146,25 @@ class EvaluationService @Inject constructor(
146146
.distinctBy { it.packageName }
147147
}
148148

149-
suspend fun existingEvaluations(packageName: String): Result<List<StrapiElement>> =
149+
open suspend fun existingEvaluations(packageName: String): Result<List<StrapiElement>> =
150150
runCatching {
151151
val strapiAnswer = evaluationsApi.existingEvaluationsAsync(packageName)
152152
strapiAnswer.data.toList()
153153
}
154154

155-
suspend fun addEvaluation(app: UploadEvaluationHeader): Result<Unit> =
155+
open suspend fun addEvaluation(app: UploadEvaluationHeader): Result<Unit> =
156156
runCatching {
157157
evaluationsApi.addEvaluation(app)
158158
Unit
159159
}
160160

161-
suspend fun updateEvaluation(app: UploadEvaluationHeader, id: Int): Result<Unit> =
161+
open suspend fun updateEvaluation(app: UploadEvaluationHeader, id: Int): Result<Unit> =
162162
runCatching {
163163
evaluationsApi.updateEvaluation(app, id)
164164
Unit
165165
}
166166

167-
suspend fun uploadIcon(packageName: String): Result<List<IconAnswer>> {
167+
open suspend fun uploadIcon(packageName: String): Result<List<IconAnswer>> {
168168
return runCatching {
169169
val appInfo = context.packageManager.getApplicationInfo(packageName, 0)
170170
val drawable = context.packageManager.getApplicationIcon(appInfo)
@@ -182,18 +182,18 @@ class EvaluationService @Inject constructor(
182182
}
183183
}
184184

185-
suspend fun existingIcon(iconName: String): Result<List<IconAnswer>> =
185+
open suspend fun existingIcon(iconName: String): Result<List<IconAnswer>> =
186186
runCatching {
187187
evaluationsApi.existingIconAsync(iconName)
188188
}
189189

190-
suspend fun deleteIcon(id: Int): Result<Unit> =
190+
open suspend fun deleteIcon(id: Int): Result<Unit> =
191191
runCatching {
192192
evaluationsApi.deleteIcon(id)
193193
Unit
194194
}
195195

196-
suspend fun fetchEvaluation(appPackageName: String, microG: Int, rooted: Int): Result<Evaluation?> =
196+
open suspend fun fetchEvaluation(appPackageName: String, microG: Int, rooted: Int): Result<Evaluation?> =
197197
runCatching {
198198
val answer = evaluationsApi.getSingleEvaluationAsync(
199199
appPackageName,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.klee.sapio.data
2+
3+
import com.klee.sapio.data.local.Converters
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Assert.assertNull
6+
import org.junit.Test
7+
import java.util.Date
8+
9+
class ConvertersTest {
10+
11+
private val converters = Converters()
12+
13+
@Test
14+
fun `fromTimestamp returns null when value is null`() {
15+
assertNull(converters.fromTimestamp(null))
16+
}
17+
18+
@Test
19+
fun `fromTimestamp returns correct Date for given timestamp`() {
20+
val timestamp = 1700000000000L
21+
val result = converters.fromTimestamp(timestamp)
22+
assertEquals(Date(timestamp), result)
23+
}
24+
25+
@Test
26+
fun `fromTimestamp returns epoch date for zero`() {
27+
val result = converters.fromTimestamp(0L)
28+
assertEquals(Date(0), result)
29+
}
30+
31+
@Test
32+
fun `dateToTimestamp returns null when date is null`() {
33+
assertNull(converters.dateToTimestamp(null))
34+
}
35+
36+
@Test
37+
fun `dateToTimestamp returns correct timestamp for given date`() {
38+
val timestamp = 1700000000000L
39+
val date = Date(timestamp)
40+
assertEquals(timestamp, converters.dateToTimestamp(date))
41+
}
42+
43+
@Test
44+
fun `dateToTimestamp returns zero for epoch date`() {
45+
assertEquals(0L, converters.dateToTimestamp(Date(0)))
46+
}
47+
48+
@Test
49+
fun `fromTimestamp and dateToTimestamp are inverse operations`() {
50+
val timestamp = 1700000000000L
51+
val date = converters.fromTimestamp(timestamp)
52+
assertEquals(timestamp, converters.dateToTimestamp(date))
53+
}
54+
}

0 commit comments

Comments
 (0)