Skip to content

Commit 172dda9

Browse files
macgillsashishkumar468
authored andcommitted
commons-app#3843 Unit Test DepictedItem - add tests (commons-app#3844)
1 parent 2073841 commit 172dda9

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictedItem.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ data class DepictedItem constructor(
3636
place.longDescription
3737
)
3838

39-
constructor(entity: Entities.Entity, name: String, description: String) : this(
39+
private constructor(entity: Entities.Entity, name: String, description: String) : this(
4040
name,
4141
description,
4242
entity[IMAGE].primaryImageValue?.let {
@@ -69,7 +69,7 @@ private fun List<Statement_partial>?.toIds(): List<String> {
6969
}
7070

7171
private val List<Statement_partial>?.primaryImageValue: DataValue.ValueString?
72-
get() = this?.first()?.mainSnak?.dataValue as? DataValue.ValueString
72+
get() = this?.firstOrNull()?.mainSnak?.dataValue as? DataValue.ValueString
7373

7474
operator fun Entities.Entity.get(property: WikidataProperties) =
7575
statements?.get(property.propertyName)

app/src/test/kotlin/ModelFunctions.kt

+63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import com.nhaarman.mockitokotlin2.mock
2+
import com.nhaarman.mockitokotlin2.whenever
13
import fr.free.nrw.commons.category.CategoryItem
4+
import fr.free.nrw.commons.location.LatLng
5+
import fr.free.nrw.commons.nearby.Label
6+
import fr.free.nrw.commons.nearby.Place
7+
import fr.free.nrw.commons.nearby.Sitelinks
28
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
9+
import org.wikipedia.wikidata.*
310

411
fun depictedItem(
512
name: String = "label",
@@ -21,3 +28,59 @@ fun depictedItem(
2128

2229
fun categoryItem(name: String = "name", selected: Boolean = false) =
2330
CategoryItem(name, selected)
31+
32+
fun place(
33+
name: String = "name",
34+
label: Label? = null,
35+
longDescription: String = "longDescription",
36+
latLng: LatLng? = null,
37+
category: String = "category",
38+
siteLinks: Sitelinks? = null,
39+
pic: String = "pic",
40+
destroyed: String = "destroyed"
41+
): Place {
42+
return Place(name, label, longDescription, latLng, category, siteLinks, pic, destroyed)
43+
}
44+
45+
fun entityId(wikiBaseEntityValue: WikiBaseEntityValue = wikiBaseEntityValue()) =
46+
DataValue.EntityId(wikiBaseEntityValue)
47+
48+
fun wikiBaseEntityValue(
49+
entityType: String = "type",
50+
id: String = "id",
51+
numericId: Long = 0
52+
) = WikiBaseEntityValue(entityType, id, numericId)
53+
54+
fun statement(
55+
mainSnak: Snak_partial = snak(),
56+
rank: String = "rank",
57+
type: String = "type"
58+
) = Statement_partial(mainSnak, type, rank)
59+
60+
fun snak(
61+
snakType: String = "type",
62+
property: String = "property",
63+
dataValue: DataValue = valueString("")
64+
) = Snak_partial(snakType, property, dataValue)
65+
66+
fun valueString(value: String) = DataValue.ValueString(value)
67+
68+
fun entity(
69+
labels: Map<String, String> = emptyMap(),
70+
descriptions: Map<String, String> = emptyMap(),
71+
statements: Map<String, List<Statement_partial>>? = emptyMap(),
72+
id: String = "id"
73+
) = mock<Entities.Entity>().apply {
74+
val mockedLabels = labels.mockLabels()
75+
whenever(labels()).thenReturn(mockedLabels)
76+
val mockedDescriptions = descriptions.mockLabels()
77+
whenever(descriptions()).thenReturn(mockedDescriptions)
78+
whenever(this.statements).thenReturn(statements)
79+
whenever(id()).thenReturn(id)
80+
}
81+
82+
private fun Map<String, String>.mockLabels(): Map<String, Entities.Label> {
83+
return mapValues { entry ->
84+
mock<Entities.Label>().also { whenever(it.value()).thenReturn(entry.value) }
85+
}
86+
}

0 commit comments

Comments
 (0)