Skip to content

Commit

Permalink
Merge pull request #21 from alexymumo/unit-test
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
alexymumo committed Mar 24, 2024
2 parents 8521a38 + 8564b6d commit 3c0459b
Show file tree
Hide file tree
Showing 31 changed files with 834 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/android_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ jobs:
- name: Build with Gradle
run: ./gradlew build

- name: Upload coverage report to CodeCov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: alexymumo/News-App

- name: Upload a build artifact
uses: actions/upload-artifact@v3.1.3
with:
Expand Down
27 changes: 27 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

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

33 changes: 21 additions & 12 deletions .idea/deploymentTargetDropDown.xml

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

5 changes: 2 additions & 3 deletions .idea/gradle.xml

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

10 changes: 10 additions & 0 deletions .idea/migrations.xml

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

4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ android {
"proguard-rules.pro"
)
}
debug {
enableAndroidTestCoverage = true
enableUnitTestCoverage = true
}
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.database.db

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.alexmumo.database.dao.ArticleDao
import com.alexmumo.database.dao.BookMarkDao
import com.alexmumo.database.dao.RemoteKeyDao
import com.alexmumo.database.entity.ArticleEntity
import com.alexmumo.database.entity.SourceEntity
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class NewsDatabaseTest {

private lateinit var articleDao: ArticleDao
private lateinit var bookMarkDao: BookMarkDao
private lateinit var remoteKeyDao: RemoteKeyDao
private lateinit var newsDatabase: NewsDatabase

@Before
fun setUpDatabase() {
val context = ApplicationProvider.getApplicationContext<Context>()
newsDatabase = Room.inMemoryDatabaseBuilder(
context,
NewsDatabase::class.java
).build()
}

@Test
fun saveArticles_returns_a_listOf_articles() = runTest {
articleDao.saveArticles(articles)
val articles = articleDao.pagingSource()
assertThat(articles).isNotNull()
}

@Test
fun deleteArticles_returns_null() = runTest {
articleDao.saveArticles(articles)
val articles = articleDao.deleteArticles()
assertThat(articles).isNull()
}

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

companion object {
private val article = ArticleEntity(author = null, content = null, description = null, publishedAt = null, title = null, SourceEntity(id = null, name = ""), url = "", urlToImage = null)
val articles = listOf(article)
}
}
21 changes: 12 additions & 9 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,17 @@ dependencies {

implementation(libs.android.core)
implementation(libs.android.appcompat)
testImplementation(libs.junit)
androidTestImplementation(libs.expresso.core)
androidTestImplementation(libs.truth)
testImplementation(libs.truth)
androidTestImplementation(libs.core.testing)
implementation(libs.junit.androidx.ktx)
implementation(libs.core.ktx)
implementation(libs.core.test)

// Paging
implementation(libs.paging.runtime)

// Robolectric
testImplementation(libs.roboelectric)

// Coroutine
implementation(libs.bundles.coroutine)

// Room
testImplementation(libs.room.testing)
implementation(libs.room.paging)
implementation(libs.room.ktx)
api(libs.room.runtime)
Expand All @@ -69,4 +61,15 @@ dependencies {
// Hilt
implementation(libs.dagger.hilt)
kapt(libs.hilt.compiler)


testImplementation(libs.junit)
testImplementation(libs.truth)
testImplementation(libs.room.testing)
testImplementation(libs.roboelectric)

androidTestImplementation(libs.core.testing)
androidTestImplementation(libs.expresso.core)
androidTestImplementation(libs.truth)
androidTestImplementation(libs.junit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.database.db

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.alexmumo.database.dao.ArticleDao
import com.alexmumo.database.dao.BookMarkDao
import com.alexmumo.database.dao.RemoteKeyDao
import com.alexmumo.database.entity.ArticleEntity
import com.alexmumo.database.entity.SourceEntity
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class NewsDatabaseTest {

private lateinit var articleDao: ArticleDao
private lateinit var bookMarkDao: BookMarkDao
private lateinit var remoteKeyDao: RemoteKeyDao
private lateinit var newsDatabase: NewsDatabase

@Before
fun setUpDatabase() {
val context = ApplicationProvider.getApplicationContext<Context>()
newsDatabase = Room.inMemoryDatabaseBuilder(
context,
NewsDatabase::class.java
).build()
}

@Test
fun saveArticles_returns_a_listOf_articles() = runTest {
articleDao.saveArticles(articles)
val articles = articleDao.pagingSource()
assertThat(articles).isNotNull()
}

@Test
fun deleteArticles_returns_null() = runTest {
articleDao.saveArticles(articles)
val articles = articleDao.deleteArticles()
assertThat(articles).isNull()
}

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

companion object {
private val article = ArticleEntity(author = null, content = null, description = null, publishedAt = null, title = null, SourceEntity(id = null, name = ""), url = "", urlToImage = null)
val articles = listOf(article)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2024 News-App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexmumo.datastore

class NewsPreferenceTest
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.enableJetifier=true
Loading

0 comments on commit 3c0459b

Please sign in to comment.