File size: 2,778 Bytes
20e57f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from click.testing import CliRunner
#from summarizer.cli import main
from summarizer.cli import main

def test_cli_with_file(sample_text_file, sample_text, mocker):
    # If using: from .summarizer import process_text in cli.py
    mock_process = mocker.patch('summarizer.cli.process_text')
    mock_process.return_value = "Summarized text"
    
    runner = CliRunner()
    result = runner.invoke(main, ['--file', sample_text_file])

    #print("CLI Output:\n", result.output)  # Print the output for debugging
    #print("sample text:\n", sample_text)

    assert result.exit_code == 0
    assert "Summarized text" in result.output
    mock_process.assert_called_once_with(sample_text.strip(), model="t5-base", max_length=180 )

def test_cli_with_url(mocker):
    #mock_extract = mocker.patch('summarizer.utils.extract_from_url')
    #mock_process = mocker.patch('summarizer.summarizer.process_text')
    mock_extract = mocker.patch('summarizer.cli.extract_from_url')
    mock_process = mocker.patch('summarizer.cli.process_text')

    mock_extract.return_value ="""

    This domain is for use in illustrative examples in documents. You may use this

    domain in literature without prior coordination or asking for permission. More information...

    """
    mock_process.return_value = "Summarized text"
    
    runner = CliRunner()
    result = runner.invoke(main, ['--url', 'http://example.com'])
    #result = runner.invoke(main, ['--url', 'https://en.wikipedia.org/wiki/Seoul'])

    #print("CLI Output:\n", result.output)  # Print the output for debugging
    #result.output = """
    #Fetching text from URL: http://example.com 
    #Starting summarization process...
    # 
    #Summary:
    #================================================================================
    #Summarized text
    #================================================================================
    #""" 
    
    assert result.exit_code == 0
    assert "Summarized text" in result.output

    mock_extract.assert_called_once_with('http://example.com')
    #mock_extract.assert_called_once_with('https://en.wikipedia.org/wiki/Seoul')
    #mock_process.assert_called_once_with("Extracted text", model='t5-base', max_length=180)
    mock_process.assert_called_once_with(mock_extract.return_value, model='t5-base', max_length=180)

def test_cli_no_input():
    runner = CliRunner()
    result = runner.invoke(main, [])
    
    assert result.exit_code != 0
    assert "Please provide either --url or --file" in result.output

def test_cli_invalid_file():
    runner = CliRunner()
    result = runner.invoke(main, ['--file', 'nonexistent.txt'])
    
    assert result.exit_code != 0
    assert "Error" in result.output