"""Test FITS conversion"""

from ctao_datamodel import fits_header_to_instance, instance_to_fits_header


def test_instance_to_fits_header(example_model_instance):
    header = instance_to_fits_header(example_model_instance)

    # check a FITS keyword
    assert float(header["DEC_PNT"]) - -20.1 < 1.0e-10

    # check a long keyword
    assert header["HIERARCH CTAO REFERENCE_VERSION"] == "2.0"

    # check that a comment is made:
    assert header.comments["LICENSE"] == "License for this data product"

    # now test round trip:

    instance = fits_header_to_instance(
        header=header, model=example_model_instance.__class__
    )

    assert instance == example_model_instance


def test_different_mappings():
    from ctao_datamodel import AstroField, ModelBase

    class SubVerOne(ModelBase):
        x: float = AstroField("example", fits_keyword="EXAMPLE", default=0.1)

    class SubVerTwo(ModelBase):
        x: float = AstroField("example", fits_keyword="NEWEX", default=10.0)

    class Example(ModelBase):
        sub: SubVerOne | SubVerTwo

    i1 = Example(sub=SubVerOne())
    i2 = Example(sub=SubVerTwo())

    h1 = instance_to_fits_header(i1)
    h2 = instance_to_fits_header(i2)

    assert "EXAMPLE" in h1
    assert "NEWEX" not in h1
    assert "NEWEX" in h2
    assert "EXAMPLE" not in h2
