# Copyright (c) 2013-2015 University Corporation for Atmospheric Research/Unidata.
# Distributed under the terms of the MIT License.
# SPDX-License-Identifier: MIT
"""Test the parsing of the NCSS dataset.xml."""

import logging
import xml.etree.ElementTree as ET

from siphon.http_util import urlopen
from siphon.ncss_dataset import _Types, NCSSDataset
from siphon.testing import get_recorder

log = logging.getLogger('siphon.ncss_dataset')
log.setLevel(logging.WARNING)

recorder = get_recorder(__file__)

#
# tested:
#
#  attribute
#  values
#  projectionBox
#  axisRef
#  coordTransRef
#  grid
#  parameter
#  featureType
#  variable
#
#  projectionBox
#  axisRef
#  coordTransRef
#  coordTransform
#  gridSet
#  axis
#  LatLonBox
#  TimeSpan
#  AcceptList


class TestSimpleTypes(object):
    """Test parsing simple types from NCSS dataset.xml."""

    @classmethod
    def setup_class(cls):
        """Set up the test class."""
        cls.types = _Types()

    def test_attribute_byte(self):
        """Test parsing a byte attribute."""
        xml = '<attribute name="missing_value" type="byte" value="1"/>'
        element = ET.fromstring(xml)
        expected = {'missing_value': [1]}
        actual = self.types.handle_attribute(element)
        assert expected == actual

    def test_attribute_invalid_byte(self, caplog):
        """Test parsing an invalid byte attribute."""
        xml = '<attribute name="missing_value" type="byte" value="a"/>'
        element = ET.fromstring(xml)
        expected = {'missing_value': ['a']}
        actual = self.types.handle_attribute(element)
        assert 'Cannot convert [\'a\'] to int. Keeping type as str.' in caplog.text
        assert expected == actual

    def test_attribute_short(self):
        """Test parsing a short attribute."""
        xml = '<attribute name="missing_value" type="short" value="-999"/>'
        element = ET.fromstring(xml)
        expected = {'missing_value': [-999]}
        actual = self.types.handle_attribute(element)
        assert expected == actual

    def test_attribute_invalid_short(self, caplog):
        """Test parsing an invalid short attribute."""
        xml = '<attribute name="missing_value" type="short" value="a"/>'
        element = ET.fromstring(xml)
        expected = {'missing_value': ['a']}