test_update_website.py 4.02 KB
Newer Older
hackhard's avatar
hackhard committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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_count = 50
        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)
hackhard's avatar
hackhard committed
49
        self.assertEqual(website_data[0], self.db_website_query.first().url)
hackhard's avatar
hackhard committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    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)
hackhard's avatar
hackhard committed
68
        self.assertEqual(website_data[0], self.db_website_query.first().url)
hackhard's avatar
hackhard committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    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

hackhard's avatar
hackhard committed
83
84
        # # Check if the url table is empty
        # self.assertEqual(self.db_website_query.count(), 0)
hackhard's avatar
hackhard committed
85
86
87
88
89

        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)
hackhard's avatar
hackhard committed
90
        self.assertEqual(self.db_website_query.first().url, website_data[0])
hackhard's avatar
hackhard committed
91
92
93
94
95
96

        # 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)
hackhard's avatar
hackhard committed
97
        self.assertEqual(self.db_website_query.first().url, website_data[0])
hackhard's avatar
hackhard committed
98
99
100
101
102
103
104
105
106
107
108

        # 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)