templates.test_initial_balance

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class InitialBalance(sp.Contract):
 7        def __init__(self):
 8            self.data.x = 2
 9
10        @sp.private(with_storage="read-write")
11        def sub(self, params):
12            self.data.x = 3
13
14
15@sp.add_test()
16def test():
17    scenario = sp.test_scenario("Sub", main)
18
19    initialBalance = sp.mutez(123)
20
21    c1 = main.InitialBalance()
22    c1.set_initial_balance(initialBalance)
23    scenario += c1
24    scenario.verify(c1.balance == initialBalance)
25
26    c2 = main.InitialBalance()
27    c2.set_initial_balance(initialBalance)
28    c2.set_initial_balance(initialBalance - sp.mutez(1))
29    c2.set_initial_balance(initialBalance)
30    scenario += c2
31    scenario.verify(c2.balance == initialBalance)
32
33    try:
34        c2.set_initial_balance(initialBalance)
35    except:
36        pass
37    else:
38        # Don't raise error if the scenario failed earlier.
39        # Needed for mockup.
40        if not scenario.failed:
41            raise AssertionError("set_initial_balance should fail after origination")