Skip to content

Commit

Permalink
Added unit test for use case GetNote
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
Pooja shaji committed Oct 12, 2022
1 parent cac6111 commit c4aabfb
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
31 changes: 31 additions & 0 deletions My Notes.plantuml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
defaultConfig {
applicationId "com.dlight.mynotes"
minSdk 23
targetSdk 28
targetSdk 32
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Note>()

override fun getNotes(): Flow<List<Note>> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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<Note>()
('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)
}
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c4aabfb

Please sign in to comment.