Skip to content

Commit

Permalink
utils(tests): check cache access of finge
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvrs committed Dec 26, 2023
1 parent 93d8ce1 commit b7e9bb7
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions api/utils/tests/test_web_scraping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
from utils import web_scraping as wbp
from django.urls import reverse
from pathlib import Path
from django.core.cache import cache
import random
import json


class WebScrapingTest(APITestCase):

def setUp(self):
# Clean cache
for key in cache.keys("*"):
cache.delete(key)

def cookie(self):
cookie = ""
for _ in range(32):
cookie += random.choice(letters + digits)

return cookie

def make_disciplines_request(self, path_name: str):
def generate_args(self, path_name: str):
current_path = Path(__file__).parents[1].absolute()
infos_path = current_path / f"mock/infos.json"

Expand All @@ -29,20 +36,36 @@ def make_disciplines_request(self, path_name: str):

url = reverse(f'utils:sigaa', kwargs={"path": path_name})
args = [department, year, period, url, self.client, self.cookie()]
disciplines = wbp.get_department_disciplines(*args)

return args

def make_disciplines_request(self, path_name: str):
args = self.generate_args(path_name)

scraper = wbp.DisciplineWebScraper(*args)
disciplines = scraper.get_disciplines()

return disciplines

def create_fingerprint(self, path_name: str):
args = self.generate_args(path_name)

scraper = wbp.DisciplineWebScraper(*args)

return scraper.create_page_fingerprint()

def test_get_list_of_departments(self):
response = self.client.get(reverse('utils:sigaa', kwargs={"path": "sigaa"}))
response = self.client.get(
reverse('utils:sigaa', kwargs={"path": "sigaa"}))

departments = wbp.get_list_of_departments(response)
self.assertEqual(type(list()), type(departments))
if len(departments):
self.assertEqual(type(str()), type(departments[0]))

def test_get_list_of_departments_when_empty(self):
response = self.client.get(reverse('utils:sigaa', kwargs={"path": "empty"}))
response = self.client.get(
reverse('utils:sigaa', kwargs={"path": "empty"}))

departments = wbp.get_list_of_departments(response)
self.assertIsNone(departments)
Expand Down Expand Up @@ -70,4 +93,24 @@ def test_get_department_disciplines_when_without_tr_html_tag(self):
disciplines = self.make_disciplines_request('table')

self.assertFalse(len(disciplines))


def test_do_not_find_nonexisting_fingerprint(self):
cache_value = cache.get('0000/2023.1')

self.assertEqual(cache_value, None)

def test_find_existing_fingerprint(self):
fingerprint = self.create_fingerprint('sigaa')

key = '0000/2023.1'
cache.set(key, fingerprint)

self.assertEqual(cache.get(key), fingerprint)

def test_find_existing_fingerprint_from_empty(self):
fingerprint = self.create_fingerprint('empty')

key = '0001/2023.1'
cache.set(key, fingerprint)

self.assertEqual(cache.get(key), 'not_content')

0 comments on commit b7e9bb7

Please sign in to comment.