import datetime as dt

from flyway.migration.logs import parse_log_entries
from flyway.migration.profile import parse_expand_attributes, parse_profile


def test_parse_log_entries():
    text = ("10/8/2024 3:05:12 PM <b>Sam Staff(sam)</b> created the Load-In record LI-1"
            "<br />10/9/2024 9:00:00 AM <b>Ada(admin)</b> completed the Load-In record LI-1")
    entries = parse_log_entries(text)
    assert [(e.at, e.action) for e in entries] == [
        (dt.datetime(2024, 10, 8, 15, 5, 12), "created the Load-In record LI-1"),
        (dt.datetime(2024, 10, 9, 9, 0, 0), "completed the Load-In record LI-1"),
    ]
    assert parse_log_entries(None) == []


def test_parse_profile_and_expand_attributes():
    xml = ('<ArrayOfKeyValueOfstringanyType><KeyValueOfstringanyType><Key>FirstName</Key>'
           '<Value i:type="d2p1:string">Sam &amp; Co</Value></KeyValueOfstringanyType>'
           '<KeyValueOfstringanyType><Key>MobileNumber</Key><Value>021</Value></KeyValueOfstringanyType>'
           '</ArrayOfKeyValueOfstringanyType>')
    names = f"Gender:S:0:1:ExpandAttribute:S:1:{len(xml)}:"
    props = parse_profile(names, "M" + xml)
    assert props["Gender"] == "M"
    assert parse_expand_attributes(props["ExpandAttribute"]) == {"FirstName": "Sam & Co", "MobileNumber": "021"}
