-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bitcoin.py
More file actions
39 lines (30 loc) · 1.07 KB
/
test_bitcoin.py
File metadata and controls
39 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import pytest
from unittest.mock import patch
from bitcoin import get_bitcoin_price, calculate_value
# Test 1: Verify the calculation logic
def test_calculate_value():
"""Ensure BTC to USD conversion math is correct."""
price = 50000.0
amount = 2.5
expected = 125000.0
assert calculate_value(amount, price) == expected
# Test 2: Verify handling of zero amount
def test_calculate_value_zero():
assert calculate_value(0, 50000.0) == 0.0
# Test 3: Mocking an API response
@patch('bitcoin.requests.get')
def test_get_bitcoin_price(mock_get):
"""Simulate a successful API response from CoinDesk or similar."""
# Mock the JSON return value
mock_get.return_value.json.return_value = {
"bpi": {"USD": {"rate_float": 62000.50}}
}
mock_get.return_value.status_code = 200
price = get_bitcoin_price()
assert price == 62000.50
# Test 4: Handling API failure
@patch('bitcoin.requests.get')
def test_get_price_api_error(mock_get):
mock_get.return_value.status_code = 404
with pytest.raises(ConnectionError):
get_bitcoin_price()