Added unit test to the Yahoo Finance sensor (#3943)

pull/3966/head
Marcelo Moreira de Mello 2016-10-20 14:14:50 -04:00 committed by Johann Kellerman
parent 2b37b4251b
commit fee01fcccc
3 changed files with 126 additions and 1 deletions

View File

@ -283,7 +283,6 @@ omit =
homeassistant/components/sensor/vasttrafik.py
homeassistant/components/sensor/worldclock.py
homeassistant/components/sensor/xbox_live.py
homeassistant/components/sensor/yahoo_finance.py
homeassistant/components/sensor/yweather.py
homeassistant/components/switch/acer_projector.py
homeassistant/components/switch/anel_pwrctrl.py

View File

@ -0,0 +1,41 @@
"""The tests for the Yahoo Finance platform."""
import json
import unittest
from unittest.mock import patch
import homeassistant.components.sensor as sensor
from homeassistant.bootstrap import setup_component
from tests.common import (
get_test_home_assistant, load_fixture, assert_setup_component)
VALID_CONFIG = {
'platform': 'yahoo_finance',
'symbol': 'YHOO'
}
class TestYahooFinanceSetup(unittest.TestCase):
"""Test the Yahoo Finance platform."""
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.config = VALID_CONFIG
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@patch('yahoo_finance.Base._request',
return_value=json.loads(load_fixture('yahoo_finance.json')))
def test_default_setup(self, m): # pylint: disable=invalid-name
"""Test the default setup."""
with assert_setup_component(1, sensor.DOMAIN):
assert setup_component(self.hass, sensor.DOMAIN, {
'sensor': VALID_CONFIG})
state = self.hass.states.get('sensor.yahoo_stock')
self.assertEqual("41.69", state.attributes.get('open'))
self.assertEqual("41.79", state.attributes.get('prev_close'))
self.assertEqual("YHOO", state.attributes.get('unit_of_measurement'))

85
tests/fixtures/yahoo_finance.json vendored Normal file
View File

@ -0,0 +1,85 @@
{
"symbol": "YHOO",
"Ask": "42.42",
"AverageDailyVolume": "11397600",
"Bid": "42.41",
"AskRealtime": null,
"BidRealtime": null,
"BookValue": "29.83",
"Change_PercentChange": "+0.62 - +1.48%",
"Change": "+0.62",
"Commission": null,
"Currency": "USD",
"ChangeRealtime": null,
"AfterHoursChangeRealtime": null,
"DividendShare": null,
"LastTradeDate": "10/18/2016",
"TradeDate": null,
"EarningsShare": "-5.18",
"ErrorIndicationreturnedforsymbolchangedinvalid": null,
"EPSEstimateCurrentYear": "0.49",
"EPSEstimateNextYear": "0.57",
"EPSEstimateNextQuarter": "0.17",
"DaysLow": "41.86",
"DaysHigh": "42.42",
"YearLow": "26.15",
"YearHigh": "44.92",
"HoldingsGainPercent": null,
"AnnualizedGain": null,
"HoldingsGain": null,
"HoldingsGainPercentRealtime": null,
"HoldingsGainRealtime": null,
"MoreInfo": null,
"OrderBookRealtime": null,
"MarketCapitalization": "40.37B",
"MarketCapRealtime": null,
"EBITDA": "151.08M",
"ChangeFromYearLow": "16.26",
"PercentChangeFromYearLow": "+62.18%",
"LastTradeRealtimeWithTime": null,
"ChangePercentRealtime": null,
"ChangeFromYearHigh": "-2.51",
"PercebtChangeFromYearHigh": "-5.59%",
"LastTradeWithTime": "9:41am - <b>42.41</b>",
"LastTradePriceOnly": "42.41",
"HighLimit": null,
"LowLimit": null,
"DaysRange": "41.86 - 42.42",
"DaysRangeRealtime": null,
"FiftydayMovingAverage": "43.16",
"TwoHundreddayMovingAverage": "39.26",
"ChangeFromTwoHundreddayMovingAverage": "3.15",
"PercentChangeFromTwoHundreddayMovingAverage": "+8.03%",
"ChangeFromFiftydayMovingAverage": "-0.75",
"PercentChangeFromFiftydayMovingAverage": "-1.74%",
"Name": "Yahoo! Inc.",
"Notes": null,
"Open": "41.69",
"PreviousClose": "41.79",
"PricePaid": null,
"ChangeinPercent": "+1.48%",
"PriceSales": "8.13",
"PriceBook": "1.40",
"ExDividendDate": null,
"PERatio": null,
"DividendPayDate": null,
"PERatioRealtime": null,
"PEGRatio": "-24.57",
"PriceEPSEstimateCurrentYear": "86.55",
"PriceEPSEstimateNextYear": "74.40",
"Symbol": "YHOO",
"SharesOwned": null,
"ShortRatio": "5.05",
"LastTradeTime": "9:41am",
"TickerTrend": null,
"OneyrTargetPrice": "43.64",
"Volume": "946198",
"HoldingsValue": null,
"HoldingsValueRealtime": null,
"YearRange": "26.15 - 44.92",
"DaysValueChange": null,
"DaysValueChangeRealtime": null,
"StockExchange": "NMS",
"DividendYield": null,
"PercentChange": "+1.48%"
}