|
| 1 | +/* |
| 2 | + * |
| 3 | + * Hedera Swift SDK |
| 4 | + * |
| 5 | + * Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +import HederaProtobufs |
| 22 | +import SnapshotTesting |
| 23 | +import XCTest |
| 24 | + |
| 25 | +@testable import Hedera |
| 26 | + |
| 27 | +internal final class FileAppendTransactionTests: XCTestCase { |
| 28 | + private static let fileId = FileId("0.0.10") |
| 29 | + private static let contents = "{foo: 231}".data(using: .utf8)! |
| 30 | + |
| 31 | + private static func makeTransaction() throws -> FileAppendTransaction { |
| 32 | + try FileAppendTransaction() |
| 33 | + .nodeAccountIds(Resources.nodeAccountIds) |
| 34 | + .transactionId(Resources.txId) |
| 35 | + .maxTransactionFee(Hbar(2)) |
| 36 | + .sign(Resources.privateKey) |
| 37 | + .fileId(fileId) |
| 38 | + .contents(contents) |
| 39 | + .freeze() |
| 40 | + } |
| 41 | + |
| 42 | + internal func testSerialize() throws { |
| 43 | + let tx = try Self.makeTransaction() |
| 44 | + |
| 45 | + // Unlike most transactions, this iteration makes sure the chunked data is properly handled. |
| 46 | + // NOTE: Without a client, dealing with chunked data is cumbersome. |
| 47 | + let bodyBytes = try tx.makeSources().signedTransactions.makeIterator().map { signed in |
| 48 | + try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes) |
| 49 | + } |
| 50 | + |
| 51 | + let txes = try bodyBytes.makeIterator().map { bytes in |
| 52 | + try Resources.checkTransactionBody(body: bytes) |
| 53 | + } |
| 54 | + |
| 55 | + assertSnapshot(of: txes, as: .description) |
| 56 | + } |
| 57 | + |
| 58 | + internal func testToFromBytes() throws { |
| 59 | + let tx = try Self.makeTransaction() |
| 60 | + |
| 61 | + let tx2 = try Transaction.fromBytes(try tx.toBytes()) |
| 62 | + |
| 63 | + // As stated above, this assignment properly handles the possibilty of the data being chunked. |
| 64 | + let txBody = try tx.makeSources().signedTransactions.makeIterator().map { signed in |
| 65 | + try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes) |
| 66 | + } |
| 67 | + |
| 68 | + let txBody2 = try tx2.makeSources().signedTransactions.makeIterator().map { signed in |
| 69 | + try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes) |
| 70 | + } |
| 71 | + |
| 72 | + XCTAssertEqual(txBody, txBody2) |
| 73 | + } |
| 74 | + |
| 75 | + internal func testGetSetFileId() throws { |
| 76 | + let tx = FileAppendTransaction.init() |
| 77 | + tx.fileId(Self.fileId) |
| 78 | + |
| 79 | + XCTAssertEqual(tx.fileId, Self.fileId) |
| 80 | + } |
| 81 | + |
| 82 | + internal func testGetSetContents() throws { |
| 83 | + let tx = FileAppendTransaction.init() |
| 84 | + tx.contents(Self.contents) |
| 85 | + |
| 86 | + XCTAssertEqual(tx.contents, Self.contents) |
| 87 | + } |
| 88 | +} |
0 commit comments