Compare commits

..

2 commits

Author SHA1 Message Date
c354aeb183
Fix tests after moving PoolManager to shared module 2024-04-28 14:07:30 +04:00
ab93124337
shared -> http_pool 2024-04-28 14:06:20 +04:00
3 changed files with 10 additions and 8 deletions

View file

@ -2,7 +2,7 @@ import re
from lxml import html, etree # type: ignore from lxml import html, etree # type: ignore
import shared import http_pool
# Matches not only latin symbols, # Matches not only latin symbols,
@ -20,7 +20,7 @@ def search(title: str, artist: str) -> str:
'''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL. '''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL.
Irrelevant texts should be picked manually''' Irrelevant texts should be picked manually'''
resp = shared.get_http_pool().request( resp = http_pool.get().request(
'GET', 'GET',
'https://searx.dc09.ru/search', 'https://searx.dc09.ru/search',
fields={ fields={
@ -41,7 +41,7 @@ def parse(url: str) -> str:
'''Requests a lyrics page and parses with libxml2, '''Requests a lyrics page and parses with libxml2,
leaving only text and line breaks''' leaving only text and line breaks'''
resp = shared.get_http_pool().request('GET', url) resp = http_pool.get().request('GET', url)
tree = html.document_fromstring(resp.data) tree = html.document_fromstring(resp.data)
divs = lyrics_xpath(tree) divs = lyrics_xpath(tree)
del resp, tree del resp, tree

View file

@ -3,7 +3,8 @@ from urllib3 import PoolManager
_http: PoolManager | None = None _http: PoolManager | None = None
def get_http_pool() -> PoolManager:
def get() -> PoolManager:
global _http global _http
if _http is None: if _http is None:
_http = PoolManager() _http = PoolManager()

View file

@ -2,6 +2,7 @@ from unittest import TestCase
import urllib3 import urllib3
import genius import genius
import http_pool
TITLE = 'A Line In The Sand' TITLE = 'A Line In The Sand'
@ -26,17 +27,17 @@ Another day'''
class TestGenius(TestCase): class TestGenius(TestCase):
def setUp(self) -> None: def setUp(self) -> None:
self.http = urllib3.PoolManager() http_pool.get()
def test_search_success(self) -> None: def test_search_success(self) -> None:
url = genius.search(self.http, TITLE, ARTIST) url = genius.search(TITLE, ARTIST)
self.assertEqual(url, URL) self.assertEqual(url, URL)
def test_lyrics_parsing(self) -> None: def test_lyrics_parsing(self) -> None:
lyrics = genius.parse(self.http, URL) lyrics = genius.parse(URL)
self.assertTrue(lyrics.startswith(LYR1)) self.assertTrue(lyrics.startswith(LYR1))
self.assertTrue(LYR2 in lyrics) self.assertTrue(LYR2 in lyrics)
self.assertTrue(LYR3 in lyrics) self.assertTrue(LYR3 in lyrics)
def tearDown(self) -> None: def tearDown(self) -> None:
self.http.clear() http_pool.get().clear()