Skip to content

Commit 94e1a8c

Browse files
Enhances tests according to changes (#45)
1 parent 31d8e19 commit 94e1a8c

8 files changed

+90
-74
lines changed

.github/workflows/e2e-subtensor-tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ jobs:
6464
run: git checkout testnet
6565

6666
- name: Install Python dependencies
67-
run: python3 -m pip install -r requirements.txt pytest
67+
run: python3 -m pip install -e . pytest
6868

6969
- name: Run all tests
7070
run: |
71-
LOCALNET_SH_PATH="${{ github.workspace }}/subtensor/scripts/localnet.sh" pytest src/tests/e2e_tests -s
71+
LOCALNET_SH_PATH="${{ github.workspace }}/subtensor/scripts/localnet.sh" pytest tests/e2e_tests -s

tests/e2e_tests/__init__.py

Whitespace-only changes.

tests/e2e_tests/conftest.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import os
33
import re
44
import shlex
5+
import shutil
56
import signal
67
import subprocess
78
import time
89

910
import pytest
10-
1111
from bittensor_cli.src.bittensor.async_substrate_interface import (
1212
AsyncSubstrateInterface,
1313
)
1414

15+
from .utils import setup_wallet
16+
1517

1618
# Fixture for setting up and tearing down a localnet.sh chain between tests
1719
@pytest.fixture(scope="function")
@@ -70,3 +72,19 @@ def wait_for_node_start(process, pattern):
7072

7173
# Ensure the process has terminated
7274
process.wait()
75+
76+
77+
@pytest.fixture(scope="function")
78+
def wallet_setup():
79+
wallet_paths = []
80+
81+
def _setup_wallet(uri: str):
82+
keypair, wallet, wallet_path, exec_command = setup_wallet(uri)
83+
wallet_paths.append(wallet_path)
84+
return keypair, wallet, wallet_path, exec_command
85+
86+
yield _setup_wallet
87+
88+
# Cleanup after the test
89+
for path in wallet_paths:
90+
shutil.rmtree(path, ignore_errors=True)

tests/e2e_tests/test_root.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import logging
21
import time
32

43
from bittensor_cli.src.bittensor.balances import Balance
54

6-
from tests.e2e_tests.utils import setup_wallet
7-
85
"""
96
Verify commands:
107
@@ -18,7 +15,7 @@
1815
"""
1916

2017

21-
def test_root_commands(local_chain):
18+
def test_root_commands(local_chain, wallet_setup):
2219
"""
2320
Test the root commands and inspects their output
2421
@@ -33,16 +30,16 @@ def test_root_commands(local_chain):
3330
Raises:
3431
AssertionError: If any of the checks or verifications fail
3532
"""
36-
logging.info("Testing Root commands 🧪")
33+
print("Testing Root commands 🧪")
3734

3835
wallet_path_alice = "//Alice"
3936
wallet_path_bob = "//Bob"
4037

4138
# Create wallet for Alice
42-
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = setup_wallet(
39+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
4340
wallet_path_alice
4441
)
45-
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = setup_wallet(
42+
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = wallet_setup(
4643
wallet_path_bob
4744
)
4845

@@ -80,16 +77,16 @@ def test_root_commands(local_chain):
8077

8178
# Capture root information and assert correct values
8279
# First two rows are labels, entries start from the third row
83-
bob_root_info = check_root_list.stdout.splitlines()[2].split()
80+
bob_root_info = check_root_list.stdout.splitlines()[3].split()
8481

8582
# UID: First uid is always 0
8683
assert bob_root_info[0] == "0"
8784

8885
# ADDRESS: Assert correct hotkey is registered
89-
assert bob_root_info[1] == wallet_bob.hotkey.ss58_address
86+
assert bob_root_info[3] == wallet_bob.hotkey.ss58_address
9087

9188
# SENATOR: Since there are senator slots empty, Bob is assigned senator status
92-
assert bob_root_info[3] == "Yes"
89+
assert bob_root_info[7] == "Yes"
9390

9491
# List all root delegates in the network
9592
check_delegates = exec_command_alice(
@@ -105,7 +102,7 @@ def test_root_commands(local_chain):
105102

106103
# Capture delegate information and assert correct values
107104
# First row are labels, entries start from the second row
108-
bob_delegate_info = check_delegates.stdout.splitlines()[1].split()
105+
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()
109106

110107
# INDEX: First uid is always 0
111108
assert bob_delegate_info[0] == "0"
@@ -166,7 +163,7 @@ def test_root_commands(local_chain):
166163
],
167164
)
168165
# Capture delegate information after setting take
169-
bob_delegate_info = check_delegates.stdout.splitlines()[1].split()
166+
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()
170167

171168
# Take percentage: This should be 18% by default
172169
take_percentage = float(bob_delegate_info[7].strip("%")) / 100
@@ -210,15 +207,13 @@ def test_root_commands(local_chain):
210207
],
211208
)
212209
# First row are headers, records start from second row
213-
alice_delegates_info = alice_delegates.stdout.splitlines()[1].split()
210+
alice_delegates_info = alice_delegates.stdout.splitlines()[4].split()
214211

215212
# WALLET: Wallet name of Alice
216213
assert alice_delegates_info[0] == wallet_alice.name
217214

218215
# SS58: address of the Bob's hotkey (Alice has staked to Bob)
219-
assert wallet_bob.hotkey.ss58_address.startswith(
220-
alice_delegates_info[1].replace("...", "")
221-
)
216+
assert wallet_bob.hotkey.ss58_address == alice_delegates_info[1]
222217

223218
# Delegation: This should be 10 as Alice delegated 10 TAO to Bob
224219
delegate_stake = Balance.from_tao(float(alice_delegates_info[2].strip("τ")))
@@ -231,7 +226,7 @@ def test_root_commands(local_chain):
231226
# Total delegated Tao: This is listed at the bottom of the information
232227
# Since Alice has only delegated to Bob, total should be 10 TAO
233228
total_delegated_tao = Balance.from_tao(
234-
float(alice_delegates.stdout.splitlines()[3].split()[3].strip("τ"))
229+
float(alice_delegates.stdout.splitlines()[7].split()[3].strip("τ"))
235230
)
236231
assert total_delegated_tao == Balance.from_tao(10)
237232

@@ -261,3 +256,5 @@ def test_root_commands(local_chain):
261256
],
262257
)
263258
assert "✅ Finalized" in undelegate_alice.stdout
259+
260+
print("✅ Passed Root commands")

tests/e2e_tests/test_staking_sudo.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import re
2-
import logging
32
import time
43

54
from bittensor_cli.src.bittensor.balances import Balance
6-
from tests.e2e_tests.utils import setup_wallet
75

86
"""
97
Verify commands:
@@ -18,7 +16,7 @@
1816
"""
1917

2018

21-
def test_staking(local_chain):
19+
def test_staking(local_chain, wallet_setup):
2220
"""
2321
Test staking & sudo commands and inspect their output
2422
@@ -33,12 +31,12 @@ def test_staking(local_chain):
3331
Raises:
3432
AssertionError: If any of the checks or verifications fail
3533
"""
36-
logging.info("Testing staking and sudo commands🧪")
34+
print("Testing staking and sudo commands🧪")
3735
netuid = 1
3836
wallet_path_alice = "//Alice"
3937

4038
# Create wallet for Alice
41-
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = setup_wallet(
39+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
4240
wallet_path_alice
4341
)
4442

@@ -123,7 +121,7 @@ def test_staking(local_chain):
123121
cleaned_stake = [
124122
re.sub(r"\s+", " ", line) for line in show_stake.stdout.splitlines()
125123
]
126-
stake_added = cleaned_stake[3].split()[4].strip("τ")
124+
stake_added = cleaned_stake[4].split()[4].strip("τ")
127125
assert Balance.from_tao(100) == Balance.from_tao(float(stake_added))
128126

129127
# TODO: Ask nucleus the rate limit and wait epoch
@@ -169,7 +167,7 @@ def test_staking(local_chain):
169167

170168
# Parse all hyperparameters and single out max_burn in TAO
171169
all_hyperparams = hyperparams.stdout.splitlines()
172-
max_burn_tao = all_hyperparams[18].split()[2]
170+
max_burn_tao = all_hyperparams[23].split()[2]
173171

174172
# Assert max_burn is 100 TAO from default
175173
assert Balance.from_tao(float(max_burn_tao.strip("τ"))) == Balance.from_tao(100)
@@ -218,9 +216,10 @@ def test_staking(local_chain):
218216

219217
# Parse updated hyperparameters
220218
all_updated_hyperparams = updated_hyperparams.stdout.splitlines()
221-
updated_max_burn_tao = all_updated_hyperparams[18].split()[2]
219+
updated_max_burn_tao = all_updated_hyperparams[23].split()[2]
222220

223221
# Assert max_burn is now 10 TAO
224222
assert Balance.from_tao(float(updated_max_burn_tao.strip("τ"))) == Balance.from_tao(
225223
10
226224
)
225+
print("✅ Passed staking and sudo commands")

0 commit comments

Comments
 (0)