mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Fixed the issue added tests (#4933)
This commit is contained in:
parent
31d63d1a2a
commit
17ac591233
2 changed files with 80 additions and 17 deletions
|
|
@ -51,26 +51,42 @@ class DepictsClient @Inject constructor(private val depictsInterface: DepictsInt
|
||||||
flatMap(::getEntities)
|
flatMap(::getEntities)
|
||||||
.map { entities ->
|
.map { entities ->
|
||||||
entities.entities().values.map { entity ->
|
entities.entities().values.map { entity ->
|
||||||
if (entity.descriptions().byLanguageOrFirstOrEmpty() == "") {
|
mapToDepictItem(entity)
|
||||||
val entities: Entities = getEntities(entity[WikidataProperties.INSTANCE_OF]
|
|
||||||
.toIds()[0]).blockingGet()
|
|
||||||
val nameAsDescription = entities.entities().values.first().labels()
|
|
||||||
.byLanguageOrFirstOrEmpty()
|
|
||||||
DepictedItem(
|
|
||||||
entity,
|
|
||||||
entity.labels().byLanguageOrFirstOrEmpty(),
|
|
||||||
nameAsDescription
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
DepictedItem(
|
|
||||||
entity,
|
|
||||||
entity.labels().byLanguageOrFirstOrEmpty(),
|
|
||||||
entity.descriptions().byLanguageOrFirstOrEmpty()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert different entities into DepictedItem
|
||||||
|
*/
|
||||||
|
private fun mapToDepictItem(entity: Entities.Entity): DepictedItem {
|
||||||
|
return if (entity.descriptions().byLanguageOrFirstOrEmpty() == "") {
|
||||||
|
val instanceOfIDs = entity[WikidataProperties.INSTANCE_OF]
|
||||||
|
.toIds()
|
||||||
|
if (instanceOfIDs.isNotEmpty()) {
|
||||||
|
val entities: Entities = getEntities(instanceOfIDs[0]).blockingGet()
|
||||||
|
val nameAsDescription = entities.entities().values.first().labels()
|
||||||
|
.byLanguageOrFirstOrEmpty()
|
||||||
|
DepictedItem(
|
||||||
|
entity,
|
||||||
|
entity.labels().byLanguageOrFirstOrEmpty(),
|
||||||
|
nameAsDescription
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
DepictedItem(
|
||||||
|
entity,
|
||||||
|
entity.labels().byLanguageOrFirstOrEmpty(),
|
||||||
|
""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DepictedItem(
|
||||||
|
entity,
|
||||||
|
entity.labels().byLanguageOrFirstOrEmpty(),
|
||||||
|
entity.descriptions().byLanguageOrFirstOrEmpty()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to get Entities.Label by default language from the map.
|
* Tries to get Entities.Label by default language from the map.
|
||||||
* If that returns null, Tries to retrieve first element from the map.
|
* If that returns null, Tries to retrieve first element from the map.
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package fr.free.nrw.commons.explore.depictions
|
||||||
import com.nhaarman.mockitokotlin2.mock
|
import com.nhaarman.mockitokotlin2.mock
|
||||||
import com.nhaarman.mockitokotlin2.whenever
|
import com.nhaarman.mockitokotlin2.whenever
|
||||||
import depictSearchItem
|
import depictSearchItem
|
||||||
|
import entity
|
||||||
import fr.free.nrw.commons.mwapi.Binding
|
import fr.free.nrw.commons.mwapi.Binding
|
||||||
import fr.free.nrw.commons.mwapi.Result
|
import fr.free.nrw.commons.mwapi.Result
|
||||||
import fr.free.nrw.commons.mwapi.SparqlResponse
|
import fr.free.nrw.commons.mwapi.SparqlResponse
|
||||||
|
|
@ -15,6 +16,7 @@ import org.junit.Test
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.MockitoAnnotations
|
import org.mockito.MockitoAnnotations
|
||||||
import org.wikipedia.wikidata.*
|
import org.wikipedia.wikidata.*
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
|
||||||
class DepictsClientTest {
|
class DepictsClientTest {
|
||||||
|
|
||||||
|
|
@ -108,4 +110,49 @@ class DepictsClientTest {
|
||||||
emptyList(), false, "Q10")
|
emptyList(), false, "Q10")
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Test mapToDepictItem when description is not empty`() {
|
||||||
|
val method: Method = DepictsClient::class.java.getDeclaredMethod(
|
||||||
|
"mapToDepictItem",
|
||||||
|
Entities.Entity::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(depictsClient, entity(descriptions = mapOf("en" to "Test")))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Test mapToDepictItem when description is empty and P31 doesn't exists`() {
|
||||||
|
val method: Method = DepictsClient::class.java.getDeclaredMethod(
|
||||||
|
"mapToDepictItem",
|
||||||
|
Entities.Entity::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(depictsClient, entity())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `Test mapToDepictItem when description is empty and P31 exists`() {
|
||||||
|
val entities = mock<Entities>()
|
||||||
|
val entity = mock<Entities.Entity>()
|
||||||
|
val statementPartial = mock<Statement_partial>()
|
||||||
|
whenever(entity.statements).thenReturn(mapOf("P31" to listOf(statementPartial)))
|
||||||
|
whenever(statementPartial.mainSnak).thenReturn(
|
||||||
|
Snak_partial("test", "P31",
|
||||||
|
DataValue.EntityId(
|
||||||
|
WikiBaseEntityValue("wikibase-entityid", "Q10", 10L)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
whenever(depictsInterface.getEntities("Q10")).thenReturn(Single.just(entities))
|
||||||
|
whenever(entities.entities())
|
||||||
|
.thenReturn(mapOf("test" to entity))
|
||||||
|
whenever(entity.id()).thenReturn("Q10")
|
||||||
|
val method: Method = DepictsClient::class.java.getDeclaredMethod(
|
||||||
|
"mapToDepictItem",
|
||||||
|
Entities.Entity::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(depictsClient, entity)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue