#1386 get tests compiling

This commit is contained in:
Sean Mac Gillicuddy 2019-09-03 17:10:11 +01:00
parent 8458d721d5
commit f7f4d6e1f1
7 changed files with 1621 additions and 1029 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,74 +3,6 @@
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
"entities": [
{
"id": "1:7257718270326155947",
"lastPropertyId": "17:8085320504542486236",
"name": "DownloadEntity",
"properties": [
{
"id": "1:2266566996008201697",
"name": "id"
},
{
"id": "2:1953917250527765737",
"name": "downloadId"
},
{
"id": "5:6575412958851693470",
"name": "bookId"
},
{
"id": "6:1075612111256674117",
"name": "title"
},
{
"id": "7:2831524841121029990",
"name": "description"
},
{
"id": "8:2334902404590133038",
"name": "language"
},
{
"id": "9:5087250349738158996",
"name": "creator"
},
{
"id": "10:6128960350043895299",
"name": "publisher"
},
{
"id": "11:3850323036475883785",
"name": "date"
},
{
"id": "12:5288623325038033644",
"name": "url"
},
{
"id": "13:2501711400901908648",
"name": "articleCount"
},
{
"id": "14:3550975911715416030",
"name": "mediaCount"
},
{
"id": "15:8949996430663588693",
"name": "size"
},
{
"id": "16:7554483297276446029",
"name": "name"
},
{
"id": "17:8085320504542486236",
"name": "favIcon"
}
],
"relations": []
},
{
"id": "3:5536749840871435068",
"lastPropertyId": "16:6142333908132117423",
@ -367,7 +299,8 @@
"modelVersion": 4,
"modelVersionParserMinimum": 4,
"retiredEntityUids": [
349148274283701276
349148274283701276,
7257718270326155947
],
"retiredIndexUids": [
1293695782925933448,
@ -394,7 +327,22 @@
7273406943564025911,
428251106490095982,
5162677841083528491,
7886541039889727771
7886541039889727771,
2266566996008201697,
1953917250527765737,
6575412958851693470,
1075612111256674117,
2831524841121029990,
2334902404590133038,
5087250349738158996,
6128960350043895299,
3850323036475883785,
5288623325038033644,
2501711400901908648,
3550975911715416030,
8949996430663588693,
7554483297276446029,
8085320504542486236
],
"retiredRelationUids": [],
"version": 1

View File

@ -51,9 +51,7 @@ class DownloadViewHolder(override val containerView: View) : RecyclerView.ViewHo
stop.setOnClickListener {
itemClickListener.invoke(downloadItem)
}
downloadState.text = toReadableState(
downloadItem.downloadState, containerView.context
)
downloadState.text = toReadableState(downloadItem.downloadState, containerView.context)
eta.text = downloadItem.eta.takeIf { it.seconds > 0L }?.toHumanReadableTime() ?: ""
}

View File

@ -61,7 +61,7 @@ class FileSearchTest {
every { Environment.getExternalStorageDirectory() } returns externalStorageDirectory
every { externalStorageDirectory.absolutePath } returns "/externalStorageDirectory"
every { context.contentResolver } returns contentResolver
every { StorageDeviceUtils.getReadableStorage(context, false) } returns arrayListOf(
every { StorageDeviceUtils.getReadableStorage(context) } returns arrayListOf(
storageDevice
)
every { storageDevice.name } returns "/deviceDir"
@ -80,7 +80,7 @@ class FileSearchTest {
@Test
fun `scan of directory that doesn't exist returns nothing`() {
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
fileSearch.scan("doesNotExist")
fileSearch.scan()
.test()
.assertValue(listOf())
}
@ -91,7 +91,8 @@ class FileSearchTest {
val zimaaFile = File.createTempFile("fileToFind2", ".zimaa")
File.createTempFile("willNotFind", ".txt")
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
val fileList = fileSearch.scan(zimFile.parent)
every { storageDevice.name } returns zimFile.parent
val fileList = fileSearch.scan()
.test()
.values()[0]
assertThat(fileList).containsExactlyInAnyOrder(zimFile, zimaaFile)
@ -106,7 +107,8 @@ class FileSearchTest {
".zim",
File("$tempRoot${File.separator}dir").apply { mkdirs() })
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
val fileList = fileSearch.scan(zimFile.parentFile.parent)
every { storageDevice.name } returns tempRoot
val fileList = fileSearch.scan()
.test()
.values()[0]
assertThat(fileList).containsExactlyInAnyOrder(zimFile)
@ -120,7 +122,7 @@ class FileSearchTest {
fun `scan media store, if files are readable they are returned`() {
val fileToFind = File.createTempFile("fileToFind", ".zim")
expectFromMediaStore(fileToFind)
fileSearch.scan("")
fileSearch.scan()
.test()
.assertValue(listOf(fileToFind))
}
@ -130,7 +132,7 @@ class FileSearchTest {
val unreadableFile = File.createTempFile("fileToFind", ".zim")
expectFromMediaStore(unreadableFile)
unreadableFile.delete()
fileSearch.scan("")
fileSearch.scan()
.test()
.assertValue(listOf())
}

View File

@ -25,6 +25,7 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import io.reactivex.Single
import io.reactivex.processors.BehaviorProcessor
import io.reactivex.processors.PublishProcessor
import io.reactivex.schedulers.TestScheduler
import org.junit.jupiter.api.AfterAll
@ -38,17 +39,12 @@ import org.kiwix.kiwixmobile.book
import org.kiwix.kiwixmobile.bookOnDisk
import org.kiwix.kiwixmobile.data.DataSource
import org.kiwix.kiwixmobile.data.remote.KiwixService
import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewDownloadDao
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao
import org.kiwix.kiwixmobile.downloadItem
import org.kiwix.kiwixmobile.downloadModel
import org.kiwix.kiwixmobile.downloadStatus
import org.kiwix.kiwixmobile.downloader.Downloader
import org.kiwix.kiwixmobile.downloader.model.DownloadItem
import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.downloader.model.DownloadState
import org.kiwix.kiwixmobile.downloader.model.DownloadStatus
import org.kiwix.kiwixmobile.downloader.model.UriToFileConverter
import org.kiwix.kiwixmobile.language
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.libraryNetworkEntity
@ -65,7 +61,6 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.LibraryListItem
import java.io.File
import java.util.Locale
import java.util.concurrent.TimeUnit.MILLISECONDS
import java.util.concurrent.TimeUnit.SECONDS
@ -73,17 +68,15 @@ import java.util.concurrent.TimeUnit.SECONDS
@ExtendWith(InstantExecutorExtension::class)
class ZimManageViewModelTest {
private val newDownloadDao: NewDownloadDao = mockk()
private val downloadDao: FetchDownloadDao = mockk()
private val newBookDao: NewBookDao = mockk()
private val newLanguagesDao: NewLanguagesDao = mockk()
private val downloader: Downloader = mockk()
private val storageObserver: StorageObserver = mockk()
private val kiwixService: KiwixService = mockk()
private val application: Application = mockk()
private val connectivityBroadcastReceiver: ConnectivityBroadcastReceiver = mockk()
private val bookUtils: BookUtils = mockk()
private val fat32Checker: Fat32Checker = mockk()
private val uriToFileConverter: UriToFileConverter = mockk()
private val defaultLanguageProvider: DefaultLanguageProvider = mockk()
private val dataSource: DataSource = mockk()
lateinit var viewModel: ZimManageViewModel
@ -92,7 +85,7 @@ class ZimManageViewModelTest {
private val booksOnFileSystem: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create()
private val books: PublishProcessor<List<BookOnDisk>> = PublishProcessor.create()
private val languages: PublishProcessor<List<Language>> = PublishProcessor.create()
private val fileSystemStates: PublishProcessor<FileSystemState> = PublishProcessor.create()
private val fileSystemStates: BehaviorProcessor<FileSystemState> = BehaviorProcessor.create()
private val networkStates: PublishProcessor<NetworkState> = PublishProcessor.create()
private val booksOnDiskListItems: PublishProcessor<List<BooksOnDiskListItem>> =
PublishProcessor.create()
@ -112,7 +105,7 @@ class ZimManageViewModelTest {
fun init() {
clearAllMocks()
every { connectivityBroadcastReceiver.action } returns "test"
every { newDownloadDao.downloads() } returns downloads
every { downloadDao.downloads() } returns downloads
every { newBookDao.books() } returns books
every { storageObserver.booksOnFileSystem } returns booksOnFileSystem
every { newLanguagesDao.languages() } returns languages
@ -121,9 +114,17 @@ class ZimManageViewModelTest {
every { application.registerReceiver(any(), any()) } returns mockk()
every { dataSource.booksOnDiskAsListItems() } returns booksOnDiskListItems
viewModel = ZimManageViewModel(
newDownloadDao, newBookDao, newLanguagesDao, downloader,
storageObserver, kiwixService, application, connectivityBroadcastReceiver, bookUtils,
fat32Checker, uriToFileConverter, defaultLanguageProvider, dataSource
downloadDao,
newBookDao,
newLanguagesDao,
storageObserver,
kiwixService,
application,
connectivityBroadcastReceiver,
bookUtils,
fat32Checker,
defaultLanguageProvider,
dataSource
)
testScheduler.triggerActions()
}
@ -150,63 +151,19 @@ class ZimManageViewModelTest {
@Nested
inner class Downloads {
@Test
fun `on emission from database query and render downloads`() {
val expectedStatus = downloadStatus()
expectStatusWith(listOf(expectedStatus))
fun `on emission from database render downloads`() {
expectDownloads()
viewModel.downloadItems
.test()
.assertValue(listOf(DownloadItem(expectedStatus)))
.assertValue(listOf(downloadItem()))
}
@Test
fun `on emission of successful status create a book and delete the download`() {
every { uriToFileConverter.convert(any()) } returns File("test")
val expectedStatus = downloadStatus(
downloadId = 10L,
downloadState = DownloadState.Successful
)
expectStatusWith(listOf(expectedStatus))
val element = expectedStatus.toBookOnDisk(uriToFileConverter)
verify {
newBookDao.insert(listOf(element))
newDownloadDao.delete(10L)
}
}
@Test
fun `if statuses don't have a matching Id for download in db over 3 secs then delete`() {
expectStatusWith(
listOf(downloadStatus(downloadId = 1)),
listOf(downloadModel(downloadId = 1), downloadModel(downloadId = 3))
)
testScheduler.advanceTimeBy(3, SECONDS)
testScheduler.triggerActions()
verify {
newDownloadDao.delete(3)
}
}
@Test
fun `if statuses do have a matching Id for download in db over 3 secs then don't delete`() {
expectStatusWith(
listOf(downloadStatus(downloadId = 1)),
listOf(downloadModel(downloadId = 1))
)
testScheduler.advanceTimeBy(3, SECONDS)
testScheduler.triggerActions()
verify(exactly = 0) {
newDownloadDao.delete(any())
}
}
private fun expectStatusWith(
expectedStatuses: List<DownloadStatus>,
private fun expectDownloads(
expectedDownloads: List<DownloadModel> = listOf(
downloadModel()
)
) {
every { application.getString(any()) } returns ""
every { downloader.queryStatus(expectedDownloads) } returns expectedStatuses
downloads.offer(expectedDownloads)
testScheduler.triggerActions()
testScheduler.advanceTimeBy(1, SECONDS)
@ -387,7 +344,6 @@ class ZimManageViewModelTest {
@Test
fun `library update removes from sources`() {
every { downloader.queryStatus(any()) } returns emptyList()
every { application.getString(R.string.your_languages) } returns "1"
every { application.getString(R.string.other_languages) } returns "2"
val bookAlreadyOnDisk = book(

View File

@ -32,7 +32,7 @@ import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.book
import org.kiwix.kiwixmobile.bookOnDisk
import org.kiwix.kiwixmobile.data.ZimContentProvider
import org.kiwix.kiwixmobile.database.newdb.dao.NewDownloadDao
import org.kiwix.kiwixmobile.database.newdb.dao.FetchDownloadDao
import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.resetSchedulers
@ -44,7 +44,7 @@ import java.io.File
class StorageObserverTest {
private val sharedPreferenceUtil: SharedPreferenceUtil = mockk()
private val newDownloadDao: NewDownloadDao = mockk()
private val downloadDao: FetchDownloadDao = mockk()
private val fileSearch: FileSearch = mockk()
private val downloadModel = mockk<DownloadModel>()
private val file = mockk<File>()
@ -66,9 +66,9 @@ class StorageObserverTest {
@BeforeEach fun init() {
clearAllMocks()
every { sharedPreferenceUtil.prefStorage } returns "a"
every { fileSearch.scan("a") } returns files
every { newDownloadDao.downloads() } returns downloads
storageObserver = StorageObserver(newDownloadDao, fileSearch)
every { fileSearch.scan() } returns files
every { downloadDao.downloads() } returns downloads
storageObserver = StorageObserver(downloadDao, fileSearch)
}
@Test

View File

@ -17,10 +17,15 @@
*/
package org.kiwix.kiwixmobile
import com.tonyodev.fetch2.Error
import com.tonyodev.fetch2.Status
import com.tonyodev.fetch2.Status.NONE
import org.kiwix.kiwixmobile.downloader.model.Base64String
import org.kiwix.kiwixmobile.downloader.model.DownloadItem
import org.kiwix.kiwixmobile.downloader.model.DownloadModel
import org.kiwix.kiwixmobile.downloader.model.DownloadState
import org.kiwix.kiwixmobile.downloader.model.DownloadState.Pending
import org.kiwix.kiwixmobile.downloader.model.DownloadStatus
import org.kiwix.kiwixmobile.downloader.model.Seconds
import org.kiwix.kiwixmobile.language.adapter.LanguageListItem.LanguageItem
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
@ -39,29 +44,37 @@ fun bookOnDisk(
file: File = File("")
) = BookOnDisk(databaseId, book, file)
fun downloadStatus(
downloadId: Long = 0L,
title: String = "",
description: String = "",
downloadState: DownloadState = Pending,
bytesDownloadedSoFar: Long = 0L,
totalSizeBytes: Long = 0L,
lastModified: String = "",
localUri: String? = null,
mediaProviderUri: String? = null,
mediaType: String? = null,
uri: String? = null,
fun downloadModel(
databaseId: Long = 1L,
downloadId: Long = 1L,
file: String = "",
etaInMilliSeconds: Long = 0L,
bytesDownloaded: Long = 1L,
totalSizeOfDownload: Long = 1L,
status: Status = NONE,
error: Error = Error.NONE,
progress: Int = 1,
book: Book = book()
) = DownloadStatus(
downloadId, title, description, downloadState, bytesDownloadedSoFar,
totalSizeBytes, lastModified, localUri, mediaProviderUri, mediaType, uri, book
) = DownloadModel(
databaseId, downloadId, file, etaInMilliSeconds, bytesDownloaded, totalSizeOfDownload,
status, error, progress, book
)
fun downloadModel(
databaseId: Long? = 1L,
fun downloadItem(
downloadId: Long = 1L,
book: Book = book()
) = DownloadModel(databaseId, downloadId, book)
favIcon: Base64String = Base64String("favIcon"),
title: String = "title",
description: String = "description",
bytesDownloaded: Long = 1L,
totalSizeBytes: Long = 1L,
progress: Int = 1,
eta: Seconds = Seconds(0),
state: DownloadState = Pending
) =
DownloadItem(
downloadId, favIcon, title, description, bytesDownloaded,
totalSizeBytes, progress, eta, state
)
fun language(
id: Long = 0,