Skip to content

Unit Testing #208

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

Merged
merged 49 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9ee4158
Unit Testing | Patch 1
ShiftHackZ Jul 11, 2024
eb1a320
Unit Testing | Patch 2
ShiftHackZ Jul 13, 2024
340ac57
Unit Testing | Patch 3
ShiftHackZ Jul 13, 2024
733afac
Unit Testing | Patch 4
ShiftHackZ Jul 13, 2024
2632ec8
Unit Testing | Patch 5
ShiftHackZ Jul 14, 2024
9cb62cd
Unit Testing | Patch 6
ShiftHackZ Jul 14, 2024
66ad9df
Unit Testing | Patch 7
ShiftHackZ Jul 14, 2024
0644ce3
Unit Testing | Patch 8
ShiftHackZ Jul 14, 2024
dd27df8
Unit Testing | Patch 9
ShiftHackZ Jul 15, 2024
8d32685
Unit Testing | Patch 10
ShiftHackZ Jul 16, 2024
eef063e
Unit Testing | Patch 11
ShiftHackZ Jul 17, 2024
02b3c98
Unit Testing | Patch 12
ShiftHackZ Jul 17, 2024
45cc988
Unit Testing | Patch 13
ShiftHackZ Jul 18, 2024
78dbf74
Unit Testing | Patch 14
ShiftHackZ Jul 18, 2024
79e1549
Unit Testing | Patch 15
ShiftHackZ Jul 22, 2024
b395710
Unit Testing | Patch 16
ShiftHackZ Jul 22, 2024
4c8cc56
Unit Testing | Patch 17
ShiftHackZ Jul 22, 2024
76ca62b
Unit Testing | Patch 18
ShiftHackZ Jul 22, 2024
32db32e
Unit Testing | Patch 19
ShiftHackZ Jul 22, 2024
f2d6505
Unit Testing | Patch 20
ShiftHackZ Jul 22, 2024
db830c5
Unit Testing | Patch 21
ShiftHackZ Jul 26, 2024
f5e66f9
Unit Testing | Patch 22
ShiftHackZ Jul 26, 2024
cb1467f
Unit Testing | Patch 23
ShiftHackZ Jul 26, 2024
21b3c70
Unit Testing | Patch 24
ShiftHackZ Jul 27, 2024
0b894a1
Unit Testing | Patch 24
ShiftHackZ Jul 28, 2024
9ae1c16
Unit Testing | Patch 25
ShiftHackZ Jul 28, 2024
9a2d817
Unit Testing | Patch 26
ShiftHackZ Jul 28, 2024
a8def3f
Unit Testing | Patch 27
ShiftHackZ Jul 28, 2024
ea2e716
Unit Testing | Patch 28
ShiftHackZ Jul 28, 2024
562e93f
Unit Testing | Patch 29
ShiftHackZ Jul 28, 2024
6a385e3
Unit Testing | Patch 30
ShiftHackZ Jul 28, 2024
7f4ac93
Unit Testing | Patch 31
ShiftHackZ Jul 29, 2024
da45b15
Unit Testing | Patch 32
ShiftHackZ Jul 29, 2024
1ab1dc1
Unit Testing | Patch 33
ShiftHackZ Jul 29, 2024
bff56b5
Unit Testing | Patch 34
ShiftHackZ Jul 29, 2024
cca340e
Unit Testing | Patch 35
ShiftHackZ Jul 29, 2024
1f67795
Unit Testing | Patch 36
ShiftHackZ Jul 29, 2024
23eba7e
Unit Testing | Patch 37
ShiftHackZ Jul 29, 2024
0c3a761
Unit Testing | Patch 38
ShiftHackZ Jul 29, 2024
e8122d7
Unit Testing | Patch 39
ShiftHackZ Jul 29, 2024
fa682b6
Unit Testing | Patch 40
ShiftHackZ Aug 3, 2024
b563a32
Unit Testing | Patch 41
ShiftHackZ Aug 3, 2024
436859f
Unit Testing | Patch 42
ShiftHackZ Aug 3, 2024
ec93a4e
Unit Testing | Patch 43
ShiftHackZ Aug 3, 2024
1ab6e10
Unit Testing | Patch 44
ShiftHackZ Aug 3, 2024
d6b6b1b
Unit Testing | Patch 45
ShiftHackZ Aug 3, 2024
963e4fc
Unit Testing | Patch 46
ShiftHackZ Aug 3, 2024
e6f6425
Unit Testing | Patch 47
ShiftHackZ Aug 3, 2024
255a05d
Unit Testing | Patch 48
ShiftHackZ Aug 3, 2024
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 .github/workflows/android_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
run: chmod +x ./gradlew

- name: Run Tests with Gradle
run: ./gradlew test
run: ./gradlew clean testDebugUnitTest
10 changes: 10 additions & 0 deletions app/src/main/java/com/shifthackz/aisdv1/app/di/ProvidersModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.shifthackz.aisdv1.core.common.appbuild.BuildVersion
import com.shifthackz.aisdv1.core.common.file.FileProviderDescriptor
import com.shifthackz.aisdv1.core.common.links.LinksProvider
import com.shifthackz.aisdv1.core.common.schedulers.SchedulersProvider
import com.shifthackz.aisdv1.core.common.time.TimeProvider
import com.shifthackz.aisdv1.domain.entity.ServerSource
import com.shifthackz.aisdv1.domain.feature.auth.AuthorizationCredentials
import com.shifthackz.aisdv1.domain.feature.auth.AuthorizationStore
Expand All @@ -24,6 +25,7 @@ import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.schedulers.Schedulers
import org.koin.android.ext.koin.androidApplication
import org.koin.dsl.module
import java.util.Date
import java.util.concurrent.Executor
import java.util.concurrent.Executors

Expand Down Expand Up @@ -134,6 +136,14 @@ val providersModule = module {
}
}

single<TimeProvider> {
object : TimeProvider {
override fun nanoTime(): Long = System.nanoTime()
override fun currentTimeMillis(): Long = System.currentTimeMillis()
override fun currentDate(): Date = Date()
}
}

single<FileProviderDescriptor> {
object : FileProviderDescriptor {
override val providerPath: String = "${androidApplication().packageName}.fileprovider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ package com.shifthackz.aisdv1.core.common.extensions
private const val PROTOCOL_DELIMITER = "://"
private const val PROTOCOL_HOLDER = "[[_PROTOCOL_]]"

fun String.withoutUrlProtocol(): String {
if (!this.contains(PROTOCOL_DELIMITER)) return this
val decomposed = this.split(PROTOCOL_DELIMITER)
if (decomposed.size < 2) return this
return decomposed.last()
}

fun String.fixUrlSlashes(): String = this
.replace(PROTOCOL_DELIMITER, PROTOCOL_HOLDER)
.replace(Regex("/{2,}"), "/")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.shifthackz.aisdv1.core.common.time

import java.util.Date

interface TimeProvider {
fun nanoTime(): Long
fun currentTimeMillis(): Long
fun currentDate(): Date
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.shifthackz.aisdv1.domain.entity
package com.shifthackz.aisdv1.core.common.appbuild

import com.shifthackz.aisdv1.core.common.appbuild.BuildVersion
import org.junit.Assert
import org.junit.Test

class AppVersionTest {
class BuildVersionTest {

@Test
fun `Parse 1_0_0, expected success`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.shifthackz.aisdv1.core.common.extensions

import org.junit.Assert
import org.junit.Test
import java.util.Date

class DateExtensionsTest {

companion object {
private val date = Date(894333955000) // 1998-05-05 05:05:55
}

@Test
fun `given date 05_05_1998, then getRawDay, expected 5`() {
val expected = 5
val actual = date.getRawDay()
Assert.assertEquals(expected, actual)
}

@Test
fun `given date 05_05_1998, then getRawMonth, expected 5`() {
val expected = 5
val actual = date.getRawMonth()
Assert.assertEquals(expected, actual)
}

@Test
fun `given date 05_05_1998, then getRawYear, expected 1998`() {
val expected = 1998
val actual = date.getRawYear()
Assert.assertEquals(expected, actual)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.shifthackz.aisdv1.core.common.extensions

import org.junit.Assert
import org.junit.Test

class KotlinExtensionsTest {

@Test
fun `given TestClass with null value, then applyIf with true predicate, expected test value changed`() {
class TestClass {
var testValue: String? = null
}

val instance = TestClass()
val predicate = true
instance.applyIf(predicate) {
testValue = "5598"
}

val expected = "5598"
val actual = instance.testValue
Assert.assertEquals(expected, actual)
}

@Test
fun `given TestClass with null value, then applyIf with false predicate, expected test value NOT changed`() {
class TestClass {
var testValue: String? = null
}

val instance = TestClass()
val predicate = false
instance.applyIf(predicate) {
testValue = "5598"
}

val expected = null
val actual = instance.testValue
Assert.assertEquals(expected, actual)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.shifthackz.aisdv1.core.common.math

import org.junit.Assert
import org.junit.Test

class MathUtilsTest {

@Test
fun `given Double with 8 fraction digits, then roundTo(2), expected Double with 2 fraction digits`() {
val value = 55.98238462
val expected = 55.98.toString()
val actual = value.roundTo(2).toString()
Assert.assertEquals(expected, actual)
}

@Test
fun `given Float with 6 fraction digits, then roundTo(2), expected Float with 2 fraction digits`() {
val value = 55.982384f
val expected = 55.98f.toString()
val actual = value.roundTo(2).toString()
Assert.assertEquals(expected, actual)
}
}
2 changes: 2 additions & 0 deletions core/validation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ android {
dependencies {
implementation di.koinCore
implementation reactive.rxkotlin
testImplementation test.junit
testImplementation test.mockk
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shifthackz.aisdv1.core.validation.horde
package com.shifthackz.aisdv1.core.validation.common

import com.shifthackz.aisdv1.core.validation.ValidationResult

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.shifthackz.aisdv1.core.validation.horde
package com.shifthackz.aisdv1.core.validation.common

import com.shifthackz.aisdv1.core.validation.ValidationResult

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.shifthackz.aisdv1.core.validation.di

import com.shifthackz.aisdv1.core.validation.common.CommonStringValidator
import com.shifthackz.aisdv1.core.validation.common.CommonStringValidatorImpl
import com.shifthackz.aisdv1.core.validation.dimension.DimensionValidator
import com.shifthackz.aisdv1.core.validation.dimension.DimensionValidatorImpl
import com.shifthackz.aisdv1.core.validation.horde.CommonStringValidator
import com.shifthackz.aisdv1.core.validation.horde.CommonStringValidatorImpl
import com.shifthackz.aisdv1.core.validation.url.UrlValidator
import com.shifthackz.aisdv1.core.validation.url.UrlValidatorImpl
import org.koin.core.module.dsl.factoryOf
Expand All @@ -13,7 +13,7 @@ import org.koin.dsl.module
val validatorsModule = module {
// !!! Do not use [factoryOf] for DimensionValidatorImpl, it has 2 default Ints in constructor
factory<DimensionValidator> { DimensionValidatorImpl() }
factory<UrlValidator> { UrlValidatorImpl() }

factoryOf(::UrlValidatorImpl) bind UrlValidator::class
factoryOf(::CommonStringValidatorImpl) bind CommonStringValidator::class
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.util.Patterns
import android.webkit.URLUtil
import com.shifthackz.aisdv1.core.validation.ValidationResult
import java.net.URI
import java.util.regex.Pattern

internal class UrlValidatorImpl : UrlValidator {
internal class UrlValidatorImpl(
private val webUrlPattern: Pattern = Patterns.WEB_URL,
) : UrlValidator {

override operator fun invoke(input: String?): ValidationResult<UrlValidator.Error> = when {
input == null -> ValidationResult(
Expand All @@ -32,7 +35,7 @@ internal class UrlValidatorImpl : UrlValidator {
isValid = false,
validationError = UrlValidator.Error.Invalid,
)
!Patterns.WEB_URL.matcher(input).matches() -> ValidationResult(
!webUrlPattern.matcher(input).matches() -> ValidationResult(
isValid = false,
validationError = UrlValidator.Error.Invalid,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.shifthackz.aisdv1.core.validation.common

import com.shifthackz.aisdv1.core.validation.ValidationResult
import org.junit.Assert
import org.junit.Test

class CommonStringValidatorImplTest {

private val validator = CommonStringValidatorImpl()

@Test
fun `given input is null, expected not valid with Empty error`() {
val expected = ValidationResult<CommonStringValidator.Error>(
isValid = false,
validationError = CommonStringValidator.Error.Empty,
)
val actual = validator(null)
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is empty, expected not valid with Empty error`() {
val expected = ValidationResult<CommonStringValidator.Error>(
isValid = false,
validationError = CommonStringValidator.Error.Empty,
)
val actual = validator("")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is blank, expected not valid with Empty error`() {
val expected = ValidationResult<CommonStringValidator.Error>(
isValid = false,
validationError = CommonStringValidator.Error.Empty,
)
val actual = validator(" ")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is non empty string, expected valid`() {
val expected = ValidationResult<CommonStringValidator.Error>(true)
val actual = validator("5598 is my favorite")
Assert.assertEquals(expected, actual)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.shifthackz.aisdv1.core.validation.dimension

import com.shifthackz.aisdv1.core.validation.ValidationResult
import org.junit.Assert
import org.junit.Test

class DimensionValidatorImplTest {

private val validator = DimensionValidatorImpl(MIN, MAX)

@Test
fun `given input is null, expected not valid with Empty error`() {
val expected = ValidationResult<DimensionValidator.Error>(
isValid = false,
validationError = DimensionValidator.Error.Empty,
)
val actual = validator(null)
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is empty, expected not valid with Empty error`() {
val expected = ValidationResult<DimensionValidator.Error>(
isValid = false,
validationError = DimensionValidator.Error.Empty,
)
val actual = validator("")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is unparsable to int, expected not valid with Unexpected error`() {
val expected = ValidationResult<DimensionValidator.Error>(
isValid = false,
validationError = DimensionValidator.Error.Unexpected,
)
val actual = validator("5598❤")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is less than minimum allowed value, expected not valid with LessThanMinimum error`() {
val expected = ValidationResult<DimensionValidator.Error>(
isValid = false,
validationError = DimensionValidator.Error.LessThanMinimum(MIN),
)
val actual = validator("55")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is bigger than maximum allowed value, expected not valid with BiggerThanMaximum error`() {
val expected = ValidationResult<DimensionValidator.Error>(
isValid = false,
validationError = DimensionValidator.Error.BiggerThanMaximum(MAX),
)
val actual = validator("5598")
Assert.assertEquals(expected, actual)
}

@Test
fun `given input is valid parsable int value, expected valid`() {
val expected = ValidationResult<DimensionValidator.Error>(true)
val actual = validator("1024")
Assert.assertEquals(expected, actual)
}

companion object {
private const val MIN = 64
private const val MAX = 2048
}
}
Loading
Loading