def test_series_invalid_values(values): series = {"name": "test-name", "values": values} with pytest.raises( ValueError, match=re.escape(f"Invalid metric values: {values!r}") ): Series.unmarshal(series)
def test_metrics_results_unmarshal_with_currently_released(): data = { "metrics": [ { "buckets": ["2021-01-01"], "metric_name": "weekly_installed_base_by_channel", "series": [ {"currently_released": False, "name": "beta", "values": [62]}, {"currently_released": True, "name": "edge", "values": [38]}, ], "snap_id": "test-snap-id", "status": "OK", } ] } metrics_results = MetricsResults.unmarshal(data) assert metrics_results == MetricsResults( metrics=[ MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_installed_base_by_channel", buckets=["2021-01-01"], series=[ Series(name="beta", values=[62], currently_released=False), Series(name="edge", values=[38], currently_released=True), ], ) ] ) assert metrics_results.marshal() == data
def test_metrics_results_unmarshal_two_buckets(): data = { "metrics": [ { "buckets": ["2021-01-01", "2021-01-02"], "metric_name": "daily_device_change", "series": [ {"name": "continued", "values": [11, 15]}, {"name": "lost", "values": [3, 0]}, {"name": "new", "values": [4, 1]}, ], "snap_id": "test-snap-id", "status": "OK", } ] } metrics_results = MetricsResults.unmarshal(data) assert metrics_results == MetricsResults( metrics=[ MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="daily_device_change", buckets=["2021-01-01", "2021-01-02"], series=[ Series(name="continued", values=[11, 15], currently_released=None), Series(name="lost", values=[3, 0], currently_released=None), Series(name="new", values=[4, 1], currently_released=None), ], ) ] ) assert metrics_results.marshal() == data
def test_metrics_results_unmarshal_one_bucket(metric_name): data = { "metrics": [ { "buckets": ["2021-01-01"], "metric_name": metric_name, "series": [ {"name": "continued", "values": [11]}, {"name": "lost", "values": [3]}, {"name": "new", "values": [4]}, ], "snap_id": "test-snap-id", "status": "OK", } ] } metrics_results = MetricsResults.unmarshal(data) assert metrics_results == MetricsResults( metrics=[ MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name=metric_name, buckets=["2021-01-01"], series=[ Series(name="continued", values=[11], currently_released=None), Series(name="lost", values=[3], currently_released=None), Series(name="new", values=[4], currently_released=None), ], ) ] ) assert metrics_results.marshal() == data
def test_series_invalid_currently_released(currently_released): series = { "name": "test-name", "values": [], "currently_released": currently_released, } with pytest.raises( ValueError, match=re.escape(f"Invalid metric currently_released: {currently_released!r}"), ): Series.unmarshal(series)
def test_series(values, currently_released): series = Series( name="test-name", values=values, currently_released=currently_released ) expected_data = { "name": "test-name", "values": values, "currently_released": currently_released, } if currently_released is None: expected_data.pop("currently_released") assert series.marshal() == expected_data assert Series.unmarshal(expected_data) == series
def test_metric_results(buckets, series, status): results = MetricResults( status=MetricsStatus[status], snap_id="test-id", metric_name="test-metric", buckets=buckets, series=[Series.unmarshal(s) for s in series], ) expected_data = { "status": status, "snap_id": "test-id", "metric_name": "test-metric", "buckets": buckets, "series": series, } assert results.marshal() == expected_data assert MetricResults.unmarshal(expected_data) == results
def test_series_invalid_name(name): series = {"name": name, "values": []} with pytest.raises(ValueError, match=f"Invalid metric name: {name!r}"): Series.unmarshal(series)
def test_metrics_results_unmarshal_multiple_metrics(): metrics = [ { "buckets": ["2021-01-01"], "metric_name": n.value, "series": [ {"name": "blah", "values": [1]}, {"name": "blahh", "values": [1, 2]}, {"name": "blahhh", "values": [1, 2, 3]}, ], "snap_id": "test-snap-id", "status": "OK", } for n in MetricsNames ] data = {"metrics": metrics} metrics_results = MetricsResults.unmarshal(data) assert metrics_results == MetricsResults( metrics=[ MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="daily_device_change", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="installed_base_by_channel", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="installed_base_by_country", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="installed_base_by_operating_system", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="installed_base_by_version", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_device_change", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_installed_base_by_channel", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_installed_base_by_country", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_installed_base_by_operating_system", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), MetricResults( status=MetricsStatus["OK"], snap_id="test-snap-id", metric_name="weekly_installed_base_by_version", buckets=["2021-01-01"], series=[ Series(name="blah", values=[1], currently_released=None), Series(name="blahh", values=[1, 2], currently_released=None), Series(name="blahhh", values=[1, 2, 3], currently_released=None), ], ), ] ) assert metrics_results.marshal() == data