Skip to content
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

load test resources #65

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
13 changes: 13 additions & 0 deletions data/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ kotlin {
implementation(libs.org.jetbrains.kotlin.test.junit)
}
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(getByName("commonTest"))

iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
dependencies {
implementation(libs.org.jetbrains.kotlinx.coroutines.test)
}
}
named("commonTest") {
dependencies {
implementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This file is part of Dodo.
*
* Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Dodo. If not, see <https://www.gnu.org/licenses/>.
*/
package social.androiddev.common

import java.io.File

actual fun readBinaryResource(resourcePath: String): ByteArray {
return File(resourcePath).readBytes()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.ktor.client.request.post
import io.ktor.http.HttpHeaders
import io.ktor.http.URLProtocol
import io.ktor.http.path
import io.ktor.serialization.JsonConvertException
import kotlinx.serialization.SerializationException
import social.androiddev.common.network.model.Application
import social.androiddev.common.network.model.Instance
Expand Down Expand Up @@ -76,6 +77,8 @@ internal class MastodonApiKtor(
)
} catch (exception: SerializationException) {
Result.failure(exception = exception)
} catch (exception: JsonConvertException) {
Result.failure(exception = exception)
} catch (exception: ResponseException) {
Result.failure(exception = exception)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.serialization.Serializable
*/
@Serializable
data class Application(
val id: String,
val name: String,
@SerialName("vapid_key") val vapidKey: String,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* This file is part of Dodo.
*
* Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Dodo. If not, see <https://www.gnu.org/licenses/>.
*/
package social.androiddev.common

/**
* the resourcePath is based on the module
*
* e.g.: "src/commonTest/resources/resource_name.json"
*/
expect fun readBinaryResource(resourcePath: String): ByteArray
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ package social.androiddev.common.network.model

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import social.androiddev.common.readBinaryResource
import kotlin.test.Test
import kotlin.test.assertEquals

class AccountTests {
// TODO: fix loading json from resources
@Ignore
@Test
fun `deserialize required fields should succeed`() {
// given
// val json: String = javaClass.classLoader.getResource("response_account_required.json").readText()
val json: String = ""
val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_account_required.json")
val json: String = byteArray.decodeToString()

// when
val account = Json.decodeFromString<Account>(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ package social.androiddev.common.network.model

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import social.androiddev.common.readBinaryResource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class ContextTests {
// TODO: fix loading json from resources
@Ignore
@Test
fun `deserialize required fields should succeed`() {
// given
// val json: String = javaClass.classLoader.getResource("response_context_required.json").readText()
val json: String = ""
val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_context_required.json")
val json: String = byteArray.decodeToString()

// when
val context = Json.decodeFromString<Context>(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ package social.androiddev.common.network.model

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import social.androiddev.common.readBinaryResource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class ConversationTests {
// TODO: fix loading json from resources
@Ignore
@Test
fun `deserialize required fields should succeed`() {
// given
// val json: String = javaClass.classLoader.getResource("response_conversation_required.json").readText()
val json: String = ""
val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_conversation_required.json")
val json: String = byteArray.decodeToString()

// when
val conversation = Json.decodeFromString<Conversation>(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ package social.androiddev.common.network.model

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import social.androiddev.common.readBinaryResource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class NotificationTests {
// TODO: fix loading json from resources
@Ignore
@Test
fun `deserialize required fields should succeed`() {
// given
// val json: String = javaClass.classLoader.getResource("response_notification_required.json").readText()
val json: String = ""
val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_notification_required.json")
val json: String = byteArray.decodeToString()

// when
val notification = Json.decodeFromString<Notification>(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ package social.androiddev.common.network.model

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlin.test.Ignore
import social.androiddev.common.readBinaryResource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class StatusTests {
// TODO: fix loading json from resources
@Ignore
@Test
fun `deserialize required fields should succeed`() {
// given
// val json: String = javaClass.classLoader.getResource("response_status_required.json").readText()
val json: String = ""
val byteArray: ByteArray = readBinaryResource("src/commonTest/resources/response_status_required.json")
val json: String = byteArray.decodeToString()

// when
val status = Json.decodeFromString<Status>(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"application":
{
"name": "Web",
"website": null
"website": null,
"vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M="
},
"account":
{
Expand Down Expand Up @@ -114,7 +115,8 @@
"application":
{
"name": "Web",
"website": null
"website": null,
"vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M="
},
"account":
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"application":
{
"name": "Web",
"website": null
"website": null,
"vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M="
},
"account":
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
"application":
{
"name": "Web",
"website": null
"website": null,
"vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M="
},
"account":
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"application":
{
"name": "Web",
"website": null
"website": null,
"vapid_key": "BCk-QqERU0q-CfYZjcuB6lnyyOYfJ2AifKqfeGIm7Z-HiTU5T9eTG5GxVA0_OH5mMlI4UkkDTpaZwozy0TzdZ2M="
},
"account":
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This file is part of Dodo.
*
* Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Dodo. If not, see <https://www.gnu.org/licenses/>.
*/
package social.androiddev.common

import java.io.File

actual fun readBinaryResource(resourcePath: String): ByteArray {
return File(resourcePath).readBytes()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of Dodo.
*
* Dodo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* Dodo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Dodo. If not, see <https://www.gnu.org/licenses/>.
*/
package social.androiddev.common

import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import platform.Foundation.NSBundle
import platform.Foundation.NSData
import platform.Foundation.NSUTF8StringEncoding
import platform.Foundation.dataWithContentsOfFile
import platform.posix.memcpy

actual fun readBinaryResource(resourcePath: String): ByteArray {
val path = resourcePath.substringBeforeLast(".")
val fileType = resourcePath.substringAfterLast(".")
println("resourcePath=$resourcePath, path=$path, fileType=$fileType")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's just test, probably can remove debug print statements in this file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it for debugging since it is not due to a flaky CI. It is currently not working for iOS / native.
I read there is another gradle task needed to copy the assets into the build directory.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the copy task from the article or a different one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I haven't added it already.


val absolutePath = NSBundle.mainBundle.pathForResource(path, fileType)
println("absolutePath=$absolutePath")

return NSData.dataWithContentsOfFile(absolutePath!!, NSUTF8StringEncoding, null)!!.toByteArray()
}

internal fun NSData.toByteArray(): ByteArray {
return ByteArray(length.toInt()).apply {
usePinned {
memcpy(it.addressOf(0), bytes, length)
}
}
}