1from datetime import date, datetime
2
3from hypothesis import given, strategies as st
4import numpy as np
5import pytest
6
7from pandas._libs.tslibs import ccalendar
8
9import pandas as pd
10
11
12@pytest.mark.parametrize(
13    "date_tuple,expected",
14    [
15        ((2001, 3, 1), 60),
16        ((2004, 3, 1), 61),
17        ((1907, 12, 31), 365),  # End-of-year, non-leap year.
18        ((2004, 12, 31), 366),  # End-of-year, leap year.
19    ],
20)
21def test_get_day_of_year_numeric(date_tuple, expected):
22    assert ccalendar.get_day_of_year(*date_tuple) == expected
23
24
25def test_get_day_of_year_dt():
26    dt = datetime.fromordinal(1 + np.random.randint(365 * 4000))
27    result = ccalendar.get_day_of_year(dt.year, dt.month, dt.day)
28
29    expected = (dt - dt.replace(month=1, day=1)).days + 1
30    assert result == expected
31
32
33@pytest.mark.parametrize(
34    "input_date_tuple, expected_iso_tuple",
35    [
36        [(2020, 1, 1), (2020, 1, 3)],
37        [(2019, 12, 31), (2020, 1, 2)],
38        [(2019, 12, 30), (2020, 1, 1)],
39        [(2009, 12, 31), (2009, 53, 4)],
40        [(2010, 1, 1), (2009, 53, 5)],
41        [(2010, 1, 3), (2009, 53, 7)],
42        [(2010, 1, 4), (2010, 1, 1)],
43        [(2006, 1, 1), (2005, 52, 7)],
44        [(2005, 12, 31), (2005, 52, 6)],
45        [(2008, 12, 28), (2008, 52, 7)],
46        [(2008, 12, 29), (2009, 1, 1)],
47    ],
48)
49def test_dt_correct_iso_8601_year_week_and_day(input_date_tuple, expected_iso_tuple):
50    result = ccalendar.get_iso_calendar(*input_date_tuple)
51    expected_from_date_isocalendar = date(*input_date_tuple).isocalendar()
52    assert result == expected_from_date_isocalendar
53    assert result == expected_iso_tuple
54
55
56@given(
57    st.datetimes(
58        min_value=pd.Timestamp.min.to_pydatetime(warn=False),
59        max_value=pd.Timestamp.max.to_pydatetime(warn=False),
60    )
61)
62def test_isocalendar(dt):
63    expected = dt.isocalendar()
64    result = ccalendar.get_iso_calendar(dt.year, dt.month, dt.day)
65    assert result == expected
66