Skip to content

Commit

Permalink
Migrate to testFixtures, closes android#451
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMarquis committed Nov 19, 2022
1 parent 9fa3dad commit 1e5dadf
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 1,486 deletions.
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,10 +18,11 @@ 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.nextFakeTopic
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
import kotlin.random.Random
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -87,7 +88,7 @@ class GetFollowableTopicsStreamUseCaseTest {
}

private val testTopics = listOf(
Topic("1", "Headlines", "", "", "", ""),
Topic("2", "Android Studio", "", "", "", ""),
Topic("3", "Compose", "", "", "", ""),
Random.nextFakeTopic(id = "1", name = "Headlines"),
Random.nextFakeTopic(id = "2", name = "Android Studio"),
Random.nextFakeTopic(id = "3", name = "Compose"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
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.nextFakeAuthor
import com.google.samples.apps.nowinandroid.core.model.data.nextFakeNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.nextFakeTopic
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.random.Random
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 +124,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 = Random.nextFakeTopic(id = "1", name = "Headlines")
private val sampleTopic2 = Random.nextFakeTopic(id = "2", name = "UI")
private val sampleAuthor1 = Random.nextFakeAuthor(id = "1")
private val sampleAuthor2 = Random.nextFakeAuthor(id = "2")

private val sampleNewsResources = listOf(
NewsResource(
Random.nextFakeNewsResource(
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(
Random.nextFakeNewsResource(
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(
Random.nextFakeNewsResource(
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,10 +17,11 @@
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.nextFakeAuthor
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
import kotlin.random.Random
import kotlin.test.assertEquals
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -64,34 +65,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 = Random.nextFakeAuthor(id = "1", name = "Mandy")
private val sampleAuthor2 = Random.nextFakeAuthor(id = "2", name = "Andy")
private val sampleAuthor3 = Random.nextFakeAuthor(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,35 @@
/*
* 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 kotlin.random.Random

fun Random.nextFakeAuthor(
id: String = nextLong().toString(),
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 = "",
): 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,43 @@
/*
* 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 kotlin.random.Random
import kotlinx.datetime.Instant

fun Random.nextFakeNewsResource(
id: String = nextLong().toString(),
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,35 @@
/*
* 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 kotlin.random.Random

fun Random.nextFakeTopic(
id: String = Random.nextLong().toString(),
name: String = "Topic $id",
shortDescription: String = "",
longDescription: String = "",
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,
)
Loading

0 comments on commit 1e5dadf

Please sign in to comment.