Commit 63b12e60 authored by hackhard's avatar hackhard Committed by Barkin Simsek
Browse files

Typo fixed and tests added

parent 98da2ed8
......@@ -12,7 +12,7 @@ from captchamonitor.utils.website_parser import WebsiteParser
class UpdateWebsite:
"""
FFetches alexa topsites and moz500 website and parses the list of urls in the website and inserts the urls listed there into the
Fetches Alexa topsites and Moz500 website and parses the list of urls in the website and inserts the urls listed there into the
database
"""
......@@ -87,7 +87,7 @@ class UpdateWebsite:
def update(self) -> None:
"""
Fetches alexa topsites and moz500 website and parses the list of urls in the website.
Fetches Alexa topsites and Moz500 website and parses the list of urls in the website.
Later, adds the websites to the database.
"""
website = WebsiteParser()
......@@ -96,5 +96,5 @@ class UpdateWebsite:
website_list = list(website.uniq_website_list)
self.__insert_website_into_db(website_list)
self.__logger.info(
"Done with updating the unique website list of both moz and alexa sites"
"Done with updating the unique website list of both Moz and Alexa sites"
)
import unittest
from datetime import datetime
import pytest
from freezegun import freeze_time
from captchamonitor.utils.config import Config
from captchamonitor.utils.models import URL
from captchamonitor.utils.database import Database
from captchamonitor.core.update_website import UpdateWebsite
from captchamonitor.utils.website_parser import WebsiteParser
class TestUpdateWebsite(unittest.TestCase):
def setUp(self):
self.config = Config()
self.database = Database(
self.config["db_host"],
self.config["db_port"],
self.config["db_name"],
self.config["db_user"],
self.config["db_password"],
)
self.db_session = self.database.session()
self.db_website_query = self.db_session.query(URL)
self.alexa_url = "google.com"
self.alexa_url_count = 50
self.moz_url = "youtube.com"
self.moz_url_count = 500
def tearDown(self):
self.db_session.close()
def test__insert_alexa_website_into_db(self):
update_website = UpdateWebsite(
config=self.config, db_session=self.db_session, auto_update=False
)
# Get website data
website_list = WebsiteParser()
website_list.get_alexa_top_50()
website_data = website_list.website_list
# Check if the url table is empty
self.assertEqual(self.db_website_query.count(), 0)
update_website._UpdateWebsite__insert_website_into_db(website_data)
# Check if the url table was populated with correct data
self.assertEqual(self.db_website_query.count(), self.alexa_url_count)
self.assertEqual(self.db_website_query.first().url, self.alexa_url)
def test__insert_moz_website_into_db(self):
update_website = UpdateWebsite(
config=self.config, db_session=self.db_session, auto_update=False
)
# Get website data
website_list = WebsiteParser()
website_list.get_moz_top_500()
website_data = website_list.website_list
# Check if the url table is empty
self.assertEqual(self.db_website_query.count(), 0)
update_website._UpdateWebsite__insert_website_into_db(website_data)
# Check if the url table was populated with correct data
self.assertEqual(self.db_website_query.count(), self.moz_url_count)
self.assertEqual(self.db_website_query.first().url, self.moz_url)
def test_update_url_init_with_already_populated_table(self):
# Prepopulate the table
update_website = UpdateWebsite(
config=self.config, db_session=self.db_session, auto_update=False
)
# Check if the url table is empty
self.assertEqual(self.db_website_query.count(), 0)
# Get website data
website_list = WebsiteParser()
website_list.get_alexa_top_50()
website_data = website_list.website_list
# Check if the url table is empty
self.assertEqual(self.db_website_query.count(), 0)
update_website._UpdateWebsite__insert_website_into_db(website_data)
# Check if the url table was populated with correct data
self.assertEqual(self.db_website_query.count(), self.alexa_url_count)
self.assertEqual(self.db_website_query.first().url, self.alexa_url)
# Try inserting the same url again with different details
update_website._UpdateWebsite__insert_website_into_db(website_data)
# Make sure there still only one url
self.assertEqual(self.db_website_query.count(), self.alexa_url_count)
self.assertEqual(self.db_website_query.first().url, self.alexa_url)
# Add lists from moz website on top of alexa websites
website_list.get_moz_top_500()
website_data = website_list.website_list
# Unique length of website
unique_length_of_website = len(website_list.uniq_website_list)
update_website._UpdateWebsite__insert_website_into_db(website_data)
# Check if the count of url table is equal to the length of unique websites
self.assertEqual(self.db_website_query.count(), unique_length_of_website)
......@@ -22,12 +22,16 @@ class TestWebsiteParser(unittest.TestCase):
website.get_alexa_top_50()
self.assertEqual(website.number_of_websites, self.get_alexa_top_50_number)
self.assertIn(self.valid_website_alexa, website.website_list)
with self.assertRaises(AssertionError):
self.assertIn(self.invalid_website_alexa, website.website_list)
def test_get_moz_top_500(self):
website = WebsiteParser()
website.get_moz_top_500()
self.assertEqual(website.number_of_websites, self.get_moz_top_500_number)
self.assertIn(self.valid_website_moz, website.website_list)
with self.assertRaises(AssertionError):
self.assertIn(self.invalid_website_moz, website.website_list)
def test_get_all_website(self):
website = WebsiteParser()
......@@ -36,4 +40,11 @@ class TestWebsiteParser(unittest.TestCase):
self.assertEqual(website.number_of_websites, self.get_total_website_number)
self.assertIn(self.valid_website_moz, website.uniq_website_list)
self.assertIn(self.valid_website_alexa, website.uniq_website_list)
self.assertTrue(len(website.uniq_website_list) >= 500)
self.assertTrue(
len(website.uniq_website_list) >= 500
and len(website.uniq_website_list) <= 550
)
with self.assertRaises(AssertionError):
self.assertIn(self.invalid_website_alexa, website.website_list)
with self.assertRaises(AssertionError):
self.assertIn(self.invalid_website_moz, website.website_list)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment