Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test search view model and cleanup #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions.coroutines}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.coroutines}"

// AndroidX
Expand Down Expand Up @@ -106,6 +107,7 @@ dependencies {
testImplementation "com.google.truth:truth:${versions.truth}"
testImplementation "androidx.arch.core:core-testing:${versions.archTesting}"
testImplementation "org.robolectric:robolectric:${versions.robolectric}"
testImplementation "io.mockk:mockk:${versions.mockk}"

// Instrumentation Testing
androidTestImplementation "androidx.test:runner:${versions.androidxTestRunner}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MediaSessionCallback(

override fun onPlayFromSearch(query: String?, extras: Bundle?) {
query?.let {
val song = songsRepository.searchSongs(query, 1)
val song = songsRepository.getSongs(query, 1)
if (song.isNotEmpty()) {
songPlayer.playSong(song.first())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface SongsRepository {

fun getSongFromPath(songPath: String): Song

fun searchSongs(searchString: String, limit: Int): List<Song>
fun getSongs(paramString: String, limit: Int): List<Song>

fun deleteTracks(ids: LongArray): Int
}
Expand Down Expand Up @@ -91,11 +91,11 @@ class RealSongsRepository(
} ?: throw IllegalStateException("Unable to query $uri, system returned null.")
}

override fun searchSongs(searchString: String, limit: Int): List<Song> {
val result = makeSongCursor("title LIKE ?", arrayOf("$searchString%"))
override fun getSongs(paramString: String, limit: Int): List<Song> {
val result = makeSongCursor("title LIKE ?", arrayOf("$paramString%"))
.mapList(true) { Song.fromCursor(this) }
if (result.size < limit) {
val moreSongs = makeSongCursor("title LIKE ?", arrayOf("%_$searchString%"))
val moreSongs = makeSongCursor("title LIKE ?", arrayOf("%_$paramString%"))
.mapList(true) { Song.fromCursor(this) }
result += moreSongs
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.naman14.timberx.ui.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.naman14.timberx.models.Album
import com.naman14.timberx.models.Artist
Expand All @@ -35,13 +36,13 @@ class SearchViewModel(
private val searchData = SearchData()
private val _searchLiveData = MutableLiveData<SearchData>()

val searchLiveData = _searchLiveData
val searchLiveData: LiveData<SearchData> = _searchLiveData

fun search(query: String) {
if (query.length >= 3) {
launch {
val songs = withContext(IO) {
songsRepository.searchSongs(query, 10)
songsRepository.getSongs(query, 10)
}
if (songs.isNotEmpty()) {
searchData.songs = songs.toMutableList()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2019 Naman Dwivedi.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
package com.naman14.timberx.ui.viewmodels

import com.naman14.timberx.repository.AlbumRepository
import com.naman14.timberx.repository.ArtistRepository
import com.naman14.timberx.repository.SongsRepository
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.setMain
import org.junit.Before
import org.junit.Test

class SearchViewModelTest {
val songsRepo = mockk<SongsRepository>()
val albumRepo = mockk<AlbumRepository>()
val artistRepo = mockk<ArtistRepository>()
val tested = SearchViewModel(songsRepo, albumRepo, artistRepo)

@Before
fun setup() {
Dispatchers.setMain(Dispatchers.IO)
}

@Test
fun `given query when search called then searchSongs called with query and limit 10`() {
// given
val query = "song"
// when
tested.search(query)
// then
verify { songsRepo.getSongs(query, 10) }
}

@Test
fun `given query when search called then searchAlbums called with query and limit 7`() {
// given
val query = "song"
// when
tested.search(query)
// then
verify { albumRepo.getAlbums(query, 7) }
}

@Test
fun `given query when search called then searchArtists called with query and limit 7`() {
// given
val query = "song"
// when
tested.search(query)
// then
verify { artistRepo.getArtists(query, 7) }
}
}
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ext.versions = [
mockitoKotlin : "2.1.0",
truth : "0.42",
robolectric : "4.1",
mockk : "1.9.3",

// Instrumentation Testing
androidxTestRunner: "1.1.1",
Expand Down