Skip to content

Commit

Permalink
Merge pull request #32 from Architect-Coders/feature/integration_tests
Browse files Browse the repository at this point in the history
Create integration tests
  • Loading branch information
PabloJC authored Mar 8, 2020
2 parents 49b935a + e258494 commit a50c0f3
Show file tree
Hide file tree
Showing 60 changed files with 505 additions and 213 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/pabji/myfridge/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class MyApp : Application() {
super.onCreate()
initDI()
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/pabji/myfridge/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ val scopesModule = module {
viewModel { BarcodeReaderViewModel(get(), get()) }
scoped { SearchProductsByBarcode(get()) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class AndroidPermissionChecked(private val application: Application) : Permissio
private fun PermissionChecker.Permission.toAndroidId() = when (this) {
PermissionChecker.Permission.CAMERA -> Manifest.permission.CAMERA
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/pabji/myfridge/model/ItemProduct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ fun Product.toItemProduct(): ItemProduct =
)

fun ItemProduct.toProduct(): Product =
Product(id = id, name = name, barcode = barcode)
Product(id = id, name = name, barcode = barcode)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.pabji.myfridge.model.common.extensions

fun String.getListByDelimit(delimit: String) = run { split(delimit).map { it.trim() } }
fun String.getListByDelimit(delimit: String) = run { split(delimit).map { it.trim() } }
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ class RoomDataSource(database: RoomDatabase) : LocalDatasource {

private val productDao = database.productDao()

override suspend fun getProductListByBarcodeList(barcodeList: List<String>) =
Either.Right(productDao.getProductsByBarcode(barcodeList).map { it.toProduct() })

override suspend fun getProductByBarcode(barcode: String?) =
productDao.getProductByBarcode(barcode ?: "")?.run {
Either.Right(toProduct())
} ?: Either.Left(DetailError)
withContext(Dispatchers.IO) {
productDao.getProductByBarcode(barcode ?: "")?.run {
Either.Right(toProduct())
} ?: Either.Left(DetailError)
}

override suspend fun getProductById(productId: Long) =
productDao.getProductById(productId)?.run {
Either.Right(toProduct())
} ?: Either.Left(DetailError)
withContext(Dispatchers.IO) {
productDao.getProductById(productId)?.run {
Either.Right(toProduct())
} ?: Either.Left(DetailError)
}

override suspend fun getProductList() =
withContext(Dispatchers.IO) { productDao.getAll().map { it.toProduct() } }

override suspend fun saveProduct(product: Product) =
productDao.insert(product.toProductEntity())
withContext(Dispatchers.IO) {
productDao.insert(product.toProductEntity())
}

override suspend fun removeProduct(product: Product) =
productDao.remove(product.toProductEntity())
withContext(Dispatchers.IO) {
productDao.remove(product.toProductEntity())
}

override suspend fun getProductsByTerm(searchTerm: String) =
withContext(Dispatchers.IO) {
productDao.getProductsByTerm(searchTerm).map { it.toProduct() }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import com.pabji.myfridge.model.database.entities.ProductEntity
@Database(entities = [ProductEntity::class], version = 1)
abstract class RoomDatabase : RoomDatabase() {
abstract fun productDao(): ProductDao
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ interface ProductDao {
@Query("SELECT * FROM products WHERE barcode = :barcode")
suspend fun getProductByBarcode(barcode: String): ProductEntity?

@Query("SELECT * FROM products WHERE barcode IN(:barcodeList)")
suspend fun getProductsByBarcode(barcodeList: List<String>): List<ProductEntity>

@Query("SELECT * FROM products WHERE name LIKE :searchTerm")
suspend fun getProductsByTerm(searchTerm: String): List<ProductEntity>

Expand All @@ -26,4 +23,4 @@ interface ProductDao {

@Delete
suspend fun remove(product: ProductEntity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ internal fun Product.toProductEntity() = ProductEntity(
ingredientsText,
imageIngredientsUrl,
categories
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class RetrofitDataSource(private val apiService: RetrofitApiService) : RemoteDat
}
}


override suspend fun getProductByBarcode(barcode: String) =
with(apiService.getProductDetailById(barcode, DETAIL_FIELDS.joinToString(","))) {
if (isSuccessful) {
Expand All @@ -36,4 +35,4 @@ class RetrofitDataSource(private val apiService: RetrofitApiService) : RemoteDat
Either.Left(DetailError)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ val DETAIL_FIELDS = listOf(
IMAGE_INGREDIENTS_URL,
CATEGORIES,
BRANDS
) + SIMPLE_FIELDS
) + SIMPLE_FIELDS
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ object RetrofitApiClient {
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build().create(RetrofitApiService::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ interface RetrofitApiService {

@GET("cgi/search.pl?search_simple=1&json=1&action=process")
suspend fun searchProductsByName(
@Query("search_terms") name: String? = null, @Query("page") page: Int = 1, @Query(
"fields"
) fields: String
@Query("search_terms") name: String? = null,
@Query("page") page: Int = 1,
@Query("fields") fields: String
): Response<SearchResponse>

@GET("api/v0/product/{id}.json")
suspend fun getProductDetailById(@Path("id") id: String, @Query("fields") fields: String): Response<ProductDetailResponse>

}
suspend fun getProductDetailById(
@Path("id") id: String,
@Query("fields") fields: String
): Response<ProductDetailResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import com.pabji.myfridge.model.network.api.PRODUCT

data class ProductDetailResponse(
@SerializedName(PRODUCT) val product: ProductResponse?
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ internal fun ProductResponse.toProduct() =
ingredientsText = ingredientsText ?: "",
imageIngredientsUrl = ingredientsUrl ?: "",
categories = categories ?: ""
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.google.gson.annotations.SerializedName
import com.pabji.myfridge.model.network.api.PRODUCTS

data class SearchResponse(
@SerializedName(PRODUCTS) val products: List<ProductResponse> = emptyList()
)
@SerializedName(PRODUCTS) val products: List<ProductResponse> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BarcodeReaderActivity : AppCompatActivity(), OnRequestPermissionsResultCal
initRecycler()
viewModel.model.observe(this, Observer(::updateUi))
viewModel.navigation.observe(this, Observer { event ->
event.getContentIfNotHandled()?.let {
event.getContent()?.let {
startActivity<ProductDetailActivity> {
putExtra(ProductDetailActivity.INTENT_PRODUCT, it)
}
Expand All @@ -55,7 +55,6 @@ class BarcodeReaderActivity : AppCompatActivity(), OnRequestPermissionsResultCal
it.adapter =
ProductListAdapter { product ->
viewModel.onProductClicked(product)

}.apply {
adapter = this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class BarcodeReaderViewModel(
fun onProductClicked(product: ItemProduct) {
_navigation.value = Event(product)
}
}
}
11 changes: 1 addition & 10 deletions app/src/main/java/com/pabji/myfridge/ui/common/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package com.pabji.myfridge.ui.common

import androidx.fragment.app.Fragment
import com.pabji.myfridge.ui.common.extensions.hideKeyboard

abstract class BaseFragment : Fragment() {

protected fun finish() {
activity?.run {
hideKeyboard()
onBackPressed()
}
}
}
abstract class BaseFragment : Fragment()
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ open class BaseViewModel(uiDispatcher: CoroutineDispatcher) : ViewModel(),
cancelScope()
super.onCleared()
}
}
}
12 changes: 3 additions & 9 deletions app/src/main/java/com/pabji/myfridge/ui/common/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ package com.pabji.myfridge.ui.common
*/
data class Event<out T>(private val content: T) {

var hasBeenHandled = false
private set // Allow external read but not write
private var hasBeenHandled = false

/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
fun getContent(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}

/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class PermissionRequester(private val activity: Activity, private val permission
}
).check()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
package com.pabji.myfridge.ui.common.extensions

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle


import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.annotation.IntegerRes
import androidx.appcompat.app.AppCompatActivity
import com.pabji.myfridge.ui.common.BaseFragment

inline fun <reified T : BaseFragment> AppCompatActivity.setFragment(
fragment: T, @IntegerRes fragmentContainer: Int,
addToBackStack: Boolean = false
) {

val tag = T::class.simpleName
supportFragmentManager.beginTransaction().run {
if (addToBackStack) {
addToBackStack(tag)
}
replace(fragmentContainer, fragment, tag)
commitAllowingStateLoss()
}
}


fun Activity.showKeyboard(editText: EditText) {
editText.requestFocus()
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, 0)
}

fun Activity.hideKeyboard() {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
Expand All @@ -57,4 +27,4 @@ inline fun <reified T : Any> Activity.startActivity(
}
}

inline fun <reified T : Any> newIntent(activity: Activity): Intent = Intent(activity, T::class.java)
inline fun <reified T : Any> newIntent(activity: Activity): Intent = Intent(activity, T::class.java)
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.pabji.myfridge.ui.common.extensions

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.EditText
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -29,20 +26,8 @@ fun <T> RecyclerView.Adapter<*>.notifyChanges(
diff.dispatchUpdatesTo(this)
}

fun EditText.setOnTextChange(onTextChanged: (String) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
onTextChanged(p0?.toString() ?: "")
}
})
}

fun SearchView.onTextChange(result: (String) -> Unit) {
setOnQueryTextListener(object : SearchView.OnQueryTextListener{
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
Expand All @@ -51,12 +36,10 @@ fun SearchView.onTextChange(result: (String) -> Unit) {
result(newText ?: "")
return false
}

})
}

fun <T : View> T?.visible(): T? = this?.apply { visibility = VISIBLE }
fun <T : View> T?.gone(): T? = this?.apply { visibility = GONE }

fun <T : View> T?.setVisible(isVisible: Boolean) = if (isVisible) visible() else gone()

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class MainViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycl

override fun getItemCount() = list.size
override fun createFragment(position: Int) = list[position]
}
}
Loading

0 comments on commit a50c0f3

Please sign in to comment.