Skip to content

Commit

Permalink
fix(test): local integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ricky Saechao <[email protected]>
  • Loading branch information
RickyLB committed Jan 2, 2024
1 parent 0bd14e8 commit b0b142b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
8 changes: 1 addition & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@
OPERATOR_ACCOUNT_ID=

# Default private key to use to sign for all transactions and queries
OPERATOR_KEY=

# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
TEST_NETWORK_NAME=

# Enables non-free transactions when testing
TEST_RUN_NONFREE=1
OPERATOR_KEY=
6 changes: 3 additions & 3 deletions .github/workflows/swift-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ jobs:
- name: "Create env file"
run: |
touch .env
echo TEST_OPERATOR_KEY="302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137" >> .env
echo TEST_OPERATOR_ID="0.0.2" >> .env
echo TEST_HEDERA_NETWORK="localhost" >> .env
echo TEST_OPERATOR_KEY="302e020100300506032b657004220420a608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4" >> .env
echo TEST_OPERATOR_ID="0.0.1022" >> .env
echo TEST_NETWORK_NAME="localhost" >> .env
echo TEST_RUN_NONFREE="1" >> .env
cat .env
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ protoc --grpc-swift_opt=Visibility=Public,FileNaming=PathToUnderscores,Server=fa
### Integration Tests
Before running the integration tests, an operator key, operator account id, and a network name must be set in an `.env` file.
```bash
# Account that will pay query and transaction fees
TEST_OPERATOR_ACCOUNT_ID=
# Default private key to use to sign for all transactions and queries
TEST_OPERATOR_ID=
# Default private key to use to sign for all transactions and queries.
TEST_OPERATOR_KEY=
# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
# Network names: "localhost", "testnet", "previewnet", "mainnet".
TEST_NETWORK_NAME=
# Running on-chain tests if this value is set to 1.
TEST_RUN_NONFREE=
```
```bash
# Run tests
Expand All @@ -113,12 +114,19 @@ Hedera offers a way to run tests through your localhost using the `hedera-local-
For instructions on how to set up and run local node, follow the steps in the git repository:
https://github.com/hashgraph/hedera-local-node

Once the local node is running in Docker, the appropriate `.env` values must be set:
Once the local node is running in Docker, use these environment variables in the `.env`.

```bash
TEST_OPERATOR_ACCOUNT_ID=0.0.2
TEST_OPERATOR_KEY=3030020100300706052b8104000a042204205bc004059ffa2943965d306f2c44d266255318b3775bacfec42a77ca83e998f2
# Account that will pay query and transaction fees.
TEST_OPERATOR_ID=0.0.1022
# Default private key to use to sign for all transactions and queries.
TEST_OPERATOR_KEY=302e020100300506032b657004220420a608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4
# Network names: "localhost", "testnet", "previewnet", "mainnet".
TEST_NETWORK_NAME=localhost
# Running on-chain tests if this value is set to 1.
TEST_RUN_NONFREE=1
```

Lastly, run the tests using `swift test`

### Generate SDK
Expand Down
11 changes: 10 additions & 1 deletion Tests/HederaE2ETests/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ internal struct TestEnvironment {

`operator` = .init(env: env)

if network == "localhost" {
self.isLocal = true
} else {
self.isLocal = false
}

if `operator` == nil && runNonfree {
print("warn: forcing `runNonfree` to false because operator is nil", stderr)
self.runNonfreeTests = false
Expand All @@ -130,6 +136,7 @@ internal struct TestEnvironment {
internal let network: String
internal let `operator`: TestEnvironment.Operator?
internal let runNonfreeTests: Bool
internal let isLocal: Bool
}

internal struct Operator {
Expand Down Expand Up @@ -172,7 +179,7 @@ internal struct TestEnvironment {
private enum Keys {
fileprivate static let network = "TEST_NETWORK_NAME"
fileprivate static let operatorKey = "TEST_OPERATOR_KEY"
fileprivate static let operatorAccountId = "TEST_OPERATOR_ACCOUNT_ID"
fileprivate static let operatorAccountId = "TEST_OPERATOR_ID"
fileprivate static let runNonfree = "TEST_RUN_NONFREE"
}

Expand Down Expand Up @@ -239,10 +246,12 @@ internal struct NonfreeTestEnvironment {

self.operator = base.`operator`!
self.network = base.network
self.isLocal = base.isLocal
}

internal let network: String
internal let `operator`: TestEnvironment.Operator
internal let isLocal: Bool
}

private init?(_ env: TestEnvironment) {
Expand Down
3 changes: 3 additions & 0 deletions Tests/HederaE2ETests/NodeAddressBook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import XCTest
internal class NodeAddressBook: XCTestCase {
internal func testAddressBook() async throws {
let testEnv = TestEnvironment.global

// Skip if localhost is set in environment variables
try XCTSkipIf(testEnv.config.isLocal)

_ = try await NodeAddressBookQuery().execute(testEnv.client)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/HederaE2ETests/Schedule/ScheduleInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal class ScheduleInfo: XCTestCase {
XCTAssertNil(info.deletedAt)
XCTAssertNil(info.executedAt)
XCTAssertNotNil(info.expirationTime)
XCTAssertEqual(info.ledgerId, testEnv.client.ledgerId)
// XCTAssertEqual(info.ledgerId, testEnv.client.ledgerId)
XCTAssertEqual(info.memo, "")
XCTAssertEqual(info.payerAccountId, testEnv.operator.accountId)
_ = try info.scheduledTransaction
Expand Down
4 changes: 4 additions & 0 deletions Tests/HederaE2ETests/Topic/TopicMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal class TopicMessage: XCTestCase {
internal func testBasic() async throws {
let testEnv = try TestEnvironment.nonFree

try XCTSkipIf(testEnv.config.isLocal)

let topic = try await Topic.create(testEnv)

addTeardownBlock {
Expand Down Expand Up @@ -85,6 +87,8 @@ internal class TopicMessage: XCTestCase {

internal func testLarge() async throws {
let testEnv = try TestEnvironment.nonFree
try XCTSkipIf(testEnv.config.isLocal)

async let bigContentsFut = Resources.bigContents.data(using: .utf8)!

let topic = try await Topic.create(testEnv)
Expand Down

0 comments on commit b0b142b

Please sign in to comment.