コード例 #1
0
class PortfolioValueTest(unittest.TestCase):
    def fake_current_prices(self):
        return {"IBM": 140.0, "HPQ": 32.0}

    def setUp(self):
        self.p = Portfolio()
        self.p.buy("IBM", 100, 120.0)
        self.p.buy("HPQ", 100, 30.0)
        self.p.current_prices = self.fake_current_prices

    def test_value(self):
        self.assertEqual(self.p.value(), 17200)
コード例 #2
0
class PortfolioValueTest(unittest.TestCase):
    def setUp(self):
        # Save the real requests, and install our fake.
        self.old_requests = portfolio.portfolio3.requests
        portfolio.portfolio3.requests = FakeRequests()

        self.p = Portfolio()
        self.p.buy("IBM", 100, 120.0)
        self.p.buy("HPQ", 100, 30.0)

    def test_value(self):
        self.assertEqual(self.p.value(), 17200)

    def tearDown(self):
        # Restore the real requests.
        portfolio.portfolio3.requests = self.old_requests
コード例 #3
0
class PortfolioValueTest(unittest.TestCase):
    def setUp(self):
        self.p = Portfolio()
        self.p.buy("IBM", 100, 120.0)
        self.p.buy("HPQ", 100, 30.0)

    def test_value(self):
        # Create a mock requests.get.
        with mock.patch("portfolio.portfolio3.requests.get") as req_get:

            # When called, it will return this value:
            req_get.return_value = SimpleNamespace(
                text="\nIBM,,,140\nHPQ,,,32\n")

            # Run the test!
            self.assertEqual(self.p.value(), 17200)

            # We can ask the mock what its arguments were.
            self.assertEqual(len(req_get.call_args_list), 1)
            called_url = req_get.call_args_list[0][0][0]
            self.assertIn("api.worldtradingdata.com/api/v1/stock", called_url)
            self.assertIn("symbol=HPQ,IBM", called_url)