1# -*- coding: utf-8 -*-
2from decimal import Decimal
3import unittest
4
5from wikipedia import wikipedia
6from request_mock_data import mock_data
7
8
9# mock out _wiki_request
10def _wiki_request(params):
11  return mock_data["_wiki_request calls"][tuple(sorted(params.items()))]
12wikipedia._wiki_request = _wiki_request
13
14
15class TestPageSetUp(unittest.TestCase):
16  """Test the functionality of wikipedia.page's __init__ and load functions."""
17
18  def test_missing(self):
19    """Test that page raises a PageError for a nonexistant page."""
20    # Callicarpa?
21    purpleberry = lambda: wikipedia.page("purpleberry", auto_suggest=False)
22    self.assertRaises(wikipedia.PageError, purpleberry)
23
24  def test_redirect_true(self):
25    """Test that a page successfully redirects a query."""
26    # no error should be raised if redirect is test_redirect_true
27    mp = wikipedia.page("Menlo Park, New Jersey")
28
29    self.assertEqual(mp.title, "Edison, New Jersey")
30    self.assertEqual(mp.url, "http://en.wikipedia.org/wiki/Edison,_New_Jersey")
31
32  def test_redirect_false(self):
33    """Test that page raises an error on a redirect when redirect == False."""
34    mp = lambda: wikipedia.page("Menlo Park, New Jersey", auto_suggest=False, redirect=False)
35    self.assertRaises(wikipedia.RedirectError, mp)
36
37  def test_redirect_no_normalization(self):
38    """Test that a page with redirects but no normalization query loads correctly"""
39    the_party = wikipedia.page("Communist Party", auto_suggest=False)
40    self.assertIsInstance(the_party, wikipedia.WikipediaPage)
41    self.assertEqual(the_party.title, "Communist party")
42
43  def test_redirect_with_normalization(self):
44    """Test that a page redirect with a normalized query loads correctly"""
45    the_party = wikipedia.page("communist Party", auto_suggest=False)
46    self.assertIsInstance(the_party, wikipedia.WikipediaPage)
47    self.assertEqual(the_party.title, "Communist party")
48
49  def test_redirect_normalization(self):
50    """Test that a page redirect loads correctly with or without a query normalization"""
51    capital_party = wikipedia.page("Communist Party", auto_suggest=False)
52    lower_party = wikipedia.page("communist Party", auto_suggest=False)
53
54    self.assertIsInstance(capital_party, wikipedia.WikipediaPage)
55    self.assertIsInstance(lower_party, wikipedia.WikipediaPage)
56    self.assertEqual(capital_party.title, "Communist party")
57    self.assertEqual(capital_party, lower_party)
58
59  def test_disambiguate(self):
60    """Test that page raises an error when a disambiguation page is reached."""
61    try:
62      ram = wikipedia.page("Dodge Ram (disambiguation)", auto_suggest=False, redirect=False)
63      error_raised = False
64    except wikipedia.DisambiguationError as e:
65      error_raised = True
66      options = e.options
67
68    self.assertTrue(error_raised)
69    self.assertEqual(options, [u'Dodge Ramcharger', u'Dodge Ram Van', u'Dodge Mini Ram', u'Dodge Caravan C/V', u'Dodge Caravan C/V', u'Ram C/V', u'Dodge Ram 50', u'Dodge D-Series', u'Dodge Rampage', u'Ram (brand)'])
70
71  def test_auto_suggest(self):
72    """Test that auto_suggest properly corrects a typo."""
73    # yum, butter.
74    butterfly = wikipedia.page("butteryfly")
75
76    self.assertEqual(butterfly.title, "Butterfly")
77    self.assertEqual(butterfly.url, "http://en.wikipedia.org/wiki/Butterfly")
78
79
80class TestPage(unittest.TestCase):
81  """Test the functionality of the rest of wikipedia.page."""
82
83  def setUp(self):
84    # shortest wikipedia articles with images and sections
85    self.celtuce = wikipedia.page("Celtuce")
86    self.cyclone = wikipedia.page("Tropical Depression Ten (2005)")
87    self.great_wall_of_china = wikipedia.page("Great Wall of China")
88
89  def test_from_page_id(self):
90    """Test loading from a page id"""
91    self.assertEqual(self.celtuce, wikipedia.page(pageid=1868108))
92
93  def test_title(self):
94    """Test the title."""
95    self.assertEqual(self.celtuce.title, "Celtuce")
96    self.assertEqual(self.cyclone.title, "Tropical Depression Ten (2005)")
97
98  def test_url(self):
99    """Test the url."""
100    self.assertEqual(self.celtuce.url, "http://en.wikipedia.org/wiki/Celtuce")
101    self.assertEqual(self.cyclone.url, "http://en.wikipedia.org/wiki/Tropical_Depression_Ten_(2005)")
102
103  def test_content(self):
104    """Test the plain text content."""
105    self.assertEqual(self.celtuce.content, mock_data['data']["celtuce.content"])
106    self.assertEqual(self.cyclone.content, mock_data['data']["cyclone.content"])
107
108  def test_revision_id(self):
109    """Test the revision id."""
110    self.assertEqual(self.celtuce.revision_id, mock_data['data']["celtuce.revid"])
111    self.assertEqual(self.cyclone.revision_id, mock_data['data']["cyclone.revid"])
112
113  def test_parent_id(self):
114    """Test the parent id."""
115    self.assertEqual(self.celtuce.parent_id, mock_data['data']["celtuce.parentid"])
116    self.assertEqual(self.cyclone.parent_id, mock_data['data']["cyclone.parentid"])
117
118
119  def test_summary(self):
120    """Test the summary."""
121    self.assertEqual(self.celtuce.summary, mock_data['data']["celtuce.summary"])
122    self.assertEqual(self.cyclone.summary, mock_data['data']["cyclone.summary"])
123
124  def test_images(self):
125    """Test the list of image URLs."""
126    self.assertEqual(sorted(self.celtuce.images), mock_data['data']["celtuce.images"])
127    self.assertEqual(sorted(self.cyclone.images), mock_data['data']["cyclone.images"])
128
129  def test_references(self):
130    """Test the list of reference URLs."""
131    self.assertEqual(self.celtuce.references, mock_data['data']["celtuce.references"])
132    self.assertEqual(self.cyclone.references, mock_data['data']["cyclone.references"])
133
134  def test_links(self):
135    """Test the list of titles of links to Wikipedia pages."""
136    self.assertEqual(self.celtuce.links, mock_data['data']["celtuce.links"])
137    self.assertEqual(self.cyclone.links, mock_data['data']["cyclone.links"])
138
139  def test_categories(self):
140    """Test the list of categories of Wikipedia pages."""
141    self.assertEqual(self.celtuce.categories, mock_data['data']["celtuce.categories"])
142    self.assertEqual(self.cyclone.categories, mock_data['data']["cyclone.categories"])
143
144  def test_html(self):
145    """Test the full HTML method."""
146    self.assertEqual(self.celtuce.html(), mock_data['data']["celtuce.html"])
147
148  def test_sections(self):
149    """Test the list of section titles."""
150    self.assertEqual(sorted(self.cyclone.sections), mock_data['data']["cyclone.sections"])
151
152  def test_section(self):
153    """Test text content of a single section."""
154    self.assertEqual(self.cyclone.section("Impact"), mock_data['data']["cyclone.section.impact"])
155    self.assertEqual(self.cyclone.section("History"), None)
156
157  def test_coordinates(self):
158    """Test geo coordinates of a page"""
159    lat, lon = self.great_wall_of_china.coordinates
160    self.assertEqual(str(lat.quantize(Decimal('1.000'))), mock_data['data']['great_wall_of_china.coordinates.lat'])
161    self.assertEqual(str(lon.quantize(Decimal('1.000'))), mock_data['data']['great_wall_of_china.coordinates.lon'])
162