def test_transaction_not_found(self): # Arrange # Act bc = make_blockchain() # Assert assert bc._contain_tx(random_transaction()) is False
def test_use_given_date(self): bc = make_blockchain(end_with_empty_block=True) dt = date(2001, 3, 22) tx = bc.make_daily_guziboxes_tx(dt) assert tx.guzis_positions == [[[dt.isoformat()], [0]]]
def test_transaction_found(self): # Arrange # Act bc = make_blockchain(days=2, tx_per_block=1) # Assert assert bc._contain_tx(bc[2].transactions[0]) is True
def test_change_transactions_after_signed_ko(self): bc = make_blockchain(days=4, tx_per_block=4) bc.last_block().transactions[0].amount = 12 result = bc.is_valid() assert result is False
def test_return_0_for_total_0(self): bc = make_blockchain(end_with_empty_block=True) bc.last_block().total = 0 result = bc._get_guzis_amount() assert result == 1
def test_create_6_guzibox_if_total_is_125(self): bc = make_blockchain(end_with_empty_block=True) bc.last_block().total = 125 tx = bc.make_daily_guziboxes_tx() assert tx.guzis_positions == [[[date.today().isoformat()], list(range(6))]]
def test_last_block_takes_the_transaction(self): bc = make_blockchain(days=1, tx_per_block=2, close_last_block=False) tx = Transaction(VERSION, Transaction.GUZI_CREATE, NEW_USER_PUB_KEY, 0) bc._add_transaction(tx) assert len(bc.last_block().transactions) == 3 assert tx in bc.last_block().transactions
def test_ok(self): bc = make_blockchain(total=32**3) tx = bc.engage_guzis_to_user(target=REF_PUB_KEY, numdays=1, daily_amount=1) assert tx.tx_type == Transaction.GUZI_ENGAGEMENT assert tx.guzis_positions == [[[date(2000, 1, 1).isoformat()], [32]]]
def test_return_None_if_not_found(self): # Arrange blockchain = make_blockchain() # Act result = blockchain._find_tx(random_transaction()) # Assert assert result is None
def test_return_block_containing_transaction(self): # Arrange blockchain = make_blockchain(days=2, tx_per_block=1) # Act result = blockchain._find_tx(blockchain[2].transactions[0]) # Assert assert result == blockchain[2]
def test_hex_format(self): blockchain_ref = make_blockchain() outfile = BytesIO() blockchain_ref.save_to_file(outfile) outfile.seek(0) blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.from_file(outfile) assert blockchain == blockchain_ref
def test_hex_format(self): # Arrange blockchain_ref = make_blockchain() b = blockchain_ref.pack() # Act blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.from_bytes(b) # Assert assert blockchain == blockchain_ref
def test_all_blocks_be_in(self): bc = make_blockchain(days=2, tx_per_block=1) block0 = bc.last_block().pack().hex() block1 = bc[1].pack().hex() outfile = BytesIO() bc.save_to_file(outfile) outfile.seek(0) content = outfile.read().hex() assert block0 in content assert block1 in content
def test_fill_init_block(self): vk = ecdsa.VerifyingKey.from_string(REF_PUB_KEY, curve=ecdsa.SECP256k1) bc = make_blockchain() init_block = bc.last_block() expected_data = init_block.hash() assert init_block.version == VERSION assert init_block.close_date == date(2000, 1, 1) assert len( init_block.transactions) == 3 # 1.birth, 2.guzis 3.guziboxes assert bc.is_valid() is True assert vk.verify(init_block.signature, expected_data) is True
def test_return_a_refusal_transaction(self): bc = make_blockchain(days=1, close_last_block=False) refused = bc.last_block().transactions[0] refusal = bc.refuse_transaction(refused) assert refusal.tx_type == Transaction.REFUSAL assert refusal.date == date(2011, 12, 13) assert refusal.signer == refused.target_user assert refusal.amount == refused.amount assert refusal.target_company is None assert refusal.target_user == refused.signer assert refusal.guzis_positions == [] assert refusal.detail == refused.signature
def test_transactions_are_the_same(self): blockchain_ref = make_blockchain() ref_transactions = blockchain_ref[0].transactions outfile = BytesIO() blockchain_ref.save_to_file(outfile) outfile.seek(0) blockchain = Blockchain(NEW_USER_PUB_KEY) blockchain.from_file(outfile) transactions = blockchain[0].transactions assert len(transactions) == 3 assert transactions[0] == ref_transactions[0] assert transactions[1] == ref_transactions[1] assert transactions[2] == ref_transactions[2]
def init(self, amount=2): self.bc = make_blockchain(days=1, close_last_block=False) return self.bc.pay_to_user(REF_PUB_KEY, amount)
def test_different_signature(self): bc1 = make_blockchain() bc2 = make_blockchain() assert bc1 != bc2 assert bc2 != bc1
def test_blockchain_with_multiple_tx_per_block_ok(self): bc = make_blockchain(days=4, tx_per_block=4) result = bc.is_valid() assert result is True
def test_blockchain_with_empty_ending_block_ok(self): bc = make_blockchain(days=1, tx_per_block=1, end_with_empty_block=True) result = bc.is_valid() assert result is True
def test_blockchain_with_some_transactions_ok(self): bc = make_blockchain(days=5, tx_per_block=1) result = bc.is_valid() assert result is True
def test_validated_blockchain_ok(self): bc = make_blockchain() result = bc.is_valid() assert result is True
def test_raise_error_for_existing_guzi_creation(self): bc = make_blockchain(days=1, close_last_block=False) tx = bc.make_daily_guzis_tx(date(2000, 1, 2)) with pytest.raises(GuziError): bc._add_transaction(tx)
def test_raise_error_for_existing_guzi_creation_bis(self): bc = make_blockchain() tx = bc.make_daily_guzis_tx(date(2000, 1, 1)) with pytest.raises(GuziError): bc._add_transaction(tx)
def test_raise_error_if_transaction_is_sealed_in_old_block(self): bc = make_blockchain(days=2) with pytest.raises(NotRemovableTransactionError): bc.refuse_transaction(bc[1].transactions[0])
def test_raise_error_if_transaction_is_sealed(self): bc = make_blockchain(days=1) with pytest.raises(NotRemovableTransactionError): bc.refuse_transaction(bc.last_block().transactions[0])
def test_remove_tx_from_last_block(self): bc = make_blockchain(days=1, tx_per_block=1, close_last_block=False) assert len(bc.last_block().transactions) == 1 bc.refuse_transaction(bc.last_block().transactions[0]) assert len(bc.last_block().transactions) == 0
def test_create_1_guzibox_if_total_is_0(self): bc = make_blockchain(end_with_empty_block=True) bc.last_block().total = 0 tx = bc.make_daily_guziboxes_tx(date(2000, 1, 2)) assert tx.guzis_positions == [[[date(2000, 1, 2).isoformat()], [0]]]
def test_equal_itself(self): bc = make_blockchain() assert bc == bc
def test_raise_error_for_negative_total(self): bc = make_blockchain(end_with_empty_block=True) bc.last_block().total = -125 with pytest.raises(InvalidBlockchainError): bc._get_guzis_amount()