Skip to content

Commit

Permalink
feat: android unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff committed Oct 22, 2023
1 parent 2069238 commit 7b6cd0a
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 47 deletions.
52 changes: 26 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/SDK/Language/Android.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public function getFiles(): array
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/KeepAliveService.kt',
'template' => '/android/library/src/main/java/io/appwrite/KeepAliveService.kt.twig',
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/Response.kt',
'template' => '/android/library/src/main/java/io/appwrite/Response.kt.twig',
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/views/CallbackActivity.kt',
Expand Down Expand Up @@ -225,6 +230,16 @@ public function getFiles(): array
'destination' => '/library/src/main/AndroidManifest.xml',
'template' => '/android/library/src/main/AndroidManifest.xml.twig',
],
[
'scope' => 'service',
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}ServiceTest.kt',
'template' => '/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/cookies/CookiesTest.kt',
'template' => '/android/library/src/test/java/io/appwrite/cookies/CookiesTest.kt.twig',
],
[
'scope' => 'default',
'destination' => '/library/build.gradle',
Expand Down Expand Up @@ -387,6 +402,11 @@ public function getFiles(): array
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
],
[
'scope' => 'definition',
'destination' => 'library/src/test/java/io/appwrite/models/{{ definition.name | caseUcfirst }}Test.kt',
'template' => '/android/library/src/test/java/io/appwrite/models/ModelTest.kt.twig',
],
];
}
}
2 changes: 1 addition & 1 deletion templates/android/build.gradle.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "com.android.tools.build:gradle:7.4.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'

Expand Down
2 changes: 1 addition & 1 deletion templates/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jun 01 15:55:54 IST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
17 changes: 14 additions & 3 deletions templates/android/library/build.gradle.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ android {
consumerProguardFiles("consumer-rules.pro")
}

testOptions {
unitTests.includeAndroidResources = true
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -70,11 +74,18 @@ dependencies {
implementation("androidx.activity:activity-ktx:1.6.1")
implementation("androidx.browser:browser:1.4.0")

testImplementation 'junit:junit:4.13.2'
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "io.mockk:mockk:1.13.7"
testImplementation "androidx.test.ext:junit-ktx:1.1.5"
testImplementation "androidx.test:core-ktx:1.5.0"
testImplementation "org.robolectric:robolectric:4.5.1"
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1")
testImplementation "org.robolectric:robolectric:4.10.3"
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")

androidTestImplementation 'androidx.test:runner:1.5.2'
}

tasks.withType(Test).configureEach {
useJUnit()
}

apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"
33 changes: 19 additions & 14 deletions templates/android/library/src/main/java/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resume
import okhttp3.Response as Okhttp3Response

class Client @JvmOverloads constructor(
context: Context,
Expand Down Expand Up @@ -266,7 +267,10 @@ class Client @JvmOverloads constructor(
.get()
.build()

return awaitResponse(request, responseType, converter)
val response = awaitResponse(request, responseType)

@Suppress("UNCHECKED_CAST")
return converter?.invoke(response.data) ?: response.data as T
}

val body = if (MultipartBody.FORM.toString() == headers["content-type"]) {
Expand Down Expand Up @@ -303,7 +307,10 @@ class Client @JvmOverloads constructor(
.method(method, body)
.build()

return awaitResponse(request, responseType, converter)
val response = awaitResponse(request, responseType)

@Suppress("UNCHECKED_CAST")
return converter?.invoke(response.data) ?: response.data as T
}

/**
Expand Down Expand Up @@ -441,11 +448,10 @@ class Client @JvmOverloads constructor(
* @return [T]
*/
@Throws({{ spec.title | caseUcfirst }}Exception::class)
private suspend fun <T> awaitResponse(
internal suspend fun <T> awaitResponse(
request: Request,
responseType: Class<T>,
converter: ((Any) -> T)? = null
) = suspendCancellableCoroutine<T> {
) = suspendCancellableCoroutine<Response<Any>> {
http.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
if (it.isCancelled) {
Expand All @@ -454,8 +460,7 @@ class Client @JvmOverloads constructor(
it.cancel(e)
}

@Suppress("UNCHECKED_CAST")
override fun onResponse(call: Call, response: Response) {
override fun onResponse(call: Call, response: Okhttp3Response) {
if (!response.isSuccessful) {
val body = response.body!!
.charStream()
Expand All @@ -481,19 +486,19 @@ class Client @JvmOverloads constructor(
}
when {
responseType == Boolean::class.java -> {
it.resume(true as T)
it.resume(Response(true))
return
}
responseType == ByteArray::class.java -> {
it.resume(response.body!!
val data = response.body!!
.byteStream()
.buffered()
.use(BufferedInputStream::readBytes) as T
)
.use(BufferedInputStream::readBytes)
it.resume(Response(data))
return
}
response.body == null -> {
it.resume(true as T)
it.resume(Response(true))
return
}
}
Expand All @@ -502,15 +507,15 @@ class Client @JvmOverloads constructor(
.buffered()
.use(BufferedReader::readText)
if (body.isEmpty()) {
it.resume(true as T)
it.resume(Response(true))
return
}
val map = gson.fromJson<Any>(
body,
object : TypeToken<Any>(){}.type
)
it.resume(
converter?.invoke(map) ?: map as T
Response(map)
)
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package {{ sdk.namespace | caseDot }}

import {{ sdk.namespace | caseDot }}.extensions.toJson

class Response<T>(val data: T) {
override fun toString(): String = if (data is Map<*, *>) toJson() else data.toString()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package {{ sdk.namespace | caseDot }}.models

import com.google.gson.annotations.SerializedName
import io.appwrite.extensions.jsonCast
import {{ sdk.namespace | caseDot }}.extensions.jsonCast

/**
* {{ definition.description }}
Expand Down
12 changes: 12 additions & 0 deletions templates/android/library/src/test/java/io/appwrite/IDTest.kt.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package {{ sdk.namespace | caseDot }}

import org.junit.Test
import kotlin.test.assertEquals

class IDTest {
@Test
fun testUnique() = assertEquals("unique()", ID.unique())

@Test
fun testCustom() = assertEquals("custom", ID.custom("custom"))
}
Loading

0 comments on commit 7b6cd0a

Please sign in to comment.