aboutsummaryrefslogtreecommitdiff
path: root/tests/models/prodege/test_survey_participation.py
blob: b85cc91b59e96c41ce7bba5e11e5240e975c1511 (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
from datetime import timezone, datetime, timedelta


class TestProdegeParticipation:

    def test_exclude(self):
        from generalresearch.models.prodege import ProdegePastParticipationType
        from generalresearch.models.prodege.survey import (
            ProdegePastParticipation,
            ProdegeUserPastParticipation,
        )

        now = datetime.now(tz=timezone.utc)
        pp = ProdegePastParticipation.from_api(
            {
                "participation_project_ids": [152677146, 152803285],
                "filter_type": "exclude",
                "in_past_days": 7,
                "participation_types": ["complete"],
            }
        )
        # User has no history, so is eligible
        assert pp.is_eligible([])

        # user abandoned. its a click, not complete, so he's eligible
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(hours=69), survey_id="152677146"
            )
        ]
        assert pp.is_eligible(upps)

        # user completes. ineligible
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(hours=69),
                survey_id="152677146",
                ext_status_code_1="1",
            )
        ]
        assert not pp.is_eligible(upps)

        # user completed. but too long ago
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(days=100),
                survey_id="152677146",
                ext_status_code_1="1",
            )
        ]
        assert pp.is_eligible(upps)

        # remove day filter, should be ineligble again
        pp = ProdegePastParticipation.from_api(
            {
                "participation_project_ids": [152677146, 152803285],
                "filter_type": "exclude",
                "in_past_days": 0,
                "participation_types": ["complete"],
            }
        )
        assert not pp.is_eligible(upps)

        # I almost forgot this.... a "complete" IS ALSO A "click"!!!
        pp = ProdegePastParticipation.from_api(
            {
                "participation_project_ids": [152677146, 152803285],
                "filter_type": "exclude",
                "in_past_days": 0,
                "participation_types": ["click"],
            }
        )
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(hours=69),
                survey_id="152677146",
                ext_status_code_1="1",
            )
        ]
        assert {
            ProdegePastParticipationType.COMPLETE,
            ProdegePastParticipationType.CLICK,
        } == upps[0].participation_types
        assert not pp.is_eligible(upps)

    def test_include(self):
        from generalresearch.models.prodege.survey import (
            ProdegePastParticipation,
            ProdegeUserPastParticipation,
        )

        now = datetime.now(tz=timezone.utc)
        pp = ProdegePastParticipation.from_api(
            {
                "participation_project_ids": [152677146, 152803285],
                "filter_type": "include",
                "in_past_days": 7,
                "participation_types": ["complete"],
            }
        )
        # User has no history, so is IN-eligible
        assert not pp.is_eligible([])

        # user abandoned. its a click, not complete, so he's INeligible
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(hours=69), survey_id="152677146"
            )
        ]
        assert not pp.is_eligible(upps)

        # user completes, eligible
        upps = [
            ProdegeUserPastParticipation(
                started=now - timedelta(hours=69),
                survey_id="152677146",
                ext_status_code_1="1",
            )
        ]
        assert pp.is_eligible(upps)