Skip to content

Commit 6d6841c

Browse files
committed
[SPT-777] New structure of functional tests
1 parent 60d531f commit 6d6841c

14 files changed

+681
-610
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,3 @@ jobs:
6464
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
6565
# sh -ex ./snet/cli/test/utils/run_all_functional.sh
6666
python3 ./snet/cli/test/functional_tests/test_entry_point.py
67-
python3 ./snet/cli/test/functional_tests/func_tests.py

snet/cli/test/functional_tests/func_tests.py

Lines changed: 0 additions & 608 deletions
Large diffs are not rendered by default.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
from func_tests import BaseTest, execute, ADDR, IDENTITY, PRIVATE_KEY, INFURA, INFURA_KEY
3+
4+
5+
class TestAAMainPreparations(BaseTest):
6+
def setUp(self):
7+
super().setUp()
8+
self.new_network = "auto_test"
9+
10+
def test_1_identity_create(self):
11+
execute(["identity", "create", IDENTITY, "key", "--private-key", PRIVATE_KEY, "-de"], self.parser, self.conf)
12+
result = execute(["session"], self.parser, self.conf)
13+
assert f"identity: {IDENTITY}" in result
14+
15+
def test_2_set_network_mainnet(self):
16+
execute(["network", "mainnet"], self.parser, self.conf)
17+
result = execute(["session"], self.parser, self.conf)
18+
assert "network: mainnet" in result
19+
20+
def test_3_set_network_sepolia(self):
21+
execute(["network", "sepolia"], self.parser, self.conf)
22+
result = execute(["session"], self.parser, self.conf)
23+
assert "network: sepolia" in result
24+
25+
def test_41_network_create(self):
26+
result = execute(["network", "create", self.new_network, INFURA], self.parser, self.conf)
27+
assert f"add network with name='{self.new_network}'" in result
28+
29+
def test_42_network_create_confirmation(self):
30+
execute(["network", self.new_network], self.parser, self.conf)
31+
result = execute(["session"], self.parser, self.conf)
32+
execute(["network", "sepolia"], self.parser, self.conf)
33+
assert "network: ", self.new_network in result
34+
35+
def test_5_set_infura(self):
36+
execute(["set", "default_eth_rpc_endpoint", INFURA], self.parser, self.conf)
37+
result = execute(["session"], self.parser, self.conf)
38+
assert INFURA_KEY in result
39+
40+
def test_6_print_account(self):
41+
result = execute(["account", "print"], self.parser, self.conf)
42+
print(result)
43+
assert ADDR in result
44+
45+
if __name__ == "__main__":
46+
unittest.main()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from func_tests import BaseTest, execute, ADDR
2+
import unittest
3+
import re
4+
5+
6+
class TestChannels(BaseTest):
7+
def setUp(self):
8+
super().setUp()
9+
self.org_id = "SNet"
10+
self.org_2_id = "singularitynet"
11+
self.amount = "0.001"
12+
self.password = "12345"
13+
self.group = "default_group"
14+
data = execute(["channel", "print-filter-sender"], self.parser, self.conf)
15+
lines = data.split("\n")
16+
self.max_id = ""
17+
for line in lines:
18+
parts = line.split()
19+
if len(parts) >= 6 and parts[0].isdigit() and parts[-1].isdigit():
20+
self.max_id = parts[0]
21+
22+
def test_channel_1_extend(self):
23+
execute(["account", "deposit", self.amount, "-y", "-q"], self.parser, self.conf)
24+
if self.max_id:
25+
result1 = execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
26+
else:
27+
block_number = int(execute(["channel", "block-number"], self.parser, self.conf))
28+
channel_open_output = execute(["channel", "open", self.org_id, self.group, self.amount, f"{block_number+10000}", "-y", "--open-new-anyway"], self.parser, self.conf)
29+
match = re.search(r"#channel_id\s+(\d+)", channel_open_output)
30+
self.max_id = match.group(1)
31+
execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
32+
result1 = execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser,
33+
self.conf)
34+
# result2 = execute(["channel", "extend-add-for-org", self.org_id, "default_group", "--channel-id", f"{self.max_id}", "-y"], self.parser, self.conf)
35+
print(result1)
36+
assert "event: ChannelAddFunds" in result1
37+
38+
def test_channel_2_print_filter_sender(self):
39+
result = execute(["channel", "print-filter-sender"], self.parser, self.conf)
40+
print(result)
41+
assert "Channels for sender: ", ADDR in result
42+
43+
def test_channel_3_print_filter_group_sender(self):
44+
result = execute(["channel", "print-filter-group-sender", self.org_id, self.group], self.parser, self.conf)
45+
assert "Channels for sender: ", ADDR in result
46+
47+
def test_channel_4_print_filter_group(self):
48+
result = execute(["channel", "print-filter-group", self.org_id, self.group], self.parser, self.conf)
49+
assert self.max_id in result
50+
51+
def test_channel_5_print_filter_recipient(self):
52+
result = execute(["channel", "print-filter-recipient"], self.parser, self.conf)
53+
assert "Channels for recipient:", ADDR in result
54+
55+
def test_channel_5_claim(self):
56+
execute(["account", "deposit", self.amount, "-y", "-q"], self.parser, self.conf)
57+
if self.max_id:
58+
execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
59+
result1 = execute(["channel", "claim-timeout", f"{self.max_id}", "-y"], self.parser, self.conf)
60+
execute(["account", "deposit", self.amount, "-y", "-q"], self.parser, self.conf)
61+
execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
62+
result2 = execute(["channel", "claim-timeout-all", "-y"], self.parser, self.conf)
63+
else:
64+
block_number = int(execute(["channel", "block-number"], self.parser, self.conf))
65+
execute(["channel", "open", self.org_id, self.group, self.amount, f"{block_number-1}", "-y"], self.parser, self.conf)
66+
execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
67+
result1 = execute(["channel", "claim-timeout", f"{self.max_id}", "-y"], self.parser, self.conf)
68+
execute(["account", "deposit", self.amount, "-y", "-q"], self.parser, self.conf)
69+
execute(["channel", "extend-add", self.max_id, "--amount", self.amount, "-y"], self.parser, self.conf)
70+
result2 = execute(["channel", "claim-timeout-all", "-y"], self.parser, self.conf)
71+
print(result1)
72+
assert ("event: ChannelSenderClaim" in result1) and ("event: ChannelSenderClaim" in result2)
73+
74+
def test_channel_6_print_all(self):
75+
result = execute(["channel", "print-all", "-ds"], self.parser, self.conf)
76+
assert self.max_id in result
77+
78+
if __name__ == "__main__":
79+
unittest.main()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from func_tests import BaseTest, execute, ADDR, PRIVATE_KEY, INFURA
2+
import os
3+
import unittest
4+
5+
6+
class TestClient(BaseTest):
7+
def setUp(self):
8+
super().setUp()
9+
self.org_id = "egor-sing-test"
10+
self.service_id = "hate-detection"
11+
self.group = "default_group"
12+
self.identity_name = "some__name"
13+
self.method = "runsync"
14+
self.params = "./detection.json"
15+
self.endpoint = "https://ai-ui-service.singularitynet.io:8001"
16+
self.max_id = "357"
17+
self.nonce = "1"
18+
self.amount_in_cogs = "1"
19+
20+
def test_0_preparations(self):
21+
identity_list=execute(["identity", "list"], self.parser, self.conf)
22+
if self.identity_name not in identity_list:
23+
execute(["identity", "create", self.identity_name, "key", "--private-key", PRIVATE_KEY, "-de"], self.parser, self.conf)
24+
execute(["network", "sepolia"], self.parser, self.conf)
25+
result = execute(["session"], self.parser, self.conf)
26+
assert "network: sepolia" in result
27+
28+
def test_1_channel_open(self):
29+
execute(["set", "default_eth_rpc_endpoint", INFURA], self.parser, self.conf)
30+
execute(["account", "deposit", "0.1", "-y"], self.parser, self.conf)
31+
self.block=int(execute(["channel", "block-number"], self.parser, self.conf))
32+
print(self.block)
33+
result = execute(["channel", "print-filter-group-sender", self.org_id, self.group], self.parser, self.conf)
34+
if ADDR not in result[13:]:
35+
result=execute(["channel", "open", self.org_id, "default_group", "0.0001", f"{self.block+100}", "-y"], self.parser, self.conf)
36+
else:
37+
pass
38+
print(result)
39+
assert "#channel_id" in result
40+
41+
def test_2_service_call(self):
42+
params_file = open("detection.json", "w+")
43+
params_file.write("""{
44+
"input": {
45+
"text": "Hello man answer me soon"
46+
}
47+
}
48+
""")
49+
params_file.close()
50+
result=execute(["client", "call", self.org_id, self.service_id, self.group, self.method, self.params, "-y"], self.parser, self.conf)
51+
assert "spam" in result
52+
53+
def test_3_service_get_channel_state(self):
54+
result=execute(["client", "get-channel-state", self.max_id, self.endpoint], self.parser, self.conf)
55+
assert "current_unspent_amount_in_cogs = " in result
56+
57+
def test_4_call_low_level(self):
58+
result = execute(["client", "call-lowlevel", self.org_id, self.service_id, self.group, self.max_id, self.nonce, self.amount_in_cogs, self.method, self.params], self.parser, self.conf)
59+
assert "spam" in result
60+
61+
def test_5_get_api_registry(self):
62+
execute(["service", "get-api-registry", self.org_id, self.service_id, "./"], self.parser, self.conf)
63+
assert os.path.exists("./hate.proto")
64+
65+
66+
if __name__ == "__main__":
67+
unittest.main()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from func_tests import BaseTest, execute, ADDR
3+
import importlib
4+
5+
6+
class TestCommands(BaseTest):
7+
def setUp(self):
8+
super().setUp()
9+
10+
def test_balance_output(self):
11+
result = execute(["account", "balance"], self.parser, self.conf)
12+
assert len(result.split("\n")) >= 4
13+
14+
def test_balance_address(self):
15+
result = execute(["account", "balance"], self.parser, self.conf)
16+
assert result.split("\n")[0].split()[1] == ADDR
17+
18+
def test_version(self):
19+
file_path = "./version.py"
20+
spec = importlib.util.spec_from_file_location("version", file_path)
21+
version_module = importlib.util.module_from_spec(spec)
22+
spec.loader.exec_module(version_module)
23+
self.version = version_module.__version__
24+
result = execute(["version"], self.parser, self.conf)
25+
assert f"version: {self.version}" in result
26+
27+
28+
if __name__ == "__main__":
29+
unittest.main()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from func_tests import BaseTest, execute, ADDR
2+
import unittest
3+
4+
class TestContract(BaseTest):
5+
def setUp(self):
6+
super().setUp()
7+
self.last_channel_id = int(execute(["contract", "MultiPartyEscrow", "nextChannelId"], self.parser, self.conf)) - 1
8+
self.amount = "1"
9+
self.block_number = int(execute(["channel", "block-number"], self.parser, self.conf))
10+
self.channel = execute(["channel", "print-filter-sender"], self.parser, self.conf).split()[11]
11+
12+
def test_SingularityNetToken_name(self):
13+
result = execute(["contract", "SingularityNetToken", "name"], self.parser, self.conf)
14+
assert "SingularityNet Token" in result
15+
16+
def test_SingularityNetToken_decimals(self):
17+
result = execute(["contract", "SingularityNetToken", "decimals"], self.parser, self.conf)
18+
assert "8" in result
19+
20+
def test_SingularityNetToken_symbol(self):
21+
result = execute(["contract", "SingularityNetToken", "symbol"], self.parser, self.conf)
22+
assert "AGIX" in result
23+
24+
def test_SingularityNetToken_totalSupply(self):
25+
result = int(execute(["contract", "SingularityNetToken", "totalSupply"], self.parser, self.conf))
26+
assert 1772090920768158 <= result
27+
28+
def test_SingularityNetToken_paused(self):
29+
result=execute(["contract", "SingularityNetToken", "paused"], self.parser, self.conf)
30+
assert "False" in result
31+
32+
def test_MultiPartyEscrow_1_balances(self):
33+
result = int(execute(["contract", "MultiPartyEscrow", "balances", ADDR], self.parser, self.conf))
34+
assert result > 0
35+
36+
def test_MultiPartyEscrow_token(self):
37+
result = execute(["contract", "MultiPartyEscrow", "token"], self.parser, self.conf)
38+
assert "0xf703b9aB8931B6590CFc95183be4fEf278732016" in result
39+
40+
def test_MultiPartyEscrow_channels(self):
41+
result = execute(["contract", "MultiPartyEscrow", "channels", f"{self.last_channel_id}"], self.parser, self.conf)
42+
assert len(result) > 150
43+
44+
def test_MultiPartyEscrow_addFunds(self):
45+
print(self.channel)
46+
result = execute(["contract", "MultiPartyEscrow", "channelAddFunds", self.channel, self.amount, "-y"], self.parser, self.conf)
47+
assert "event: ChannelAddFunds" in result
48+
49+
def test_MultiPartyEscrow_channelExtend(self):
50+
result = execute(["contract", "MultiPartyEscrow", "channelExtend", self.channel, f"{self.block_number+100}", "-y"], self.parser, self.conf).split()[11]
51+
assert "event: ChannelExtend" in result
52+
53+
def test_Registry_getOrganizationById(self):
54+
result = execute(["contract", "Registry", "getOrganizationById", "SNet"], self.parser, self.conf)
55+
assert len(result) > 400
56+
57+
def test_Registry_listOrganizations(self):
58+
result = execute(["contract", "Registry", "listOrganizations"], self.parser, self.conf)
59+
assert len(result) > 12000
60+
61+
def test_Registry_getServiceRegistrationById(self):
62+
result = execute(["contract", "Registry", "getServiceRegistrationById", "SNet", "example-service-constructor"], self.parser, self.conf)
63+
assert "True" in result
64+
65+
def test_Registry_listServicesForOrganization(self):
66+
result = execute(["contract", "Registry", "listServicesForOrganization", "SNet"], self.parser, self.conf)
67+
assert "True" in result
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
from func_tests import BaseTest, execute, ADDR
3+
import math
4+
5+
6+
class TestDepositWithdrawTransfer(BaseTest):
7+
def setUp(self):
8+
super().setUp()
9+
self.balance_1: int
10+
self.balance_2: int
11+
self.amount = 0.1
12+
13+
def test_deposit(self):
14+
result = execute(["account", "balance"], self.parser, self.conf)
15+
self.balance_1 = float(result.split("\n")[3].split()[1])
16+
execute(["account", "deposit", f"{self.amount}", "-y", "-q"], self.parser, self.conf)
17+
result = execute(["account", "balance"], self.parser, self.conf)
18+
self.balance_2 = float(result.split("\n")[3].split()[1])
19+
assert math.isclose(self.balance_2, self.balance_1 + self.amount, rel_tol=1e-3)
20+
21+
def test_withdraw(self):
22+
result = execute(["account", "balance"], self.parser, self.conf)
23+
self.balance_1 = float(result.split("\n")[3].split()[1])
24+
execute(["account", "withdraw", f"{self.amount}", "-y", "-q"], self.parser, self.conf)
25+
result = execute(["account", "balance"], self.parser, self.conf)
26+
self.balance_2 = float(result.split("\n")[3].split()[1])
27+
assert math.isclose(self.balance_2, self.balance_1 - self.amount, rel_tol=1e-3)
28+
29+
def test_transfer(self):
30+
result = execute(["account", "transfer", ADDR, f"{self.amount}", "-y"], self.parser, self.conf)
31+
assert "TransferFunds" in result
32+
33+
34+
if __name__ == "__main__":
35+
unittest.main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unittest
2+
from func_tests import BaseTest, execute, PRIVATE_KEY
3+
import unittest.mock as mock
4+
from snet.cli.commands.commands import BlockchainCommand
5+
6+
7+
class TestEncryptionKey(BaseTest):
8+
def setUp(self):
9+
super().setUp()
10+
self.key = PRIVATE_KEY
11+
self.password = "some_pass"
12+
self.name = "some_name"
13+
self.default_name = "default_name"
14+
result = execute(["identity", "list"], self.parser, self.conf)
15+
if self.default_name not in result:
16+
execute(["identity", "create", self.default_name, "key", "--private-key", self.key, "-de"],
17+
self.parser,
18+
self.conf)
19+
20+
def test_1_create_identity_with_encryption_key(self):
21+
with mock.patch('getpass.getpass', return_value=self.password):
22+
execute(["identity", "create", self.name, "key", "--private-key", self.key],
23+
self.parser,
24+
self.conf)
25+
result = execute(["identity", "list"], self.parser, self.conf)
26+
assert self.name in result
27+
28+
def test_2_get_encryption_key(self):
29+
with mock.patch('getpass.getpass', return_value=self.password):
30+
execute(["identity", self.name], self.parser, self.conf)
31+
cmd = BlockchainCommand(self.conf, self.parser.parse_args(['session']))
32+
enc_key = cmd.config.get_session_field("private_key")
33+
res_key = cmd._get_decrypted_secret(enc_key)
34+
assert res_key == self.key
35+
36+
def test_3_delete_identity(self):
37+
with mock.patch('getpass.getpass', return_value=self.password):
38+
execute(["identity", self.default_name], self.parser, self.conf)
39+
execute(["identity", "delete", self.name], self.parser, self.conf)
40+
result = execute(["identity", "list"], self.parser, self.conf)
41+
assert self.name not in result
42+
43+
44+
if __name__ == "__main__":
45+
unittest.main()

snet/cli/test/functional_tests/test_entry_point.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,7 @@ def test_version(self):
6565

6666

6767
if __name__ == '__main__':
68-
unittest.main()
68+
cli_tests = unittest.TestLoader().loadTestsFromTestCase(TestEntryPoint)
69+
functional_tests = unittest.TestLoader().discover("./snet/cli/test/functional_tests", pattern="test_*.py")
70+
all_tests = unittest.TestSuite([cli_tests, functional_tests])
71+
unittest.TextTestRunner().run(all_tests)

0 commit comments

Comments
 (0)