File: test_rank_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (75 lines) | stat: -rw-r--r-- 2,798 bytes parent folder | download
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
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from devtools_testutils import AzureRecordedTestCase
from devtools_testutils.aio import recorded_by_proxy_async
import personalizer_helpers_async

import personalizer_helpers


class TestRank(AzureRecordedTestCase):
    @personalizer_helpers.PersonalizerPreparer()
    @recorded_by_proxy_async
    async def test_rank_with_no_context_features(self, **kwargs):
        personalizer_endpoint = kwargs.pop("personalizer_endpoint_single_slot")
        personalizer_api_key = kwargs.pop("personalizer_api_key_single_slot")
        client = personalizer_helpers_async.create_async_personalizer_client(
            personalizer_endpoint, personalizer_api_key
        )
        actions = [
            {
                "id": "Person",
                "features": [
                    {"videoType": "documentary", "videoLength": 35, "director": "CarlSagan"},
                    {"mostWatchedByAge": "30-35"},
                ],
            }
        ]
        request = {"actions": actions}
        await client.rank(request)

    @personalizer_helpers.PersonalizerPreparer()
    @recorded_by_proxy_async
    async def test_rank_with_context_features(self, **kwargs):
        personalizer_endpoint = kwargs.pop("personalizer_endpoint_single_slot")
        personalizer_api_key = kwargs.pop("personalizer_api_key_single_slot")
        client = personalizer_helpers_async.create_async_personalizer_client(
            personalizer_endpoint, personalizer_api_key
        )
        actions = [
            {
                "id": "Video1",
                "features": [
                    {"videoType": "documentary", "videoLength": 35, "director": "CarlSagan"},
                    {"mostWatchedByAge": "30-35"},
                ],
            },
            {
                "id": "Video2",
                "features": [
                    {"videoType": "documentary", "videoLength": 35, "director": "CarlSagan"},
                    {"mostWatchedByAge": "40-45"},
                ],
            },
        ]
        context_features = [
            {"currentContext": {"day": "tuesday", "time": "night", "weather": "rainy"}},
            {
                "userContext": {
                    "payingUser": True,
                    "favoriteGenre": "documentary",
                    "hoursOnSite": 0.12,
                    "lastWatchedType": "movie",
                },
            },
        ]
        event_id = "123456789"
        request = {
            "eventId": event_id,
            "actions": actions,
            "contextFeatures": context_features,
            "excludedActions": ["Video1"],
        }
        await client.rank(request)