| 1 |
import datetime |
| 2 |
import logging |
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import osmapi |
| 6 |
|
| 7 |
|
| 8 |
def test_dom_get_attributes(): |
| 9 |
mock_domelement = mock.Mock() |
| 10 |
mock_domelement.attributes = { |
| 11 |
"uid": "12345", |
| 12 |
"open": "false", |
| 13 |
"visible": "true", |
| 14 |
"lat": "47.1234", |
| 15 |
"date": "2021-12-10T21:28:03Z", |
| 16 |
"new_attribute": "Test 123", |
| 17 |
} |
| 18 |
|
| 19 |
result = osmapi.dom._dom_get_attributes(mock_domelement) |
| 20 |
|
| 21 |
assert isinstance(result, dict) |
| 22 |
assert result["uid"] == 12345 |
| 23 |
assert result["open"] is False |
| 24 |
assert result["visible"] is True |
| 25 |
assert result["lat"] == 47.1234 |
| 26 |
assert result["date"] == datetime.datetime(2021, 12, 10, 21, 28, 3) |
| 27 |
assert result["new_attribute"] == "Test 123" |
| 28 |
|
| 29 |
|
| 30 |
def test_parse_date(): |
| 31 |
assert osmapi.dom._parse_date("2021-02-25T09:49:33Z") == datetime.datetime( |
| 32 |
2021, 2, 25, 9, 49, 33 |
| 33 |
) |
| 34 |
assert osmapi.dom._parse_date("2021-02-25 09:49:33 UTC") == datetime.datetime( |
| 35 |
2021, 2, 25, 9, 49, 33 |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
def test_parse_date_unparsable_value_is_returned_unchanged(caplog): |
| 40 |
with caplog.at_level(logging.DEBUG, logger="osmapi.dom"): |
| 41 |
assert osmapi.dom._parse_date("2021-02-25") == "2021-02-25" |
| 42 |
assert osmapi.dom._parse_date("") == "" |
| 43 |
assert osmapi.dom._parse_date(None) is None |
| 44 |
|
| 45 |
assert [(r.levelname, r.name, r.getMessage()) for r in caplog.records] == [ |
| 46 |
("DEBUG", "osmapi.dom", "2021-02-25 does not match %Y-%m-%d %H:%M:%S UTC"), |
| 47 |
("DEBUG", "osmapi.dom", "2021-02-25 does not match %Y-%m-%dT%H:%M:%SZ"), |
| 48 |
("DEBUG", "osmapi.dom", " does not match %Y-%m-%d %H:%M:%S UTC"), |
| 49 |
("DEBUG", "osmapi.dom", " does not match %Y-%m-%dT%H:%M:%SZ"), |
| 50 |
("DEBUG", "osmapi.dom", "None does not match %Y-%m-%d %H:%M:%S UTC"), |
| 51 |
("DEBUG", "osmapi.dom", "None does not match %Y-%m-%dT%H:%M:%SZ"), |
| 52 |
] |