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

More tests #274

Merged
merged 22 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions Sources/Hedera/Schedule/ScheduleCreateTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final class ScheduleCreateTransaction: Transaction {
expirationTime = data.hasExpirationTime ? .fromProtobuf(data.expirationTime) : nil
isWaitForExpiry = data.waitForExpiry
scheduleMemo = data.memo
payerAccountId = data.hasPayerAccountID ? try .fromProtobuf(data.payerAccountID) : nil
adminKey = data.hasAdminKey ? try .fromProtobuf(data.adminKey) : nil

try super.init(protobuf: proto)
}
Expand Down
195 changes: 195 additions & 0 deletions Tests/HederaTests/AccountCreateTransactionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import HederaProtobufs
import SnapshotTesting
import XCTest

@testable import Hedera

internal class AccountCreateTransactionTests: XCTestCase {
private static let testKey = Key.single(Resources.publicKey)
private static let testMaxAutomaticTokenAssociations: UInt32 = 101
private static let testAutoRenewPeriod = Duration.hours(10)
private static let testAutoRenewAccountId: AccountId = 30
private static let testStakedAccountId: AccountId = 3
private static let testStakedNodeId: UInt64 = 4
private static let testAccountMemo = "fresh water"
private static let testInitialBalance = Hbar.fromTinybars(1000)
private static let testMaxTransactionFee = Hbar.fromTinybars(100_000)

private static func makeTransaction() throws -> AccountCreateTransaction {
let evmAddress = try EvmAddress.fromBytes("0x000000000000000000".data(using: .utf8)!)

let tx = try AccountCreateTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.key(testKey)
.initialBalance(testInitialBalance)
.accountMemo(testAccountMemo)
.receiverSignatureRequired(true)
.stakedAccountId(testStakedAccountId)
.autoRenewPeriod(testAutoRenewPeriod)
.autoRenewAccountId(testAutoRenewAccountId)
.alias(evmAddress)
.stakedNodeId(testStakedNodeId)
.maxAutomaticTokenAssociations(testMaxAutomaticTokenAssociations)
.maxTransactionFee(testMaxTransactionFee)
.freeze()
.sign(Resources.privateKey)

return tx
}

private static func makeTransaction2() throws -> AccountCreateTransaction {
try AccountCreateTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.key(testKey)
.initialBalance(testInitialBalance)
.accountMemo(testAccountMemo)
.receiverSignatureRequired(true)
.stakedAccountId(testStakedAccountId)
.autoRenewPeriod(testAutoRenewPeriod)
.autoRenewAccountId(testAutoRenewAccountId)
.stakedNodeId(testStakedNodeId)
.maxAutomaticTokenAssociations(testMaxAutomaticTokenAssociations)
.maxTransactionFee(testMaxTransactionFee)
.freeze()
.sign(Resources.privateKey)
}

internal func testSerialize() throws {
let tx = try Self.makeTransaction().makeProtoBody()

assertSnapshot(matching: tx, as: .description)
}

internal func testToFromBytes() throws {
let tx = try Self.makeTransaction()
let tx2 = try Transaction.fromBytes(tx.toBytes())

XCTAssertEqual(try tx.makeProtoBody(), try tx2.makeProtoBody())
}

internal func testSerialize2() throws {
let tx = try Self.makeTransaction2().makeProtoBody()

assertSnapshot(matching: tx, as: .description)
}

internal func testToFromBytes2() throws {
let tx = try Self.makeTransaction2()
let tx2 = try Transaction.fromBytes(tx.toBytes())

XCTAssertEqual(try tx.makeProtoBody(), try tx2.makeProtoBody())
}

internal func testProperties() throws {
let tx = try Self.makeTransaction()

XCTAssertEqual(tx.key, Self.testKey)
XCTAssertEqual(tx.initialBalance, Self.testInitialBalance)
XCTAssertEqual(tx.receiverSignatureRequired, true)
XCTAssertEqual(tx.autoRenewPeriod, Self.testAutoRenewPeriod)
XCTAssertEqual(tx.maxAutomaticTokenAssociations, Self.testMaxAutomaticTokenAssociations)
XCTAssertEqual(tx.accountMemo, Self.testAccountMemo)
XCTAssertEqual(tx.stakedAccountId, Self.testStakedAccountId)
XCTAssertEqual(tx.stakedNodeId, Self.testStakedNodeId)
XCTAssertEqual(tx.declineStakingReward, false)
XCTAssertEqual(tx.alias, try EvmAddress.fromBytes("0x000000000000000000".data(using: .utf8)!))
}

internal func testFromProtoBody() throws {
let protoData = Proto_CryptoCreateTransactionBody.with { proto in
proto.alias = "0x000000000000000000".data(using: .utf8)!
proto.autoRenewPeriod = Self.testAutoRenewPeriod.toProtobuf()
proto.initialBalance = 1000
proto.memo = Self.testAccountMemo
proto.key = Self.testKey.toProtobuf()
proto.stakedNodeID = Int64(Self.testStakedNodeId)
proto.stakedAccountID = Self.testStakedAccountId.toProtobuf()
proto.maxAutomaticTokenAssociations = Int32(Self.testMaxAutomaticTokenAssociations)
}

let protoBody = Proto_TransactionBody.with { proto in
proto.cryptoCreateAccount = protoData
proto.transactionID = Resources.txId.toProtobuf()
}

let tx = try AccountCreateTransaction(protobuf: protoBody, protoData)

XCTAssertEqual(tx.alias, try EvmAddress.fromBytes("0x000000000000000000".data(using: .utf8)!))
XCTAssertEqual(tx.accountMemo, Self.testAccountMemo)
XCTAssertEqual(tx.initialBalance, Self.testInitialBalance)
XCTAssertEqual(tx.stakedAccountId, Self.testStakedAccountId)
XCTAssertEqual(tx.autoRenewPeriod, Self.testAutoRenewPeriod)
XCTAssertEqual(tx.maxAutomaticTokenAssociations, Self.testMaxAutomaticTokenAssociations)
}

internal func testGetSetKey() throws {
let tx = AccountCreateTransaction()
tx.key(Self.testKey)

XCTAssertEqual(tx.key, Self.testKey)
}

internal func testGetSetInitialBalance() throws {
let tx = AccountCreateTransaction()
tx.initialBalance(Self.testInitialBalance)

XCTAssertEqual(tx.initialBalance, Self.testInitialBalance)
}

internal func testGetSetAutoRenewPeriod() throws {
let tx = AccountCreateTransaction()
tx.autoRenewPeriod(Self.testAutoRenewPeriod)

XCTAssertEqual(tx.autoRenewPeriod, Self.testAutoRenewPeriod)
}

internal func testGetSetAutoRenewAccountId() throws {
let tx = AccountCreateTransaction()
tx.autoRenewAccountId(Self.testAutoRenewAccountId)

XCTAssertEqual(tx.autoRenewAccountId, Self.testAutoRenewAccountId)
}

internal func testGetSetAccountMemo() throws {
let tx = AccountCreateTransaction()
tx.accountMemo(Self.testAccountMemo)

XCTAssertEqual(tx.accountMemo, Self.testAccountMemo)
}

internal func testGetSetAlias() throws {
let tx = AccountCreateTransaction()
tx.alias(try EvmAddress.fromBytes("0x000000000000000000".data(using: .utf8)!))

XCTAssertEqual(tx.alias, try EvmAddress.fromBytes("0x000000000000000000".data(using: .utf8)!))
}

internal func testGetSetStakedAccountId() throws {
let tx = AccountCreateTransaction()
tx.stakedAccountId(Self.testStakedAccountId)

XCTAssertEqual(tx.stakedAccountId, Self.testStakedAccountId)
}
}
86 changes: 86 additions & 0 deletions Tests/HederaTests/AccountDeleteTransactionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import HederaProtobufs
import SnapshotTesting
import XCTest

@testable import Hedera

internal class AccountDeleteTransactionTests: XCTestCase {
private static let testTransferAccountId = AccountId("0.0.5007")
private static let testAccountId = Resources.accountId
private static let testMaxTransactionFee = Hbar.fromTinybars(100_000)

private static func makeTransaction() throws -> AccountDeleteTransaction {
try AccountDeleteTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.transferAccountId(testTransferAccountId)
.accountId(testAccountId)
.maxTransactionFee(testMaxTransactionFee)
.freeze()
.sign(Resources.privateKey)
}

internal func testSerialize() throws {
let tx = try Self.makeTransaction().makeProtoBody()

assertSnapshot(matching: tx, as: .description)
}

internal func testToFromBytes() throws {
let tx = try Self.makeTransaction()
let tx2 = try Transaction.fromBytes(tx.toBytes())

XCTAssertEqual(try tx.makeProtoBody(), try tx2.makeProtoBody())
}

internal func testFromProtoBody() throws {
let protoData = Proto_CryptoDeleteTransactionBody.with { proto in
proto.deleteAccountID = Self.testAccountId.toProtobuf()
proto.transferAccountID = Self.testTransferAccountId.toProtobuf()
}

let protoBody = Proto_TransactionBody.with { proto in
proto.cryptoDelete = protoData
proto.transactionID = Resources.txId.toProtobuf()
}

let tx = try AccountDeleteTransaction(protobuf: protoBody, protoData)

XCTAssertEqual(tx.accountId, Self.testAccountId)
XCTAssertEqual(tx.transferAccountId, Self.testTransferAccountId)
}

internal func testGetSetAccountId() throws {
let tx = AccountDeleteTransaction()
tx.accountId(Self.testAccountId)

XCTAssertEqual(tx.accountId, Self.testAccountId)
}

internal func testGetSetTransferAccountId() throws {
let tx = AccountDeleteTransaction()
tx.transferAccountId(Self.testTransferAccountId)

XCTAssertEqual(tx.transferAccountId, Self.testTransferAccountId)
}
}
3 changes: 0 additions & 3 deletions Tests/HederaTests/ContractInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import struct HederaProtobufs.Proto_Key
@testable import Hedera

internal final class ContractInfoTests: XCTestCase {
internal static let unusedPrivateKey: PrivateKey =
"302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10"

private static let info: Proto_ContractGetInfoResponse.ContractInfo = .with { proto in
proto.contractID = ContractId(num: 1).toProtobuf()
proto.accountID = AccountId(num: 5006).toProtobuf()
Expand Down
78 changes: 78 additions & 0 deletions Tests/HederaTests/FeeSchedulesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import SnapshotTesting
import XCTest

@testable import Hedera

internal final class FeeSchedulesTests: XCTestCase {
private static func makeFeeComponent(_ min: UInt64, _ max: UInt64) -> FeeComponents {
FeeComponents(
min: min, max: max, constant: 0, bandwidthByte: 0, verification: 0, storageByteHour: 0, ramByteHour: 0,
contractTransactionGas: 0, transferVolumeHbar: 0, responseMemoryByte: 0, responseDiskByte: 0)
}

private let feeSchedules: FeeSchedules = FeeSchedules(
current: FeeSchedule.init(
transactionFeeSchedules: [
TransactionFeeSchedule(
requestType: nil,
fees: [
FeeData.init(
node: makeFeeComponent(0, 0), network: makeFeeComponent(2, 5),
service: makeFeeComponent(0, 0), kind: FeeDataType.default)
])
], expirationTime: Timestamp(seconds: 1_554_158_542, subSecondNanos: 0)),
next: FeeSchedule.init(
transactionFeeSchedules: [
TransactionFeeSchedule(
requestType: nil,
fees: [
FeeData.init(
node: makeFeeComponent(1, 2), network: makeFeeComponent(0, 0),
service: makeFeeComponent(0, 0), kind: FeeDataType.default)
])
], expirationTime: Timestamp(seconds: 1_554_158_542, subSecondNanos: 0))
)

internal func testSerialize() throws {
assertSnapshot(matching: feeSchedules, as: .description)
}

internal func testToFromBytes() throws {
assertSnapshot(matching: try FeeSchedules.fromBytes(feeSchedules.toBytes()), as: .description)
}

internal func testSerializeDefault() throws {
let schedules = FeeSchedules(current: nil, next: nil)

assertSnapshot(matching: schedules, as: .description)
}

internal func testToFromBytesDefault() throws {
let schedules = FeeSchedules(current: nil, next: nil)

let bytesSchedule = try FeeSchedules.fromBytes(schedules.toBytes())

assertSnapshot(matching: bytesSchedule, as: .description)

}
}
7 changes: 7 additions & 0 deletions Tests/HederaTests/FileContentsQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ internal final class FileContentsQueryTests: XCTestCase {

assertSnapshot(matching: query, as: .description)
}

internal func testGetSetFileId() throws {
let query = FileContentsQuery()
query.fileId(Resources.fileId)

XCTAssertEqual(query.fileId, Resources.fileId)
}
}
Loading