aboutsummaryrefslogtreecommitdiff
path: root/tests/models/admin/test_report_request.py
blob: cf4c405e7c3fe59bfdd3a650e2a64a29515df94c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from datetime import timezone, datetime

import pandas as pd
import pytest
from pydantic import ValidationError


class TestReportRequest:
    def test_base(self, utc_60days_ago):
        from generalresearch.models.admin.request import (
            ReportRequest,
            ReportType,
        )

        rr = ReportRequest()

        assert isinstance(rr.start, datetime), "rr.start incorrect type"
        assert isinstance(rr.start_floor, datetime), "rr.start_floor incorrect type"

        assert rr.report_type == ReportType.POP_SESSION
        assert rr.start != rr.start_floor, "rr.start != rr.start_floor"
        assert rr.start_floor.tzinfo == timezone.utc, "rr.start_floor.tzinfo not utc"

        rr1 = ReportRequest.model_validate(
            {
                "start": datetime(
                    year=datetime.now().year,
                    month=1,
                    day=1,
                    hour=0,
                    minute=30,
                    second=25,
                    microsecond=35,
                    tzinfo=timezone.utc,
                ),
                "interval": "1h",
            }
        )

        assert isinstance(rr1.start, datetime), "rr1.start incorrect type"
        assert isinstance(rr1.start_floor, datetime), "rr1.start_floor incorrect type"

        rr2 = ReportRequest.model_validate(
            {
                "start": datetime(
                    year=datetime.now().year,
                    month=1,
                    day=1,
                    hour=6,
                    minute=30,
                    second=25,
                    microsecond=35,
                    tzinfo=timezone.utc,
                ),
                "interval": "1d",
            }
        )

        assert isinstance(rr2.start, datetime), "rr2.start incorrect type"
        assert isinstance(rr2.start_floor, datetime), "rr2.start_floor incorrect type"

        assert rr1.start != rr2.start, "rr1.start != rr2.start"
        assert rr1.start_floor == rr2.start_floor, "rr1.start_floor == rr2.start_floor"

        # datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc)
        # datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc)

        #  datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc) =
        #  ReportRequest(report_type=<ReportType.POP_SESSION: 'pop_session'>,
        #  index0='started', index1='product_id',
        #  start=datetime.datetime(2025, 7, 9, 0, 46, 23, 145756, tzinfo=datetime.timezone.utc),
        #  end=datetime.datetime(2025, 9, 7, 0, 46, 23, 149195, tzinfo=datetime.timezone.utc),
        #  interval='1h', include_open_bucket=True,
        #  start_floor=datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc)).start_floor

        #  datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc) =
        #  ReportRequest(report_type=<ReportType.POP_SESSION: 'pop_session'>,
        #  index0='started', index1='product_id',
        #  start=datetime.datetime(2025, 7, 9, 0, 46, 23, 145756, tzinfo=datetime.timezone.utc),
        #  end=datetime.datetime(2025, 9, 7, 0, 46, 23, 149267, tzinfo=datetime.timezone.utc),
        #  interval='1d', include_open_bucket=True,
        #  start_floor=datetime.datetime(2025, 7, 9, 0, 0, tzinfo=datetime.timezone.utc)).start_floor

    def test_start_end_range(self, utc_90days_ago, utc_30days_ago):
        from generalresearch.models.admin.request import ReportRequest

        with pytest.raises(expected_exception=ValidationError) as cm:
            ReportRequest.model_validate(
                {"start": utc_30days_ago, "end": utc_90days_ago}
            )

        with pytest.raises(expected_exception=ValidationError) as cm:
            ReportRequest.model_validate(
                {
                    "start": datetime(year=1990, month=1, day=1),
                    "end": datetime(year=1950, month=1, day=1),
                }
            )

    def test_start_end_range_tz(self):
        from generalresearch.models.admin.request import ReportRequest
        from zoneinfo import ZoneInfo

        pacific_tz = ZoneInfo("America/Los_Angeles")

        with pytest.raises(expected_exception=ValidationError) as cm:
            ReportRequest.model_validate(
                {
                    "start": datetime(year=2000, month=1, day=1, tzinfo=pacific_tz),
                    "end": datetime(year=2000, month=6, day=1, tzinfo=pacific_tz),
                }
            )

    def test_start_floor_naive(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()

        assert rr.start_floor_naive.tzinfo is None

    def test_end_naive(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()

        assert rr.end_naive.tzinfo is None

    def test_pd_interval(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()

        assert isinstance(rr.pd_interval, pd.Interval)

    def test_interval_timedelta(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()

        assert isinstance(rr.interval_timedelta, pd.Timedelta)

    def test_buckets(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()

        assert isinstance(rr.buckets(), pd.DatetimeIndex)

    def test_bucket_ranges(self):
        from generalresearch.models.admin.request import ReportRequest

        rr = ReportRequest()
        assert isinstance(rr.bucket_ranges(), list)

        rr = ReportRequest.model_validate(
            {
                "interval": "1d",
                "start": datetime(year=2000, month=1, day=1, tzinfo=timezone.utc),
                "end": datetime(year=2000, month=1, day=10, tzinfo=timezone.utc),
            }
        )

        assert len(rr.bucket_ranges()) == 10