Skip to content

Commit

Permalink
Added some functioning Unit Tests. Set unique attributes. Database wo…
Browse files Browse the repository at this point in the history
…rking correctly
  • Loading branch information
OhhTuRnz committed Mar 7, 2024
1 parent b193373 commit 1e0ddcf
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 15 deletions.
48 changes: 48 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dependencies {
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.google.code.gson:gson:2.10")
implementation("androidx.test:core-ktx:1.5.0")
implementation("androidx.room:room-ktx:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")
annotationProcessor("com.github.bumptech.glide:compiler:4.12.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
Expand Down
21 changes: 14 additions & 7 deletions app/src/androidTest/java/com/example/mad_2024_app/DAOUnitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.mad_2024_app.DAOs.ShopDAO
import com.example.mad_2024_app.database.Shop
import com.example.mad_2024_app.database.User
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
Expand All @@ -15,28 +16,22 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class DAOUnitTest {
private lateinit var database: AppDatabase
private lateinit var dao: ShopDAO

@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
.allowMainThreadQueries()
.build()
dao = database.shopDao()
}

@After
fun closeDb() {
database.close()
}

@Test
fun test(){
assertEquals(true, true)
}
@Test
fun insertAndRetrieveShop() {
val dao = database.shopDao()
val shop = Shop(name = "Test Shop", description = "Test Description")
dao.insert(shop)

Expand All @@ -45,4 +40,16 @@ class DAOUnitTest {

dao.delete(retrievedShop)
}

@Test
fun insertAndRetrieveUser() {
val dao = database.userDao()
val user = User(username = "Test User", email = "martini@chinchong.com", uuid = "5de7c711-8c70-4515-853a-dcaf67300183")
dao.insert(user)

val retrievedUser = dao.getUserById(1)
assertEquals(user.username, retrievedUser.username)

dao.delete(retrievedUser)
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/mad_2024_app/DAOs/UserDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface UserDAO {
fun getUserById(userId: Int): User

@Delete
fun deleteUser(user: User)
fun delete(user: User)

// Optionally, if you need to delete by ID:
@Query("DELETE FROM User WHERE userId = :userId")
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/example/mad_2024_app/database/Donut.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.example.mad_2024_app.database

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity
@Entity(indices = [Index(value = ["name"], unique = true)])
data class Donut(
@PrimaryKey(autoGenerate = true) val donutId: Int = 0,
val name: String,
@ColumnInfo(name = "name") val name: String,
val type: String,
val color: String? = null
)
12 changes: 7 additions & 5 deletions app/src/main/java/com/example/mad_2024_app/database/User.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.example.mad_2024_app.database

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity
@Entity(indices = [Index(value = ["username", "email", "uuid"], unique = true)])
data class User(
@PrimaryKey(autoGenerate = true) val userId: Int = 0,
val username: String,
val email: String,
val iconPath: String?, // This could be a path or a resource identifier,
val uuid: String,
@ColumnInfo(name = "username") val username: String,
@ColumnInfo(name = "email") val email: String,
val iconPath: String? = null, // This could be a path or a resource identifier,
@ColumnInfo(name = "uuid") val uuid: String,
val homeLatitude : Double? = null,
val homeLongitude : Double? = null
)
Expand Down

0 comments on commit 1e0ddcf

Please sign in to comment.