82 lines
4.1 KiB
Python
82 lines
4.1 KiB
Python
import os
|
||
import tempfile
|
||
import unittest
|
||
|
||
import ara
|
||
import internet
|
||
|
||
SAYFA = '''
|
||
<div class="result results_links results_links_deep web-result ">
|
||
<h2 class="result__title"><a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fornek.com%2Fbir%3Fa%3D1&rut=x">Bir <b>başlık</b></a></h2>
|
||
<a class="result__snippet" href="//x">Kısa & öz <b>özet</b></a>
|
||
</div>
|
||
<div class="result results_links web-result ">
|
||
<a class="result__a" href="https://duckduckgo.com/y.js?ad=1">Reklam</a>
|
||
</div>
|
||
<div class="result results_links web-result ">
|
||
<a class="result__a" href="https://ornek.org/iki">İkinci</a>
|
||
</div>
|
||
'''
|
||
|
||
|
||
class AraTest(unittest.TestCase):
|
||
def test_results_are_parsed_and_ads_skipped(self):
|
||
self.assertEqual(ara.sonuclari_coz(SAYFA), [("Bir başlık", "https://ornek.com/bir?a=1", "Kısa & öz özet"),
|
||
("İkinci", "https://ornek.org/iki", "")])
|
||
self.assertEqual(len(ara.sonuclari_coz(SAYFA, adet=1)), 1)
|
||
|
||
def test_search_output_and_empty_result(self):
|
||
metin = ara.ara("x", indirici=lambda adres: (SAYFA, "text/html"))
|
||
self.assertIn("1. Bir başlık\n https://ornek.com/bir?a=1", metin)
|
||
self.assertIn("Sonuç bulunamadı", ara.ara("x", indirici=lambda adres: ('<div class="no-results">no results</div>', "text/html")))
|
||
|
||
def test_bot_challenge_uses_one_rss_backup_not_false_no_results(self):
|
||
calls = []
|
||
def indirici(adres):
|
||
calls.append(adres)
|
||
return (('<form id="challenge-form">Bot challenge</form>' if len(calls) == 1 else
|
||
'<rss><channel><item><title>OmniRoute</title><link>https://ornek.com/</link></item></channel></rss>'), 'text/html')
|
||
self.assertIn('https://ornek.com/', ara.ara('OmniRoute', indirici=indirici))
|
||
self.assertEqual(len(calls), 2)
|
||
|
||
def test_two_invalid_services_report_failure_not_no_results(self):
|
||
with self.assertRaisesRegex(RuntimeError, 'Arama hizmeti'):
|
||
ara.ara('x', indirici=lambda adres: ('<html>challenge</html>', 'text/html'))
|
||
|
||
def test_page_is_turned_into_text_and_cut(self):
|
||
sayfa = "<html><head><style>a{}</style><script>x()</script></head><body><nav>menü</nav><p>Bir & iki</p><div>üç</div></body></html>"
|
||
self.assertEqual(ara.oku("ornek.com", indirici=lambda adres: (sayfa, "text/html")), "Bir & iki\nüç")
|
||
uzun = ara.oku("https://ornek.com", en_cok=10, indirici=lambda adres: ("<p>" + "a" * 50 + "</p>", "text/html"))
|
||
self.assertTrue(uzun.startswith("a" * 10 + "\n[... kesildi: 50"))
|
||
|
||
def test_main_reports_errors_without_raising(self):
|
||
self.assertEqual(ara.main(["oku", "http://127.0.0.1:9/yok"]), 1)
|
||
|
||
|
||
class InternetTest(unittest.TestCase):
|
||
def test_note_names_both_commands_and_is_added_once(self):
|
||
with tempfile.TemporaryDirectory() as d:
|
||
betik = os.path.join(d, "ara.py")
|
||
open(betik, "w").close()
|
||
metin = internet.talimat(None, betik, python="py.exe")
|
||
on = '& ' if os.name == 'nt' else ''
|
||
self.assertIn(f'{on}"py.exe" "{betik}" ara "sorgu"', metin)
|
||
self.assertIn(f'"py.exe" "{betik}" oku <adres>', metin)
|
||
req = {"input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "araştır"}]}]}
|
||
self.assertTrue(internet.ekle(req, metin))
|
||
self.assertFalse(internet.ekle(req, metin))
|
||
self.assertEqual(req["input"][0]["role"], "developer")
|
||
self.assertEqual(len(req["input"]), 2)
|
||
self.assertIsNone(internet.talimat({"enabled": False}, betik))
|
||
self.assertIsNone(internet.talimat(None, os.path.join(d, "ara.py"))) # betik yoksa not eklenmez
|
||
|
||
def test_windowless_python_is_swapped_for_the_console_one(self):
|
||
with tempfile.TemporaryDirectory() as d:
|
||
open(os.path.join(d, "python.exe"), "w").close()
|
||
self.assertEqual(internet.python_yolu(os.path.join(d, "pythonw.exe")), os.path.join(d, "python.exe"))
|
||
self.assertEqual(internet.python_yolu(os.path.join("yok", "pythonw.exe")), os.path.join("yok", "pythonw.exe"))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|