test_time.py (4734B)
1 # standard imports 2 import unittest 3 import logging 4 import datetime 5 6 # external imports 7 from chainlib.eth.nonce import RPCNonceOracle 8 from chainlib.eth.tx import TxFactory 9 from chainlib.eth.tx import receipt 10 from chainlib.eth.address import to_checksum_address 11 from eth_erc20 import ERC20 12 from giftable_erc20_token.unittest import TestGiftableToken 13 14 # local imports 15 from evm_booking.unittest import TestBooking 16 from evm_booking.time import * 17 18 logging.basicConfig(level=logging.DEBUG) 19 logg = logging.getLogger() 20 21 22 class TestBookingTime(TestGiftableToken): 23 24 def setUp(self): 25 super(TestBookingTime, self).setUp() 26 self.token_address = self.address 27 28 self.start = datetime.datetime(year=1984, month=1, day=1) 29 d = int(self.start.timestamp()) 30 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc) 31 c = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR, start_seconds=d, signer=self.signer, nonce_oracle=nonce_oracle) 32 (tx_hash, o) = c.constructor(self.accounts[0], self.token_address) 33 self.rpc.do(o) 34 o = receipt(tx_hash) 35 r = self.rpc.do(o) 36 self.assertEqual(r['status'], 1) 37 self.address = to_checksum_address(r['contract_address']) 38 self.booking_address = self.address 39 self.caller = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR, start_seconds=d) 40 41 42 def test_end(self): 43 c = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR) 44 o = c.expires(self.booking_address, sender_address=self.accounts[0]) 45 r = self.rpc.do(o) 46 expect = self.start + datetime.timedelta(seconds=PERIOD_LEAPYEAR * PERIOD_HALFHOUR) 47 expect_seconds = expect.timestamp() + 1 48 self.assertEqual(int(r, 16), expect_seconds) 49 50 51 def test_uneven(self): 52 d = datetime.datetime(year=1984, month=1, day=1) 53 d = int(d.timestamp()) 54 with self.assertRaises(ValueError): 55 TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR - 1, start_seconds=d) 56 57 58 def test_check_capacity(self): 59 logg.debug('foo {}'.format(self.caller.capacity)) 60 o = self.caller.capacity(self.booking_address, sender_address=self.accounts[0]) 61 r = self.rpc.do(o) 62 capacity = int(r, 16) 63 self.assertEqual(capacity, int(PERIOD_LEAPYEAR / PERIOD_HALFHOUR)) 64 65 66 def test_by_date(self): 67 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc) 68 d = datetime.datetime(year=1984, month=1, day=1) 69 c = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR, start_seconds=d.timestamp(), signer=self.signer, nonce_oracle=nonce_oracle) 70 start_date = datetime.datetime(year=1984, month=3, day=8, hour=12, minute=30) 71 count = int(PERIOD_DAY / PERIOD_HALFHOUR) 72 (tx_hash, o) = c.share_date(self.booking_address, self.accounts[0], start_date, count) 73 self.rpc.do(o) 74 o = receipt(tx_hash) 75 r = self.rpc.do(o) 76 self.assertEqual(r['status'], 1) 77 78 o = c.raw(self.booking_address, count=80, offset=3216, sender_address=self.accounts[0]) 79 r = self.rpc.do(o) 80 raw = c.parse_raw(r) 81 self.assertEqual("000000feffffffffff01", raw) 82 83 84 def test_consume_by_date(self): 85 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc) 86 d = datetime.datetime(year=1984, month=1, day=1) 87 start_date = datetime.datetime(year=1984, month=3, day=8, hour=12, minute=30) 88 count = int(PERIOD_DAY / PERIOD_HALFHOUR) 89 c = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR, start_seconds=d.timestamp(), signer=self.signer, nonce_oracle=nonce_oracle) 90 (tx_hash, o) = c.consume_date(self.booking_address, self.accounts[0], start_date, count) 91 self.rpc.do(o) 92 o = receipt(tx_hash) 93 r = self.rpc.do(o) 94 self.assertEqual(r['status'], 0) 95 96 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 97 (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[0], self.address, self.initial_supply) 98 self.rpc.do(o) 99 100 c = TimeBooking(self.chain_spec, PERIOD_LEAPYEAR, PERIOD_HALFHOUR, start_seconds=d.timestamp(), signer=self.signer, nonce_oracle=nonce_oracle) 101 (tx_hash, o) = c.consume_date(self.booking_address, self.accounts[0], start_date, count) 102 self.rpc.do(o) 103 o = receipt(tx_hash) 104 r = self.rpc.do(o) 105 self.assertEqual(r['status'], 1) 106 107 o = c.raw(self.booking_address, count=80, offset=3216, sender_address=self.accounts[0]) 108 r = self.rpc.do(o) 109 raw = c.parse_raw(r) 110 self.assertEqual("000000feffffffffff01", raw) 111 112 113 if __name__ == '__main__': 114 unittest.main()