Esempio n. 1
0
def test_sensor_make_api_request():
    """Test making API requests."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.requests") as patched_requests:
        patched_requests.get.return_value = {}
        with patch("openerz_api.main.datetime") as patched_time:
            patched_time.now.return_value = mock_datetime
            test_openerz = OpenERZConnector(zip_code, waste_type)
            test_openerz.end_date = mock_datetime.replace(year=2020,
                                                          month=1,
                                                          day=10)
            test_openerz.make_api_request()

            expected_headers = {"accept": "application/json"}
            expected_url = "http://openerz.metaodi.ch/api/calendar/glass.json"
            expected_payload = {
                "zip": 1234,
                "start": "2019-12-10",
                "end": "2020-01-10",
                "offset": 0,
                "limit": 0,
                "lang": "en",
                "sort": "date",
            }
            used_args, used_kwargs = patched_requests.get.call_args_list[0]
            assert used_args[0] == expected_url
            assertDictEqual(used_kwargs["headers"], expected_headers)
            assertDictEqual(used_kwargs["params"], expected_payload)
Esempio n. 2
0
def test_sensor_parse_api_response_wrong_type():
    """Test handling unexpected waste type in API response."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)
        test_openerz.end_date = mock_datetime.replace(year=2020,
                                                      month=1,
                                                      day=10)

        response_data = {
            "_metadata": {
                "total_count": 1
            },
            "result": [{
                "zip": 1234,
                "type": "metal",
                "date": "2020-01-10"
            }],
        }

        with LogCapture() as captured_logs:
            test_openerz.last_api_response = MockAPIResponse(
                True, 200, response_data)

            test_pickup_date = test_openerz.parse_api_response()
            assert test_pickup_date is None
            captured_logs.check_present((
                "openerz_api.main",
                "WARNING",
                "Either zip or waste type does not match the ones specified in the configuration.",
            ))
Esempio n. 3
0
def test_sensor_parse_api_response_ok():
    """Test whether API response is parsed correctly."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)
        test_openerz.end_date = mock_datetime.replace(year=2020,
                                                      month=1,
                                                      day=10)

        response_data = {
            "_metadata": {
                "total_count": 1
            },
            "result": [{
                "zip": 1234,
                "type": "glass",
                "date": "2020-01-10"
            }],
        }
        test_openerz.last_api_response = MockAPIResponse(
            True, 200, response_data)

        test_pickup_date = test_openerz.parse_api_response()
        assert test_pickup_date == "2020-01-10"
Esempio n. 4
0
def test_sensor_find_end_date():
    """Test whether end date is correctly set."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)

        test_openerz.find_end_date()

        expected_end_date = datetime(year=2020,
                                     month=1,
                                     day=10,
                                     hour=11,
                                     minute=15,
                                     second=0,
                                     microsecond=0)
        assert test_openerz.end_date == expected_end_date
Esempio n. 5
0
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the sensor platform."""
    api_connector = OpenERZConnector(config[CONF_ZIP], config[CONF_WASTE_TYPE])
    add_entities([OpenERZSensor(api_connector, config.get(CONF_NAME))], True)
Esempio n. 6
0
def test_sensor_update_start_date():
    """Test whether start date is updated correctly."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)

        patched_time.now.return_value = mock_datetime.replace(day=11)
        test_openerz.update_start_date()

        expected_start_date = datetime(year=2019,
                                       month=12,
                                       day=11,
                                       hour=11,
                                       minute=15,
                                       second=0,
                                       microsecond=0)
        assert test_openerz.start_date == expected_start_date
Esempio n. 7
0
def test_sensor_make_api_request_connection_error():
    """Test making API requests."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.requests.get") as patched_get:
        patched_get.side_effect = RequestException("Connection timed out")
        with patch("openerz_api.main.datetime") as patched_time:
            patched_time.now.return_value = mock_datetime
            test_openerz = OpenERZConnector(zip_code, waste_type)
            test_openerz.end_date = mock_datetime.replace(year=2020,
                                                          month=1,
                                                          day=10)
            with LogCapture() as captured_logs:
                test_openerz.make_api_request()
                captured_logs.check_present(
                    (
                        "openerz_api.main",
                        "ERROR",
                        "RequestException while making request to OpenERZ: Connection timed out",
                    ),
                    order_matters=False,
                )
Esempio n. 8
0
def test_init():
    """Test whether all values initialized properly."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime

        test_openerz = OpenERZConnector(zip_code, waste_type)

        assert test_openerz.zip == 1234
        assert test_openerz.waste_type == "glass"
        assert test_openerz.start_date == mock_datetime
        assert test_openerz.end_date is None
        assert test_openerz.last_api_response is None
Esempio n. 9
0
def test_sensor_parse_api_response_not_ok():
    """Test handling of an erroneous API response."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)
        test_openerz.end_date = mock_datetime.replace(year=2020,
                                                      month=1,
                                                      day=10)

        response_data = {"result": [{}]}

        with LogCapture() as captured_logs:
            test_openerz.last_api_response = MockAPIResponse(
                False, 404, response_data)

            test_pickup_date = test_openerz.parse_api_response()
            assert test_pickup_date is None
            captured_logs.check_present((
                "openerz_api.main",
                "WARNING",
                "Last request to OpenERZ was not successful. Status code: 404",
            ))
Esempio n. 10
0
def test_sensor_parse_api_response_no_data():
    """Test whether API response is parsed correctly when no data returned."""
    mock_datetime, zip_code, waste_type = setup_method()
    with patch("openerz_api.main.datetime") as patched_time:
        patched_time.now.return_value = mock_datetime
        test_openerz = OpenERZConnector(zip_code, waste_type)
        test_openerz.end_date = mock_datetime.replace(year=2020,
                                                      month=1,
                                                      day=10)

        response_data = {"_metadata": {"total_count": 0}, "result": []}

        with LogCapture() as captured_logs:
            test_openerz.last_api_response = MockAPIResponse(
                True, 200, response_data)

            test_pickup_date = test_openerz.parse_api_response()
            assert test_pickup_date is None
            captured_logs.check_present((
                "openerz_api.main",
                "WARNING",
                "Request to OpenERZ returned no results.",
            ))
Esempio n. 11
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the sensor platform."""
    api_connector = OpenERZConnector(config[CONF_ZIP], config[CONF_WASTE_TYPE])
    add_entities([OpenERZSensor(api_connector, config.get(CONF_NAME))], True)
Esempio n. 12
0
from openerz_api.main import OpenERZConnector

ZIP = 8001
WASTE = "paper"

# initialize the connector given zip code and waste type
connector = OpenERZConnector(zip_code=ZIP, waste_type=WASTE)

# retrieve the next pick-up date within a period of choice
next_pickup = connector.find_next_pickup(day_offset=15)

print(f"Next pickup date for {WASTE} in {ZIP} area: {next_pickup}")