Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to testFixtures, closes #451 #452

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ dependencies {
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.serialization.json)

testImplementation(testFixtures(project(":core:model")))
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package com.google.samples.apps.nowinandroid.core.database.model

import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import kotlin.test.assertEquals
import kotlinx.datetime.Instant
import org.junit.Test
Expand Down Expand Up @@ -58,39 +56,20 @@ class PopulatedNewsResourceKtTest {
)
),
)
val newsResource = populatedNewsResource.asExternalModel()

assertEquals(
NewsResource(
id = "1",
title = "news",
content = "Hilt",
url = "url",
headerImageUrl = "headerImageUrl",
type = Video,
publishDate = Instant.fromEpochMilliseconds(1),
authors = listOf(
Author(
id = "2",
name = "name",
imageUrl = "imageUrl",
twitter = "twitter",
mediumPage = "mediumPage",
bio = "bio",
)
),
topics = listOf(
Topic(
id = "3",
name = "name",
shortDescription = "short description",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
)
)
id = populatedNewsResource.entity.id,
title = populatedNewsResource.entity.title,
content = populatedNewsResource.entity.content,
url = populatedNewsResource.entity.url,
headerImageUrl = populatedNewsResource.entity.headerImageUrl,
publishDate = populatedNewsResource.entity.publishDate,
type = populatedNewsResource.entity.type,
authors = populatedNewsResource.authors.map(AuthorEntity::asExternalModel),
topics = populatedNewsResource.topics.map(TopicEntity::asExternalModel),
),
newsResource
populatedNewsResource.asExternalModel()
)
}
}
2 changes: 2 additions & 0 deletions core/domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ dependencies {

implementation(libs.hilt.android)
kapt(libs.hilt.compiler)

testImplementation(testFixtures(project(":core:model")))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.google.samples.apps.nowinandroid.core.domain

import com.google.samples.apps.nowinandroid.core.domain.TopicSortField.NAME
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.model.data.fakeTopic
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
Expand Down Expand Up @@ -87,7 +87,7 @@ class GetFollowableTopicsStreamUseCaseTest {
}

private val testTopics = listOf(
Topic("1", "Headlines", "", "", "", ""),
Topic("2", "Android Studio", "", "", "", ""),
Topic("3", "Compose", "", "", "", ""),
fakeTopic(id = "1", name = "Headlines"),
fakeTopic(id = "2", name = "Android Studio"),
fakeTopic(id = "3", name = "Compose"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
package com.google.samples.apps.nowinandroid.core.domain

import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.model.data.fakeAuthor
import com.google.samples.apps.nowinandroid.core.model.data.fakeNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.fakeTopic
import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.Rule
import org.junit.Test

Expand Down Expand Up @@ -125,81 +123,25 @@ class GetSaveableNewsResourcesStreamUseCaseTest {
}
}

private val sampleTopic1 = Topic(
id = "Topic1",
name = "Headlines",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
)

private val sampleTopic2 = Topic(
id = "Topic2",
name = "UI",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
)

private val sampleAuthor1 =
Author(
id = "Author1",
name = "Android Dev",
imageUrl = "",
twitter = "",
mediumPage = "",
bio = "",
)

private val sampleAuthor2 =
Author(
id = "Author2",
name = "Android Dev",
imageUrl = "",
twitter = "",
mediumPage = "",
bio = "",
)
private val sampleTopic1 = fakeTopic(id = "1", name = "Headlines")
private val sampleTopic2 = fakeTopic(id = "2", name = "UI")
private val sampleAuthor1 = fakeAuthor(id = "1")
private val sampleAuthor2 = fakeAuthor(id = "2")

private val sampleNewsResources = listOf(
NewsResource(
fakeNewsResource(
id = "1",
title = "Thanks for helping us reach 1M YouTube Subscribers",
content = "Thank you everyone for following the Now in Android series and everything the " +
"Android Developers YouTube channel has to offer. During the Android Developer " +
"Summit, our YouTube channel reached 1 million subscribers! Here’s a small video to " +
"thank you all.",
url = "https://youtu.be/-fJ6poHQrjM",
headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video,
topics = listOf(sampleTopic1),
authors = listOf(sampleAuthor1)
authors = listOf(sampleAuthor1),
topics = listOf(sampleTopic1)
),
NewsResource(
fakeNewsResource(
id = "2",
title = "Transformations and customisations in the Paging Library",
content = "A demonstration of different operations that can be performed with Paging. " +
"Transformations like inserting separators, when to create a new pager, and " +
"customisation options for consuming PagingData.",
url = "https://youtu.be/ZARz0pjm5YM",
headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-01T00:00:00.000Z"),
type = Video,
topics = listOf(sampleTopic1, sampleTopic2),
authors = listOf(sampleAuthor1)
authors = listOf(sampleAuthor1),
topics = listOf(sampleTopic1, sampleTopic2)
),
NewsResource(
fakeNewsResource(
id = "3",
title = "Community tip on Paging",
content = "Tips for using the Paging library from the developer community",
url = "https://youtu.be/r5JgIyS3t3s",
headerImageUrl = "https://i.ytimg.com/vi/r5JgIyS3t3s/maxresdefault.jpg",
publishDate = Instant.parse("2021-11-08T00:00:00.000Z"),
type = Video,
topics = listOf(sampleTopic2),
authors = listOf(sampleAuthor2)
authors = listOf(sampleAuthor2),
topics = listOf(sampleTopic2)
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.samples.apps.nowinandroid.core.domain

import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor
import com.google.samples.apps.nowinandroid.core.model.data.Author
import com.google.samples.apps.nowinandroid.core.model.data.fakeAuthor
import com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
Expand Down Expand Up @@ -64,34 +64,8 @@ class GetSortedFollowableAuthorsStreamUseCaseTest {
}
}

private val sampleAuthor1 =
Author(
id = "Author1",
name = "Mandy",
imageUrl = "",
twitter = "",
mediumPage = "",
bio = "",
)

private val sampleAuthor2 =
Author(
id = "Author2",
name = "Andy",
imageUrl = "",
twitter = "",
mediumPage = "",
bio = "",
)

private val sampleAuthor3 =
Author(
id = "Author2",
name = "Sandy",
imageUrl = "",
twitter = "",
mediumPage = "",
bio = "",
)
private val sampleAuthor1 = fakeAuthor(id = "1", name = "Mandy")
private val sampleAuthor2 = fakeAuthor(id = "2", name = "Andy")
private val sampleAuthor3 = fakeAuthor(id = "3", name = "Sandy")

private val sampleAuthors = listOf(sampleAuthor1, sampleAuthor2, sampleAuthor3)
2 changes: 2 additions & 0 deletions core/model/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
id("kotlin")
`java-test-fixtures`
}

dependencies {
implementation(libs.kotlinx.datetime)
testFixturesImplementation(libs.kotlinx.datetime)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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.google.samples.apps.nowinandroid.core.model.data

fun fakeAuthor(
id: String,
name: String = "Android Dev $id",
imageUrl: String = "https://example.org/dev-android/$id.png",
twitter: String = "@dev-android-$id",
mediumPage: String = "https://medium.com/dev-android/$id",
bio: String = "At vero eos et accusamus et iusto odio dignissimos ducimus qui.",
): Author = Author(
id = id,
name = name,
imageUrl = imageUrl,
twitter = twitter,
mediumPage = mediumPage,
bio = bio,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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.google.samples.apps.nowinandroid.core.model.data

import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown
import kotlinx.datetime.Instant

fun fakeNewsResource(
id: String,
title: String = "News resource $id",
content: String = "",
url: String = "https://example.org/news/$id",
headerImageUrl: String? = "https://example.org/news/$id.png",
publishDate: Instant = Instant.fromEpochMilliseconds(0),
type: NewsResourceType = Unknown,
authors: List<Author> = emptyList(),
topics: List<Topic> = emptyList(),
): NewsResource = NewsResource(
id = id,
title = title,
content = content,
url = url,
headerImageUrl = headerImageUrl,
publishDate = publishDate,
type = type,
authors = authors,
topics = topics,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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.google.samples.apps.nowinandroid.core.model.data

fun fakeTopic(
id: String,
name: String = "Topic $id",
shortDescription: String = "At vero eos et accusamus.",
longDescription: String = "At vero eos et accusamus et iusto odio dignissimos ducimus qui.",
url: String = "https://example.org/topic/$id",
imageUrl: String = "https://example.org/topic/$id.png"
): Topic = Topic(
id = id,
name = name,
shortDescription = shortDescription,
longDescription = longDescription,
url = url,
imageUrl = imageUrl,
)
2 changes: 2 additions & 0 deletions feature/author/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ android {

dependencies {
implementation(libs.kotlinx.datetime)
androidTestImplementation(testFixtures(project(":core:model")))
testImplementation(testFixtures(project(":core:model")))
}
Loading