Write a test for your Bitcoin program (from the API topic) that converts a number of Bitcoin to their value in US Dollars. Structure your
Write a test for your Bitcoin program (from the API topic) that converts a number of Bitcoin to their value in US Dollars.
Structure your program so you don't need to mock user input.
Structure your program so it's possible to isolate and then mock the API call.
Mock the API call by providing a mock JSON response. Assert that your program calculates the correct value in dollars.
import requests url = https://api.coindesk.com/v1/bpi/currentprice.json" def main(): bitcoin get_bitcoin_amount() dollars convert_bitcoin_to_dollars (bitcoin) display_results (bitcoin, dollars) def get_bitcoin_amount(): while True: try: if bitcoin - float(input('Enter the number of bitcoin: ')) if bitcoin > 0: return bitcoin else: print(" Please enter a number greater than e') except ValueError: def convert_bitcoin_to_dollars (bitcoin): rate_json = get_bitcoin_data() exchange_rate= extract_rate(rate_json) print('Enter a positive number.') bitcoin = exchange rate * bitcoin return bitcoin # this function will be mocked i def get bitcoin_data(): name main() return requests.get(url).json() def extract_rate(rate_json): return rate_json["bpi']['USD']['rate_float'] def display_results (bitcoin, dollars): print (f {bitcoin) bitcoin is equal to ${dollars}') main
Step by Step Solution
3.46 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
To write a test for your Bitcoin program you can use the unittest library in Python to create a test ...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started