Fixes 4539 : When Wikidata has no description showing instance_of's label as a description (#4799)

* Showing instance_of's name as a description

* Indentation fixed

* DepictsClient Test added
This commit is contained in:
Ayan Sarkar 2022-02-16 10:49:27 +05:30 committed by GitHub
parent c2bc5b4f23
commit 27e3f20ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 8 deletions

View file

@ -7,14 +7,14 @@ import fr.free.nrw.commons.mwapi.Binding
import fr.free.nrw.commons.mwapi.Result
import fr.free.nrw.commons.mwapi.SparqlResponse
import fr.free.nrw.commons.upload.depicts.DepictsInterface
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
import fr.free.nrw.commons.wikidata.model.DepictSearchResponse
import io.reactivex.Single
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.wikipedia.wikidata.Entities
import org.wikipedia.wikidata.*
class DepictsClientTest {
@ -29,7 +29,6 @@ class DepictsClientTest {
}
@Test
@Ignore()
fun searchForDepictions() {
val depictSearchResponse = mock<DepictSearchResponse>()
whenever(depictsInterface.searchForDepicts("query", "1", "en", "en", "0"))
@ -52,7 +51,7 @@ class DepictsClientTest {
}
@Test
fun toDepictions() {
fun `Test toDepictions when description is empty`() {
val sparqlResponse = mock<SparqlResponse>()
val result = mock<Result>()
whenever(sparqlResponse.results).thenReturn(result)
@ -62,10 +61,51 @@ class DepictsClientTest {
whenever(binding1.id).thenReturn("1")
whenever(binding2.id).thenReturn("2")
val entities = mock<Entities>()
val entity = mock<Entities.Entity>()
val statementPartial = mock<Statement_partial>()
whenever(depictsInterface.getEntities("1|2")).thenReturn(Single.just(entities))
whenever(entities.entities()).thenReturn(emptyMap())
whenever(entities.entities()).thenReturn(mapOf("en" to entity))
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(entity.id()).thenReturn("Q10")
depictsClient.toDepictions(Single.just(sparqlResponse))
.test()
.assertValue(emptyList())
.assertValue(listOf(
DepictedItem("", "", null,
listOf("Q10"), emptyList(), false, "Q10")
))
}
@Test
fun `Test toDepictions when description is not empty`() {
val sparqlResponse = mock<SparqlResponse>()
val result = mock<Result>()
whenever(sparqlResponse.results).thenReturn(result)
val binding1 = mock<Binding>()
val binding2 = mock<Binding>()
whenever(result.bindings).thenReturn(listOf(binding1, binding2))
whenever(binding1.id).thenReturn("1")
whenever(binding2.id).thenReturn("2")
val entities = mock<Entities>()
val entity = mock<Entities.Entity>()
whenever(depictsInterface.getEntities("1|2")).thenReturn(Single.just(entities))
whenever(entities.entities()).thenReturn(mapOf("en" to entity))
whenever(entity.descriptions()).thenReturn(mapOf("en" to
Entities.Label("en", "Test description")
))
whenever(entity.id()).thenReturn("Q10")
depictsClient.toDepictions(Single.just(sparqlResponse))
.test()
.assertValue(listOf(
DepictedItem("", "", null, listOf("Q10"),
emptyList(), false, "Q10")
))
}
}