1from __future__ import unicode_literals
2
3import pytest
4import astral
5from datetime import datetime, timedelta
6import pytz
7from astral import sun
8
9
10def _next_event(obs: astral.Observer, dt: datetime, event: str):
11    for offset in range(0, 365):
12        newdate = dt + timedelta(days=offset)
13        try:
14            t = getattr(sun, event)(date=newdate, observer=obs)
15            return t
16        except ValueError:
17            pass
18    assert False, "Should be unreachable"  # pragma: no cover
19
20
21def test_NorwaySunUp():
22    """Test location in Norway where the sun doesn't set in summer."""
23    june = datetime(2019, 6, 5, tzinfo=pytz.utc)
24    obs = astral.Observer(69.6, 18.8, 0.0)
25
26    with pytest.raises(ValueError):
27        sun.sunrise(obs, june)
28    with pytest.raises(ValueError):
29        sun.sunset(obs, june)
30
31    # Find the next sunset and sunrise:
32    next_sunrise = _next_event(obs, june, "sunrise")
33    next_sunset = _next_event(obs, june, "sunset")
34
35    assert next_sunset < next_sunrise
36