Skip to content

Commit

Permalink
Add tests to the remote package
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Jan 17, 2025
1 parent 331ec32 commit c7d8446
Show file tree
Hide file tree
Showing 19 changed files with 1,098 additions and 16 deletions.
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
@@ -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,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ch.srg.dataProvider.integrationlayer.data.remote

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class ListResultTest {
@Test
fun `list result null data`() {
val listResult = StringListResult(data = null)

assertEquals(emptyList(), listResult.list)
assertEquals(0, listResult.size)
assertTrue(listResult.isEmpty())
assertFalse(listResult.contains(""))
assertFalse(listResult.contains("c"))
assertTrue(listResult.containsAll(emptyList()))
assertFalse(listResult.containsAll(listOf("b", "d")))
assertNotNull(listResult.iterator())
}

@Test(expected = IndexOutOfBoundsException::class)
fun `list result null data with get item`() {
val listResult = StringListResult(data = null)

listResult[0]
}

@Test
fun `list result empty data`() {
val listResult = StringListResult(data = emptyList())

assertEquals(emptyList(), listResult.list)
assertEquals(0, listResult.size)
assertTrue(listResult.isEmpty())
assertFalse(listResult.contains(""))
assertFalse(listResult.contains("c"))
assertTrue(listResult.containsAll(emptyList()))
assertFalse(listResult.containsAll(listOf("b", "d")))
assertNotNull(listResult.iterator())
}

@Test(expected = IndexOutOfBoundsException::class)
fun `list result empty data with get item`() {
val listResult = StringListResult(data = emptyList())

listResult[0]
}

@Test
fun `list result`() {
val data = listOf("a", "b", "c", "d", "e", "f")
val listResult = StringListResult(data = data)

assertEquals(data, listResult.list)
assertEquals(data.size, listResult.size)
assertFalse(listResult.isEmpty())
assertFalse(listResult.contains(""))
assertTrue(listResult.contains("c"))
assertTrue(listResult.containsAll(emptyList()))
assertTrue(listResult.containsAll(listOf("b", "d")))
assertFalse(listResult.containsAll(listOf("e", "g")))
assertNotNull(listResult.iterator())
}

private class StringListResult(
override val data: List<String>?,
) : ListResult<String>() {
override val next = null
}
}
Loading

0 comments on commit c7d8446

Please sign in to comment.