Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb503e6

Browse files
lucasmouradarius
authored andcommitted
Use mock to remove network requirement from tests (aimacode#495)
1 parent 34409d9 commit fb503e6

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

tests/test_nlp.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import pytest
22
import nlp
3+
34
from nlp import loadPageHTML, stripRawHTML, findOutlinks, onlyWikipediaURLS
45
from nlp import expand_pages, relevant_pages, normalize, ConvergenceDetector, getInlinks
56
from nlp import getOutlinks, Page
67
from nlp import Rules, Lexicon
78
# Clumsy imports because we want to access certain nlp.py globals explicitly, because
89
# they are accessed by function's within nlp.py
910

11+
from unittest.mock import patch
12+
from io import BytesIO
13+
1014

1115
def test_rules():
1216
assert Rules(A="B C | D E") == {'A': [['B', 'C'], ['D', 'E']]}
@@ -27,6 +31,19 @@ def test_lexicon():
2731
< href="/wiki/TestThing" > href="/wiki/TestBoy"
2832
href="/wiki/TestLiving" href="/wiki/TestMan" >"""
2933
testHTML2 = "Nothing"
34+
testHTML3 = """
35+
<!DOCTYPE html>
36+
<html>
37+
<head>
38+
<title>Page Title</title>
39+
</head>
40+
<body>
41+
42+
<p>AIMA book</p>
43+
44+
</body>
45+
</html>
46+
"""
3047

3148
pA = Page("A", 1, 6, ["B", "C", "E"], ["D"])
3249
pB = Page("B", 2, 5, ["E"], ["A", "C", "D"])
@@ -52,12 +69,14 @@ def test_lexicon():
5269
# assert all(loadedPages.get(key,"") != "" for key in addresses)
5370

5471

55-
def test_stripRawHTML():
72+
@patch('urllib.request.urlopen', return_value=BytesIO(testHTML3.encode()))
73+
def test_stripRawHTML(html_mock):
5674
addr = "https://en.wikipedia.org/wiki/Ethics"
5775
aPage = loadPageHTML([addr])
5876
someHTML = aPage[addr]
5977
strippedHTML = stripRawHTML(someHTML)
6078
assert "<head>" not in strippedHTML and "</head>" not in strippedHTML
79+
assert "AIMA book" in someHTML and "AIMA book" in strippedHTML
6180

6281

6382
def test_determineInlinks():

0 commit comments

Comments
 (0)