Skip to content

Commit ec9c153

Browse files
committed
Complete section: Test the application
1 parent b11860b commit ec9c153

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ source blockchain-env/bin/activate
77
```
88
pip install -r requirements.txt
99
```
10+
11+
**Run the tests**
12+
13+
14+
Make sure to activate the virtual environment.
15+
16+
17+
```
18+
python3 -m pytest backend/tests
19+
```

backend/blockchain/block.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
from backend.util.crypto_hash import crypto_hash
44

5+
GENESIS_DATA = {
6+
'timestamp': 1,
7+
'last_hash': 'genesis_last_hash',
8+
'hash': 'genesis_hash',
9+
'data': []
10+
}
11+
512
class Block:
613
"""
714
Block: a unit of storage.
@@ -41,8 +48,13 @@ def genesis():
4148
"""
4249
Generate the genesis block.
4350
"""
44-
return Block(1, 'genesis_last_hash', 'genesis_hash', [])
45-
51+
# return Block(
52+
# timestamp=GENESIS_DATA['timestamp'],
53+
# last_hash=GENESIS_DATA['last_hash'],
54+
# hash=GENESIS_DATA['hash'],
55+
# data=GENESIS_DATA['data']
56+
# )
57+
return Block(**GENESIS_DATA)
4658

4759
def main():
4860
genesis_block = Block.genesis()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from backend.blockchain.block import Block, GENESIS_DATA
2+
3+
def test_mine_block():
4+
last_block = Block.genesis()
5+
data = 'test-data'
6+
block = Block.mine_block(last_block, data)
7+
8+
assert isinstance(block, Block)
9+
assert block.data == data
10+
assert block.last_hash == last_block.hash
11+
12+
def test_genesis():
13+
genesis = Block.genesis()
14+
assert isinstance(genesis, Block)
15+
for key, value in GENESIS_DATA.items():
16+
assert getattr(genesis, key) == value
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from backend.blockchain.blockchain import Blockchain
2+
from backend.blockchain.block import GENESIS_DATA
3+
4+
def test_blockchain_instance():
5+
blockchain = Blockchain()
6+
assert blockchain.chain[0].hash == GENESIS_DATA['hash']
7+
8+
def test_add_block():
9+
blockchain = Blockchain()
10+
data = 'test-data'
11+
blockchain.add_block(data)
12+
13+
assert blockchain.chain[-1].data == data
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from backend.util.crypto_hash import crypto_hash
2+
3+
def test_crypto_hash():
4+
# It should create the same hash with arguments of different data types in
5+
# any order
6+
assert crypto_hash(1, [2], 'three') == crypto_hash('three', 1, [2])
7+
assert crypto_hash('foo') == 'b2213295d564916f89a6a42455567c87c3f480fcd7a1c15e220f17d7169a790b'

0 commit comments

Comments
 (0)