Skip to content

Instantly share code, notes, and snippets.

@AABur
Last active April 15, 2021 11:09
Show Gist options
  • Select an option

  • Save AABur/99746d981e71978ef5d3738bbe4a1546 to your computer and use it in GitHub Desktop.

Select an option

Save AABur/99746d981e71978ef5d3738bbe4a1546 to your computer and use it in GitHub Desktop.
# coding: utf-8
import lxml.html
from .abstract import get_strategy
import pytest
@pytest.fixture
def standard_selectors():
return {
"lvl0": "h1",
"lvl1": "h2",
"lvl2": "h3",
"content": "p"
}
class TestPageRank:
def test_default_page_rank_should_be_zero(self, standard_selectors):
# Given
strategy = get_strategy({
'selectors': standard_selectors
})
strategy.dom = lxml.html.fromstring("""
<html><body>
<h1>Foo</h1>
</body></html>
""")
# When
actual = strategy.get_records_from_dom()
# Then
assert actual[0]['weight']['page_rank'] == 0
def test_positive_page_rank(self, standard_selectors):
# Given
strategy = get_strategy({
'start_urls': [{
'url': 'http://foo.bar/api',
'page_rank': 1
}],
'selectors': standard_selectors
})
strategy.dom = lxml.html.fromstring("""
<html><body>
<h1>Foo</h1>
</body></html>
""")
# When
actual = strategy.get_records_from_dom("http://foo.bar/api")
# Then
assert actual[0]['weight']['page_rank'] == 1
def test_positive_sub_page_page_rank(self, standard_selectors):
# Given
strategy = get_strategy({
'start_urls': [{
'url': 'http://foo.bar/api',
'page_rank': 1
}],
'selectors': standard_selectors
})
strategy.dom = lxml.html.fromstring("""
<html><body>
<h1>Foo</h1>
</body></html>
""")
# When
actual = strategy.get_records_from_dom("http://foo.bar/api/test")
# Then
assert actual[0]['weight']['page_rank'] == 1
def test_negative_page_rank(self, standard_selectors):
# Given
strategy = get_strategy({
'start_urls': [{
'url': 'http://foo.bar/api',
'page_rank': -1
}],
'selectors': standard_selectors
})
strategy.dom = lxml.html.fromstring("""
<html><body>
<h1>Foo</h1>
</body></html>
""")
# When
actual = strategy.get_records_from_dom("http://foo.bar/api/test")
# Then
assert actual[0]['weight']['page_rank'] == -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment