<commit_before># -*- coding: utf-8 -*-

"""Tests of sparse matrix structures."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

from numpy.testing import assert_array_equal
from pytest import raises

from ....ext.six import itervalues, iterkeys
from ..cluster_metadata import _cluster_info, ClusterMetadata


#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_structure():
    """Test the structure holding all cluster metadata."""
    data = _cluster_info([('a', 1), ('b', 2)])

    assert isinstance(data[3], dict)
    assert data[3]['a'] == 1
    assert data[3]['b'] == 2

    data[3]['b'] = 10
    assert data[3]['b'] == 10

    with raises(KeyError):
        data[3]['c']


def test_default_function():
    meta = ClusterMetadata([('field', lambda: 9)])
    assert meta.data[3]['field'] == 9


def test_cluster_metadata():
    meta = ClusterMetadata()
    assert meta.data is not None

    assert meta[0]['group'] is not None

    assert meta[2]['color'] == 1
    assert meta[2]['group'] == 3

    assert meta[10]['color'] == 1
    assert meta[10]['group'] == 3

    meta.set([10], 'color', 5)
    assert meta[10]['color'] == 5

    # Alternative __setitem__ syntax.
    meta.set([10, 11], 'color', 5)
    assert meta[10]['color'] == 5
    assert meta[11]['color'] == 5

    meta.set([10, 11], 'color', [6, 7])
    assert meta[10]['color'] == 6
    assert meta.data[11]['color'] == 7

    meta[10]['color'] = 10
    assert meta[10]['color'] == 10


def test_metadata_history():
    """Test ClusterMetadata history."""

    data = {2: {'group': 2, 'color': 7}, 4: {'group': 5}}

    meta = ClusterMetadata(data=data)

    # Values set in 'data'.
    assert meta.get(2, 'group') == 2
    assert meta.get(2, 'color') == 7

    # Default values.
    assert meta.get(3, 'group') == 3
    assert meta.get(3, 'color') == 1

    assert meta.get(4, 'group') == 5
    assert meta.get(4, 'color') == 1

    ###########

    meta.undo()
    meta.redo()

    # Action 1.
    meta.set(2, 'group', 20)
    assert meta.get(2, 'group') == 20

    # Action 2.
    meta.set(3, 'color', 30)
    assert meta.get(3, 'color') == 30

    # Action 3.
    meta.set(2, 'color', 40)
    assert meta.get(2, 'color') == 40

    ###########

    # Undo 3.
    meta.undo()
    assert meta.get(2, 'color') == 7

    # Undo 2.
    meta.undo()
    assert meta.get(3, 'color') == 1

    # Redo 2.
    meta.redo()
    assert meta.get(3, 'color') == 30
    assert meta.get(2, 'group') == 20

    # Undo 2.
    meta.undo()
    # Undo 1.
    meta.undo()
    assert meta.get(2, 'group') == 2

    meta.undo()
    meta.undo()<commit_msg>Test UpdateInfo for cluster metadata.<commit_after># -*- coding: utf-8 -*-

"""Tests of sparse matrix structures."""

#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------

from numpy.testing import assert_array_equal
from pytest import raises

from ....ext.six import itervalues, iterkeys
from ..cluster_metadata import _cluster_info, ClusterMetadata


#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------

def test_structure():
    """Test the structure holding all cluster metadata."""
    data = _cluster_info([('a', 1), ('b', 2)])

    assert isinstance(data[3], dict)
    assert data[3]['a'] == 1
    assert data[3]['b'] == 2

    data[3]['b'] = 10
    assert data[3]['b'] == 10

    with raises(KeyError):
        data[3]['c']


def test_default_function():
    meta = ClusterMetadata([('field', lambda: 9)])
    assert meta.data[3]['field'] == 9


def test_cluster_metadata():
    meta = ClusterMetadata()
    assert meta.data is not None

    assert meta[0]['group'] is not None

    assert meta[2]['color'] == 1
    assert meta[2]['group'] == 3

    assert meta[10]['color'] == 1
    assert meta[10]['group'] == 3

    info = meta.set([10], 'color', 5)
    assert meta[10]['color'] == 5
    assert info.description == 'color'
    assert info.metadata_changed == [10]

    # Alternative __setitem__ syntax.
    info = meta.set([10, 11], 'color', 5)
    assert meta[10]['color'] == 5
    assert meta[11]['color'] == 5
    assert info.description == 'color'
    assert info.metadata_changed == [10, 11]

    info = meta.set([10, 11], 'color', [6, 7])
    assert meta[10]['color'] == 6
    assert meta.data[11]['color'] == 7
    assert info.description == 'color'
    assert info.metadata_changed == [10, 11]

    meta[10]['color'] = 10
    assert meta[10]['color'] == 10


def test_metadata_history():
    """Test ClusterMetadata history."""

    data = {2: {'group': 2, 'color': 7}, 4: {'group': 5}}

    meta = ClusterMetadata(data=data)

    # Values set in 'data'.
    assert meta.get(2, 'group') == 2
    assert meta.get(2, 'color') == 7

    # Default values.
    assert meta.get(3, 'group') == 3
    assert meta.get(3, 'color') == 1

    assert meta.get(4, 'group') == 5
    assert meta.get(4, 'color') == 1

    ###########

    meta.undo()
    meta.redo()

    # Action 1.
    info = meta.set(2, 'group', 20)
    assert meta.get(2, 'group') == 20
    assert info.description == 'group'
    assert info.metadata_changed == [2]

    # Action 2.
    info = meta.set(3, 'color', 30)
    assert meta.get(3, 'color') == 30
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Action 3.
    info = meta.set(2, 'color', 40)
    assert meta.get(2, 'color') == 40
    assert info.description == 'color'
    assert info.metadata_changed == [2]

    ###########

    # Undo 3.
    info = meta.undo()
    assert meta.get(2, 'color') == 7
    assert info.description == 'color'
    assert info.metadata_changed == [2]

    # Undo 2.
    info = meta.undo()
    assert meta.get(3, 'color') == 1
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Redo 2.
    info = meta.redo()
    assert meta.get(3, 'color') == 30
    assert meta.get(2, 'group') == 20
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Undo 2.
    info = meta.undo()
    assert info.description == 'color'
    assert info.metadata_changed == [3]

    # Undo 1.
    info = meta.undo()
    assert meta.get(2, 'group') == 2
    assert info.description == 'group'
    assert info.metadata_changed == [2]

    info = meta.undo()
    assert info is None

    info = meta.undo()
    assert info is None
{'fraction_non_alphanumeric': 0.25989035412653727, 'fraction_numerical': 0.03852422581123129, 'mean_word_length': 2.0269058295964126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4664196', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2035, 'n_words': 574}