Skip to content

Commit 14d1011

Browse files
authored
Merge pull request #354 from tejhande/patch-2
Enhance tests for FetchNode with mocking
2 parents 5dc6165 + 320f13f commit 14d1011

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

tests/nodes/fetch_node_test.py

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,104 @@
11
import os
22
import pytest
3+
from unittest.mock import patch, MagicMock
34
from scrapegraphai.nodes import FetchNode
45

5-
def test_fetch_node_html():
6+
def get_file_path(file_name):
67
"""
7-
Run the tests
8+
Helper function to get the absolute file path.
89
"""
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()
920
fetch_node = FetchNode(
1021
input="url | local_dir",
1122
output=["doc"],
1223
node_config={
1324
"headless": False
1425
}
1526
)
16-
1727
state = {
1828
"url": "https://twitter.com/home"
1929
}
20-
2130
result = fetch_node.execute(state)
22-
2331
assert result is not None
32+
mock_execute.assert_called_once_with(state)
2433

25-
def test_fetch_node_json():
34+
@patch('scrapegraphai.nodes.FetchNode.execute')
35+
def test_fetch_node_json(mock_execute):
2636
"""
27-
Run the tests
37+
Test FetchNode with JSON input.
2838
"""
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")
3341
state_json = {
3442
"json": file_path_json
3543
}
36-
3744
fetch_node_json = FetchNode(
3845
input="json",
3946
output=["doc"],
4047
)
41-
4248
result_json = fetch_node_json.execute(state_json)
43-
4449
assert result_json is not None
50+
mock_execute.assert_called_once_with(state_json)
4551

46-
def test_fetch_node_xml():
52+
@patch('scrapegraphai.nodes.FetchNode.execute')
53+
def test_fetch_node_xml(mock_execute):
4754
"""
48-
Run the tests
55+
Test FetchNode with XML input.
4956
"""
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")
5459
state_xml = {
5560
"xml": file_path_xml
5661
}
57-
5862
fetch_node_xml = FetchNode(
5963
input="xml",
6064
output=["doc"],
6165
)
62-
6366
result_xml = fetch_node_xml.execute(state_xml)
64-
6567
assert result_xml is not None
68+
mock_execute.assert_called_once_with(state_xml)
6669

67-
def test_fetch_node_csv():
70+
@patch('scrapegraphai.nodes.FetchNode.execute')
71+
def test_fetch_node_csv(mock_execute):
6872
"""
69-
Run the tests
73+
Test FetchNode with CSV input.
7074
"""
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")
7577
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
7779
}
78-
7980
fetch_node_csv = FetchNode(
8081
input="csv",
8182
output=["doc"],
8283
)
83-
8484
result_csv = fetch_node_csv.execute(state_csv)
85-
8685
assert result_csv is not None
86+
mock_execute.assert_called_once_with(state_csv)
8787

88-
def test_fetch_node_txt():
88+
@patch('scrapegraphai.nodes.FetchNode.execute')
89+
def test_fetch_node_txt(mock_execute):
8990
"""
90-
Run the tests
91+
Test FetchNode with TXT input.
9192
"""
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")
9695
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
9897
}
99-
10098
fetch_node_txt = FetchNode(
10199
input="txt",
102100
output=["doc"],
103101
)
104-
105102
result_txt = fetch_node_txt.execute(state_txt)
106-
107103
assert result_txt is not None
104+
mock_execute.assert_called_once_with(state_txt)

0 commit comments

Comments
 (0)