add unittests for database

This commit is contained in:
Trent Palmer 2021-08-21 09:56:14 -07:00
parent 4dec9f77c9
commit c4199c9bc7
4 changed files with 124 additions and 5 deletions

59
test/database.py Normal file
View File

@ -0,0 +1,59 @@
from rank_hugo_themes import (
engine, Hugothemes,
Hugothemes_from_gitlab, sessionmaker
)
def get_theme_count():
session = sessionmaker(bind=engine)()
theme_count = session.query(Hugothemes).count()
print(theme_count)
return theme_count
def get_theme_count_from_gitlab():
session = sessionmaker(bind=engine)()
theme_count_from_gitlab = session.query(Hugothemes_from_gitlab).count()
print(theme_count_from_gitlab)
return theme_count_from_gitlab
def get_newest_update_time():
session = sessionmaker(bind=engine)()
newest_commit_time = session.query(
Hugothemes.commit_date_in_seconds
).order_by(Hugothemes.commit_date_in_seconds.desc()).first()
return newest_commit_time
def get_newest_update_time_from_gitlab():
session = sessionmaker(bind=engine)()
newest_commit_time = session.query(
Hugothemes_from_gitlab.commit_date_in_seconds
).order_by(
Hugothemes_from_gitlab.commit_date_in_seconds.desc()).first()
return newest_commit_time
def get_themes():
session = sessionmaker(bind=engine)()
return session.query(Hugothemes).all()
def get_themes_from_gitlab_table():
session = sessionmaker(bind=engine)()
return session.query(Hugothemes_from_gitlab).all()
def get_themes_from_gitlab_table_by_date():
session = sessionmaker(bind=engine)()
return session.query(
Hugothemes_from_gitlab).order_by(
Hugothemes_from_gitlab.commit_date_in_seconds).all()
def get_gitlab_themes():
session = sessionmaker(bind=engine)()
return session.query(Hugothemes).filter(
Hugothemes.url.contains("gitlab")).order_by(
Hugothemes.commit_date_in_seconds).all()

57
test/test_database.py Normal file
View File

@ -0,0 +1,57 @@
from unittest import TestCase
from test.database import (
get_newest_update_time, get_newest_update_time_from_gitlab,
get_themes_from_gitlab_table_by_date, get_gitlab_themes,
get_themes, get_themes_from_gitlab_table
)
import time
class TestDataBase(TestCase):
def test_newest_commit_time(self):
newest = get_newest_update_time()
current_time = int(time.time())
# print(current_time, newest[0], (current_time - newest[0]))
self.assertTrue((current_time - newest[0]) < 259200)
def test_newest_commit_time_from_gitlab(self):
newest = get_newest_update_time_from_gitlab()
current_time = int(time.time())
self.assertTrue((current_time - newest[0]) < 864000)
def test_duplicates(self):
t_list = get_themes()
t_list_names = [x.name for x in t_list]
t_list_shas = [x.commit_sha for x in t_list]
for x in t_list:
self.assertEqual(t_list_names.count(x.name), 1)
self.assertEqual(t_list_shas.count(x.commit_sha), 1)
class TestGitLabData(TestCase):
def test_duplicates_gitlab_data(self):
t_list = get_themes_from_gitlab_table()
t_list_names = [x.name for x in t_list]
t_list_shas = [x.commit_sha for x in t_list]
for x in t_list:
self.assertEqual(t_list_names.count(x.name), 1)
self.assertEqual(t_list_shas.count(x.commit_sha), 1)
def test_gitlab_data(self):
glt_list = get_themes_from_gitlab_table_by_date()
list_glt = get_gitlab_themes()
self.assertEqual(len(list_glt), len(glt_list))
for i, x in enumerate(glt_list):
self.assertEqual(x.name, list_glt[i].name)
self.assertEqual(x.url, list_glt[i].url)
self.assertEqual(x.commit_sha, list_glt[i].commit_sha)
self.assertEqual(x.commit_date, list_glt[i].commit_date)
self.assertEqual(
x.commit_date_in_seconds,
list_glt[i].commit_date_in_seconds)
self.assertEqual(
x.star_count, list_glt[i].stargazers_count)
self.assertEqual(
x.themes_toml_content,
list_glt[i].themes_toml_content)

View File

@ -1,4 +1,4 @@
import unittest from unittest import TestCase
from pathlib import Path from pathlib import Path
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.service import Service
@ -12,7 +12,7 @@ SOURCE_FILE = Path('hugo-themes-report/hugo-themes-report.html').resolve()
SOURCE_PAGE = f'file://{str(SOURCE_FILE)}' SOURCE_PAGE = f'file://{str(SOURCE_FILE)}'
class TestSelenium(unittest.TestCase): class TestSelenium(TestCase):
def setUp(self): def setUp(self):
self.driver = webdriver.Chrome(options=OPTIONS, service=SERVICE) self.driver = webdriver.Chrome(options=OPTIONS, service=SERVICE)
self.driver.get(SOURCE_PAGE) self.driver.get(SOURCE_PAGE)

View File

@ -1,8 +1,8 @@
from test.test_selenium import TestSelenium from test.test_selenium import TestSelenium
import unittest from unittest import TestCase
class TestTitle(TestSelenium, unittest.TestCase): class TestTitle(TestSelenium, TestCase):
def setUp(self): def setUp(self):
super(TestTitle, self).setUp() super(TestTitle, self).setUp()
self.x = self.driver.find_elements_by_tag_name('h1') self.x = self.driver.find_elements_by_tag_name('h1')
@ -20,5 +20,8 @@ class TestTitle(TestSelenium, unittest.TestCase):
def test_title_anchor(self): def test_title_anchor(self):
x_anchors = self.x[0].find_elements_by_tag_name('a') x_anchors = self.x[0].find_elements_by_tag_name('a')
self.assertEqual(len(x_anchors), 1) self.assertEqual(len(x_anchors), 1)
self.assertEqual(x_anchors[0].get_attribute('href'), 'https://github.com/TrentSPalmer/hugo_themes_report') self.assertEqual(
x_anchors[0].get_attribute('href'),
'https://github.com/TrentSPalmer/hugo_themes_report'
)
self.assertEqual(x_anchors[0].get_attribute('target'), '_blank') self.assertEqual(x_anchors[0].get_attribute('target'), '_blank')