|
| 1 | +"""Tests for the hits metrics module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | +from responses import matchers |
| 6 | + |
| 7 | +from posit import connect |
| 8 | + |
| 9 | +from ..api import load_mock |
| 10 | + |
| 11 | + |
| 12 | +class TestHitsFetch: |
| 13 | + @responses.activate |
| 14 | + def test_fetch(self): |
| 15 | + # Set up mock response |
| 16 | + mock_get = responses.get( |
| 17 | + "https://connect.example/__api__/v1/instrumentation/content/hits", |
| 18 | + json=load_mock("v1/instrumentation/content/hits.json"), |
| 19 | + ) |
| 20 | + |
| 21 | + # Create client with required version for hits API |
| 22 | + c = connect.Client("https://connect.example", "12345") |
| 23 | + c._ctx.version = "2025.04.0" |
| 24 | + |
| 25 | + # Fetch hits |
| 26 | + hits = list(c.metrics.hits.fetch()) |
| 27 | + |
| 28 | + # Verify request was made |
| 29 | + assert mock_get.call_count == 1 |
| 30 | + |
| 31 | + # Verify results |
| 32 | + assert len(hits) == 2 |
| 33 | + assert hits[0]["id"] == 1001 |
| 34 | + assert hits[0]["content_guid"] == "bd1d2285-6c80-49af-8a83-a200effe3cb3" |
| 35 | + assert hits[0]["timestamp"] == "2025-05-01T10:00:00-05:00" |
| 36 | + assert hits[0]["data"]["path"] == "/dashboard" |
| 37 | + |
| 38 | + @responses.activate |
| 39 | + def test_fetch_with_params(self): |
| 40 | + # Set up mock response |
| 41 | + mock_get = responses.get( |
| 42 | + "https://connect.example/__api__/v1/instrumentation/content/hits", |
| 43 | + json=load_mock("v1/instrumentation/content/hits.json"), |
| 44 | + match=[ |
| 45 | + matchers.query_param_matcher( |
| 46 | + { |
| 47 | + "from": "2025-05-01T00:00:00Z", |
| 48 | + "to": "2025-05-02T00:00:00Z", |
| 49 | + } |
| 50 | + ), |
| 51 | + ], |
| 52 | + ) |
| 53 | + |
| 54 | + # Create client with required version for hits API |
| 55 | + c = connect.Client("https://connect.example", "12345") |
| 56 | + c._ctx.version = "2025.04.0" |
| 57 | + |
| 58 | + # Fetch hits with parameters |
| 59 | + hits = list( |
| 60 | + c.metrics.hits.fetch(**{"from": "2025-05-01T00:00:00Z", "to": "2025-05-02T00:00:00Z"}) |
| 61 | + ) |
| 62 | + |
| 63 | + # Verify request was made with proper parameters |
| 64 | + assert mock_get.call_count == 1 |
| 65 | + |
| 66 | + # Verify results |
| 67 | + assert len(hits) == 2 |
| 68 | + |
| 69 | + |
| 70 | +class TestHitsFindBy: |
| 71 | + @responses.activate |
| 72 | + def test_find_by(self): |
| 73 | + # Set up mock response |
| 74 | + mock_get = responses.get( |
| 75 | + "https://connect.example/__api__/v1/instrumentation/content/hits", |
| 76 | + json=load_mock("v1/instrumentation/content/hits.json"), |
| 77 | + ) |
| 78 | + |
| 79 | + # Create client with required version for hits API |
| 80 | + c = connect.Client("https://connect.example", "12345") |
| 81 | + c._ctx.version = "2025.04.0" |
| 82 | + |
| 83 | + # Find hits by content_guid |
| 84 | + hit = c.metrics.hits.find_by(content_guid="bd1d2285-6c80-49af-8a83-a200effe3cb3") |
| 85 | + |
| 86 | + # Verify request was made |
| 87 | + assert mock_get.call_count == 1 |
| 88 | + |
| 89 | + # Verify results |
| 90 | + assert hit is not None |
| 91 | + assert hit["id"] == 1001 |
| 92 | + assert hit["content_guid"] == "bd1d2285-6c80-49af-8a83-a200effe3cb3" |
| 93 | + |
| 94 | + @responses.activate |
| 95 | + def test_find_by_not_found(self): |
| 96 | + # Set up mock response |
| 97 | + mock_get = responses.get( |
| 98 | + "https://connect.example/__api__/v1/instrumentation/content/hits", |
| 99 | + json=load_mock("v1/instrumentation/content/hits.json"), |
| 100 | + ) |
| 101 | + |
| 102 | + # Create client with required version for hits API |
| 103 | + c = connect.Client("https://connect.example", "12345") |
| 104 | + c._ctx.version = "2025.04.0" |
| 105 | + |
| 106 | + # Try to find hit with non-existent content_guid |
| 107 | + hit = c.metrics.hits.find_by(content_guid="non-existent-guid") |
| 108 | + |
| 109 | + # Verify request was made |
| 110 | + assert mock_get.call_count == 1 |
| 111 | + |
| 112 | + # Verify no result was found |
| 113 | + assert hit is None |
| 114 | + |
| 115 | + |
| 116 | +class TestHitsVersionRequirement: |
| 117 | + @responses.activate |
| 118 | + def test_version_requirement(self): |
| 119 | + # Create client with version that's too old |
| 120 | + c = connect.Client("https://connect.example", "12345") |
| 121 | + c._ctx.version = "2024.04.0" |
| 122 | + |
| 123 | + with pytest.raises(RuntimeError): |
| 124 | + h = c.metrics.hits |
0 commit comments