Skip to content

Commit

Permalink
Remove repetitive assert_tx_failed
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidKnott committed Nov 12, 2017
1 parent 03cb93e commit 8768bd7
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions examples/stock/test_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ethereum.tools import tester as t
from ethereum import utils

from tests.setup_transaction_tests import assert_tx_failed
from viper import compiler

@pytest.fixture
Expand All @@ -18,14 +19,7 @@ def tester():
args=[tester.company_address, 1000, 10**6])
return tester

@pytest.fixture
def assert_tx_failed(function_to_test, exception = t.TransactionFailed):
initial_state = tester.s.snapshot()
with pytest.raises(exception):
function_to_test()
tester.s.revert(initial_state)

def test_overbuy(tester):
def test_overbuy(tester, assert_tx_failed):
# If all the stock has been bought, no one can buy more
test_shares = int(tester.c.get_total_shares() / 2)
test_value = int(test_shares * tester.c.get_price())
Expand All @@ -37,7 +31,7 @@ def test_overbuy(tester):
assert_tx_failed(lambda: tester.c.buy_stock(sender=t.k1, value=one_stock))
assert_tx_failed(lambda: tester.c.buy_stock(sender=t.k2, value=one_stock))

def test_sell_without_stock(tester):
def test_sell_without_stock(tester, assert_tx_failed):
# If you don't have any stock, you can't sell
assert_tx_failed(lambda: tester.c.sell_stock(1, sender=t.k1))
assert_tx_failed(lambda: tester.c.sell_stock(1, sender=t.k2))
Expand All @@ -52,14 +46,14 @@ def test_sell_without_stock(tester):
# But only until you run out
assert_tx_failed(lambda: tester.c.sell_stock(1, sender=t.k1))

def test_oversell(tester):
def test_oversell(tester, assert_tx_failed):
# You can't sell more than you own
test_shares = int(tester.c.get_total_shares())
test_value = int(test_shares * tester.c.get_price())
tester.c.buy_stock(sender=t.k1, value=test_value)
assert_tx_failed(lambda: tester.c.sell_stock(test_shares + 1, sender=t.k1))

def test_transfer(tester):
def test_transfer(tester, assert_tx_failed):
# If you don't have any stock, you can't transfer
assert_tx_failed(lambda: tester.c.transfer_stock(t.a2, 1, sender=t.k1))
assert_tx_failed(lambda: tester.c.transfer_stock(t.a1, 1, sender=t.k2))
Expand All @@ -75,7 +69,7 @@ def test_transfer(tester):
# But the other person does
tester.c.sell_stock(test_shares, sender=t.k2)

def test_paybill(tester):
def test_paybill(tester, assert_tx_failed):
# Only the company can authorize payments
assert_tx_failed(lambda: tester.c.pay_bill(t.a2, 1, sender=t.k1))
# A company can only pay someone if it has the money
Expand Down

0 comments on commit 8768bd7

Please sign in to comment.