Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yes, this is a unit test file for the EinsteinTrade class. It includes test cases for buying, selling, getting stock prices, and placing orders. The tests use the unittest module and the patch decorator from the unittest.mock module. The test cases check if the trade app functions correctly by comparing expected values with actual values. Overall, it looks like a comprehensive test suite for the EinsteinTrade class. #16

Open
kalihatreese opened this issue Aug 28, 2023 · 1 comment

Comments

@kalihatreese
Copy link
Owner

import requests
import unittest
from unittest.mock import patch
from einstein_trade import EinsteinTrade

class TestEinsteinTrade(unittest.TestCase):
def setUp(self):
self.trade_app = EinsteinTrade()

def test_buy(self):
    self.trade_app.balance = 1000
    self.trade_app.buy("AAPL", 10, 142.82)
    self.assertEqual(self.trade_app.portfolio["AAPL"], 10)
    self.assertEqual(self.trade_app.balance, 1000 - (10 * 142.82))

def test_sell(self):
    self.trade_app.balance = 2000
    self.trade_app.portfolio["AAPL"] = 10
    self.trade_app.sell("AAPL", 3, 142.82)
    self.assertEqual(self.trade_app.portfolio["AAPL"], 7)
    self.assertEqual(self.trade_app.balance, 2000 + (3 * 142.82))

@patch("einstein_trade.requests.get")
def test_get_stock_price(self, mock_get):
    mock_response = mock_get.return_value
    mock_response.status_code = 200
    mock_response.json.return_value = {
        "Global Quote": {
            "05. price": "142.82"
        }
    }
    price = self.trade_app.get_stock_price("AAPL")
    self.assertEqual(price, 142.82)

def test_place_order_buy(self):
    self.trade_app.get_stock_price = lambda symbol: 142.82
    self.trade_app.buy = lambda symbol, quantity, price: None
    self.trade_app.place_order("AAPL", 10, "buy")
    self.assertEqual(self.trade_app.portfolio["AAPL"], 10)

def test_place_order_sell(self):
    self.trade_app.get_stock_price = lambda symbol: 142.82
    self.trade_app.sell = lambda symbol, quantity, price: None
    self.trade_app.portfolio["AAPL"] = 10
    self.trade_app.place_order("AAPL", 3, "sell")
    self.assertEqual(self.trade_app.portfolio["AAPL"], 7)

if name == "main":
unittest.main()

@kalihatreese
Copy link
Owner Author

Yes, I see that you have written unit tests for the EinsteinTrade class. It's great to see that you're ensuring the functionality of your code. Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant