From b7e9bb775afaa7524d60f93b147a0ac210db33a4 Mon Sep 17 00:00:00 2001 From: mateuvrs Date: Tue, 26 Dec 2023 20:53:38 -0300 Subject: [PATCH] utils(tests): check cache access of finge --- api/utils/tests/test_web_scraping.py | 53 +++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/api/utils/tests/test_web_scraping.py b/api/utils/tests/test_web_scraping.py index 00edeb85..c9cf46bb 100644 --- a/api/utils/tests/test_web_scraping.py +++ b/api/utils/tests/test_web_scraping.py @@ -4,11 +4,18 @@ 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): @@ -16,7 +23,7 @@ def cookie(self): 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" @@ -29,12 +36,27 @@ 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)) @@ -42,7 +64,8 @@ def test_get_list_of_departments(self): 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) @@ -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)) - \ No newline at end of file + + 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')