<commit_before>"""
Tests for apyori.create_next_candidates.
"""

from nose.tools import eq_
from mock import Mock

from apyori import SupportRecord
from apyori import TransactionManager
from apyori import gen_support_records


def test_empty():
    """
    Test for gen_supports_record.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = []
    support_records_gen = gen_support_records(transaction_manager, 0.1)
    support_records = [x for x in support_records_gen]
    eq_(support_records, [])


def test_infinite():
    """
    Test for gen_supports_record with no limits.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = [
        frozenset(['A']), frozenset(['B']), frozenset(['C'])]
    transaction_manager.calc_support.side_effect = lambda key: {
        frozenset(['A']): 0.8,
        frozenset(['B']): 0.6,
        frozenset(['C']): 0.3,
        frozenset(['A', 'B']): 0.3,
        frozenset(['A', 'C']): 0.2,
    }.get(key, 0.0)
    support_records_gen = gen_support_records(transaction_manager, 0.3)
    # Convert into frozenset to ignore orders.
    support_records = frozenset([x for x in support_records_gen])
    eq_(support_records, frozenset([
        SupportRecord(frozenset(['A']), 0.8),
        SupportRecord(frozenset(['B']), 0.6),
        SupportRecord(frozenset(['C']), 0.3),
        SupportRecord(frozenset(['A', 'B']), 0.3),
    ]))

def test_length():
    """
    Test for gen_supports_record that limits the length.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = [
        frozenset(['A']), frozenset(['B']), frozenset(['C'])]
    transaction_manager.calc_support.side_effect = lambda key: {
        frozenset(['A']): 0.7,
        frozenset(['B']): 0.5,
        frozenset(['C']): 0.2,
        frozenset(['A', 'B']): 0.2,
        frozenset(['A', 'C']): 0.1,
    }.get(key, 0.0)
    support_records_gen = gen_support_records(
        transaction_manager, 0.05, max_length=1)
    # Convert into frozenset to ignore orders.
    support_records = frozenset([x for x in support_records_gen])
    eq_(support_records, frozenset([
        SupportRecord(frozenset(['A']), 0.5),
        SupportRecord(frozenset(['B']), 0.3),
        SupportRecord(frozenset(['C']), 0.1),
    ]))<commit_msg>Fix test errors.<commit_after>"""
Tests for apyori.create_next_candidates.
"""

from nose.tools import eq_
from mock import Mock

from apyori import SupportRecord
from apyori import TransactionManager
from apyori import gen_support_records


def test_empty():
    """
    Test for gen_supports_record.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = []
    support_records_gen = gen_support_records(transaction_manager, 0.1)
    support_records = [x for x in support_records_gen]
    eq_(support_records, [])


def test_infinite():
    """
    Test for gen_supports_record with no limits.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = [
        frozenset(['A']), frozenset(['B']), frozenset(['C'])]
    transaction_manager.calc_support.side_effect = lambda key: {
        frozenset(['A']): 0.8,
        frozenset(['B']): 0.6,
        frozenset(['C']): 0.3,
        frozenset(['A', 'B']): 0.3,
        frozenset(['A', 'C']): 0.2,
    }.get(key, 0.0)
    support_records_gen = gen_support_records(transaction_manager, 0.3)
    # Convert into frozenset to ignore orders.
    support_records = frozenset([x for x in support_records_gen])
    eq_(support_records, frozenset([
        SupportRecord(frozenset(['A']), 0.8),
        SupportRecord(frozenset(['B']), 0.6),
        SupportRecord(frozenset(['C']), 0.3),
        SupportRecord(frozenset(['A', 'B']), 0.3),
    ]))

def test_length():
    """
    Test for gen_supports_record that limits the length.
    """
    transaction_manager = Mock(spec=TransactionManager)
    transaction_manager.initial_candidates.return_value = [
        frozenset(['A']), frozenset(['B']), frozenset(['C'])]
    transaction_manager.calc_support.side_effect = lambda key: {
        frozenset(['A']): 0.7,
        frozenset(['B']): 0.5,
        frozenset(['C']): 0.2,
        frozenset(['A', 'B']): 0.2,
        frozenset(['A', 'C']): 0.1,
    }.get(key, 0.0)
    # TODO: Apply a patch to create_next_candidates.
    support_records_gen = gen_support_records(
        transaction_manager, 0.05, max_length=1)
    # Convert into frozenset to ignore orders.
    support_records = frozenset([x for x in support_records_gen])
    eq_(support_records, frozenset([
        SupportRecord(frozenset(['A']), 0.7),
        SupportRecord(frozenset(['B']), 0.5),
        SupportRecord(frozenset(['C']), 0.2),
    ]))
{'fraction_non_alphanumeric': 0.14989816700610997, 'fraction_numerical': 0.018737270875763747, 'mean_word_length': 3.6242937853107344, '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': '4989978', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1598, 'n_words': 320}