1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-11-28 10:30:46 +00:00

migration

This commit is contained in:
2019-12-03 11:07:25 +06:00
parent 24d11d2c2b
commit f3fc0769ef
449 changed files with 265 additions and 178 deletions

View File

@@ -0,0 +1,60 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.types.actions.*
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
@ImplicitReflectionSerializer
class BotActionTests {
@Serializable
data class Example(
val botAction: BotAction
)
private fun checkBotAction(example: Example, sourceAction: BotAction) {
assertEquals(
sourceAction.actionName,
when (example.botAction) {
TypingAction -> example.botAction.actionName
UploadPhotoAction -> example.botAction.actionName
RecordVideoAction -> example.botAction.actionName
UploadVideoAction -> example.botAction.actionName
RecordAudioAction -> example.botAction.actionName
UploadAudioAction -> example.botAction.actionName
UploadDocumentAction -> example.botAction.actionName
FindLocationAction -> example.botAction.actionName
}
)
}
private fun checkBotActionSerializeDeserialize(example: Example) {
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"botAction\":\"${example.botAction.actionName}\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
checkBotAction(deserialized, example.botAction)
}
@Test
fun `BotAction correctly serialized and deserialized`() {
fun BotAction.example() = Example(this)
listOf(
TypingAction.example(),
UploadPhotoAction.example(),
RecordVideoAction.example(),
UploadVideoAction.example(),
RecordAudioAction.example(),
UploadAudioAction.example(),
UploadDocumentAction.example(),
FindLocationAction.example()
).forEach {
checkBotActionSerializeDeserialize(it)
}
}
}

View File

@@ -0,0 +1,79 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.*
@ImplicitReflectionSerializer
private const val chatIdentifierChatId: Identifier = 123L
@ImplicitReflectionSerializer
private const val chatIdentifierLink = "tg://user?id=$chatIdentifierChatId"
@ImplicitReflectionSerializer
private const val testUsername = "@Example"
@ImplicitReflectionSerializer
class ChatIdentifierTests {
@Test
fun `Cast from Int to ChatId is working correctly`() {
val chatId = chatIdentifierChatId.toInt().toChatId()
assertEquals(chatIdentifierChatId, chatId.chatId)
}
@Test
fun `Cast from Byte to ChatId is working correctly`() {
val chatId = chatIdentifierChatId.toByte().toChatId()
assertEquals(chatIdentifierChatId, chatId.chatId)
}
@Test
fun `Cast from Identifier to ChatId is working correctly`() {
val chatId = chatIdentifierChatId.toChatId()
assertEquals(chatIdentifierChatId, chatId.chatId)
}
@Test
fun `Creating link from ChatId is correct`() {
val chatId = chatIdentifierChatId.toChatId()
assertEquals(chatIdentifierLink, chatId.link)
}
@Test
fun `Cast from String to Username is working correctly`() {
assertEquals(testUsername, testUsername.toUsername().username)
assertFails("Username creating must fail when trying to create from string which is not starting from @ symbol") {
testUsername.replace("@", "").toUsername().username
}
}
@Test
fun `Deserializing from String must work correctly`() {
@Serializable
data class Example(
val identifier: ChatIdentifier
)
Example(chatIdentifierChatId.toChatId()).let { withChatId ->
val stringified = Json.plain.stringify(Example.serializer(), withChatId)
assertEquals(stringified, "{\"identifier\":$chatIdentifierChatId}")
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(withChatId, deserialized)
}
Example(testUsername.toUsername()).let { withUsername ->
val stringified = Json.plain.stringify(Example.serializer(), withUsername)
assertEquals(stringified, "{\"identifier\":\"$testUsername\"}")
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(withUsername, deserialized)
}
// Replace @ by empty string, because from time to time we can retrieve from Telegram system
// username without starting @ symbol
Example(testUsername.toUsername()).let { withUsername ->
val stringified = Json.plain.stringify(Example.serializer(), withUsername).replace("@", "")
assertEquals("{\"identifier\":\"${testUsername.replace("@", "")}\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(withUsername, deserialized)
}
}
}

View File

@@ -0,0 +1,38 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.*
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
@ImplicitReflectionSerializer
class ParseModeTests {
@Serializable
data class Example(
val mode: ParseMode
)
@Test
fun `Markdown parse mode correctly serializing and deserializing`() {
val example = Example(Markdown)
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"mode\":\"Markdown\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
}
@Test
fun `HTML parse mode correctly serializing and deserializing`() {
val example = Example(HTML)
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"mode\":\"HTML\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
}
}

View File

@@ -0,0 +1,33 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.soywiz.klock.DateTime
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertEquals
private val dateTimeMillis = System.currentTimeMillis()
private val dateTimeUnix = TimeUnit.MILLISECONDS.toSeconds(dateTimeMillis)
private val dateTime = DateTime(TimeUnit.SECONDS.toMillis(dateTimeUnix))
@ImplicitReflectionSerializer
class TelegramDateTests {
@Serializable
data class Example(
val dateTime: TelegramDate
)
@Test
fun `Serializtion of TelegramDate is working correctly`() {
val example = Example(TelegramDate(dateTimeUnix))
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"dateTime\":$dateTimeUnix}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
assertEquals(dateTime, deserialized.dateTime.asDate)
}
}