From 16df4918e2e5b08e9026ff4ba8ca6998542b21ff Mon Sep 17 00:00:00 2001 From: KP9lK Date: Wed, 9 Apr 2025 00:11:44 +0300 Subject: [PATCH] add tests --- .idea/deploymentTargetSelector.xml | 8 +----- .idea/gradle.xml | 1 + .idea/misc.xml | 1 - .../ExampleInstrumentedTest.kt | 28 +++++++++++++++---- .../domain/usecase/AuthUseCase.kt | 2 ++ .../domain/validator/EmailValidator.kt | 8 ++++++ .../domain/validator/Validator.kt | 5 ++++ 7 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/example/netwrok_datastore/domain/validator/EmailValidator.kt create mode 100644 app/src/main/java/com/example/netwrok_datastore/domain/validator/Validator.kt diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index 5c91657..576e96c 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -5,13 +5,7 @@ - - - - - + diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 639c779..7b3006b 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -13,6 +13,7 @@ + diff --git a/.idea/misc.xml b/.idea/misc.xml index 2cdc89a..f7608ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/androidTest/java/com/example/netwrok_datastore/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/netwrok_datastore/ExampleInstrumentedTest.kt index 263887c..d3e18f8 100644 --- a/app/src/androidTest/java/com/example/netwrok_datastore/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/com/example/netwrok_datastore/ExampleInstrumentedTest.kt @@ -2,23 +2,39 @@ package com.example.netwrok_datastore import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.example.netwrok_datastore.domain.validator.EmailValidator import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* +import org.junit.runners.Parameterized +import org.junit.runners.Parameterized.Parameters /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { +@RunWith(Parameterized::class) +class EmailValidationTest(val email: String, val resultTest: Boolean) { + @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.example.netwrok_datastore", appContext.packageName) + fun testValidationEmail(){ + + val result = EmailValidator().validate(email) + assertEquals(result, resultTest) + } + + + companion object{ + @JvmStatic + @Parameters + fun getData() = listOf( + arrayOf("test", false), + arrayOf("@", false), + arrayOf("test@", true), + arrayOf("test.ru", false) + ) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/netwrok_datastore/domain/usecase/AuthUseCase.kt b/app/src/main/java/com/example/netwrok_datastore/domain/usecase/AuthUseCase.kt index 4082c4e..742b634 100644 --- a/app/src/main/java/com/example/netwrok_datastore/domain/usecase/AuthUseCase.kt +++ b/app/src/main/java/com/example/netwrok_datastore/domain/usecase/AuthUseCase.kt @@ -4,6 +4,7 @@ import com.example.netwrok_datastore.data.AuthRepository import com.example.netwrok_datastore.data.local.LocalStorage import com.example.netwrok_datastore.data.remote.NetworkResponse import com.example.netwrok_datastore.data.remote.dto.request.RegistrationRequest +import com.example.netwrok_datastore.domain.validator.EmailValidator import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow @@ -13,6 +14,7 @@ class AuthUseCase(private val localStorage: LocalStorage, suspend fun registration(registrationRequest: RegistrationRequest): Flow = flow{ try { emit(NetworkResponse.Loading) + if (!EmailValidator().validate(registrationRequest.email)) emit(NetworkResponse.Error("Validation Error")) val result = authRepository.registration(registrationRequest) localStorage.setToken(result.second) emit(NetworkResponse.Success(result)) diff --git a/app/src/main/java/com/example/netwrok_datastore/domain/validator/EmailValidator.kt b/app/src/main/java/com/example/netwrok_datastore/domain/validator/EmailValidator.kt new file mode 100644 index 0000000..c22c9ce --- /dev/null +++ b/app/src/main/java/com/example/netwrok_datastore/domain/validator/EmailValidator.kt @@ -0,0 +1,8 @@ +package com.example.netwrok_datastore.domain.validator + +class EmailValidator: Validator { + override fun validate(value: T): Boolean { + val email = value as String + return email.contains("@") + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/netwrok_datastore/domain/validator/Validator.kt b/app/src/main/java/com/example/netwrok_datastore/domain/validator/Validator.kt new file mode 100644 index 0000000..d814001 --- /dev/null +++ b/app/src/main/java/com/example/netwrok_datastore/domain/validator/Validator.kt @@ -0,0 +1,5 @@ +package com.example.netwrok_datastore.domain.validator + +interface Validator { + fun validate(value: T): Boolean +} \ No newline at end of file