From 4e6f2db375d729502eacbf0364779a95495123c7 Mon Sep 17 00:00:00 2001 From: Alex Mumo Date: Sun, 24 Mar 2024 02:22:47 +0300 Subject: [PATCH] Add core module tests --- .../alexmumo/database/db/NewsDatabaseTest.kt | 65 +++++++++++++++++++ .../alexmumo/datastore/NewsPreferenceTest.kt | 18 +++++ core/database/build.gradle.kts | 2 + .../alexmumo/database/db/NewsDatabaseTest.kt | 65 +++++++++++++++++++ .../alexmumo/datastore/NewsPreferenceTest.kt | 18 +++++ 5 files changed, 168 insertions(+) create mode 100644 core/build/spotless/spotlessKotlin/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt create mode 100644 core/build/spotless/spotlessKotlin/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt create mode 100644 core/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt create mode 100644 core/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt diff --git a/core/build/spotless/spotlessKotlin/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt b/core/build/spotless/spotlessKotlin/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt new file mode 100644 index 0000000..83147b2 --- /dev/null +++ b/core/build/spotless/spotlessKotlin/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt @@ -0,0 +1,65 @@ +/* + * 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 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 + +@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() + newsDatabase = Room.inMemoryDatabaseBuilder( + context, + NewsDatabase::class.java + ).build() + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun saveArticles_returns_a_listOf_articles() = runTest { + } + + @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) + } +} diff --git a/core/build/spotless/spotlessKotlin/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt b/core/build/spotless/spotlessKotlin/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt new file mode 100644 index 0000000..2d47d08 --- /dev/null +++ b/core/build/spotless/spotlessKotlin/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt @@ -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 diff --git a/core/database/build.gradle.kts b/core/database/build.gradle.kts index 6a1c7bd..e4eebad 100644 --- a/core/database/build.gradle.kts +++ b/core/database/build.gradle.kts @@ -39,9 +39,11 @@ dependencies { implementation(libs.android.core) implementation(libs.android.appcompat) + implementation(libs.junit.androidx.ktx) testImplementation(libs.junit) androidTestImplementation(libs.expresso.core) androidTestImplementation(libs.truth) + androidTestImplementation(libs.junit) testImplementation(libs.truth) androidTestImplementation(libs.core.testing) implementation(libs.core.ktx) diff --git a/core/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt b/core/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt new file mode 100644 index 0000000..83147b2 --- /dev/null +++ b/core/database/src/androidTest/java/com/alexmumo/database/db/NewsDatabaseTest.kt @@ -0,0 +1,65 @@ +/* + * 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 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 + +@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() + newsDatabase = Room.inMemoryDatabaseBuilder( + context, + NewsDatabase::class.java + ).build() + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun saveArticles_returns_a_listOf_articles() = runTest { + } + + @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) + } +} diff --git a/core/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt b/core/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt new file mode 100644 index 0000000..2d47d08 --- /dev/null +++ b/core/datastore/src/test/java/com/alexmumo/datastore/NewsPreferenceTest.kt @@ -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