|
| 1 | +import re |
| 2 | + |
| 3 | +import pytest |
| 4 | +from munch import Munch |
| 5 | + |
| 6 | +from grafana_wtf.model import DatasourceItem |
| 7 | + |
| 8 | +DATA = dict(uid="foo", name="bar", type="baz", url="qux") |
| 9 | + |
| 10 | + |
| 11 | +def test_datasource_item_basic(): |
| 12 | + item = DatasourceItem(**DATA) |
| 13 | + assert item.uid == "foo" |
| 14 | + |
| 15 | + |
| 16 | +def test_datasource_item_dict_success(): |
| 17 | + item = DatasourceItem.from_payload(DATA) |
| 18 | + assert item.uid == "foo" |
| 19 | + |
| 20 | + |
| 21 | +def test_datasource_item_munch(): |
| 22 | + item = DatasourceItem.from_payload(Munch(**DATA)) |
| 23 | + assert item.uid == "foo" |
| 24 | + |
| 25 | + |
| 26 | +def test_datasource_item_str(): |
| 27 | + item = DatasourceItem.from_payload("Hotzenplotz") |
| 28 | + assert item.uid is None |
| 29 | + assert item.name == "Hotzenplotz" |
| 30 | + |
| 31 | + |
| 32 | +def test_datasource_item_dict_unknown_attribute(): |
| 33 | + mydata = DATA.copy() |
| 34 | + mydata.update({"more": "data"}) |
| 35 | + with pytest.raises(TypeError) as ex: |
| 36 | + DatasourceItem.from_payload(mydata) |
| 37 | + assert ex.match(re.escape("__init__() got an unexpected keyword argument 'more'")) |
| 38 | + |
| 39 | + |
| 40 | +def test_datasource_item_dict_compensate_datasource(): |
| 41 | + """ |
| 42 | + Validate that the `datasource` attribute is ignored. |
| 43 | +
|
| 44 | + TypeError: DatasourceItem.__init__() got an unexpected keyword argument 'datasource' |
| 45 | + https://github.com/panodata/grafana-wtf/issues/110 |
| 46 | + """ |
| 47 | + mydata = DATA.copy() |
| 48 | + mydata.update({"datasource": "unknown"}) |
| 49 | + with pytest.warns(UserWarning) as record: |
| 50 | + item = DatasourceItem.from_payload(mydata) |
| 51 | + assert item.uid == "foo" |
| 52 | + |
| 53 | + # Check that only one warning was raised. |
| 54 | + assert len(record) == 1 |
| 55 | + |
| 56 | + # Check that the message matches. |
| 57 | + assert "The `datasource` attribute is ignored for the time being" in record[0].message.args[0] |
0 commit comments