From e1b99144b8cf24b3e83335925f706415cd29e25a Mon Sep 17 00:00:00 2001 From: Arslan Mushtaq Date: Sun, 26 Apr 2020 13:45:24 +0500 Subject: [PATCH 1/2] end-project --- app/build.gradle | 4 + .../roomdbtests/CustomerDatabaseTest.kt | 140 +++++++++++++++++- .../roomdbtests/ExampleInstrumentedTest.kt | 24 --- .../arslan/roomdbtests/LiveDataTestUtil.kt | 33 ++++- .../arslan/roomdbtests/data/CustomerDAO.kt | 22 ++- .../roomdbtests/db/CustomerRoomDatabase.kt | 13 ++ 6 files changed, 204 insertions(+), 32 deletions(-) delete mode 100644 app/src/androidTest/java/tech/appclub/arslan/roomdbtests/ExampleInstrumentedTest.kt create mode 100644 app/src/main/java/tech/appclub/arslan/roomdbtests/db/CustomerRoomDatabase.kt diff --git a/app/build.gradle b/app/build.gradle index 4529fac..ea7b686 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,6 +44,10 @@ dependencies { implementation "androidx.room:room-runtime:$room_version" kapt "androidx.room:room-compiler:$room_version" + implementation "androidx.room:room-ktx:$room_version" testImplementation "androidx.room:room-testing:$room_version" + implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" + androidTestImplementation "androidx.arch.core:core-testing:2.1.0" + } diff --git a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt index a1ad654..17975c8 100644 --- a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt +++ b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt @@ -1,6 +1,20 @@ package tech.appclub.arslan.roomdbtests +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 junit.framework.Assert.assertEquals +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test import org.junit.runner.RunWith +import tech.appclub.arslan.roomdbtests.data.Customer +import tech.appclub.arslan.roomdbtests.data.CustomerDAO +import tech.appclub.arslan.roomdbtests.db.CustomerRoomDatabase /* * We will be writing all the tests in this class @@ -8,8 +22,132 @@ import org.junit.runner.RunWith * Author - Arslan Mushtaq (contact@appclub.tech) * */ +@RunWith(AndroidJUnit4::class) class CustomerDatabaseTest { - + @get:Rule + val instantTaskExecutorRule = InstantTaskExecutorRule() + + private lateinit var mCustomerDAO: CustomerDAO + private lateinit var mCustomerDatabase: CustomerRoomDatabase + + @Before + fun createDatabase() { + val context = ApplicationProvider.getApplicationContext() + mCustomerDatabase = Room.inMemoryDatabaseBuilder(context, CustomerRoomDatabase::class.java) + .allowMainThreadQueries() + .build() + mCustomerDAO = mCustomerDatabase.customerDAO() + LiveDataTestUtil.addCustomers(mCustomerDAO) + } + + @Test + @Throws(Exception::class) + fun verifyInsertedData() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals("Bill Hoffman", mAllCustomers[0].fullName) + } + + @Test + @Throws(Exception::class) + fun deleteAllCustomers() { + mCustomerDAO.deleteAllCustomers() + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(0, mAllCustomers.size) + } + + @Test + @Throws(Exception::class) + fun deleteACustomer() { + val mCustomerListBefore = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + mCustomerDAO.deleteACustomer(mCustomerListBefore[0]) + val mCustomerListAfter = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(mCustomerListBefore.size - 1, mCustomerListAfter.size) + } + + @Test + @Throws(Exception::class) + fun allAdultCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllAdultCustomers()) + assertEquals(5, mAllCustomers.size) + assertEquals("Bill Hoffman", mAllCustomers[0].fullName) + } + + @Test + @Throws(Exception::class) + fun allMaleCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllMaleCustomers()) + assertEquals(2, mAllCustomers.size) + assertEquals("John Smith", mAllCustomers[1].fullName) + } + + @Test + @Throws(Exception::class) + fun allFemaleCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllFemaleCustomers()) + assertEquals(2, mAllCustomers.size) + assertEquals("Maria Garcia", mAllCustomers[0].fullName) + } + + @Test + @Throws(Exception::class) + fun allOtherGenderCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllOtherGenderCustomers()) + assertEquals(2, mAllCustomers.size) + assertEquals("Catherine Jones", mAllCustomers[0].fullName) + } + + @Test + @Throws(Exception::class) + fun allActiveCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllActiveCustomers()) + assertEquals(3, mAllCustomers.size) + assertEquals("Maria Garcia", mAllCustomers[2].fullName) + } + + @Test + @Throws(Exception::class) + fun allNonActiveCustomers() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllNonActiveCustomers()) + assertEquals(3, mAllCustomers.size) + assertEquals("John Smith", mAllCustomers[1].fullName) + } + + @Test + @Throws(Exception::class) + fun updateCustomerStatus() { + val customer = Customer(id = 1, fullName = "Bill Hoffman", age = 43, gender = 0, isCustomer = 1) + mCustomerDAO.updateACustomer(customer) + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(1, mAllCustomers[0].isCustomer) + } + + @Test + @Throws(Exception::class) + fun totalCustomers() { + val count = mCustomerDAO.totalCustomers() + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(count, mAllCustomers.size) + } + + @Test + @Throws(Exception::class) + fun isIDIncrementing() { + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(2, mAllCustomers[1].id) + assertEquals(3, mAllCustomers[2].id) + } + + @Test + @Throws(Exception::class) + fun eldestCustomer() { + val eldest = mCustomerDAO.eldestCustomer() + assertEquals(43, eldest) + } + + @After + fun closeDatabase() { + mCustomerDatabase.close() + } } \ No newline at end of file diff --git a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/ExampleInstrumentedTest.kt b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/ExampleInstrumentedTest.kt deleted file mode 100644 index 2e06b3a..0000000 --- a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package tech.appclub.arslan.roomdbtests - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("tech.appclub.arslan.roomdbtests", appContext.packageName) - } -} diff --git a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/LiveDataTestUtil.kt b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/LiveDataTestUtil.kt index 941f229..a8d7f63 100644 --- a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/LiveDataTestUtil.kt +++ b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/LiveDataTestUtil.kt @@ -2,13 +2,15 @@ package tech.appclub.arslan.roomdbtests import androidx.lifecycle.LiveData import androidx.lifecycle.Observer +import tech.appclub.arslan.roomdbtests.data.Customer +import tech.appclub.arslan.roomdbtests.data.CustomerDAO import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit object LiveDataTestUtil { @Throws(InterruptedException::class) - fun getValue(liveData: LiveData): T? { + fun getValue(liveData: LiveData): T { val data = arrayOfNulls(1) val latch = CountDownLatch(1) val observer: Observer = object : Observer { @@ -21,6 +23,33 @@ object LiveDataTestUtil { liveData.observeForever(observer) latch.await(2, TimeUnit.SECONDS) @Suppress("UNCHECKED_CAST") - return data[0] as T? + return data[0] as T + } + + fun addCustomers(mCustomerDAO: CustomerDAO) { + + var customer = + Customer(fullName = "Bill Hoffman", age = 43, gender = 0, isCustomer = 0) + mCustomerDAO.insertCustomer(customer) + + customer = + Customer(fullName = "Catherine Jones", age = 19, gender = 2, isCustomer = 0) + mCustomerDAO.insertCustomer(customer) + + customer = + Customer(fullName = "Henry Williams", age = 28, gender = 2, isCustomer = 1) + mCustomerDAO.insertCustomer(customer) + + customer = Customer(fullName = "John Smith", age = 21, gender = 0, isCustomer = 1) + mCustomerDAO.insertCustomer(customer) + + customer = + Customer(fullName = "Maria Garcia", age = 17, gender = 1, isCustomer = 0) + mCustomerDAO.insertCustomer(customer) + + customer = + Customer(fullName = "Martha Stewart", age = 33, gender = 1, isCustomer = 1) + mCustomerDAO.insertCustomer(customer) + } } \ No newline at end of file diff --git a/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt b/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt index 30ff218..dd140d8 100644 --- a/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt +++ b/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt @@ -11,23 +11,27 @@ interface CustomerDAO { fun getAllCustomer(): LiveData> // Get all adult customers - @Query("SELECT * FROM customer_table WHERE age > 18") + @Query("SELECT * FROM customer_table WHERE age > 18 ORDER BY fullName ASC") fun getAllAdultCustomers(): LiveData> // Get all male customers - @Query("SELECT * FROM customer_table WHERE gender = 1") + @Query("SELECT * FROM customer_table WHERE gender = 0 ORDER BY fullName ASC") fun getAllMaleCustomers(): LiveData> // Get all female customers - @Query("SELECT * FROM customer_table WHERE gender = 2") + @Query("SELECT * FROM customer_table WHERE gender = 1 ORDER BY fullName ASC") fun getAllFemaleCustomers(): LiveData> + // Get all other gender customers + @Query("SELECT * FROM customer_table WHERE gender = 2 ORDER BY fullName ASC") + fun getAllOtherGenderCustomers(): LiveData> + // Get all active customers - @Query("SELECT * FROM customer_table WHERE isCustomer = 0") + @Query("SELECT * FROM customer_table WHERE isCustomer = 0 ORDER BY fullName ASC") fun getAllActiveCustomers(): LiveData> // Get all non-active customers - @Query("SELECT * FROM customer_table WHERE isCustomer = 1") + @Query("SELECT * FROM customer_table WHERE isCustomer = 1 ORDER BY fullName ASC") fun getAllNonActiveCustomers(): LiveData> // Delete all customers @@ -46,4 +50,12 @@ interface CustomerDAO { @Update fun updateACustomer(customer: Customer) + // Total customers + @Query("SELECT COUNT(*) FROM customer_table") + fun totalCustomers(): Int + + // Get eldest customer + @Query("SELECT MAX(age) FROM customer_table LIMIT 1") + fun eldestCustomer(): Long + } \ No newline at end of file diff --git a/app/src/main/java/tech/appclub/arslan/roomdbtests/db/CustomerRoomDatabase.kt b/app/src/main/java/tech/appclub/arslan/roomdbtests/db/CustomerRoomDatabase.kt new file mode 100644 index 0000000..b244146 --- /dev/null +++ b/app/src/main/java/tech/appclub/arslan/roomdbtests/db/CustomerRoomDatabase.kt @@ -0,0 +1,13 @@ +package tech.appclub.arslan.roomdbtests.db + +import androidx.room.Database +import androidx.room.RoomDatabase +import tech.appclub.arslan.roomdbtests.data.Customer +import tech.appclub.arslan.roomdbtests.data.CustomerDAO + +@Database(entities = [Customer::class], version = 1, exportSchema = false) +abstract class CustomerRoomDatabase : RoomDatabase() { + + abstract fun customerDAO(): CustomerDAO + +} \ No newline at end of file From 31ce6c06fd5b5300126e51b8ed5a938a50ea4d98 Mon Sep 17 00:00:00 2001 From: Arslan Mushtaq Date: Tue, 28 Apr 2020 16:54:24 +0500 Subject: [PATCH 2/2] update functions and test --- .../roomdbtests/CustomerDatabaseTest.kt | 64 +++++++++---------- .../arslan/roomdbtests/data/CustomerDAO.kt | 24 +++++++ 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt index 17975c8..d0e4e09 100644 --- a/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt +++ b/app/src/androidTest/java/tech/appclub/arslan/roomdbtests/CustomerDatabaseTest.kt @@ -1,13 +1,12 @@ package tech.appclub.arslan.roomdbtests 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 junit.framework.Assert.assertEquals import org.junit.After +import org.junit.Assert.* import org.junit.Before import org.junit.Rule import org.junit.Test @@ -45,7 +44,7 @@ class CustomerDatabaseTest { @Throws(Exception::class) fun verifyInsertedData() { val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) - assertEquals("Bill Hoffman", mAllCustomers[0].fullName) + assertEquals("Bill Hoffman", mAllCustomers.first().fullName) } @Test @@ -53,7 +52,8 @@ class CustomerDatabaseTest { fun deleteAllCustomers() { mCustomerDAO.deleteAllCustomers() val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) - assertEquals(0, mAllCustomers.size) + assertEquals(mCustomerDAO.totalCustomers(), mAllCustomers.size) + assertTrue(mAllCustomers.isEmpty()) } @Test @@ -68,72 +68,69 @@ class CustomerDatabaseTest { @Test @Throws(Exception::class) fun allAdultCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllAdultCustomers()) - assertEquals(5, mAllCustomers.size) - assertEquals("Bill Hoffman", mAllCustomers[0].fullName) + val mAllAdultCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllAdultCustomers()) + val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertNotEquals(mAllCustomers.size, mAllAdultCustomers.size) + assertEquals("Bill Hoffman", mAllAdultCustomers.first().fullName) } @Test @Throws(Exception::class) fun allMaleCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllMaleCustomers()) - assertEquals(2, mAllCustomers.size) - assertEquals("John Smith", mAllCustomers[1].fullName) + val mAllMaleCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllMaleCustomers()) + assertEquals(mCustomerDAO.totalMaleCustomers(), mAllMaleCustomers.size) + assertEquals("John Smith", mAllMaleCustomers[1].fullName) } @Test @Throws(Exception::class) fun allFemaleCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllFemaleCustomers()) - assertEquals(2, mAllCustomers.size) - assertEquals("Maria Garcia", mAllCustomers[0].fullName) + val mAllFemaleCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllFemaleCustomers()) + assertEquals(mCustomerDAO.totalFemaleCustomers(), mAllFemaleCustomers.size) + assertEquals("Maria Garcia", mAllFemaleCustomers.first().fullName) } @Test @Throws(Exception::class) fun allOtherGenderCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllOtherGenderCustomers()) - assertEquals(2, mAllCustomers.size) - assertEquals("Catherine Jones", mAllCustomers[0].fullName) + val mAllOtherGenderCustomers = + LiveDataTestUtil.getValue(mCustomerDAO.getAllOtherGenderCustomers()) + assertEquals(mCustomerDAO.totalOtherGenderCustomers(), mAllOtherGenderCustomers.size) + assertEquals("Catherine Jones", mAllOtherGenderCustomers.first().fullName) } @Test @Throws(Exception::class) fun allActiveCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllActiveCustomers()) - assertEquals(3, mAllCustomers.size) - assertEquals("Maria Garcia", mAllCustomers[2].fullName) + val mAllActiveCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllActiveCustomers()) + assertEquals(mCustomerDAO.allActiveCustomerCount(), mAllActiveCustomers.size) + assertEquals("Maria Garcia", mAllActiveCustomers[2].fullName) } @Test @Throws(Exception::class) fun allNonActiveCustomers() { - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllNonActiveCustomers()) - assertEquals(3, mAllCustomers.size) - assertEquals("John Smith", mAllCustomers[1].fullName) + val mAllNonActiveCustomers = + LiveDataTestUtil.getValue(mCustomerDAO.getAllNonActiveCustomers()) + assertEquals(mCustomerDAO.allNonActiveCustomerCount(), mAllNonActiveCustomers.size) + assertEquals("John Smith", mAllNonActiveCustomers[1].fullName) } @Test @Throws(Exception::class) fun updateCustomerStatus() { - val customer = Customer(id = 1, fullName = "Bill Hoffman", age = 43, gender = 0, isCustomer = 1) + val customer = + Customer(id = 1, fullName = "Bill Hoffman", age = 43, gender = 0, isCustomer = 1) mCustomerDAO.updateACustomer(customer) val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) - assertEquals(1, mAllCustomers[0].isCustomer) - } - - @Test - @Throws(Exception::class) - fun totalCustomers() { - val count = mCustomerDAO.totalCustomers() - val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) - assertEquals(count, mAllCustomers.size) + assertEquals(1, mAllCustomers.first().isCustomer) } @Test @Throws(Exception::class) fun isIDIncrementing() { val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer()) + assertEquals(1, mAllCustomers.first().id) assertEquals(2, mAllCustomers[1].id) assertEquals(3, mAllCustomers[2].id) } @@ -142,7 +139,8 @@ class CustomerDatabaseTest { @Throws(Exception::class) fun eldestCustomer() { val eldest = mCustomerDAO.eldestCustomer() - assertEquals(43, eldest) + val maxAge = mCustomerDAO.listOfAges().max() + assertEquals(maxAge, eldest) } @After diff --git a/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt b/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt index dd140d8..3fba673 100644 --- a/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt +++ b/app/src/main/java/tech/appclub/arslan/roomdbtests/data/CustomerDAO.kt @@ -54,6 +54,30 @@ interface CustomerDAO { @Query("SELECT COUNT(*) FROM customer_table") fun totalCustomers(): Int + // Total male customers + @Query("SELECT COUNT(*) FROM customer_table WHERE gender = 0") + fun totalMaleCustomers(): Int + + // Total female customers + @Query("SELECT COUNT(*) FROM customer_table WHERE gender = 1") + fun totalFemaleCustomers(): Int + + // Total other gender customers + @Query("SELECT COUNT(*) FROM customer_table WHERE gender = 2") + fun totalOtherGenderCustomers(): Int + + // Count of all active customers + @Query("SELECT COUNT(*) FROM customer_table WHERE isCustomer = 0") + fun allActiveCustomerCount(): Int + + // Count of all non-active customers + @Query("SELECT COUNT(*) FROM customer_table WHERE isCustomer = 1") + fun allNonActiveCustomerCount(): Int + + // List of all ages + @Query("SELECT age FROM customer_table") + fun listOfAges(): List + // Get eldest customer @Query("SELECT MAX(age) FROM customer_table LIMIT 1") fun eldestCustomer(): Long