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

Unit tests: FlowArgument, FlowBlock, FlowCode #22

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 10 additions & 20 deletions src/main/kotlin/com/nftco/flow/sdk/models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,7 @@ data class FlowAddress private constructor(override val bytes: ByteArray) : Seri
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowAddress
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

val formatted: String = "0x$base16Value"
Expand All @@ -786,8 +785,7 @@ data class FlowArgument(override val bytes: ByteArray) : Serializable, BytesHold
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowArgument
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -801,8 +799,7 @@ data class FlowScript(override val bytes: ByteArray) : Serializable, BytesHolder
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowScript
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -826,8 +823,7 @@ data class FlowScriptResponse(override val bytes: ByteArray) : Serializable, Byt
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowScriptResponse
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -847,8 +843,7 @@ data class FlowSignature(override val bytes: ByteArray) : Serializable, BytesHol
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowSignature
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -866,8 +861,7 @@ data class FlowId private constructor(override val bytes: ByteArray) : Serializa
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowId
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -880,8 +874,7 @@ data class FlowCode(override val bytes: ByteArray) : Serializable, BytesHolder {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowCode
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -895,8 +888,7 @@ data class FlowPublicKey(override val bytes: ByteArray) : Serializable, BytesHol
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowPublicKey
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -909,8 +901,7 @@ data class FlowSnapshot(override val bytes: ByteArray) : Serializable, BytesHold
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowSnapshot
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand All @@ -934,8 +925,7 @@ data class FlowEventPayload(override val bytes: ByteArray) : Serializable, Bytes
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FlowEventPayload
if (!bytes.contentEquals(other.bytes)) return false
return true
return bytes.contentEquals(other.bytes)
}

override fun hashCode(): Int {
Expand Down
2 changes: 0 additions & 2 deletions src/test/kotlin/com/nftco/flow/sdk/models/FlowAddressTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.nftco.flow.sdk.models
import com.nftco.flow.sdk.FlowAddress
import com.nftco.flow.sdk.hexToBytes
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Test

class FlowAddressTest {
Expand Down
55 changes: 55 additions & 0 deletions src/test/kotlin/com/nftco/flow/sdk/models/FlowArgumentTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.nftco.flow.sdk.models

import com.nftco.flow.sdk.Flow
import com.nftco.flow.sdk.FlowArgument
import com.nftco.flow.sdk.cadence.Field
import com.nftco.flow.sdk.cadence.StringField
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Test

class FlowArgumentTest {
@Test
fun `Test initialization from byte array`() {
val bytes = byteArrayOf(0x01, 0x02, 0x03)
val flowArgument = FlowArgument(bytes)
assertEquals(bytes, flowArgument.bytes)
}

@Test
fun `Test initialization from JSON Cadence`() {
val jsonCadence = StringField("test")
val flowArgument = FlowArgument(jsonCadence)

val decodedCadence: Field<Any> = Flow.decodeJsonCadence(flowArgument.bytes)

assertEquals(jsonCadence, decodedCadence)
assertEquals(jsonCadence.decodeToAny(), decodedCadence.decodeToAny())
}

@Test
fun `Test equals`() {
val bytes1 = byteArrayOf(0x01, 0x02, 0x03)
val bytes2 = byteArrayOf(0x04, 0x05, 0x06)

val flowArgument1 = FlowArgument(bytes1)
val flowArgument2 = FlowArgument(bytes1)
val flowArgument3 = FlowArgument(bytes2)

assertEquals(flowArgument1, flowArgument2)
assertNotEquals(flowArgument1, flowArgument3)
}

@Test
fun `Test hashCode`() {
val bytes1 = byteArrayOf(0x01, 0x02, 0x03)
val bytes2 = byteArrayOf(0x04, 0x05, 0x06)

val flowArgument1 = FlowArgument(bytes1)
val flowArgument2 = FlowArgument(bytes1)
val flowArgument3 = FlowArgument(bytes2)

assertEquals(flowArgument1.hashCode(), flowArgument2.hashCode())
assertNotEquals(flowArgument1.hashCode(), flowArgument3.hashCode())
}
}
74 changes: 74 additions & 0 deletions src/test/kotlin/com/nftco/flow/sdk/models/FlowBlockTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.nftco.flow.sdk.models

import com.google.protobuf.ByteString
import com.google.protobuf.Timestamp
import com.nftco.flow.sdk.FlowBlock
import com.nftco.flow.sdk.FlowId
import com.nftco.flow.sdk.asTimestamp
import com.nftco.flow.sdk.fixedSize
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.onflow.protobuf.entities.BlockOuterClass
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset

class FlowBlockTest {
@Test
fun `Test initialization from BlockOuterClass Block`() {
val unixTimestamp = 123456789L
val timestamp = Timestamp.newBuilder().setSeconds(unixTimestamp).build()

val blockBuilder = BlockOuterClass.Block.newBuilder()
.setId(ByteString.copyFromUtf8("id"))
.setParentId(ByteString.copyFromUtf8("parent_id"))
.setHeight(123)
.setTimestamp(timestamp)

val flowBlock = FlowBlock.of(blockBuilder.build())

val expectedUtcDateTime = LocalDateTime.ofInstant(
Instant.ofEpochSecond(timestamp.seconds),
ZoneOffset.UTC
)

val actualUtcDateTime = LocalDateTime.ofInstant(
Instant.ofEpochSecond(123456789L),
ZoneOffset.UTC
)

assert(flowBlock.id.bytes.contentEquals(fixedSize("id".toByteArray(), 32)))
assert(flowBlock.parentId.bytes.contentEquals(fixedSize("parent_id".toByteArray(), 32)))
assertEquals(flowBlock.height, 123L)
assertEquals(
expectedUtcDateTime,
actualUtcDateTime,
)
assert(flowBlock.collectionGuarantees.isEmpty())
assert(flowBlock.blockSeals.isEmpty())
assert(flowBlock.signatures.isEmpty())
}

@Test
fun `Test builder`() {
val flowBlock = FlowBlock(
id = FlowId.of("id".toByteArray()),
parentId = FlowId.of("parent_id".toByteArray()),
height = 123,
timestamp = LocalDateTime.now(),
collectionGuarantees = emptyList(),
blockSeals = emptyList(),
signatures = emptyList()
)

val blockBuilder = flowBlock.builder()

assert(blockBuilder.id.toByteArray().contentEquals(fixedSize("id".toByteArray(), 32)))
assert(blockBuilder.parentId.toByteArray().contentEquals(fixedSize("parent_id".toByteArray(), 32)))
assert(blockBuilder.height == 123L)
assert(blockBuilder.timestamp == flowBlock.timestamp.asTimestamp())
assert(blockBuilder.collectionGuaranteesList.isEmpty())
assert(blockBuilder.blockSealsList.isEmpty())
assert(blockBuilder.signaturesList.isEmpty())
}
}
34 changes: 34 additions & 0 deletions src/test/kotlin/com/nftco/flow/sdk/models/FlowCodeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.nftco.flow.sdk.models

import com.nftco.flow.sdk.FlowCode
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class FlowCodeTest {
@Test
fun `Test equality`() {
val bytes1 = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7)
val bytes2 = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7)
val bytes3 = byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8)

val flowCode1 = FlowCode(bytes1)
val flowCode2 = FlowCode(bytes2)
val flowCode3 = FlowCode(bytes3)

assertEquals(flowCode1, flowCode2)
assertEquals(flowCode2, flowCode1)

assertEquals(false, flowCode1 == flowCode3)
assertEquals(false, flowCode3 == flowCode1)
}

@Test
fun `Test hashCode`() {
val bytes = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7)
val flowCode = FlowCode(bytes)

val expectedHashCode = bytes.contentHashCode()

assertEquals(expectedHashCode, flowCode.hashCode())
}
}
3 changes: 1 addition & 2 deletions src/test/kotlin/com/nftco/flow/sdk/models/FlowIdTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.nftco.flow.sdk.models

import com.nftco.flow.sdk.FlowId
import com.nftco.flow.sdk.fixedSize
import com.nftco.flow.sdk.hexToBytes
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import com.nftco.flow.sdk.fixedSize
import org.junit.jupiter.api.Test

class FlowIdTest {
Expand Down
Loading