Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ymoch/features/refactor
Features/refactor
- Loading branch information
Showing
8 changed files
with
293 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Tests for apyori.apriori. | ||
""" | ||
|
||
from nose.tools import eq_ | ||
from mock import Mock | ||
|
||
from apyori import TransactionManager | ||
from apyori import SupportRecord | ||
from apyori import RelationRecord | ||
from apyori import OrderedStatistic | ||
from apyori import apriori | ||
|
||
|
||
def test_empty(): | ||
""" | ||
Test for empty data. | ||
""" | ||
transaction_manager = Mock(spec=TransactionManager) | ||
def gen_support_records(*_): | ||
""" Mock for apyori.gen_support_records. """ | ||
return iter([]) | ||
|
||
def gen_ordered_statistics(*_): | ||
""" Mock for apyori.gen_ordered_statistics. """ | ||
yield OrderedStatistic( | ||
frozenset(['A']), frozenset(['B']), 0.1, 0.7) | ||
|
||
result = list(apriori( | ||
transaction_manager, | ||
_gen_support_records=gen_support_records, | ||
_gen_ordered_statistics=gen_ordered_statistics, | ||
)) | ||
eq_(result, []) | ||
|
||
|
||
def test_normal(): | ||
""" | ||
Test for normal data. | ||
""" | ||
transaction_manager = Mock(spec=TransactionManager) | ||
min_support = 0.1 | ||
max_length = 2 | ||
support_record = SupportRecord(frozenset(['A', 'B']), 0.5) | ||
ordered_statistic1 = OrderedStatistic( | ||
frozenset(['A']), frozenset(['B']), 0.1, 0.7) | ||
ordered_statistic2 = OrderedStatistic( | ||
frozenset(['A']), frozenset(['B']), 0.3, 0.5) | ||
|
||
def gen_support_records(*args): | ||
""" Mock for apyori.gen_support_records. """ | ||
eq_(args[1], min_support) | ||
eq_(args[2], max_length) | ||
yield support_record | ||
|
||
def gen_ordered_statistics(*_): | ||
""" Mock for apyori.gen_ordered_statistics. """ | ||
yield ordered_statistic1 | ||
yield ordered_statistic2 | ||
|
||
# Will not create any records because of confidence. | ||
result = list(apriori( | ||
transaction_manager, | ||
min_support=min_support, | ||
min_confidence=0.4, | ||
max_length=max_length, | ||
_gen_support_records=gen_support_records, | ||
_gen_ordered_statistics=gen_ordered_statistics, | ||
)) | ||
eq_(result, []) | ||
|
||
# Will create a record. | ||
result = list(apriori( | ||
transaction_manager, | ||
min_support=min_support, | ||
min_confidence=0.3, | ||
max_length=max_length, | ||
_gen_support_records=gen_support_records, | ||
_gen_ordered_statistics=gen_ordered_statistics, | ||
)) | ||
eq_(result, [RelationRecord( | ||
support_record.items, support_record.support, [ordered_statistic2] | ||
)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Tests for apyori.dump_as_json. | ||
""" | ||
|
||
import json | ||
|
||
# For Python 2 compatibility. | ||
try: | ||
from StringIO import StringIO | ||
except ImportError: | ||
from io import StringIO | ||
|
||
from nose.tools import raises | ||
from nose.tools import eq_ | ||
|
||
from apyori import RelationRecord | ||
from apyori import OrderedStatistic | ||
from apyori import dump_as_json | ||
|
||
|
||
def test_normal(): | ||
""" | ||
Test for normal data. | ||
""" | ||
test_data = RelationRecord( | ||
frozenset(['A']), 0.5, | ||
[OrderedStatistic(frozenset([]), frozenset(['A']), 0.8, 1.2)] | ||
) | ||
output_file = StringIO() | ||
dump_as_json(test_data, output_file) | ||
|
||
output_file.seek(0) | ||
result = json.loads(output_file.read()) | ||
eq_(result, { | ||
'items': ['A'], | ||
'support': 0.5, | ||
'ordered_statistics': [ | ||
{ | ||
'items_base': [], | ||
'items_add': ["A"], | ||
'confidence': 0.8, | ||
'lift': 1.2 | ||
} | ||
] | ||
}) | ||
|
||
|
||
@raises(TypeError) | ||
def test_bad(): | ||
""" | ||
Test for bad data. | ||
""" | ||
test_data = RelationRecord( | ||
set(['A']), 0.5, | ||
[OrderedStatistic(frozenset([]), frozenset(['A']), 0.8, 1.2)] | ||
) | ||
output_file = StringIO() | ||
dump_as_json(test_data, output_file) |
Oops, something went wrong.