|
1 | 1 | import os
|
2 | 2 | import pytest
|
| 3 | +from unittest.mock import patch, MagicMock |
3 | 4 | from scrapegraphai.nodes import FetchNode
|
4 | 5 |
|
5 |
| -def test_fetch_node_html(): |
| 6 | +def get_file_path(file_name): |
6 | 7 | """
|
7 |
| - Run the tests |
| 8 | + Helper function to get the absolute file path. |
8 | 9 | """
|
| 10 | + curr_dir = os.path.dirname(os.path.realpath(__file__)) |
| 11 | + file_path = os.path.join(curr_dir, file_name) |
| 12 | + return file_path |
| 13 | + |
| 14 | +@patch('scrapegraphai.nodes.FetchNode.execute') |
| 15 | +def test_fetch_node_html(mock_execute): |
| 16 | + """ |
| 17 | + Test FetchNode with HTML input. |
| 18 | + """ |
| 19 | + mock_execute.return_value = MagicMock() |
9 | 20 | fetch_node = FetchNode(
|
10 | 21 | input="url | local_dir",
|
11 | 22 | output=["doc"],
|
12 | 23 | node_config={
|
13 | 24 | "headless": False
|
14 | 25 | }
|
15 | 26 | )
|
16 |
| - |
17 | 27 | state = {
|
18 | 28 | "url": "https://twitter.com/home"
|
19 | 29 | }
|
20 |
| - |
21 | 30 | result = fetch_node.execute(state)
|
22 |
| - |
23 | 31 | assert result is not None
|
| 32 | + mock_execute.assert_called_once_with(state) |
24 | 33 |
|
25 |
| -def test_fetch_node_json(): |
| 34 | +@patch('scrapegraphai.nodes.FetchNode.execute') |
| 35 | +def test_fetch_node_json(mock_execute): |
26 | 36 | """
|
27 |
| - Run the tests |
| 37 | + Test FetchNode with JSON input. |
28 | 38 | """
|
29 |
| - FILE_NAME_JSON = "inputs/example.json" |
30 |
| - curr_dir = os.path.dirname(os.path.realpath(__file__)) |
31 |
| - file_path_json = os.path.join(curr_dir, FILE_NAME_JSON) |
32 |
| - |
| 39 | + mock_execute.return_value = MagicMock() |
| 40 | + file_path_json = get_file_path("inputs/example.json") |
33 | 41 | state_json = {
|
34 | 42 | "json": file_path_json
|
35 | 43 | }
|
36 |
| - |
37 | 44 | fetch_node_json = FetchNode(
|
38 | 45 | input="json",
|
39 | 46 | output=["doc"],
|
40 | 47 | )
|
41 |
| - |
42 | 48 | result_json = fetch_node_json.execute(state_json)
|
43 |
| - |
44 | 49 | assert result_json is not None
|
| 50 | + mock_execute.assert_called_once_with(state_json) |
45 | 51 |
|
46 |
| -def test_fetch_node_xml(): |
| 52 | +@patch('scrapegraphai.nodes.FetchNode.execute') |
| 53 | +def test_fetch_node_xml(mock_execute): |
47 | 54 | """
|
48 |
| - Run the tests |
| 55 | + Test FetchNode with XML input. |
49 | 56 | """
|
50 |
| - FILE_NAME_XML = "inputs/books.xml" |
51 |
| - curr_dir = os.path.dirname(os.path.realpath(__file__)) |
52 |
| - file_path_xml = os.path.join(curr_dir, FILE_NAME_XML) |
53 |
| - |
| 57 | + mock_execute.return_value = MagicMock() |
| 58 | + file_path_xml = get_file_path("inputs/books.xml") |
54 | 59 | state_xml = {
|
55 | 60 | "xml": file_path_xml
|
56 | 61 | }
|
57 |
| - |
58 | 62 | fetch_node_xml = FetchNode(
|
59 | 63 | input="xml",
|
60 | 64 | output=["doc"],
|
61 | 65 | )
|
62 |
| - |
63 | 66 | result_xml = fetch_node_xml.execute(state_xml)
|
64 |
| - |
65 | 67 | assert result_xml is not None
|
| 68 | + mock_execute.assert_called_once_with(state_xml) |
66 | 69 |
|
67 |
| -def test_fetch_node_csv(): |
| 70 | +@patch('scrapegraphai.nodes.FetchNode.execute') |
| 71 | +def test_fetch_node_csv(mock_execute): |
68 | 72 | """
|
69 |
| - Run the tests |
| 73 | + Test FetchNode with CSV input. |
70 | 74 | """
|
71 |
| - FILE_NAME_CSV = "inputs/username.csv" |
72 |
| - curr_dir = os.path.dirname(os.path.realpath(__file__)) |
73 |
| - file_path_csv = os.path.join(curr_dir, FILE_NAME_CSV) |
74 |
| - |
| 75 | + mock_execute.return_value = MagicMock() |
| 76 | + file_path_csv = get_file_path("inputs/username.csv") |
75 | 77 | state_csv = {
|
76 |
| - "csv": file_path_csv # Definire un dizionario con la chiave "csv" e il valore come percorso del file CSV |
| 78 | + "csv": file_path_csv |
77 | 79 | }
|
78 |
| - |
79 | 80 | fetch_node_csv = FetchNode(
|
80 | 81 | input="csv",
|
81 | 82 | output=["doc"],
|
82 | 83 | )
|
83 |
| - |
84 | 84 | result_csv = fetch_node_csv.execute(state_csv)
|
85 |
| - |
86 | 85 | assert result_csv is not None
|
| 86 | + mock_execute.assert_called_once_with(state_csv) |
87 | 87 |
|
88 |
| -def test_fetch_node_txt(): |
| 88 | +@patch('scrapegraphai.nodes.FetchNode.execute') |
| 89 | +def test_fetch_node_txt(mock_execute): |
89 | 90 | """
|
90 |
| - Run the tests |
| 91 | + Test FetchNode with TXT input. |
91 | 92 | """
|
92 |
| - FILE_NAME_TXT = "inputs/plain_html_example.txt" |
93 |
| - curr_dir = os.path.dirname(os.path.realpath(__file__)) |
94 |
| - file_path_txt = os.path.join(curr_dir, FILE_NAME_TXT) |
95 |
| - |
| 93 | + mock_execute.return_value = MagicMock() |
| 94 | + file_path_txt = get_file_path("inputs/plain_html_example.txt") |
96 | 95 | state_txt = {
|
97 |
| - "txt": file_path_txt # Definire un dizionario con la chiave "txt" e il valore come percorso del file TXT |
| 96 | + "txt": file_path_txt |
98 | 97 | }
|
99 |
| - |
100 | 98 | fetch_node_txt = FetchNode(
|
101 | 99 | input="txt",
|
102 | 100 | output=["doc"],
|
103 | 101 | )
|
104 |
| - |
105 | 102 | result_txt = fetch_node_txt.execute(state_txt)
|
106 |
| - |
107 | 103 | assert result_txt is not None
|
| 104 | + mock_execute.assert_called_once_with(state_txt) |
0 commit comments