add test_sort_by.py (and theme_compare.py and add function to

database.py)
This commit is contained in:
Trent Palmer 2021-08-24 18:37:10 -07:00
parent b622235a39
commit 152074386e
3 changed files with 140 additions and 0 deletions

View File

@ -40,6 +40,18 @@ def get_themes_orderedby_cname():
Hugothemes).order_by(asc(func.lower(Hugothemes.cname))).all() Hugothemes).order_by(asc(func.lower(Hugothemes.cname))).all()
def get_themes_as_dicts_of_sortable_columns():
session = sessionmaker(bind=engine)()
return [
{
'name': x.cname,
'date': x.commit_date[0:10],
'stars': str(x.stargazers_count),
'min_ver': '' if x.min_ver is None else x.min_ver,
'license': x.theme_license,
} for x in session.query(Hugothemes).all()]
def get_themes(): def get_themes():
session = sessionmaker(bind=engine)() session = sessionmaker(bind=engine)()
return session.query(Hugothemes).all() return session.query(Hugothemes).all()

85
test/test_sort_by.py Normal file
View File

@ -0,0 +1,85 @@
from test.test_selenium import TestSelenium
from unittest import TestCase
from test.database import get_themes_as_dicts_of_sortable_columns
from itertools import permutations
from test.theme_compare import theme_compare
from functools import cmp_to_key
from bs4 import BeautifulSoup
SBB = [
'sortByDate',
'sortByStars',
'sortByName',
'sortByMinVer',
'sortByLicense',
] # SBB -> sort_by_buttons
class TestsSortBy(TestSelenium, TestCase):
def setUp(self):
super(TestsSortBy, self).setUp()
for x in [
'plus-button',
'button-for-showing-columns',
'commit-column-selection-input',
'min_ver-column-selection-input',
'license-column-selection-input',
'button-for-showing-sort-option',
]:
self.driver.find_element_by_id(x).click()
self.themes = get_themes_as_dicts_of_sortable_columns()
self.tc = len(self.themes)
def test_sort_by(self):
for y in permutations(SBB):
'''
every time you click a sort_by radio button
that button moves to the far left
and then the table rows are re-sorted cumulatively
'''
'''
so first click all the sort_by radio buttons in the
reverse order of the current permutation
'''
for x in y[::-1]:
self.driver.find_element_by_id(x).click()
sort_by_inputs = [x.get_attribute(
'id') for x in self.driver.find_element_by_id(
'sortByRow').find_elements_by_tag_name('input')]
'''
and then assert that the sort_by button row is now
in the same order left->right, as the current
permutation
'''
self.assertEqual(sort_by_inputs, list(y))
'''
then sort the list of themes we pulled from the database
'''
self.themes.sort(
key=cmp_to_key(lambda a, b: theme_compare(a, b, y)))
results_table_div = self.driver.find_element_by_id('results')
rows = BeautifulSoup(results_table_div.get_attribute(
'innerHTML'), features='lxml').find('table').findAll('tr')
'''
and finally compare the list of themes to the contents of
the html table in order...PHEW!
'''
for i, row in enumerate(rows[1:]):
tds = row.findAll('td')
tds_txt = [x.text for x in tds]
self.assertEqual(
tds_txt,
[
self.themes[i]['name'],
self.themes[i]['date'],
self.themes[i]['stars'],
self.themes[i]['min_ver'],
self.themes[i]['license'],
],
)

43
test/theme_compare.py Normal file
View File

@ -0,0 +1,43 @@
import re
MATCH = re.compile(r'\d+')
def compare_jk(j, k):
if j == k: return 0
else: return 1 if j < k else -1
def semver_split(item):
split_list = [MATCH.search(i).group() if
len(item) > 0 else '0' for i in item.split('.')]
if len(split_list) == 1: split_list.append('0')
if len(split_list) == 2: split_list.append('0')
return [int(x) for x in split_list]
def compare_theme(x, y, sort_key):
if sort_key == 'sortByName':
return compare_jk(y['name'].lower(), x['name'].lower())
elif sort_key == 'sortByStars':
return compare_jk(int(x['stars']), int(y['stars']))
elif sort_key == 'sortByMinVer':
x_list = semver_split(x['min_ver'])
y_list = semver_split(y['min_ver'])
return compare_jk(x_list, y_list)
elif sort_key == 'sortByDate':
return compare_jk(x['date'], y['date'])
elif sort_key == 'sortByLicense':
return compare_jk(y['license'].lower(), x['license'].lower())
else:
return 0
def theme_compare(a, b, y):
if len(y) == 0: return -1
rslt = compare_theme(a, b, y[0])
return theme_compare(a, b, y[1:]) if rslt == 0 else rslt