コード例 #1
0
ファイル: test_config_flow.py プロジェクト: Claret-Srl/core
async def test_flow_entry_created_user_input_faulty() -> None:
    """Test that create data from user input and are faulty.

    Test when the form should show when user puts faulty location
    in the config gui. Then the form should show with error
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}

    # Test that entry created when user_input name not exists
    with patch.object(flow, "_check_location", return_value=True), patch.object(
        flow, "_show_config_form", return_value=None
    ) as config_form, patch.object(
        flow, "_name_in_configuration_exists", return_value=False
    ), patch.object(
        flow, "_homeassistant_location_exists", return_value=False
    ), patch.object(
        config_flow,
        "smhi_locations",
        return_value={"test": "something", "name_exist": "config"},
    ), patch.object(
        flow, "_check_location", return_value=False
    ):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
コード例 #2
0
async def test_flow_show_form_name_exists() -> None:
    """Test show form if name already exists.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass
    test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}
    # Test show form when Home Assistant config exists and
    # home is already configured, then new config is allowed
    with patch.object(flow, "_show_config_form",
                      return_value=None) as config_form, patch.object(
                          flow,
                          "_name_in_configuration_exists",
                          return_value=True), patch.object(
                              config_flow,
                              "smhi_locations",
                              return_value={
                                  "test": "something",
                                  "name_exist": "config"
                              },
                          ), patch.object(flow,
                                          "_check_location",
                                          return_value=True):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
コード例 #3
0
async def test_name_in_configuration_exists() -> None:
    """Test if home location exists in configuration."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    # Test exists
    hass.config.location_name = "Home"
    hass.config.latitude = 17.8419
    hass.config.longitude = 59.3262

    # Check not exists
    with patch.object(
            config_flow,
            "smhi_locations",
            return_value={
                "test": "something",
                "test2": "something else"
            },
    ):

        assert flow._name_in_configuration_exists("no_exist_name") is False

    # Check exists
    with patch.object(
            config_flow,
            "smhi_locations",
            return_value={
                "test": "something",
                "name_exist": "config"
            },
    ):

        assert flow._name_in_configuration_exists("name_exist") is True
コード例 #4
0
async def test_flow_show_form_name_exists() -> None:
    """Test show form if name already exists.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass
    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}
    # Test show form when home assistant config exists and
    # home is already configured, then new config is allowed
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=True), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
コード例 #5
0
async def test_name_in_configuration_exists() -> None:
    """Test if home location exists in configuration."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    # Test exists
    hass.config.location_name = 'Home'
    hass.config.latitude = 17.8419
    hass.config.longitude = 59.3262

    # Check not exists
    with patch.object(config_flow, 'smhi_locations',
                      return_value={
                          'test': 'something', 'test2': 'something else'
                          }):

        assert flow._name_in_configuration_exists('no_exist_name') is False

    # Check exists
    with patch.object(config_flow, 'smhi_locations',
                      return_value={
                          'test': 'something', 'name_exist': 'config'
                          }):

        assert flow._name_in_configuration_exists('name_exist') is True
コード例 #6
0
async def test_flow_entry_created_user_input_faulty() -> None:
    """Test that create data from user input and are faulty.

    Test when the form should show when user puts faulty location
    in the config gui. Then the form should show with error
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}

    # Test that entry created when user_input name not exists
    with \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)), \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=False), \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(False)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(False)):

        await flow.async_step_user(user_input=test_data)

        assert len(config_form.mock_calls) == 1
        assert len(flow._errors) == 1
コード例 #7
0
async def test_flow_entry_created_from_user_input() -> None:
    """Test that create data from user input.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {'name': 'home', CONF_LONGITUDE: '0', CONF_LATITUDE: '0'}

    # Test that entry created when user_input name not exists
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_name_in_configuration_exists',
                     return_value=False), \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(False)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }), \
        patch.object(flow, '_check_location',
                     return_value=mock_coro(True)):

        result = await flow.async_step_user(user_input=test_data)

        assert result['type'] == 'create_entry'
        assert result['data'] == test_data
        assert not config_form.mock_calls
コード例 #8
0
async def test_flow_entry_created_from_user_input() -> None:
    """Test that create data from user input.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    test_data = {"name": "home", CONF_LONGITUDE: "0", CONF_LATITUDE: "0"}

    # Test that entry created when user_input name not exists
    with patch.object(flow, "_show_config_form",
                      return_value=None) as config_form, patch.object(
                          flow,
                          "_name_in_configuration_exists",
                          return_value=False), patch.object(
                              flow,
                              "_homeassistant_location_exists",
                              return_value=False), patch.object(
                                  config_flow,
                                  "smhi_locations",
                                  return_value={
                                      "test": "something",
                                      "name_exist": "config"
                                  },
                              ), patch.object(flow,
                                              "_check_location",
                                              return_value=True):

        result = await flow.async_step_user(user_input=test_data)

        assert result["type"] == "create_entry"
        assert result["data"] == test_data
        assert not config_form.mock_calls
コード例 #9
0
ファイル: test_config_flow.py プロジェクト: Claret-Srl/core
async def test_show_config_form_default_values() -> None:
    """Test show configuration form."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    result = await flow._show_config_form(name="test", latitude="65", longitude="17")

    assert result["type"] == "form"
    assert result["step_id"] == "user"
コード例 #10
0
async def test_show_config_form() -> None:
    """Test show configuration form."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    result = await flow._show_config_form()

    assert result["type"] == "form"
    assert result["step_id"] == "user"
コード例 #11
0
async def test_show_config_form() -> None:
    """Test show configuration form."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    result = await flow._show_config_form()

    assert result['type'] == 'form'
    assert result['step_id'] == 'user'
コード例 #12
0
async def test_check_location_correct() -> None:
    """Test check location when correct input."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with patch.object(config_flow.aiohttp_client,
                      "async_get_clientsession"), patch.object(
                          SmhiApi, "async_get_forecast", return_value=None):

        assert await flow._check_location("58", "17") is True
コード例 #13
0
async def test_show_config_form_default_values() -> None:
    """Test show configuration form."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    result = await flow._show_config_form(
        name="test", latitude='65', longitude='17')

    assert result['type'] == 'form'
    assert result['step_id'] == 'user'
コード例 #14
0
ファイル: test_config_flow.py プロジェクト: Claret-Srl/core
async def test_check_location_faulty() -> None:
    """Test check location when faulty input."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with patch.object(
        config_flow.aiohttp_client, "async_get_clientsession"
    ), patch.object(SmhiApi, "async_get_forecast", side_effect=SmhiForecastException()):

        assert await flow._check_location("58", "17") is False
コード例 #15
0
async def test_check_location_correct() -> None:
    """Test check location when correct input."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with \
        patch.object(config_flow.aiohttp_client, 'async_get_clientsession'),\
        patch.object(SmhiApi, 'async_get_forecast',
                     return_value=mock_coro()):

        assert await flow._check_location('58', '17') is True
コード例 #16
0
async def test_flow_with_home_location(hass) -> None:
    """Test config flow .

    Tests the flow when a default location is configured
    then it should return a form with default values
    """
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with patch.object(flow, "_check_location", return_value=True):
        hass.config.location_name = "Home"
        hass.config.latitude = 17.8419
        hass.config.longitude = 59.3262

        result = await flow.async_step_user()
        assert result["type"] == "form"
        assert result["step_id"] == "user"
コード例 #17
0
async def test_flow_with_home_location(hass) -> None:
    """Test config flow .

    Tests the flow when a default location is configured
    then it should return a form with default values
    """
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    with patch.object(flow, '_check_location',
                      return_value=mock_coro(True)):
        hass.config.location_name = 'Home'
        hass.config.latitude = 17.8419
        hass.config.longitude = 59.3262

        result = await flow.async_step_user()
        assert result['type'] == 'form'
        assert result['step_id'] == 'user'
コード例 #18
0
async def test_homeassistant_location_exists() -> None:
    """Test if Home Assistant location exists it should return True."""
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass
    with patch.object(flow, "_check_location", return_value=True):
        # Test exists
        hass.config.location_name = "Home"
        hass.config.latitude = 17.8419
        hass.config.longitude = 59.3262

        assert await flow._homeassistant_location_exists() is True

        # Test not exists
        hass.config.location_name = None
        hass.config.latitude = 0
        hass.config.longitude = 0

        assert await flow._homeassistant_location_exists() is False
コード例 #19
0
async def test_flow_show_form() -> None:
    """Test show form scenarios first time.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    # Test show form when Home Assistant config exists and
    # home is already configured, then new config is allowed
    with patch.object(flow, "_show_config_form",
                      return_value=None) as config_form, patch.object(
                          flow,
                          "_homeassistant_location_exists",
                          return_value=True), patch.object(
                              config_flow,
                              "smhi_locations",
                              return_value={
                                  "test": "something",
                                  "name_exist": "config"
                              },
                          ):
        await flow.async_step_user()
        assert len(config_form.mock_calls) == 1

    # Test show form when Home Assistant config not and
    # home is not configured
    with patch.object(flow, "_show_config_form",
                      return_value=None) as config_form, patch.object(
                          flow,
                          "_homeassistant_location_exists",
                          return_value=False), patch.object(
                              config_flow,
                              "smhi_locations",
                              return_value={
                                  "test": "something",
                                  "name_exist": "config"
                              },
                          ):

        await flow.async_step_user()
        assert len(config_form.mock_calls) == 1
コード例 #20
0
async def test_flow_show_form() -> None:
    """Test show form scenarios first time.

    Test when the form should show when no configurations exists
    """
    hass = Mock()
    flow = config_flow.SmhiFlowHandler()
    flow.hass = hass

    # Test show form when home assistant config exists and
    # home is already configured, then new config is allowed
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(True)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }):
        await flow.async_step_user()
        assert len(config_form.mock_calls) == 1

    # Test show form when home assistant config not and
    # home is not configured
    with \
        patch.object(flow, '_show_config_form',
                     return_value=mock_coro()) as config_form, \
        patch.object(flow, '_homeassistant_location_exists',
                     return_value=mock_coro(False)), \
        patch.object(config_flow, 'smhi_locations',
                     return_value={
                         'test': 'something', 'name_exist': 'config'
                         }):

        await flow.async_step_user()
        assert len(config_form.mock_calls) == 1