-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BeautifulSoup logic in separate file #56
Open
MaxxxZu
wants to merge
14
commits into
master
Choose a base branch
from
issue-55-Move_out_Be_logic_to_sep_PR
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5ec6547
Inital commit
MaxxxZu 50efe5f
test_bshelper_instance_created added
MaxxxZu e1e46d2
get_bs_object func & tests added
MaxxxZu 8ac0977
TestBsSafeSelect and bs_safe_select added
MaxxxZu 42ffea0
TestBsSafeget and bs_safe_get func added
MaxxxZu ec0dcb5
get_bs_object renamed to bs_object
MaxxxZu ca84c3e
Deleted setUp in TestBSHelperInstance
MaxxxZu 4234720
Testclasses for methods renamed
MaxxxZu 6342d7a
Test_bs_object refactored
MaxxxZu 6e6b987
test_bs_safe_select_return_expected_text_with_many_selectors refactored
MaxxxZu 3faccda
self.expected added in Test_bs_safe_select& expected_text varin test_…
MaxxxZu 34358c8
expected_text to Test_bs_safe_get setUp
MaxxxZu b789612
expected_attribute to setUp
MaxxxZu 06a5df1
mocked_soup.get.return_value ti setUp
MaxxxZu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from bs4 import BeautifulSoup | ||
|
||
|
||
class BSHelper(object): | ||
""" | ||
The help class for BeautifulSoup library | ||
""" | ||
def __init__(self, html): | ||
self.html = html | ||
|
||
@property | ||
def bs_object(self): | ||
""" | ||
Utility funtcion: | ||
- Accepts html and checks its validity using BeautifulSoup library, | ||
return BS object or False | ||
""" | ||
try: | ||
soup = BeautifulSoup(self.html, 'html.parser') | ||
except TypeError: | ||
return False | ||
return soup | ||
|
||
def bs_safe_select(self, html, *args): | ||
""" | ||
Utility function used to get a content string from a | ||
HTML and tuple of selectors. Returns False | ||
if no object is found for the given selector | ||
""" | ||
for arg in args: | ||
selectedElems = html.select_one(arg) | ||
if selectedElems is not None: | ||
return selectedElems | ||
return False | ||
|
||
def bs_safe_get(self, html, attribute): | ||
""" | ||
Utility function used to get a content string from a | ||
HTML and attribute. Returns False | ||
if no object is found for the given selector | ||
""" | ||
element = html.get(attribute) | ||
if element is not None: | ||
return element | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import unittest | ||
import sys | ||
import os | ||
|
||
from unittest.mock import patch, PropertyMock | ||
|
||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
'..'))) | ||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
'..', 'smarsy'))) | ||
# excluding following line for linter as it complains that | ||
# from import is supposed to be at the top of the file | ||
|
||
from smarsy.bs_helper import BSHelper # noqa | ||
|
||
|
||
class TestBSHelperInstance(unittest.TestCase): | ||
def test_bshelper_instance_created(self): | ||
html = 'some html' | ||
source_page = BSHelper(html) | ||
self.assertEqual(source_page.html, html) | ||
|
||
|
||
class Test_bs_object(unittest.TestCase): | ||
@patch('smarsy.bs_helper.BeautifulSoup', new_callable=PropertyMock) | ||
def test_bs_object_called_with_expected_html(self, mocked_soup): | ||
html = '<tr></tr>' | ||
source_page = BSHelper(html).bs_object | ||
mocked_soup.assert_called_with(html, 'html.parser') | ||
|
||
@patch('smarsy.bs_helper.BeautifulSoup', side_effect=TypeError) | ||
def test_bs_object_return_false_with_unexpected_html( | ||
self, mocked_soup): | ||
source_page = BSHelper(12345) | ||
self.assertFalse(source_page.bs_object) | ||
|
||
|
||
class Test_bs_safe_select(unittest.TestCase): | ||
@patch('smarsy.bs_helper.BeautifulSoup') | ||
def setUp(self, mocked_soup): | ||
self.source_page = BSHelper('some html') | ||
self.mocked_soup = mocked_soup | ||
self.mocked_soup.select_one.return_value = 'some text' | ||
self.selector = 'some_tag' | ||
self.selectors = 'some_tag1', 'some_tag2', 'some_tag3' | ||
self.select_one_values = ('some text1', 'some text2', 'some text3') | ||
self.expected = 'some text' | ||
|
||
def test_bs_safe_select_return_expected_text_with_single_selector(self): | ||
actual = self.source_page.bs_safe_select(self.mocked_soup, | ||
self.selector) | ||
self.assertEqual(actual, self.expected) | ||
|
||
def test_bs_safe_select_return_expected_text_with_many_selectors(self): | ||
select_one = None | ||
for select_one_value in self.select_one_values: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the test is re-producing the same logic as in the function that is not good. This test is bad, but before answering what exactly is bad you need to answer the question posted under this function's source code. |
||
select_one = select_one_value | ||
self.mocked_soup.select_one.return_value = select_one | ||
actual = self.source_page.bs_safe_select(self.mocked_soup, | ||
self.selectors) | ||
self.assertEqual(actual, select_one) | ||
dkultasev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_bs_safe_select_return_false_when_no_object_is_found( | ||
self): | ||
self.mocked_soup.select_one.return_value = '' | ||
self.assertFalse(self.source_page.bs_safe_select(self.mocked_soup, | ||
self.selector)) | ||
|
||
|
||
class Test_bs_safe_get(unittest.TestCase): | ||
@patch('smarsy.bs_helper.BeautifulSoup') | ||
def setUp(self, mocked_soup): | ||
self.source_page = BSHelper('some html') | ||
self.mocked_soup = mocked_soup | ||
self.expected_text = 'some text' | ||
self.expected_attribute = 'some attribute' | ||
self.mocked_soup.get.return_value = 'some text' | ||
|
||
def test_bs_get_called_with_expected_html_and_attribute(self): | ||
self.source_page.bs_safe_get(self.mocked_soup, self.expected_attribute) | ||
self.mocked_soup.get.assert_called_with(self.expected_attribute) | ||
|
||
def test_bs_safe_get_return_false_when_element_is_empty( | ||
self): | ||
self.mocked_soup.get.return_value = '' | ||
self.assertFalse(self.source_page.bs_safe_get(self.mocked_soup, | ||
self.expected_attribute)) | ||
|
||
def test_bs_safe_get_return_expected_text(self): | ||
actual = self.source_page.bs_safe_get(self.mocked_soup, | ||
self.expected_attribute) | ||
self.assertEqual(actual, self.expected_text) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't it always take the last output of the
select_one
? It's not adding, for every iteration it re-assignsselectedElems
with the new value. No? or is it expected?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is what is expected. Each new iteration overrides a variable selectedElems
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then I don't get it, does it supposed to return:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we take the object, apply the method select_one with selector 1 to it, then apply the method select_one with selector 2 to the received object, then apply the method select_one with the selector X to the received object and return the object or False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please provide real example from smarsy website? expected call with expected result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From issue#51
<TD valign=top align="left" width="120"><img src="https://smarsy.ua/images/mypage/parent_1.png"></TD>
We must find td with valign=top and in received object find img[src]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and what would be the function call for that html?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
html.select_one([valign=top]).select_one('img[src]')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will it work? Additionally here you are passing single value parameter, but in your function you are expecting array. Please provide an example with array