Skip to content

Commit 7b6cd0a

Browse files
committed
feat: android unit tests
1 parent 2069238 commit 7b6cd0a

File tree

16 files changed

+501
-47
lines changed

16 files changed

+501
-47
lines changed

composer.lock

+26-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SDK/Language/Android.php

+20
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public function getFiles(): array
175175
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/KeepAliveService.kt',
176176
'template' => '/android/library/src/main/java/io/appwrite/KeepAliveService.kt.twig',
177177
],
178+
[
179+
'scope' => 'default',
180+
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/Response.kt',
181+
'template' => '/android/library/src/main/java/io/appwrite/Response.kt.twig',
182+
],
178183
[
179184
'scope' => 'default',
180185
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/views/CallbackActivity.kt',
@@ -225,6 +230,16 @@ public function getFiles(): array
225230
'destination' => '/library/src/main/AndroidManifest.xml',
226231
'template' => '/android/library/src/main/AndroidManifest.xml.twig',
227232
],
233+
[
234+
'scope' => 'service',
235+
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/services/{{ service.name | caseUcfirst }}ServiceTest.kt',
236+
'template' => '/android/library/src/test/java/io/appwrite/services/ServiceTest.kt.twig',
237+
],
238+
[
239+
'scope' => 'default',
240+
'destination' => '/library/src/test/java/{{ sdk.namespace | caseSlash }}/cookies/CookiesTest.kt',
241+
'template' => '/android/library/src/test/java/io/appwrite/cookies/CookiesTest.kt.twig',
242+
],
228243
[
229244
'scope' => 'default',
230245
'destination' => '/library/build.gradle',
@@ -387,6 +402,11 @@ public function getFiles(): array
387402
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
388403
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
389404
],
405+
[
406+
'scope' => 'definition',
407+
'destination' => 'library/src/test/java/io/appwrite/models/{{ definition.name | caseUcfirst }}Test.kt',
408+
'template' => '/android/library/src/test/java/io/appwrite/models/ModelTest.kt.twig',
409+
],
390410
];
391411
}
392412
}

templates/android/build.gradle.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
mavenCentral()
1111
}
1212
dependencies {
13-
classpath "com.android.tools.build:gradle:4.2.2"
13+
classpath "com.android.tools.build:gradle:7.4.2"
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1515
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
1616

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Tue Jun 01 15:55:54 IST 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

templates/android/library/build.gradle.twig

+14-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ android {
3434
consumerProguardFiles("consumer-rules.pro")
3535
}
3636

37+
testOptions {
38+
unitTests.includeAndroidResources = true
39+
}
40+
3741
buildTypes {
3842
release {
3943
minifyEnabled false
@@ -70,11 +74,18 @@ dependencies {
7074
implementation("androidx.activity:activity-ktx:1.6.1")
7175
implementation("androidx.browser:browser:1.4.0")
7276

73-
testImplementation 'junit:junit:4.13.2'
77+
testImplementation "org.jetbrains.kotlin:kotlin-test"
78+
testImplementation "io.mockk:mockk:1.13.7"
7479
testImplementation "androidx.test.ext:junit-ktx:1.1.5"
7580
testImplementation "androidx.test:core-ktx:1.5.0"
76-
testImplementation "org.robolectric:robolectric:4.5.1"
77-
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1")
81+
testImplementation "org.robolectric:robolectric:4.10.3"
82+
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
83+
84+
androidTestImplementation 'androidx.test:runner:1.5.2'
85+
}
86+
87+
tasks.withType(Test).configureEach {
88+
useJUnit()
7889
}
7990

8091
apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"

templates/android/library/src/main/java/io/appwrite/Client.kt.twig

+19-14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import javax.net.ssl.TrustManager
3737
import javax.net.ssl.X509TrustManager
3838
import kotlin.coroutines.CoroutineContext
3939
import kotlin.coroutines.resume
40+
import okhttp3.Response as Okhttp3Response
4041

4142
class Client @JvmOverloads constructor(
4243
context: Context,
@@ -266,7 +267,10 @@ class Client @JvmOverloads constructor(
266267
.get()
267268
.build()
268269

269-
return awaitResponse(request, responseType, converter)
270+
val response = awaitResponse(request, responseType)
271+
272+
@Suppress("UNCHECKED_CAST")
273+
return converter?.invoke(response.data) ?: response.data as T
270274
}
271275

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

306-
return awaitResponse(request, responseType, converter)
310+
val response = awaitResponse(request, responseType)
311+
312+
@Suppress("UNCHECKED_CAST")
313+
return converter?.invoke(response.data) ?: response.data as T
307314
}
308315

309316
/**
@@ -441,11 +448,10 @@ class Client @JvmOverloads constructor(
441448
* @return [T]
442449
*/
443450
@Throws({{ spec.title | caseUcfirst }}Exception::class)
444-
private suspend fun <T> awaitResponse(
451+
internal suspend fun <T> awaitResponse(
445452
request: Request,
446453
responseType: Class<T>,
447-
converter: ((Any) -> T)? = null
448-
) = suspendCancellableCoroutine<T> {
454+
) = suspendCancellableCoroutine<Response<Any>> {
449455
http.newCall(request).enqueue(object : Callback {
450456
override fun onFailure(call: Call, e: IOException) {
451457
if (it.isCancelled) {
@@ -454,8 +460,7 @@ class Client @JvmOverloads constructor(
454460
it.cancel(e)
455461
}
456462

457-
@Suppress("UNCHECKED_CAST")
458-
override fun onResponse(call: Call, response: Response) {
463+
override fun onResponse(call: Call, response: Okhttp3Response) {
459464
if (!response.isSuccessful) {
460465
val body = response.body!!
461466
.charStream()
@@ -481,19 +486,19 @@ class Client @JvmOverloads constructor(
481486
}
482487
when {
483488
responseType == Boolean::class.java -> {
484-
it.resume(true as T)
489+
it.resume(Response(true))
485490
return
486491
}
487492
responseType == ByteArray::class.java -> {
488-
it.resume(response.body!!
493+
val data = response.body!!
489494
.byteStream()
490495
.buffered()
491-
.use(BufferedInputStream::readBytes) as T
492-
)
496+
.use(BufferedInputStream::readBytes)
497+
it.resume(Response(data))
493498
return
494499
}
495500
response.body == null -> {
496-
it.resume(true as T)
501+
it.resume(Response(true))
497502
return
498503
}
499504
}
@@ -502,15 +507,15 @@ class Client @JvmOverloads constructor(
502507
.buffered()
503508
.use(BufferedReader::readText)
504509
if (body.isEmpty()) {
505-
it.resume(true as T)
510+
it.resume(Response(true))
506511
return
507512
}
508513
val map = gson.fromJson<Any>(
509514
body,
510515
object : TypeToken<Any>(){}.type
511516
)
512517
it.resume(
513-
converter?.invoke(map) ?: map as T
518+
Response(map)
514519
)
515520
}
516521
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package {{ sdk.namespace | caseDot }}
2+
3+
import {{ sdk.namespace | caseDot }}.extensions.toJson
4+
5+
class Response<T>(val data: T) {
6+
override fun toString(): String = if (data is Map<*, *>) toJson() else data.toString()
7+
}

templates/android/library/src/main/java/io/appwrite/models/Model.kt.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package {{ sdk.namespace | caseDot }}.models
22

33
import com.google.gson.annotations.SerializedName
4-
import io.appwrite.extensions.jsonCast
4+
import {{ sdk.namespace | caseDot }}.extensions.jsonCast
55

66
/**
77
* {{ definition.description }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package {{ sdk.namespace | caseDot }}
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
class IDTest {
7+
@Test
8+
fun testUnique() = assertEquals("unique()", ID.unique())
9+
10+
@Test
11+
fun testCustom() = assertEquals("custom", ID.custom("custom"))
12+
}

0 commit comments

Comments
 (0)