def test_extract_from_nested_block(note_extractor, mock_roam_note_builder): child_block_json = block('{child}') parent_block_json = block('{parent}', child_block_json) page_json = page(parent_block_json) child_roam_note = RoamBlock([Cloze(['child cloze content'])], 'child source') parent_roam_note = RoamBlock([Cloze(['parent cloze content'])], 'parent source') (when(mock_roam_note_builder).called_with( parent_block_json, [page_json]).then_return(parent_roam_note)) (when(mock_roam_note_builder).called_with( child_block_json, [page_json, parent_block_json]).then_return(child_roam_note)) assert list(note_extractor([page_json ])) == [parent_roam_note, child_roam_note]
def test_cloze_formatter_formats_cloze_number( format_cloze, mock_cloze_part_formatter, ): cloze = Cloze(['part'], number=2) (when(mock_cloze_part_formatter).called_with('part').then_return( 'formatted part')) formatted_cloze = format_cloze(cloze) assert formatted_cloze == '{{c2::formatted part}}'
def test_extract_from_single_block(note_extractor, mock_roam_note_builder): content = '{note}' block_json = block(content) page_json = page(block_json) roam_note = RoamBlock([Cloze(['cloze content'])], 'source') (when(mock_roam_note_builder).called_with( block_json, [page_json]).then_return(roam_note)) assert list(note_extractor([page_json])) == [roam_note]
def test_cloze_formatter_formats_hint( format_cloze, mock_cloze_part_formatter, mock_html_formatter, ): cloze = Cloze(['part'], 'hint', number=1) (when(mock_cloze_part_formatter).called_with('part').then_return( 'formatted part')) (when(mock_html_formatter).called_with('hint').then_return('html hint')) assert format_cloze(cloze) == '{{c1::formatted part::html hint}}'
def test_cloze_formatter_joins_cloze_parts( format_cloze, mock_cloze_part_formatter, ): (when(mock_cloze_part_formatter).called_with('part 1').then_return( 'formatted part 1')) (when(mock_cloze_part_formatter).called_with('part 2').then_return( 'formatted part 2')) cloze = Cloze(['part 1', 'part 2'], number=1) formatted_cloze = format_cloze(cloze) assert formatted_cloze == '{{c1::formatted part 1formatted part 2}}'
def test_return_note_when_cloze_part( roam_note_builder, mock_roam_parser, mock_source_builder, ): block_json = block('{block text}') parent_json = page(block_json) note_parts = [Cloze(['cloze content'])] when(mock_roam_parser).called_with('{block text}').then_return(note_parts) (when(mock_source_builder).called_with( block_json, [parent_json]).then_return('source')) roam_note = roam_note_builder(block_json, [parent_json]) assert roam_note == RoamBlock(note_parts, 'source')
def test_make_note(mock_html_formatter): cloze = Cloze(['content']) roam_block = RoamBlock([cloze], 'source') cloze_enumerator = mock(ClozeEnumerator) numbered_cloze = replace(cloze, number=1) when(cloze_enumerator).called_with([cloze]).then_return([numbered_cloze]) roam_parts_formatter = mock(Formatter[Iterable[RoamPart]]) formatted_note = '{{c1::content}}' (when(roam_parts_formatter).called_with([numbered_cloze ]).then_return(formatted_note)) when(mock_html_formatter).called_with('source').then_return('html source') note_maker = AnkiNoteMaker( cloze_enumerator, roam_parts_formatter, mock_html_formatter, ) assert note_maker(roam_block) == AnkiNote(formatted_note, 'html source')
def test_simple_note_with_newline(): assert parse_roam_block('{con\ntent}') == [Cloze(['con\ntent'])]
def test_parse_cloze_containing_math(): parts = parse_roam_block(r'{$$\textrm{math}$$}') assert parts == [Cloze([Math(r'\textrm{math}')])]
def test_note_with_text_then_cloze(): assert parse_roam_block('text{c1|content}') == [ 'text', Cloze(['content'], number=1) ]
def test_note_with_cloze_then_text(): assert parse_roam_block('{c1|content} text') == [ Cloze(['content'], number=1), ' text' ]
def test_cloze_formatter_raises_value_error_on_unnumbered_cloze(format_cloze): with pytest.raises(ValueError): format_cloze(Cloze(['part']))
def test_unnumbered_then_numbered_cloze(cloze_enumerator): unnumbered_cloze = Cloze(['content2']) numbered_cloze = Cloze(['content1'], number=1) result = list(cloze_enumerator([unnumbered_cloze, numbered_cloze])) assert result == [replace(unnumbered_cloze, number=2), numbered_cloze]
def test_just_unnumbered_cloze(cloze_enumerator): unnumbered_cloze = Cloze(['content']) assert list(cloze_enumerator([unnumbered_cloze])) == [ replace(unnumbered_cloze, number=1)]
def test_cloze_formatter_raises_value_error_on_zero_numbered_cloze( format_cloze, ): with pytest.raises(ValueError): format_cloze(Cloze(['part'], number=0))
def test_parse_cloze(): assert parse_roam_block('{cloze}') == [Cloze(['cloze'])]
def test_parse_cloze_containing_code_block(): parts = parse_roam_block('{```co``de```}') assert parts == [Cloze([CodeBlock('co``de')])]
def test_parse_cloze_containing_code_inline(): parts = parse_roam_block('{`code``code`}') assert parts == [Cloze([CodeInline('code'), CodeInline('code')])]
def test_just_numbered_cloze(cloze_enumerator): numbered_cloze = Cloze(['content'], number=2) assert list(cloze_enumerator([numbered_cloze])) == [numbered_cloze]
def test_simple_note_with_hint(): assert parse_roam_block('{content|hint}') == [Cloze(['content'], 'hint')]
def test_numbered_then_unnumbered_cloze(cloze_enumerator): numbered_cloze = Cloze(['content2'], number=2) unnumbered_cloze = Cloze(['content1']) result = list(cloze_enumerator([numbered_cloze, unnumbered_cloze])) assert result == [numbered_cloze, replace(unnumbered_cloze, number=1)]
def test_note_with_cloze_number(): assert parse_roam_block('{c0|content}') == [Cloze(['content'], number=0)]
def test_renumber_invalid_numbered_cloze(cloze_enumerator): cloze = Cloze(['content'], number=0) result = list(cloze_enumerator([cloze])) assert result == [replace(cloze, number=1)]
def test_note_with_cloze_number_and_hint(): assert parse_roam_block('{c0|content|hint}') == [ Cloze(['content'], 'hint', number=0) ]