From 2676ab646d4726a5331283b08e3164d995a88e77 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Tue, 16 Jul 2024 16:14:23 -0700 Subject: [PATCH] Adds e2e for metagraph command + subtensor.metagraph --- .../e2e_tests/subcommands/subnet/__init__.py | 0 .../subcommands/subnet/test_metagraph.py | 108 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/e2e_tests/subcommands/subnet/__init__.py create mode 100644 tests/e2e_tests/subcommands/subnet/test_metagraph.py diff --git a/tests/e2e_tests/subcommands/subnet/__init__.py b/tests/e2e_tests/subcommands/subnet/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/e2e_tests/subcommands/subnet/test_metagraph.py b/tests/e2e_tests/subcommands/subnet/test_metagraph.py new file mode 100644 index 0000000000..c9334f8abf --- /dev/null +++ b/tests/e2e_tests/subcommands/subnet/test_metagraph.py @@ -0,0 +1,108 @@ +import bittensor +from bittensor.commands import ( + MetagraphCommand, + RegisterCommand, + RegisterSubnetworkCommand, +) +from tests.e2e_tests.utils import setup_wallet + +""" +Test the metagraph command before and after registering neurons. + +Verify that: +* Metagraph gets displayed +* Initially empty +------------------------- +* Register 2 neurons one by one +* Ensure both are visible in metagraph +""" + + +def test_metagraph_command(local_chain, capsys): + # Register root as Alice + keypair, exec_command, wallet = setup_wallet("//Alice") + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + + # Verify subnet 1 created successfully + assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() + + subtensor = bittensor.subtensor(network="ws://localhost:9945") + + metagraph = subtensor.metagraph(netuid=1) + + # Assert metagraph is empty + assert len(metagraph.uids) == 0 + + # Execute btcli metagraph command + exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"]) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + # Assert metagraph is printed for netuid 1 + assert "Metagraph: net: local:1" in lines[2] + + # Register Bob as neuron to the subnet + bob_keypair, bob_exec_command, bob_wallet = setup_wallet("//Bob") + bob_exec_command( + RegisterCommand, + [ + "s", + "register", + "--netuid", + "1", + ], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + # Assert neuron was registered + assert "✅ Registered" in lines[3] + + # Refresh the metagraph + metagraph = subtensor.metagraph(netuid=1) + + # Assert metagraph has registered neuron + assert len(metagraph.uids) == 1 + assert metagraph.hotkeys[0] == "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" + # Execute btcli metagraph command + exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"]) + + captured = capsys.readouterr() + + # Assert the neuron is registered and displayed + assert "Metagraph: net: local:1" and "N: 1/1" in captured.out + + # Register Dave as neuron to the subnet + dave_keypair, dave_exec_command, dave_wallet = setup_wallet("//Dave") + dave_exec_command( + RegisterCommand, + [ + "s", + "register", + "--netuid", + "1", + ], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + # Assert neuron was registered + assert "✅ Registered" in lines[3] + + # Refresh the metagraph + metagraph = subtensor.metagraph(netuid=1) + + # Assert metagraph has registered neuron + assert len(metagraph.uids) == 2 + assert metagraph.hotkeys[1] == "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy" + + # Execute btcli metagraph command + exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"]) + + captured = capsys.readouterr() + + # Assert the neuron is registered and displayed + assert "Metagraph: net: local:1" and "N: 2/2" in captured.out