forked from OrganicIrradiation/ratebeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
199 lines (169 loc) · 8.39 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# coding: utf-8
import unittest
from ratebeer import RateBeer
from ratebeer import rb_exceptions
class TestBeer(unittest.TestCase):
def is_float(self, s):
''' Checks whether a string represents a float '''
try:
float(s)
return True
except ValueError:
return False
def test_beer(self):
''' Make sure the results for a beer contain the expected data '''
results = RateBeer().beer('/beer/new-belgium-tour-de-fall/279122/')
self.assertIsNotNone(results)
self.assertTrue(results['name'] == u'New Belgium Tour de Fall')
self.assertTrue(results['style'] == u'American Pale Ale')
self.assertTrue(results['ibu'] == 38)
self.assertTrue(results['brewery'].url ==
u'/brewers/new-belgium-brewing-company/77/')
self.assertTrue(results['overall_rating'] <= 100)
self.assertTrue(results['style_rating'] <= 100)
self.assertTrue(results['num_ratings'] >= 0)
self.assertTrue(self.is_float(results['weighted_avg']))
self.assertTrue(results['weighted_avg'] <= 5.0)
self.assertTrue(results['retired'] == False)
def test_beer_with_alias(self):
''' Make sure aliased beer is correctly identified '''
uri = '/beer/camerons-lager/14782/'
with self.assertRaises(rb_exceptions.AliasedBeer) as ab:
rb = RateBeer().beer(uri)
self.assertEqual(ab.exception.oldurl, uri)
self.assertEqual(ab.exception.newurl, '/beer/camerons-lager/395361/')
def test_beer_404(self):
''' Checks to make sure that we appropriately raise a page not found
'''
rb = RateBeer()
self.assertRaises(
rb_exceptions.PageNotFound, rb.beer, "/beer/asdfasdf")
def test_beer_closed_brewery(self):
''' Handling beers from closed brewers '''
results = RateBeer().beer('/beer/hantens-hildener-landbrau/140207/')
self.assertTrue(results['brewery'].url ==
u'/brewers/hildener-landbierbrauerei/12618/')
self.assertFalse(hasattr(results, 'description'))
def test_beer_closed_contract_brewery(self):
''' Handling beers from closed contract brewers '''
results = RateBeer().beer('/beer/crew-republic-x-11-wet-hop/298026/')
self.assertTrue(results['brewery'].url ==
u'/brewers/crew-republic-brewery/13816/')
self.assertTrue(results['brewed_at'].url ==
u'/brewers/hohenthanner-schlossbrauerei/5557/')
def test_beer_contract_brewed(self):
''' Handling contract brewed beers '''
results = RateBeer().beer('/beer/benediktiner-weissbier/157144/')
self.assertTrue(results['brewery'].url ==
u'/brewers/klosterbrauerei-ettal/1943/')
self.assertTrue(results['brewed_at'].url ==
u'/brewers/licher-privatbrauerei-bitburger/1677/')
def test_beer_get_reviews(self):
''' Check to make multi-page review searches work properly '''
reviews = RateBeer().get_beer('/beer/deschutes-inversion-ipa/55610/').\
get_reviews()
for i in range(20):
self.assertIsNotNone(next(reviews))
def test_beer_get_reviews_404(self):
''' Check lazy get_reviews 404 exception '''
beer = RateBeer().get_beer('/beer/asdfasdf')
with self.assertRaises(rb_exceptions.PageNotFound):
next(beer.get_reviews())
def test_beer_unicode(self):
results = RateBeer().beer('/beer/steoji-oktoberbjor/292390/')
self.assertIsNotNone(results)
self.assertTrue(results['name'] == u'Steðji Októberbjór')
self.assertTrue(results['brewery'].name == u'Brugghús Steðja')
self.assertTrue(results['brewery'].url ==
u'/brewers/brugghus-steoja/15310/')
def test_beer_retired_beer(self):
''' Attributes for retired beers display properly '''
results = RateBeer().beer('/beer/shorts-funkin-punkin/79468/')
self.assertTrue(results['retired'] == True)
class TestBrewery(unittest.TestCase):
def test_brewery(self):
''' Make sure the results for a brewery contain the expected data '''
results = RateBeer().brewery("/brewers/deschutes-brewery/233/")
self.assertIsNotNone(results)
self.assertTrue(results['name'] == u'Deschutes Brewery')
self.assertTrue(results['type'] == u'Microbrewery')
self.assertTrue(results['city'] == u'Bend')
def test_brewery_404(self):
''' Make sure the results for a brewery contain the expected data '''
rb = RateBeer()
self.assertRaises(rb_exceptions.PageNotFound, rb.brewery,
"/brewers/qwerty/1234567890")
def test_brewery_get_beers(self):
''' Check to make multi-page review searches work properly '''
beers = RateBeer().get_brewery("/brewers/deschutes-brewery/233/").\
get_beers()
for i in range(3):
self.assertIsNotNone(next(beers))
def test_brewery_get_beers_404(self):
''' Check lazy get_beer 404 exception '''
brewery = RateBeer().get_brewery("/brewers/qwerty/1234567890")
with self.assertRaises(rb_exceptions.PageNotFound):
next(brewery.get_beers())
def test_brewery_unicode(self):
''' Check unicode brewery URLs '''
results = RateBeer().brewery("/brewers/brauhaus-18•80/12750/")
self.assertIsNotNone(results)
self.assertTrue(results['name'] == u'Brauhaus 18\x9580')
self.assertTrue(results['type'] == u'Brew Pub')
self.assertTrue(results['country'] == u'Germany')
class TestMisc(unittest.TestCase):
def test_whitespace_in_url(self):
''' The rare situation where a URL might have whitespace '''
results = RateBeer().search("13 Virtues Cleanliness Helles")
beer = results['beers'][0]
self.assertTrue(beer._has_fetched is False)
self.assertTrue(beer.url ==
u'/beer/13-virtues-cleanliness-helles/231944/')
self.assertTrue(beer.name == u'13 Virtues Cleanliness Helles')
class TestSearch(unittest.TestCase):
def test_str_ascii_search(self):
''' Test out the search function with an ASCII only str search '''
results = RateBeer().search("deschutes inversion")
self.assertListEqual(results['breweries'], [])
self.assertIsNotNone(results['beers'])
beer = results['beers'][0]
self.assertTrue(beer.url == u'/beer/deschutes-inversion-ipa/55610/')
self.assertTrue(beer.name == u'Deschutes Inversion IPA')
def test_str_nonascii_search(self):
''' Test out the search function with a str with more than ASCII
characters '''
results = RateBeer().search("to øl jule mælk")
self.assertListEqual(results['breweries'], [])
self.assertIsNotNone(results['beers'])
beer = results['beers'][0]
self.assertTrue(beer.url == u'/beer/to-ol-jule-maelk/235066/')
self.assertTrue(beer.name == u'To Øl Jule Mælk')
def test_unicode_ascii_search(self):
''' Test out the search function with an ASCII only unicode search '''
results = RateBeer().search(u"deschutes inversion")
self.assertListEqual(results['breweries'], [])
self.assertIsNotNone(results['beers'])
beer = results['beers'][0]
self.assertTrue(beer.url == u'/beer/deschutes-inversion-ipa/55610/')
self.assertTrue(beer.name == u'Deschutes Inversion IPA')
def test_unicode_nonascii_search(self):
''' Test out the search function with a unicode string with more than
ASCII characters '''
results = RateBeer().search(u"to øl jule mælk")
self.assertListEqual(results['breweries'], [])
self.assertIsNotNone(results['beers'])
beer = results['beers'][0]
self.assertTrue(beer.url == u'/beer/to-ol-jule-maelk/235066/')
self.assertTrue(beer.name == u'To Øl Jule Mælk')
class TestAlpha(unittest.TestCase):
def test_fetch_by_letter(self):
''' Make sure the results for a brewery list by index contain the
expected data '''
results = RateBeer().brewers_by_alpha("A")
self.assertIsNotNone(results, [])
beer = results[0]
self.assertTrue(beer.url == u'/brewers/a-duus-and-co/1668/')
self.assertTrue(beer.name == u'A. Duus & Co.')
if __name__ == '__main__':
unittest.main()