summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authormarc <a01200356@itesm.mx>2016-12-15 00:34:43 -0600
committermarc <a01200356@itesm.mx>2016-12-15 00:40:21 -0600
commitaf35eee10b98940c51c6e5e18629de514b4bd48d (patch)
tree804b0a4cfe08bb897541e9e8571b921a78e07992 /tests
parente0c270bd72f7b2a40222e3ed264e25d36cb0fc30 (diff)
downloadsearxng-af35eee10b98940c51c6e5e18629de514b4bd48d.tar.gz
searxng-af35eee10b98940c51c6e5e18629de514b4bd48d.zip
tests for _fetch_supported_languages in engines
and refactor method to make it testable without making requests
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/engines/test_bing.py32
-rw-r--r--tests/unit/engines/test_dailymotion.py37
-rw-r--r--tests/unit/engines/test_duckduckgo.py14
-rw-r--r--tests/unit/engines/test_gigablast.py25
-rw-r--r--tests/unit/engines/test_google.py57
-rw-r--r--tests/unit/engines/test_swisscows.py27
-rw-r--r--tests/unit/engines/test_wikipedia.py93
-rw-r--r--tests/unit/engines/test_yahoo.py30
8 files changed, 315 insertions, 0 deletions
diff --git a/tests/unit/engines/test_bing.py b/tests/unit/engines/test_bing.py
index 886584229..61f8629d0 100644
--- a/tests/unit/engines/test_bing.py
+++ b/tests/unit/engines/test_bing.py
@@ -86,3 +86,35 @@ class TestBingEngine(SearxTestCase):
self.assertEqual(results[0]['title'], 'This should be the title')
self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
self.assertEqual(results[0]['content'], 'This should be the content.')
+
+ def test_fetch_supported_languages(self):
+ html = """<html></html>"""
+ response = mock.Mock(text=html)
+ results = bing._fetch_supported_languages(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
+
+ html = """
+ <html>
+ <body>
+ <form>
+ <div id="limit-languages">
+ <div>
+ <div><input id="es" value="es"></input></div>
+ </div>
+ <div>
+ <div><input id="pt_BR" value="pt_BR"></input></div>
+ <div><input id="pt_PT" value="pt_PT"></input></div>
+ </div>
+ </div>
+ </form>
+ </body>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = bing._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 3)
+ self.assertIn('es', languages)
+ self.assertIn('pt-BR', languages)
+ self.assertIn('pt-PT', languages)
diff --git a/tests/unit/engines/test_dailymotion.py b/tests/unit/engines/test_dailymotion.py
index 368b3a7a5..72071af66 100644
--- a/tests/unit/engines/test_dailymotion.py
+++ b/tests/unit/engines/test_dailymotion.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import dailymotion
@@ -72,3 +73,39 @@ class TestDailymotionEngine(SearxTestCase):
results = dailymotion.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
+
+ def test_fetch_supported_languages(self):
+ json = r"""
+ {"list":[{"code":"af","name":"Afrikaans","native_name":"Afrikaans",
+ "localized_name":"Afrikaans","display_name":"Afrikaans"},
+ {"code":"ar","name":"Arabic","native_name":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629",
+ "localized_name":"Arabic","display_name":"Arabic"},
+ {"code":"la","name":"Latin","native_name":null,
+ "localized_name":"Latin","display_name":"Latin"}
+ ]}
+ """
+ response = mock.Mock(text=json)
+ languages = dailymotion._fetch_supported_languages(response)
+ self.assertEqual(type(languages), dict)
+ self.assertEqual(len(languages), 3)
+ self.assertIn('af', languages)
+ self.assertIn('ar', languages)
+ self.assertIn('la', languages)
+
+ self.assertEqual(type(languages['af']), dict)
+ self.assertEqual(type(languages['ar']), dict)
+ self.assertEqual(type(languages['la']), dict)
+
+ self.assertIn('name', languages['af'])
+ self.assertIn('name', languages['ar'])
+ self.assertNotIn('name', languages['la'])
+
+ self.assertIn('english_name', languages['af'])
+ self.assertIn('english_name', languages['ar'])
+ self.assertIn('english_name', languages['la'])
+
+ self.assertEqual(languages['af']['name'], 'Afrikaans')
+ self.assertEqual(languages['af']['english_name'], 'Afrikaans')
+ self.assertEqual(languages['ar']['name'], u'العربية')
+ self.assertEqual(languages['ar']['english_name'], 'Arabic')
+ self.assertEqual(languages['la']['english_name'], 'Latin')
diff --git a/tests/unit/engines/test_duckduckgo.py b/tests/unit/engines/test_duckduckgo.py
index 2aeaa1880..7d6abad22 100644
--- a/tests/unit/engines/test_duckduckgo.py
+++ b/tests/unit/engines/test_duckduckgo.py
@@ -84,3 +84,17 @@ class TestDuckduckgoEngine(SearxTestCase):
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
self.assertEqual(results[0]['content'], 'This should be the content.')
+
+ def test_fetch_supported_languages(self):
+ js = """some code...regions:{
+ "wt-wt":"All Results","ar-es":"Argentina","au-en":"Australia","at-de":"Austria","be-fr":"Belgium (fr)"
+ }some more code..."""
+ response = mock.Mock(text=js)
+ languages = duckduckgo._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 5)
+ self.assertIn('wt-WT', languages)
+ self.assertIn('es-AR', languages)
+ self.assertIn('en-AU', languages)
+ self.assertIn('de-AT', languages)
+ self.assertIn('fr-BE', languages)
diff --git a/tests/unit/engines/test_gigablast.py b/tests/unit/engines/test_gigablast.py
index 0723b064c..74bf89333 100644
--- a/tests/unit/engines/test_gigablast.py
+++ b/tests/unit/engines/test_gigablast.py
@@ -89,3 +89,28 @@ class TestGigablastEngine(SearxTestCase):
self.assertEqual(results[0]['title'], 'South by Southwest 2016')
self.assertEqual(results[0]['url'], 'www.sxsw.com')
self.assertEqual(results[0]['content'], 'This should be the content.')
+
+ def test_fetch_supported_languages(self):
+ html = """<html></html>"""
+ response = mock.Mock(text=html)
+ results = gigablast._fetch_supported_languages(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
+
+ html = """
+ <html>
+ <body>
+ <span id="menu2">
+ <a href="/search?&rxikd=1&qlang=xx"></a>
+ <a href="/search?&rxikd=1&qlang=en"></a>
+ <a href="/search?&rxikd=1&qlang=fr"></a>
+ </span>
+ </body>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = gigablast._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 2)
+ self.assertIn('en', languages)
+ self.assertIn('fr', languages)
diff --git a/tests/unit/engines/test_google.py b/tests/unit/engines/test_google.py
index c83eb3bf0..0d56b1e7b 100644
--- a/tests/unit/engines/test_google.py
+++ b/tests/unit/engines/test_google.py
@@ -177,3 +177,60 @@ class TestGoogleEngine(SearxTestCase):
self.assertEqual(results[0]['title'], '')
self.assertEqual(results[0]['content'], '')
self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')
+
+ def test_fetch_supported_languages(self):
+ html = """<html></html>"""
+ response = mock.Mock(text=html)
+ languages = google._fetch_supported_languages(response)
+ self.assertEqual(type(languages), dict)
+ self.assertEqual(len(languages), 0)
+
+ html = u"""
+ <html>
+ <body>
+ <table>
+ <tbody>
+ <tr>
+ <td>
+ <font>
+ <label>
+ <span id="ten">English</span>
+ </label>
+ </font>
+ </td>
+ <td>
+ <font>
+ <label>
+ <span id="tzh-CN">中文 (简体)</span>
+ </label>
+ <label>
+ <span id="tzh-TW">中文 (繁體)</span>
+ </label>
+ </font>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </body>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = google._fetch_supported_languages(response)
+ self.assertEqual(type(languages), dict)
+ self.assertEqual(len(languages), 3)
+
+ self.assertIn('en', languages)
+ self.assertIn('zh-CN', languages)
+ self.assertIn('zh-TW', languages)
+
+ self.assertEquals(type(languages['en']), dict)
+ self.assertEquals(type(languages['zh-CN']), dict)
+ self.assertEquals(type(languages['zh-TW']), dict)
+
+ self.assertIn('name', languages['en'])
+ self.assertIn('name', languages['zh-CN'])
+ self.assertIn('name', languages['zh-TW'])
+
+ self.assertEquals(languages['en']['name'], 'English')
+ self.assertEquals(languages['zh-CN']['name'], u'中文 (简体)')
+ self.assertEquals(languages['zh-TW']['name'], u'中文 (繁體)')
diff --git a/tests/unit/engines/test_swisscows.py b/tests/unit/engines/test_swisscows.py
index dbbc044da..27f33d70a 100644
--- a/tests/unit/engines/test_swisscows.py
+++ b/tests/unit/engines/test_swisscows.py
@@ -126,3 +126,30 @@ class TestSwisscowsEngine(SearxTestCase):
self.assertEqual(results[2]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg')
self.assertEqual(results[2]['img_src'], 'http://ts2.mm.This/should.png')
self.assertEqual(results[2]['template'], 'images.html')
+
+ def test_fetch_supported_languages(self):
+ html = """<html></html>"""
+ response = mock.Mock(text=html)
+ languages = swisscows._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 0)
+
+ html = """
+ <html>
+ <div id="regions-popup">
+ <div>
+ <ul>
+ <li><a data-val="browser"></a></li>
+ <li><a data-val="de-CH"></a></li>
+ <li><a data-val="fr-CH"></a></li>
+ </ul>
+ </div>
+ </div>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = swisscows._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 3)
+ self.assertIn('de-CH', languages)
+ self.assertIn('fr-CH', languages)
diff --git a/tests/unit/engines/test_wikipedia.py b/tests/unit/engines/test_wikipedia.py
index 0057277c5..988080b6a 100644
--- a/tests/unit/engines/test_wikipedia.py
+++ b/tests/unit/engines/test_wikipedia.py
@@ -164,3 +164,96 @@ class TestWikipediaEngine(SearxTestCase):
self.assertEqual(len(results), 2)
self.assertEqual(results[1]['infobox'], u'披頭四樂隊')
self.assertIn(u'披头士乐队...', results[1]['content'])
+
+ def test_fetch_supported_languages(self):
+ html = u"""<html></html>"""
+ response = mock.Mock(text=html)
+ languages = wikipedia._fetch_supported_languages(response)
+ self.assertEqual(type(languages), dict)
+ self.assertEqual(len(languages), 0)
+
+ html = u"""
+ <html>
+ <body>
+ <div>
+ <div>
+ <h3>Table header</h3>
+ <table class="sortable jquery-tablesorter">
+ <thead>
+ <tr>
+ <th>N</th>
+ <th>Language</th>
+ <th>Language (local)</th>
+ <th>Wiki</th>
+ <th>Articles</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>2</td>
+ <td><a>Swedish</a></td>
+ <td><a>Svenska</a></td>
+ <td><a>sv</a></td>
+ <td><a><b>3000000</b></a></td>
+ </tr>
+ <tr>
+ <td>3</td>
+ <td><a>Cebuano</a></td>
+ <td><a>Sinugboanong Binisaya</a></td>
+ <td><a>ceb</a></td>
+ <td><a><b>3000000</b></a></td>
+ </tr>
+ </tbody>
+ </table>
+ <h3>Table header</h3>
+ <table class="sortable jquery-tablesorter">
+ <thead>
+ <tr>
+ <th>N</th>
+ <th>Language</th>
+ <th>Language (local)</th>
+ <th>Wiki</th>
+ <th>Articles</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>2</td>
+ <td><a>Norwegian (Bokmål)</a></td>
+ <td><a>Norsk (Bokmål)</a></td>
+ <td><a>no</a></td>
+ <td><a><b>100000</b></a></td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </body>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = wikipedia._fetch_supported_languages(response)
+ self.assertEqual(type(languages), dict)
+ self.assertEqual(len(languages), 3)
+
+ self.assertIn('sv', languages)
+ self.assertIn('ceb', languages)
+ self.assertIn('no', languages)
+
+ self.assertEqual(type(languages['sv']), dict)
+ self.assertEqual(type(languages['ceb']), dict)
+ self.assertEqual(type(languages['no']), dict)
+
+ self.assertIn('name', languages['sv'])
+ self.assertIn('english_name', languages['sv'])
+ self.assertIn('articles', languages['sv'])
+
+ self.assertEqual(languages['sv']['name'], 'Svenska')
+ self.assertEqual(languages['sv']['english_name'], 'Swedish')
+ self.assertEqual(languages['sv']['articles'], 3000000)
+ self.assertEqual(languages['ceb']['name'], 'Sinugboanong Binisaya')
+ self.assertEqual(languages['ceb']['english_name'], 'Cebuano')
+ self.assertEqual(languages['ceb']['articles'], 3000000)
+ self.assertEqual(languages['no']['name'], u'Norsk (Bokmål)')
+ self.assertEqual(languages['no']['english_name'], u'Norwegian (Bokmål)')
+ self.assertEqual(languages['no']['articles'], 100000)
diff --git a/tests/unit/engines/test_yahoo.py b/tests/unit/engines/test_yahoo.py
index 303295e2f..82c4d99bb 100644
--- a/tests/unit/engines/test_yahoo.py
+++ b/tests/unit/engines/test_yahoo.py
@@ -147,3 +147,33 @@ class TestYahooEngine(SearxTestCase):
results = yahoo.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
+
+ def test_fetch_supported_languages(self):
+ html = """<html></html>"""
+ response = mock.Mock(text=html)
+ results = yahoo._fetch_supported_languages(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
+
+ html = """
+ <html>
+ <div>
+ <div id="yschlang">
+ <span>
+ <label><input value="lang_ar"></input></label>
+ </span>
+ <span>
+ <label><input value="lang_zh_chs"></input></label>
+ <label><input value="lang_zh_cht"></input></label>
+ </span>
+ </div>
+ </div>
+ </html>
+ """
+ response = mock.Mock(text=html)
+ languages = yahoo._fetch_supported_languages(response)
+ self.assertEqual(type(languages), list)
+ self.assertEqual(len(languages), 3)
+ self.assertIn('ar', languages)
+ self.assertIn('zh-chs', languages)
+ self.assertIn('zh-cht', languages)