From c4aabfb67a5e8869062ff132a4fddace06a98909 Mon Sep 17 00:00:00 2001 From: Pooja shaji Date: Wed, 12 Oct 2022 23:33:34 +0530 Subject: [PATCH] Added unit test for use case GetNote closes #4 --- My Notes.plantuml | 31 +++++++ app/build.gradle | 5 +- .../data/repository/FakeNoteRepository.kt | 27 ++++++ .../domain/use_cases/GetNotesTest.kt | 93 +++++++++++++++++++ build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 6 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 My Notes.plantuml create mode 100644 app/src/test/java/com/dlight/mynotes/feature_note/domain/data/repository/FakeNoteRepository.kt create mode 100644 app/src/test/java/com/dlight/mynotes/feature_note/domain/use_cases/GetNotesTest.kt diff --git a/My Notes.plantuml b/My Notes.plantuml new file mode 100644 index 0000000..5e71e2c --- /dev/null +++ b/My Notes.plantuml @@ -0,0 +1,31 @@ +@startuml + +title __MY NOTES's Component Diagram__\n + + component "My_Notes.app.unitTest" { + [My_Notes.app.main] + component "My_Notes.app.main" { + [My_Notes.app.androidTest] + component "My_Notes.app.androidTest" { + [My_Notes.app] + } + + + } + + + } + + + [My_Notes.app.androidTest] --> [My_Notes.app.main] + [My_Notes.app.unitTest] --> [My_Notes.app.main] + + +right footer + + +PlantUML diagram generated by SketchIt! (https://bitbucket.org/pmesmeur/sketch.it) +For more information about this tool, please contact philippe.mesmeur@gmail.com +endfooter + +@enduml diff --git a/app/build.gradle b/app/build.gradle index 4941ea5..5924290 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { defaultConfig { applicationId "com.dlight.mynotes" minSdk 23 - targetSdk 28 + targetSdk 32 versionCode 1 versionName "1.0" @@ -76,7 +76,6 @@ dependencies { //Dagger - Hilt implementation 'com.google.dagger:hilt-android:2.42' kapt 'com.google.dagger:hilt-android-compiler:2.42' - // implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03" kapt "androidx.hilt:hilt-compiler:1.0.0" // Room @@ -103,7 +102,7 @@ dependencies { androidTestImplementation 'com.google.dagger:hilt-android-testing:2.42' kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.42' androidTestImplementation "junit:junit:4.13.2" - androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.3' + androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4' androidTestImplementation "androidx.arch.core:core-testing:2.1.0" androidTestImplementation "com.google.truth:truth:1.1.3" androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/app/src/test/java/com/dlight/mynotes/feature_note/domain/data/repository/FakeNoteRepository.kt b/app/src/test/java/com/dlight/mynotes/feature_note/domain/data/repository/FakeNoteRepository.kt new file mode 100644 index 0000000..1f173e0 --- /dev/null +++ b/app/src/test/java/com/dlight/mynotes/feature_note/domain/data/repository/FakeNoteRepository.kt @@ -0,0 +1,27 @@ +package com.dlight.mynotes.feature_note.domain.data.repository + +import com.dlight.mynotes.feature_note.domain.model.Note +import com.dlight.mynotes.feature_note.domain.repository.NoteRepository +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow + +class FakeNoteRepository : NoteRepository { + + private val notes = mutableListOf() + + override fun getNotes(): Flow> { + return flow { emit(notes) } + } + + override suspend fun getNoteById(id: Int): Note? { + return notes.find { it.id == id } + } + + override suspend fun insertNote(note: Note) { + notes.add(note) + } + + override suspend fun deleteNote(note: Note) { + notes.remove(note) + } +} \ No newline at end of file diff --git a/app/src/test/java/com/dlight/mynotes/feature_note/domain/use_cases/GetNotesTest.kt b/app/src/test/java/com/dlight/mynotes/feature_note/domain/use_cases/GetNotesTest.kt new file mode 100644 index 0000000..0f44279 --- /dev/null +++ b/app/src/test/java/com/dlight/mynotes/feature_note/domain/use_cases/GetNotesTest.kt @@ -0,0 +1,93 @@ +package com.dlight.mynotes.feature_note.domain.use_cases + +import com.dlight.mynotes.feature_note.domain.data.repository.FakeNoteRepository +import com.dlight.mynotes.feature_note.domain.model.Note +import com.dlight.mynotes.feature_note.domain.util.NoteOrder +import com.dlight.mynotes.feature_note.domain.util.OrderType +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import org.junit.Before +import org.junit.Test + +class GetNotesTest { + + private lateinit var getNotes: GetNotes + private lateinit var fakeNoteRepository: FakeNoteRepository + + @Before + fun setUp() { + fakeNoteRepository = FakeNoteRepository() + getNotes = GetNotes(fakeNoteRepository) + + val noteToInsert = mutableListOf() + ('a'..'z').forEachIndexed { index, c -> + noteToInsert.add( + Note( + title = c.toString(), + content = c.toString(), + timestamp = index.toLong(), + color = index + ) + ) + } + noteToInsert.shuffle() + runBlocking { + noteToInsert.forEach { fakeNoteRepository.insertNote(it) } + } + } + + @Test + fun `Order notes by title ascending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Title(OrderType.Ascending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].title).isLessThan(notes[i + 1].title) + } + } + + @Test + fun `Order notes by title descending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Title(OrderType.Descending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].title).isGreaterThan(notes[i + 1].title) + } + } + + @Test + fun `Order notes by date ascending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Date(OrderType.Ascending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].timestamp).isLessThan(notes[i + 1].timestamp) + } + } + + @Test + fun `Order notes by date descending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Date(OrderType.Descending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].timestamp).isGreaterThan(notes[i + 1].timestamp) + } + } + + @Test + fun `Order notes by color ascending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Color(OrderType.Ascending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].color).isLessThan(notes[i + 1].color) + } + } + + @Test + fun `Order notes by color descending, correct order`() = runBlocking { + val notes = getNotes(NoteOrder.Color(OrderType.Descending)).first() + + for (i in 0..notes.size - 2) { + assertThat(notes[i].color).isGreaterThan(notes[i + 1].color) + } + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index e8c9651..3fdf8f0 100644 --- a/build.gradle +++ b/build.gradle @@ -4,8 +4,8 @@ buildscript { } }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '7.2.1' apply false - id 'com.android.library' version '7.2.1' apply false + id 'com.android.application' version '7.3.0' apply false + id 'com.android.library' version '7.3.0' apply false id 'org.jetbrains.kotlin.android' version '1.6.21' apply false id 'com.google.dagger.hilt.android' version '2.41' apply false } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7553caa..38ff08f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Sun Jul 03 15:16:46 IST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME