# Path: beancount_import/source/source_test.py
# def check_source_example(example_dir: str,
#                          source_spec: SourceSpec,
#                          replacements: List[Tuple[str, str]],
#                          write: Optional[bool] = None):
#     journal_path = os.path.join(example_dir, 'journal.beancount')
#     editor = JournalEditor(journal_path)
#     assert editor.errors == []
#     source = load_source(source_spec)
#     results = SourceResults()
#     source.prepare(editor, results)
#     results.pending.sort(key=lambda x: x.date)
#     account_source_map = {account: source for account in results.accounts}
#     sources = [source]
#     extractor = training.FeatureExtractor(
#         sources=sources, account_source_map=account_source_map,
#         skip_accounts=results.skip_training_accounts)
#     assert(not [m for m in results.messages if m[0] == 'error'])
#     for filename, new_contents in _add_invalid_reference_and_cleared_metadata(
#             editor, source,
#             sorted(
#                 results.invalid_references,
#                 key=invalid_source_reference_sort_key)).items():
#         test_util.check_golden_contents(
#             filename,
#             new_contents,
#             replacements=[],
#             write=write,
#         )
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'import_results.beancount'),
#         _format_import_results(
#             results.pending, extractor=extractor, source=source),
#         replacements=replacements,
#         write=write,
#     )
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'accounts.txt'),
#         ''.join(account + '\n' for account in sorted(results.accounts)),
#         replacements=replacements,
#         write=write,
#     )
# 
#     training_examples = training.MockTrainingExamples()
#     extractor.extract_examples(editor.entries, training_examples)
# 
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'training_examples.json'),
#         json.dumps(
#             [[_json_encode_prediction_input(prediction_input), target]
#              for prediction_input, target in training_examples.examples],
#             indent='  ',
#             sort_keys=True),
#         replacements=replacements,
#         write=write,
#     )


import os
import pytest
from .source_test import check_source_example



testdata_dir = os.path.realpath(
    os.path.join(
        os.path.dirname(__file__), '..', '..', 'testdata', 'source',
        'google_purchases'))

examples = [
    'test_basic',
    'test_matching',
    'test_invalid',
]


@pytest.mark.parametrize('name', examples)
def test_source(name: str):

# Path: beancount_import/source/source_test.py
# def check_source_example(example_dir: str,
#                          source_spec: SourceSpec,
#                          replacements: List[Tuple[str, str]],
#                          write: Optional[bool] = None):
#     journal_path = os.path.join(example_dir, 'journal.beancount')
#     editor = JournalEditor(journal_path)
#     assert editor.errors == []
#     source = load_source(source_spec)
#     results = SourceResults()
#     source.prepare(editor, results)
#     results.pending.sort(key=lambda x: x.date)
#     account_source_map = {account: source for account in results.accounts}
#     sources = [source]
#     extractor = training.FeatureExtractor(
#         sources=sources, account_source_map=account_source_map,
#         skip_accounts=results.skip_training_accounts)
#     assert(not [m for m in results.messages if m[0] == 'error'])
#     for filename, new_contents in _add_invalid_reference_and_cleared_metadata(
#             editor, source,
#             sorted(
#                 results.invalid_references,
#                 key=invalid_source_reference_sort_key)).items():
#         test_util.check_golden_contents(
#             filename,
#             new_contents,
#             replacements=[],
#             write=write,
#         )
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'import_results.beancount'),
#         _format_import_results(
#             results.pending, extractor=extractor, source=source),
#         replacements=replacements,
#         write=write,
#     )
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'accounts.txt'),
#         ''.join(account + '\n' for account in sorted(results.accounts)),
#         replacements=replacements,
#         write=write,
#     )
# 
#     training_examples = training.MockTrainingExamples()
#     extractor.extract_examples(editor.entries, training_examples)
# 
#     test_util.check_golden_contents(
#         os.path.join(example_dir, 'training_examples.json'),
#         json.dumps(
#             [[_json_encode_prediction_input(prediction_input), target]
#              for prediction_input, target in training_examples.examples],
#             indent='  ',
#             sort_keys=True),
#         replacements=replacements,
#         write=write,
#     )


# Path: beancount_import/source/google_purchases_test.py
import os
import pytest
from .source_test import check_source_example




testdata_dir = os.path.realpath(
    os.path.join(
        os.path.dirname(__file__), '..', '..', 'testdata', 'source',
        'google_purchases'))

examples = [
    'test_basic',
    'test_matching',
    'test_invalid',
]


@pytest.mark.parametrize('name', examples)
def test_source(name: str):

    check_source_example(