Introducing the TransfersDao#getTransfersWithMissingValueIn(symbol: String) method, used to obtain only the transfers that lack a specific equivalent value entry, and related tests

This commit is contained in:
Nelson R. Perez 2019-01-31 20:11:35 -05:00
parent a10c527956
commit f197e44262
6 changed files with 134 additions and 7 deletions

View file

@ -116,8 +116,9 @@ dependencies {
// Core library // Core library
androidTestImplementation 'androidx.test:core:1.1.0' androidTestImplementation 'androidx.test:core:1.1.0'
// testImplementation "androidx.arch.core:core-testing:$lifecycle_version" androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"
androidTestImplementation "androidx.room:room-testing:$room_version" androidTestImplementation "androidx.room:room-testing:$room_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'com.jraska.livedata:testing-ktx:1.0.0'
} }

View file

@ -0,0 +1,42 @@
package cy.agorise.bitsybitshareswallet
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
object LiveDataTestUtil {
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
data[0] = o
latch.countDown()
liveData.removeObserver(this)
}
}
liveData.observeForever(observer)
latch.await(2, TimeUnit.SECONDS)
@Suppress("UNCHECKED_CAST")
return data[0] as T
}
}

View file

@ -1,20 +1,19 @@
package cy.agorise.bitsybitshareswallet; package cy.agorise.bitsybitshareswallet;
import android.content.Context import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room import androidx.room.Room
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import androidx.test.runner.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
import cy.agorise.bitsybitshareswallet.database.entities.Merchant import cy.agorise.bitsybitshareswallet.database.entities.Merchant
import org.junit.After import org.junit.*
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import java.io.IOException import java.io.IOException
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
public class MerchantQueryTest { class MerchantQueryTest {
@get:Rule val testRule = InstantTaskExecutorRule()
private lateinit var db: BitsyDatabase private lateinit var db: BitsyDatabase
@Before @Before

View file

@ -0,0 +1,81 @@
package cy.agorise.bitsybitshareswallet
import android.content.Context
import android.util.Log
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.jraska.livedata.test
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
import cy.agorise.bitsybitshareswallet.database.entities.EquivalentValue
import cy.agorise.bitsybitshareswallet.database.entities.Transfer
import org.junit.*
import org.junit.runner.RunWith
import java.io.IOException
@RunWith(AndroidJUnit4::class)
class TransfersTests {
val TAG = "TransfersTests"
@get:Rule val testRule = InstantTaskExecutorRule()
private lateinit var db: BitsyDatabase
private val context = ApplicationProvider.getApplicationContext<Context>()
@Before
fun createDb() {
db = Room.inMemoryDatabaseBuilder(context, BitsyDatabase::class.java).build()
// We create 2 transfers for the 'transfers' table, but only one of them will have an equivalent value entry
val t1 = Transfer("1.11.702181910", 34118155, 1485018549, 264174, "1.3.0", "1.2.32567","1.2.139293",15869682,"1.3.0","")
val t2 = Transfer("1.11.684483739", 33890367, 1547171166, 11030, "1.3.0", "1.2.139293","1.2.1029856",98,"1.3.120","")
db.transferDao().insert(t1)
db.transferDao().insert(t2)
// Here's the equivalent value for the first transaction inserted (t1)
val equivalentValue = EquivalentValue("1.11.702181910", 0, "usd")
db.equivalentValueDao().insert(equivalentValue)
}
@After
@Throws(IOException::class)
fun closeDb(){
db.close()
}
/**
* This test makes use of the LiveData Testing library and its objective is to prove that
* the TransferDao#getTransfersWithMissingValueIn(symbol: String) will return only the
* second 'transfer' entry.
* <p>
* @see cy.agorise.bitsybitshareswallet.database.daos.TransferDao.getTransfersWithMissingValueIn
* @see cy.agorise.bitsybitshareswallet.LiveDataTestUtil
*/
@Test
fun testGetTransfersMissingEquivalentValues(){
db.transferDao()
.getTransfersWithMissingValueIn("usd")
.test()
.awaitValue()
.assertHasValue()
.assertValue { transfers -> transfers.size == 1 }
.assertValue { transfers -> transfers[0].id == "1.11.684483739"}
.assertValue { transfers -> transfers[0].blockNumber == 33890367L}
}
/**
* This test makes use of the simple LiveDataTestUtil class and its objective is to prove that
* the TransferDao#getTransfersWithMissingValueIn(symbol: String) will return only the
* second 'transfer' entry.
* <p>
* @see cy.agorise.bitsybitshareswallet.LiveDataTestUtil
*/
@Test
fun testGetTransfersMissingEquivalentValues2(){
val transfers: List<Transfer> = LiveDataTestUtil.getValue(db.transferDao().getTransfersWithMissingValueIn("usd"))
Assert.assertNotNull(transfers)
Assert.assertEquals(1, transfers.size)
Assert.assertEquals("1.11.684483739", transfers[0].id)
Assert.assertEquals(33890367, transfers[0].blockNumber)
Log.d(TAG, "transfer ${transfers[0]}");
}
}

View file

@ -5,6 +5,7 @@ import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.Query import androidx.room.Query
import cy.agorise.bitsybitshareswallet.database.entities.EquivalentValue import cy.agorise.bitsybitshareswallet.database.entities.EquivalentValue
import cy.agorise.bitsybitshareswallet.database.entities.Transfer
@Dao @Dao
interface EquivalentValueDao { interface EquivalentValueDao {

View file

@ -29,6 +29,9 @@ interface TransferDao {
@Query("SELECT block_number FROM transfers WHERE timestamp='0' LIMIT 1") @Query("SELECT block_number FROM transfers WHERE timestamp='0' LIMIT 1")
fun getTransferBlockNumberWithMissingTime(): LiveData<Long> fun getTransferBlockNumberWithMissingTime(): LiveData<Long>
@Query("SELECT * FROM transfers WHERE id NOT IN (SELECT transfer_id FROM equivalent_values WHERE symbol = :symbol)")
fun getTransfersWithMissingValueIn(symbol: String): LiveData<List<Transfer>>
@Query("DELETE FROM transfers") @Query("DELETE FROM transfers")
fun deleteAll() fun deleteAll()
} }