<commit_before>"""A module containing tests for the library representation of IATI data.

Todo:
    Implement tests for strict checking once validation work is underway.
"""
from lxml import etree
import pytest
import iati.core.data
import iati.core.tests.utilities


class TestDatasets(object):
    """A container for tests relating to Datasets."""

    @pytest.fixture
    def dataset_initialised(self):
        """Return an initialised dataset to work from in other tests."""
        return iati.core.Dataset(iati.core.tests.utilities.XML_STR_VALID_NOT_IATI)

    def test_dataset_no_params(self):
        """Test Dataset creation with no parameters."""
        with pytest.raises(TypeError) as excinfo:
            iati.core.Dataset()

        assert ('__init__() missing 1 required positional argument' in str(excinfo.value)) or ('__init__() takes exactly 2 arguments' in str(excinfo.value))

    def test_dataset_valid_xml_string(self):
        """Test Dataset creation with a valid XML string that is not IATI data."""
        data = iati.core.Dataset(iati.core.tests.utilities.XML_STR_VALID_NOT_IATI)

        assert data.xml_str == iati.core.tests.utilities.XML_STR_VALID_NOT_IATI.strip()
        assert etree.tostring(data.xml_tree) == etree.tostring(iati.core.tests.utilities.XML_TREE_VALID)

    def test_dataset_xml_string_leading_whitespace(self):
        """Test Dataset creation with a valid XML string that is not IATI data."""
        data = iati.core.Dataset(iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE)
        tree = etree.fromstring(iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE.strip())

        assert data.xml_str == iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE.strip()
        assert etree.tostring(data.xml_tree) == etree.tostring(tree)

    def test_dataset_valid_iati_string(self):
        """Test Dataset creation with a valid IATI XML string."""
        pass

    def test_dataset_invalid_xml_string(self):
        """Test Dataset creation with a string that is not valid XML."""
        with pytest.raises(ValueError) as excinfo:
            iati.core.Dataset(iati.core.tests.utilities.XML_STR_INVALID)

        assert 'The string provided to create a Dataset from is not valid XML.' == str(excinfo.value)

    @pytest.mark.parametrize("not_xml", iati.core.tests.utilities.find_parameter_by_type(['str'], False))
    def test_dataset_number_not_xml(self, not_xml):
        """Test Dataset creation when it's passed a number rather than a string or etree."""
        with pytest.raises(TypeError) as excinfo:
            iati.core.Dataset(not_xml)

        assert 'Datasets can only be ElementTrees or strings containing valid XML, using the xml_tree and xml_str attributes respectively. Actual type:' in str(excinfo.value)

    def test_dataset_tree(self):
        """Test Dataset creation with an etree that is not valid IATI data."""
        tree = iati.core.tests.utilities.XML_TREE_VALID
        data = iati.core.Dataset(tree)

        assert etree.tostring(data.xml_tree, pretty_print=True) == etree.tostring(tree, pretty_print=True)
        assert data.xml_str == etree.tostring(tree, pretty_print=True)

    def test_dataset_iati_tree(self):
        """Test Dataset creation with a valid IATI etree.

        Todo:
            Implement this function.

        """
        pass

    def test_dataset_xml_str_assignment_valid_str(self, dataset_initialised):
        """Test assignment to the xml_str property with a valid XML string.

        Todo:
            Check that the tree is updated correctly.

        """
        data = dataset_initialised
        data.xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI

        assert data.xml_str == iati.core.tests.utilities.XML_STR_VALID_NOT_IATI.strip()

    def test_dataset_xml_str_assignment_invalid_str(self, dataset_initialised):
        """Test assignment to the xml_str property with an invalid XML string."""
        data = dataset_initialised

        with pytest.raises(ValueError) as excinfo:
            data.xml_str = iati.core.tests.utilities.XML_STR_INVALID

        assert 'The string provided to create a Dataset from is not valid XML.' == str(excinfo.value)

    def test_dataset_xml_str_assignment_tree(self, dataset_initialised):
        """Test assignment to the xml_str property with an ElementTree."""
        data = dataset_initialised

        with pytest.raises(TypeError) as excinfo:
            data.xml_str = iati.core.tests.utilities.XML_TREE_VALID

        assert 'If setting a dataset with an ElementTree, use the xml_tree property, not the xml_str property.' == str(excinfo.value)

    @pytest.mark.parametrize("invalid_value", iati.core.tests.utilities.find_parameter_by_type(['str'], False))
    def test_dataset_xml_str_assignment_invalid_value(self, dataset_initialised, invalid_value):
        """Test assignment to the xml_str property with a value that is very much not valid."""
        data = dataset_initialised

        with pytest.raises(TypeError) as excinfo:
            data.xml_str = invalid_value

        assert 'Datasets can only be ElementTrees or strings containing valid XML, using the xml_tree and xml_str attributes respectively. Actual type:' in str(excinfo.value)

    def test_dataset_xml_str_source_at_line_valid_line_number(self):
        """Test obtaining source of a particular line. Line numbers are valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI
        data = iati.core.Dataset(xml_str)

        with pytest.raises(ValueError):
            data.source_at_line(-1)
        assert data.source_at_line(2) == '<parent>'
        assert data.source_at_line(6) == '<sub-child />'
        assert data.source_at_line(7) == '<!-- comment with blank line below -->'
        assert data.source_at_line(8) == ''

    def test_dataset_xml_str_source_at_line_invalid_line_number(self):
        """Test obtaining source of a particular line. Line numbers are not valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI
        data = iati.core.Dataset(xml_str)

        with pytest.raises(ValueError):
            data.source_at_line(-1)

        with pytest.raises(ValueError):
            data.source_at_line(len(xml_str.split('\n')))

    @pytest.mark.parametrize("invalid_value", iati.core.tests.utilities.find_parameter_by_type(['int'], False))
    def test_dataset_xml_str_source_at_line_invalid_line_type(self, invalid_value):
        """Test obtaining source of a particular line. Line numbers are not valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI
        data = iati.core.Dataset(xml_str)

        with pytest.raises(TypeError):
            data.source_at_line(invalid_value)

    def test_dataset_xml_tree_assignment_valid_tree(self, dataset_initialised):
        """Test assignment to the xml_tree property with a valid ElementTree.

        Todo:
            Check that the xml_tree attribute is updated to the new tree.

        """
        data = dataset_initialised
        data.xml_tree = iati.core.tests.utilities.XML_TREE_VALID
<commit_msg>Check obtaining source line in loop<commit_after>"""A module containing tests for the library representation of IATI data.

Todo:
    Implement tests for strict checking once validation work is underway.
"""
from lxml import etree
import pytest
import iati.core.data
import iati.core.tests.utilities


class TestDatasets(object):
    """A container for tests relating to Datasets."""

    @pytest.fixture
    def dataset_initialised(self):
        """Return an initialised dataset to work from in other tests."""
        return iati.core.Dataset(iati.core.tests.utilities.XML_STR_VALID_NOT_IATI)

    def test_dataset_no_params(self):
        """Test Dataset creation with no parameters."""
        with pytest.raises(TypeError) as excinfo:
            iati.core.Dataset()

        assert ('__init__() missing 1 required positional argument' in str(excinfo.value)) or ('__init__() takes exactly 2 arguments' in str(excinfo.value))

    def test_dataset_valid_xml_string(self):
        """Test Dataset creation with a valid XML string that is not IATI data."""
        data = iati.core.Dataset(iati.core.tests.utilities.XML_STR_VALID_NOT_IATI)

        assert data.xml_str == iati.core.tests.utilities.XML_STR_VALID_NOT_IATI.strip()
        assert etree.tostring(data.xml_tree) == etree.tostring(iati.core.tests.utilities.XML_TREE_VALID)

    def test_dataset_xml_string_leading_whitespace(self):
        """Test Dataset creation with a valid XML string that is not IATI data."""
        data = iati.core.Dataset(iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE)
        tree = etree.fromstring(iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE.strip())

        assert data.xml_str == iati.core.tests.utilities.XML_STR_LEADING_WHITESPACE.strip()
        assert etree.tostring(data.xml_tree) == etree.tostring(tree)

    def test_dataset_valid_iati_string(self):
        """Test Dataset creation with a valid IATI XML string."""
        pass

    def test_dataset_invalid_xml_string(self):
        """Test Dataset creation with a string that is not valid XML."""
        with pytest.raises(ValueError) as excinfo:
            iati.core.Dataset(iati.core.tests.utilities.XML_STR_INVALID)

        assert 'The string provided to create a Dataset from is not valid XML.' == str(excinfo.value)

    @pytest.mark.parametrize("not_xml", iati.core.tests.utilities.find_parameter_by_type(['str'], False))
    def test_dataset_number_not_xml(self, not_xml):
        """Test Dataset creation when it's passed a number rather than a string or etree."""
        with pytest.raises(TypeError) as excinfo:
            iati.core.Dataset(not_xml)

        assert 'Datasets can only be ElementTrees or strings containing valid XML, using the xml_tree and xml_str attributes respectively. Actual type:' in str(excinfo.value)

    def test_dataset_tree(self):
        """Test Dataset creation with an etree that is not valid IATI data."""
        tree = iati.core.tests.utilities.XML_TREE_VALID
        data = iati.core.Dataset(tree)

        assert etree.tostring(data.xml_tree, pretty_print=True) == etree.tostring(tree, pretty_print=True)
        assert data.xml_str == etree.tostring(tree, pretty_print=True)

    def test_dataset_iati_tree(self):
        """Test Dataset creation with a valid IATI etree.

        Todo:
            Implement this function.

        """
        pass

    def test_dataset_xml_str_assignment_valid_str(self, dataset_initialised):
        """Test assignment to the xml_str property with a valid XML string.

        Todo:
            Check that the tree is updated correctly.

        """
        data = dataset_initialised
        data.xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI

        assert data.xml_str == iati.core.tests.utilities.XML_STR_VALID_NOT_IATI.strip()

    def test_dataset_xml_str_assignment_invalid_str(self, dataset_initialised):
        """Test assignment to the xml_str property with an invalid XML string."""
        data = dataset_initialised

        with pytest.raises(ValueError) as excinfo:
            data.xml_str = iati.core.tests.utilities.XML_STR_INVALID

        assert 'The string provided to create a Dataset from is not valid XML.' == str(excinfo.value)

    def test_dataset_xml_str_assignment_tree(self, dataset_initialised):
        """Test assignment to the xml_str property with an ElementTree."""
        data = dataset_initialised

        with pytest.raises(TypeError) as excinfo:
            data.xml_str = iati.core.tests.utilities.XML_TREE_VALID

        assert 'If setting a dataset with an ElementTree, use the xml_tree property, not the xml_str property.' == str(excinfo.value)

    @pytest.mark.parametrize("invalid_value", iati.core.tests.utilities.find_parameter_by_type(['str'], False))
    def test_dataset_xml_str_assignment_invalid_value(self, dataset_initialised, invalid_value):
        """Test assignment to the xml_str property with a value that is very much not valid."""
        data = dataset_initialised

        with pytest.raises(TypeError) as excinfo:
            data.xml_str = invalid_value

        assert 'Datasets can only be ElementTrees or strings containing valid XML, using the xml_tree and xml_str attributes respectively. Actual type:' in str(excinfo.value)

    def test_dataset_xml_str_source_at_line_valid_line_number(self):
        """Test obtaining source of a particular line. Line numbers are valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI.strip()
        data = iati.core.Dataset(xml_str)

        split_xml_str = xml_str.split('\n')

        for idx, line in enumerate(split_xml_str):
            assert data.source_at_line(idx) == line.strip()

    def test_dataset_xml_str_source_at_line_invalid_line_number(self):
        """Test obtaining source of a particular line. Line numbers are not valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI
        data = iati.core.Dataset(xml_str)

        with pytest.raises(ValueError):
            data.source_at_line(-1)

        with pytest.raises(ValueError):
            data.source_at_line(len(xml_str.split('\n')))

    @pytest.mark.parametrize("invalid_value", iati.core.tests.utilities.find_parameter_by_type(['int'], False))
    def test_dataset_xml_str_source_at_line_invalid_line_type(self, invalid_value):
        """Test obtaining source of a particular line. Line numbers are not valid."""
        xml_str = iati.core.tests.utilities.XML_STR_VALID_NOT_IATI
        data = iati.core.Dataset(xml_str)

        with pytest.raises(TypeError):
            data.source_at_line(invalid_value)

    def test_dataset_xml_tree_assignment_valid_tree(self, dataset_initialised):
        """Test assignment to the xml_tree property with a valid ElementTree.

        Todo:
            Check that the xml_tree attribute is updated to the new tree.

        """
        data = dataset_initialised
        data.xml_tree = iati.core.tests.utilities.XML_TREE_VALID

{'fraction_non_alphanumeric': 0.08803291480456835, 'fraction_numerical': 0.0007803078669220402, 'mean_word_length': 3.355267222737102, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 97}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '332882', 'n_tokens_mistral': 4297, 'n_tokens_neox': 4268, 'n_words': 1065}