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

end-project #1

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Expand Up @@ -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"

}
@@ -1,15 +1,151 @@
package tech.appclub.arslan.roomdbtests

import android.content.Context
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 org.junit.After
import org.junit.Assert.*
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
* Copyright © 2020 App Club
* 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<Context>()
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.first().fullName)
}

@Test
@Throws(Exception::class)
fun deleteAllCustomers() {
mCustomerDAO.deleteAllCustomers()
val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer())
assertEquals(mCustomerDAO.totalCustomers(), mAllCustomers.size)
assertTrue(mAllCustomers.isEmpty())
}

@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 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 mAllMaleCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllMaleCustomers())
assertEquals(mCustomerDAO.totalMaleCustomers(), mAllMaleCustomers.size)
assertEquals("John Smith", mAllMaleCustomers[1].fullName)
}

@Test
@Throws(Exception::class)
fun allFemaleCustomers() {
val mAllFemaleCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllFemaleCustomers())
assertEquals(mCustomerDAO.totalFemaleCustomers(), mAllFemaleCustomers.size)
assertEquals("Maria Garcia", mAllFemaleCustomers.first().fullName)
}

@Test
@Throws(Exception::class)
fun allOtherGenderCustomers() {
val mAllOtherGenderCustomers =
LiveDataTestUtil.getValue(mCustomerDAO.getAllOtherGenderCustomers())
assertEquals(mCustomerDAO.totalOtherGenderCustomers(), mAllOtherGenderCustomers.size)
assertEquals("Catherine Jones", mAllOtherGenderCustomers.first().fullName)
}

@Test
@Throws(Exception::class)
fun allActiveCustomers() {
val mAllActiveCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllActiveCustomers())
assertEquals(mCustomerDAO.allActiveCustomerCount(), mAllActiveCustomers.size)
assertEquals("Maria Garcia", mAllActiveCustomers[2].fullName)
}

@Test
@Throws(Exception::class)
fun allNonActiveCustomers() {
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)
mCustomerDAO.updateACustomer(customer)
val mAllCustomers = LiveDataTestUtil.getValue(mCustomerDAO.getAllCustomer())
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)
}

@Test
@Throws(Exception::class)
fun eldestCustomer() {
val eldest = mCustomerDAO.eldestCustomer()
val maxAge = mCustomerDAO.listOfAges().max()
assertEquals(maxAge, eldest)
}

@After
fun closeDatabase() {
mCustomerDatabase.close()
}

}

This file was deleted.

Expand Up @@ -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 <T> getValue(liveData: LiveData<T>): T? {
fun <T> getValue(liveData: LiveData<T>): T {
val data = arrayOfNulls<Any>(1)
val latch = CountDownLatch(1)
val observer: Observer<T> = object : Observer<T> {
Expand All @@ -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)

}
}
Expand Up @@ -11,23 +11,27 @@ interface CustomerDAO {
fun getAllCustomer(): LiveData<List<Customer>>

// 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<List<Customer>>

// 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<List<Customer>>

// 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<List<Customer>>

// Get all other gender customers
@Query("SELECT * FROM customer_table WHERE gender = 2 ORDER BY fullName ASC")
fun getAllOtherGenderCustomers(): LiveData<List<Customer>>

// 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<List<Customer>>

// 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<List<Customer>>

// Delete all customers
Expand All @@ -46,4 +50,36 @@ interface CustomerDAO {
@Update
fun updateACustomer(customer: Customer)

// Total customers
@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<Long>

// Get eldest customer
@Query("SELECT MAX(age) FROM customer_table LIMIT 1")
fun eldestCustomer(): Long

}
@@ -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

}