/
test_script.py
128 lines (96 loc) · 4.06 KB
/
test_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
""" Tests for script command """
from solidbyte.common.web3 import web3c
from solidbyte.common.exceptions import InvalidScriptError
from solidbyte.deploy import Deployer
from solidbyte.compile.compiler import Compiler
from solidbyte.script import get_contracts, run_script, run_scripts
from .const import (
NETWORK_NAME,
)
def test_get_contracts(mock_project):
""" Test that get_contracts() returns proper Web3 contract objects """
with mock_project() as mock:
# Setup our environment
compiler = Compiler(project_dir=mock.paths.project)
compiler.compile_all()
# Since we're not using the pwd, we need to use this undocumented API (I know...)
web3c._load_configuration(mock.paths.networksyml)
web3 = web3c.get_web3(NETWORK_NAME)
deployer_account = web3.eth.accounts[0]
# Init the Deployer
d = Deployer(
network_name=NETWORK_NAME,
account=deployer_account,
project_dir=mock.paths.project,
)
d.deploy()
contracts = get_contracts(NETWORK_NAME)
assert len(contracts) > 0, "No contracts found"
# Make sure it has all the expected Web3 Contract props
assert hasattr(contracts['Test'], 'functions')
assert hasattr(contracts['Test'], 'address')
assert hasattr(contracts['Test'], 'abi')
def test_script(mock_project):
""" test user script running """
with mock_project() as mock:
test_script = mock.paths.scripts.joinpath('test_success.py')
assert test_script.is_file()
# Setup our environment
compiler = Compiler(project_dir=mock.paths.project)
compiler.compile_all()
# Since we're not using the pwd, we need to use this undocumented API (I know...)
web3c._load_configuration(mock.paths.networksyml)
web3 = web3c.get_web3(NETWORK_NAME)
deployer_account = web3.eth.accounts[0]
# Init the Deployer
d = Deployer(
network_name=NETWORK_NAME,
account=deployer_account,
project_dir=mock.paths.project,
)
d.deploy()
assert run_script(NETWORK_NAME, str(test_script)), "Script unexpectedly failed"
assert run_scripts(NETWORK_NAME, [str(test_script)]), "Scripts unexpectedly failed"
def test_script_failure(mock_project):
""" test that scripts fail properly """
with mock_project() as mock:
test_script = mock.paths.scripts.joinpath('test_fail.py')
assert test_script.is_file()
# Setup our environment
compiler = Compiler(project_dir=mock.paths.project)
compiler.compile_all()
# Since we're not using the pwd, we need to use this undocumented API (I know...)
web3c._load_configuration(mock.paths.networksyml)
web3 = web3c.get_web3(NETWORK_NAME)
deployer_account = web3.eth.accounts[0]
# Init the Deployer
d = Deployer(
network_name=NETWORK_NAME,
account=deployer_account,
project_dir=mock.paths.project,
)
d.deploy()
assert run_script(NETWORK_NAME, str(test_script)) is False, "Script unexpectedly succeeded"
assert run_scripts(NETWORK_NAME, [str(test_script)]) is False, (
"Scripts unexpectedly succeeded"
)
def test_invalid_script(mock_project):
""" test that invalid scripts fail """
with mock_project() as mock:
test_script = mock.paths.scripts.joinpath('test_nothing.py')
assert not test_script.is_file()
invalid_script = mock.paths.scripts.joinpath('test_invalid.py')
assert invalid_script.is_file()
try:
run_script(NETWORK_NAME, test_script)
assert False, "Should have thrown an error"
except FileNotFoundError:
pass
try:
run_script(NETWORK_NAME, invalid_script)
assert False, "Should have thrown an error"
except InvalidScriptError:
pass
def test_no_scripts(mock_project):
""" test that no scripts doesn't fail """
assert run_scripts(NETWORK_NAME, [])