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

Add tests to the data module #51

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies {
api(libs.kotlinx.serialization.json)

testRuntimeOnly(libs.robolectric)
testImplementation(libs.junit)
testImplementation(libs.kotlin.test)
testImplementation(libs.androidx.test.ext.junit)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ data class AspectRatio(val numerator: Int, val denominator: Int) {
fun parse(str: String): AspectRatio {
val numeratorDenominatorString = str.split(SEPARATOR)
require(numeratorDenominatorString.size == 2) { "Expected rational as numerator:denominator but is $str" }
val numerator = numeratorDenominatorString[0].toInt()
val denominator = numeratorDenominatorString[1].toInt()
if (denominator == 0) return Infinity
val numerator = numeratorDenominatorString[0].toInt()
if (numerator == 0) return Zero
return AspectRatio(numerator, denominator)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ data class Chapter(
}

fun doesHaveBlockedSegment(): Boolean {
for (segment in segmentList.orEmpty()) {
if (segment.isBlocked()) {
return true
}
}
return false
return segmentList.orEmpty().any { it.isBlocked() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ abstract class ListResult<T> : Iterable<T> {
abstract val next: String?

val list: List<T>
get() {
return data.orEmpty()
}
get() = data.orEmpty()

val size: Int
get() {
return list.size
}
get() = list.size

fun isEmpty(): Boolean {
return data.isNullOrEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ data class NowAndNext(
override val rawImageUrl: ImageUrl? = null,
val now: Program? = null,
val next: Program? = null
) :
SRGChannelMetadata
) : SRGChannelMetadata
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum class BlockReason {
companion object {
fun parseValue(value: String): BlockReason {
return try {
valueOf(value)
enumValueOf(value)
} catch (e: IllegalArgumentException) {
UNKNOWN
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.srg.dataProvider.integrationlayer.data

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import java.text.ParseException
import java.util.Date
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* [Testing Fundamentals](http://d.android.com/tools/testing/testing_android.html)
Expand All @@ -18,9 +18,9 @@ class DateParserTest {
@Test
fun test8601() {
val parser = ISO8601DateParser()
Assert.assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T08:36:15.079+02:00"))
Assert.assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T08:36:15+02:00"))
Assert.assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T06:36:15Z"))
assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T08:36:15.079+02:00"))
assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T08:36:15+02:00"))
assertEquals(Date(1496126175000L), parser.parseDate("2017-05-30T06:36:15Z"))
}

@Test(expected = ParseException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ch.srg.dataProvider.integrationlayer.data.remote

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class AspectRatioTest {
@Test
fun parse() {
val aspectRatio = AspectRatio.parse("16:9")

assertEquals(16, aspectRatio.numerator)
assertEquals(9, aspectRatio.denominator)
}

@Test
fun `parse denominator 0`() {
val aspectRatio = AspectRatio.parse("16:0")

assertEquals(AspectRatio.Infinity, aspectRatio)
}

@Test
fun `parse numerator 0`() {
val aspectRatio = AspectRatio.parse("0:9")

assertEquals(AspectRatio.Zero, aspectRatio)
}

@Test(expected = IllegalArgumentException::class)
fun `parse empty string`() {
AspectRatio.parse("")
}

@Test(expected = IllegalArgumentException::class)
fun `parse blank string`() {
AspectRatio.parse(" ")
}

@Test(expected = IllegalArgumentException::class)
fun `parse invalid string`() {
AspectRatio.parse("hello")
}

@Test(expected = IllegalArgumentException::class)
fun `parse string with invalid numerator`() {
AspectRatio.parse("abc:9")
}

@Test(expected = IllegalArgumentException::class)
fun `parse string with invalid denominator`() {
AspectRatio.parse("16:abc")
}

@Test(expected = IllegalArgumentException::class)
fun `parse string with too many separators`() {
AspectRatio.parse("1:2:3")
}

@Test
fun `destruction check`() {
val aspectRatio = AspectRatio(16, 9)
val (numerator, denominator) = aspectRatio

assertEquals(aspectRatio.numerator, numerator)
assertEquals(aspectRatio.denominator, denominator)
}

@Test
fun `equals check`() {
assertEquals(AspectRatio.Infinity, AspectRatio(1, 0))
assertEquals(AspectRatio.Zero, AspectRatio(0, 1))
assertEquals(AspectRatio(16, 9), AspectRatio(16, 9))
assertNotEquals(AspectRatio(4, 3), AspectRatio(16, 9))
}

@Test
fun `toString format check`() {
assertEquals("1:0", AspectRatio.Infinity.toString())
assertEquals("0:1", AspectRatio.Zero.toString())
assertEquals("16:9", AspectRatio(16, 9).toString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.srg.dataProvider.integrationlayer.data.remote

import kotlin.test.Test
import kotlin.test.assertEquals

class BlockReasonTest {
@Test
fun `parse value`() {
assertEquals(BlockReason.UNKNOWN, BlockReason.parseValue(""))
assertEquals(BlockReason.UNKNOWN, BlockReason.parseValue("INVALID_REASON"))

BlockReason.entries.forEach { blockReason ->
assertEquals(blockReason, BlockReason.parseValue(blockReason.name))
}

BlockReason.entries.forEach { blockReason ->
assertEquals(BlockReason.UNKNOWN, BlockReason.parseValue(blockReason.name.lowercase()))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ch.srg.dataProvider.integrationlayer.data.remote

import ch.srg.dataProvider.integrationlayer.data.ImageUrl
import java.util.Date
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.minutes

class ChapterTest {
@Test
fun `find segment with null segment list`() {
val chapter = createChapter(segmentList = null)

assertNull(chapter.findSegment("urn:rts:video:123456"))
}

@Test
fun `find segment with empty segment list`() {
val chapter = createChapter(segmentList = emptyList())

assertNull(chapter.findSegment("urn:rts:video:123456"))
}

@Test
fun `find segment with segment list`() {
val segment = createSegment(SEGMENT_URN)
val chapter = createChapter(segmentList = listOf(createSegment("urn:rts:video:987654"), segment))

assertNull(chapter.findSegment("urn:rts:video:123456"))
assertEquals(segment, chapter.findSegment(SEGMENT_URN))
}

@Test
fun `find 360 resource with null resource list`() {
val chapter = createChapter(resourceList = null)

assertNull(chapter.find360Resource())
assertFalse(chapter.is360())
}

@Test
fun `find 360 resource with empty resource list`() {
val chapter = createChapter(resourceList = emptyList())

assertNull(chapter.find360Resource())
assertFalse(chapter.is360())
}

@Test
fun `find 360 resource without 360 resource in resource list`() {
val chapter = createChapter(resourceList = listOf(createResource(), createResource()))

assertNull(chapter.find360Resource())
assertFalse(chapter.is360())
}

@Test
fun `find 360 resource with 360 resource in resource list`() {
val resource = createResource(presentation = Presentation.VIDEO_360)
val chapter = createChapter(resourceList = listOf(createResource(), resource))

assertEquals(resource, chapter.find360Resource())
assertTrue(chapter.is360())
}

@Test
fun `does have blocked segment with null segment list`() {
val chapter = createChapter(segmentList = null)

assertFalse(chapter.doesHaveBlockedSegment())
}

@Test
fun `does have blocked segment with empty segment list`() {
val chapter = createChapter(segmentList = emptyList())

assertFalse(chapter.doesHaveBlockedSegment())
}

@Test
fun `does have blocked segment with segment list`() {
val segment = createSegment(SEGMENT_URN, blocked = true)
val chapter = createChapter(segmentList = listOf(createSegment("urn:rts:video:987654"), segment))

assertTrue(chapter.doesHaveBlockedSegment())
}

private companion object {
private const val SEGMENT_URN = "urn:rts:video:456789"

private fun createChapter(
segmentList: List<Segment>? = null,
resourceList: List<Resource>? = null,
): Chapter {
return Chapter(
id = "chapter-id",
mediaType = MediaType.VIDEO,
vendor = Vendor.RTS,
urn = "urn:rts:video:123456",
title = "chapter-title",
imageUrl = ImageUrl("https://image.url"),
type = Type.EPISODE,
date = Date(),
duration = 90.minutes.inWholeMilliseconds,
segmentList = segmentList,
resourceList = resourceList,
)
}

private fun createSegment(
urn: String,
blocked: Boolean = false,
): Segment {
return Segment(
id = "segment-id",
mediaType = MediaType.VIDEO,
vendor = Vendor.RTS,
urn = urn,
title = "segment-title",
markIn = 0L,
markOut = 0L,
type = Type.CLIP,
date = Date(),
duration = 30.minutes.inWholeMilliseconds,
displayable = true,
playableAbroad = true,
imageUrl = ImageUrl("https://image.url"),
blockReason = BlockReason.LEGAL.takeIf { blocked },
)
}

private fun createResource(presentation: Presentation = Presentation.DEFAULT): Resource {
return Resource(
url = "https://resource.url",
quality = Quality.HD,
presentation = presentation,
streamingMethod = StreamingMethod.DASH,
)
}
}
}
Loading
Loading