Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from JLpython-py/refactor/PyTest
Browse files Browse the repository at this point in the history
Refactor/py test
  • Loading branch information
JacobLee23 authored Mar 23, 2021
2 parents 22047e9 + 417c7df commit 66e877b
Show file tree
Hide file tree
Showing 5 changed files with 629 additions and 1,082 deletions.
49 changes: 26 additions & 23 deletions FanGraphs/leaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(self, browser="chromium"):
self.page = self.__browser.new_page(
accept_downloads=True
)
self.page.goto(self.address)
self.page.goto(self.address, timeout=0)

self.soup = None
self.__refresh_parser()
Expand Down Expand Up @@ -433,6 +433,7 @@ def __init__(self, *, browser="chromium"):
self.__browser = browser_ctx.launch()
self.page = self.__browser.new_page()
self.page.goto(self.address, timeout=0)
self.page.wait_for_selector(".fg-data-grid.undefined")

self.soup = None
self.__refresh_parser()
Expand Down Expand Up @@ -497,42 +498,48 @@ def list_options(self, query: str):
def current_option(self, query: str):
"""
Retrieves the option(s) which the filter query is currently set to.
*Note: Some dropdown- and split-class filter queries can be configured to multiple options.
Since this is the case, a list is returned, even though there may only be one option.*
Most dropdown- and split-class filter queries can be configured to multiple options.
For those filter classes, a list is returned, while other filter classes return a string.
- Selection-class: ``str``
- Dropdown-class: ``list``
- Split-class: ``list``
- Switch-class: ``str``
:param query: The filter query being retrieved of its current option
:return: The option(s) which the filter query is currently set to
:rtype: list
:rtype: str or list
:raises FanGraphs.exceptions.InvalidFilterQuery: Argument ``query`` is invalid
"""
query = query.lower()
options = []
option = []
if query in self.__selections:
for sel in self.__selections[query]:
elem = self.soup.select(sel)[0]
if "isActive" in elem.get("class"):
options = [elem.getText()]
option = elem.getText()
break
elif query in self.__dropdowns:
elems = self.soup.select(
f"{self.__dropdowns[query]} ul li"
)
for elem in elems:
if "highlight-selection" in elem.get("class"):
options.append(elem.getText())
option.append(elem.getText())
elif query in self.__splits:
elems = self.soup.select(
f"{self.__splits[query]} ul li"
)
for elem in elems:
if "highlight-selection" in elem.get("class"):
options.append(elem.getText())
option.append(elem.getText())
elif query in self.__switches:
elem = self.soup.select(self.__switches[query])
options = ["True" if "isActive" in elem[0].get("class") else "False"]
option = "True" if "isActive" in elem[0].get("class") else "False"
else:
raise FanGraphs.exceptions.InvalidFilterQueryException(query)
return options
return option

def configure(self, query: str, option: str, *, autoupdate=False):
"""
Expand Down Expand Up @@ -610,7 +617,7 @@ def __configure_split(self, query: str, option: str):
elem = self.page.query_selector_all(f"{self.__splits[query]} ul li")[index]
elem.click()

def __configure_switch(self, query, option):
def __configure_switch(self, query: str, option: str):
"""
Configures a switch-class filter query ``query`` to an option ``option``.
Expand Down Expand Up @@ -654,8 +661,7 @@ def list_filter_groups(self):
:return: Names of the groups of filter queries
:rtype: list
"""
selector = ".fgBin.splits-bin-controller div"
elems = self.soup.select(selector)
elems = self.soup.select(".fgBin.splits-bin-controller div")
groups = [e.getText() for e in elems]
return groups

Expand Down Expand Up @@ -780,25 +786,22 @@ def __sortby(self, sortby, *, reverse=False):
Sorts the data by the appropriate table header.
:param sortby: The table header to sort the data by
:param reverse: If ``True``, the organizatino of the data will be reversed
:param reverse: If ``True``, the organization of the data will be reversed
"""
selector = ".table-scroll thead tr th"
elems = self.soup.select(selector)
elems = self.soup.select(".table-scroll thead tr th")
options = [e.getText() for e in elems]
index = options.index(sortby)
option = self.page.query_selector_all(selector)[index]
option.click()
elems[index].click()
if reverse:
option.click()
elems[index].click()

def __write_table_headers(self, writer: csv.writer):
"""
Writes the data table headers to the CSV file.
:param writer: The ``csv.writer`` object
"""
selector = ".table-scroll thead tr th"
elems = self.soup.select(selector)
elems = self.soup.select(".table-scroll thead tr th")
headers = [e.getText() for e in elems]
writer.writerow(headers)

Expand All @@ -808,8 +811,7 @@ def __write_table_rows(self, writer: csv.writer):
:param writer: The ``csv.writer`` object
"""
selector = ".table-scroll tbody tr"
row_elems = self.soup.select(selector)
row_elems = self.soup.select(".table-scroll tbody tr")
for row in row_elems:
elems = row.select("td")
items = [e.getText() for e in elems]
Expand Down Expand Up @@ -895,6 +897,7 @@ def __init__(self, *, browser="chromium"):
self.__browser = browser_ctx.launch()
self.page = self.__browser.new_page()
self.page.goto(self.address)
self.page.wait_for_selector(".fg-data-grid.undefined")

self.soup = None
self.__refresh_parsers()
Expand Down
2 changes: 2 additions & 0 deletions FanGraphs/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! python3
# tests/__init__.py
Loading

0 comments on commit 66e877b

Please sign in to comment.