mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Fix unit tests (#5947)
* move createLocale() method to companion object and add test dependency * use mockk() from Mockk library for mocking sealed classes * change method parameter to null-able String type * add null check for accessing property from unit tests * change method signature to match old method's signature It fixes the NullPointerException when running ImageProcessingUnitTest * Fix unresolved references and make properties public for unit tests * fix tests in UploadRepositoryUnitTest by making return type null-able
This commit is contained in:
parent
fe347c21fd
commit
e070c5dbe8
14 changed files with 56 additions and 52 deletions
|
|
@ -124,7 +124,9 @@ class CategoryClient
|
|||
}.map {
|
||||
it
|
||||
.filter { page ->
|
||||
!page.categoryInfo().isHidden
|
||||
// Null check is not redundant because some values could be null
|
||||
// for mocks when running unit tests
|
||||
page.categoryInfo()?.isHidden != true
|
||||
}.map {
|
||||
CategoryItem(
|
||||
it.title().replace(CATEGORY_PREFIX, ""),
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import android.content.ContentValues
|
|||
import android.database.Cursor
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.os.RemoteException
|
||||
import java.util.ArrayList
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
import javax.inject.Provider
|
||||
|
|
@ -163,9 +162,9 @@ class RecentLanguagesDao @Inject constructor(
|
|||
COLUMN_CODE
|
||||
)
|
||||
|
||||
private const val DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||
const val DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||
|
||||
private const val CREATE_TABLE_STATEMENT = "CREATE TABLE $TABLE_NAME (" +
|
||||
const val CREATE_TABLE_STATEMENT = "CREATE TABLE $TABLE_NAME (" +
|
||||
"$COLUMN_NAME STRING," +
|
||||
"$COLUMN_CODE STRING PRIMARY KEY" +
|
||||
");"
|
||||
|
|
|
|||
|
|
@ -19,10 +19,10 @@ import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
|||
import io.reactivex.Flowable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import timber.log.Timber
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* The repository class for UploadActivity
|
||||
|
|
@ -46,7 +46,7 @@ class UploadRepository @Inject constructor(
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
fun buildContributions(): Observable<Contribution> {
|
||||
fun buildContributions(): Observable<Contribution>? {
|
||||
return uploadModel.buildContributions()
|
||||
}
|
||||
|
||||
|
|
@ -150,7 +150,7 @@ class UploadRepository @Inject constructor(
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
fun getSelectedLicense(): String {
|
||||
fun getSelectedLicense(): String? {
|
||||
return uploadModel.selectedLicense
|
||||
}
|
||||
|
||||
|
|
@ -173,11 +173,11 @@ class UploadRepository @Inject constructor(
|
|||
* @return
|
||||
*/
|
||||
fun preProcessImage(
|
||||
uploadableFile: UploadableFile,
|
||||
uploadableFile: UploadableFile?,
|
||||
place: Place?,
|
||||
similarImageInterface: SimilarImageInterface,
|
||||
similarImageInterface: SimilarImageInterface?,
|
||||
inAppPictureLocation: LatLng?
|
||||
): Observable<UploadItem> {
|
||||
): Observable<UploadItem>? {
|
||||
return uploadModel.preProcessImage(
|
||||
uploadableFile,
|
||||
place,
|
||||
|
|
@ -193,7 +193,7 @@ class UploadRepository @Inject constructor(
|
|||
* @param location Location of the image
|
||||
* @return Quality of UploadItem
|
||||
*/
|
||||
fun getImageQuality(uploadItem: UploadItem, location: LatLng?): Single<Int> {
|
||||
fun getImageQuality(uploadItem: UploadItem, location: LatLng?): Single<Int>? {
|
||||
return uploadModel.getImageQuality(uploadItem, location)
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +213,7 @@ class UploadRepository @Inject constructor(
|
|||
* @param uploadItem UploadItem whose caption is to be checked
|
||||
* @return Quality of caption of the UploadItem
|
||||
*/
|
||||
fun getCaptionQuality(uploadItem: UploadItem): Single<Int> {
|
||||
fun getCaptionQuality(uploadItem: UploadItem): Single<Int>? {
|
||||
return uploadModel.getCaptionQuality(uploadItem)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -471,18 +471,20 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
editor.apply()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Locale based on different types of language codes
|
||||
* @param languageCode
|
||||
* @return Locale and throws error for invalid language codes
|
||||
*/
|
||||
fun createLocale(languageCode: String): Locale {
|
||||
val parts = languageCode.split("-")
|
||||
return when (parts.size) {
|
||||
1 -> Locale(parts[0])
|
||||
2 -> Locale(parts[0], parts[1])
|
||||
3 -> Locale(parts[0], parts[1], parts[2])
|
||||
else -> throw IllegalArgumentException("Invalid language code: $languageCode")
|
||||
companion object {
|
||||
/**
|
||||
* Create Locale based on different types of language codes
|
||||
* @param languageCode
|
||||
* @return Locale and throws error for invalid language codes
|
||||
*/
|
||||
fun createLocale(languageCode: String): Locale {
|
||||
val parts = languageCode.split("-")
|
||||
return when (parts.size) {
|
||||
1 -> Locale(parts[0])
|
||||
2 -> Locale(parts[0], parts[1])
|
||||
3 -> Locale(parts[0], parts[1], parts[2])
|
||||
else -> throw IllegalArgumentException("Invalid language code: $languageCode")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class ImageUtilsWrapper @Inject constructor() {
|
|||
|
||||
fun checkImageGeolocationIsDifferent(
|
||||
geolocationOfFileString: String,
|
||||
latLng: LatLng
|
||||
latLng: LatLng?
|
||||
): Single<Int> {
|
||||
return Single.fromCallable {
|
||||
ImageUtils.checkImageGeolocationIsDifferent(geolocationOfFileString, latLng)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
package fr.free.nrw.commons.utils
|
||||
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
object MediaDataExtractorUtil {
|
||||
|
||||
/**
|
||||
|
|
@ -13,8 +9,8 @@ object MediaDataExtractorUtil {
|
|||
* @return
|
||||
*/
|
||||
@JvmStatic
|
||||
fun extractCategoriesFromList(source: String): List<String> {
|
||||
if (source.isBlank()) {
|
||||
fun extractCategoriesFromList(source: String?): List<String> {
|
||||
if (source.isNullOrBlank()) {
|
||||
return emptyList()
|
||||
}
|
||||
val cats = source.split("|")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue