-
Notifications
You must be signed in to change notification settings - Fork 11
/
wishlist_test.py
215 lines (165 loc) · 7.32 KB
/
wishlist_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division, print_function, absolute_import
from unittest import TestCase
import os
from contextlib import contextmanager
import codecs
import datetime
import testdata
from bs4 import BeautifulSoup
from wishlist.core import WishlistElement, Wishlist
from wishlist.exception import ParseError
from brow.utils import Soup
testdata.basic_logging()
class BaseTestCase(TestCase):
def get_body(self, filename):
return testdata.get_contents(filename)
def get_soup(self, filename, parser=""):
body = self.get_body(filename)
soup = Soup(body, parser)
return soup
class WishlistElementTest(BaseTestCase):
def get_item(self, filename):
body = self.get_body(filename)
we = WishlistElement(body)
return we
def get_items(self, filename, name="WISHLIST_NAME"):
soup = self.get_soup(filename)
w = Wishlist(name)
items = list(w.get_items(soup, w.get_wishlist_url()))
return items
def test_zero_price(self):
we = self.get_item("zero-price-1.html")
self.assertEqual(26.03, we.price)
items = self.get_items("zero-price-2.html")
we = items[2]
self.assertEqual(0.0, we.price)
we = items[4]
self.assertEqual(0.0, we.price)
def test_element_failure_2021_08(self):
"""Fixes error that's been around for like 6 months
AttributeError: 'NavigableString' object has no attribute 'contents'
I just hadn't had time to dive back into the code and fix it, this error
has probably saved me quite a bit of money actually
"""
we = self.get_item("failed_wishlist_element_2021-08.html")
self.assertTrue(we.author)
def test_redesign_2018_06(self):
"""In mid June Amazon updated the html and it had a bug or something in it
and so my wishlist didn't parse for a few days and then it was magically
fixed, but I managed to grab the first page so I could figure out what was
wrong and it turned out I didn't need to change anything except allow
Brow to use different parsers because the more liberal non built-in parsers
were both able to parse the html"""
#body = self.get_body("redesign-2018-06")
soup = self.get_soup("html-2018-06", "html.parser")
w = Wishlist("WISHLISTNAME")
items = list(w.get_items(soup, w.get_wishlist_url()))
self.assertEqual(10, len(items))
soup = self.get_soup("html-2018-06", "lxml")
w = Wishlist("WISHLISTNAME")
items = list(w.get_items(soup, w.get_wishlist_url()))
self.assertEqual(10, len(items))
soup = self.get_soup("html-2018-06", "html5lib")
w = Wishlist("WISHLISTNAME")
items = list(w.get_items(soup, w.get_wishlist_url()))
self.assertEqual(10, len(items))
def test_permalinks(self):
we = self.get_item("permalinks.html")
we._page_url = "{}/hz/wishlist/ls/XXX?filter=DEFAULT&lek=xxxxx-xxxx&sort=default&type=wishlist".format(
we.host
)
self.assertTrue("lv_ov_lig_dp_it" in we.url)
self.assertEqual("B00A6QRKU6", we.uuid)
self.assertTrue("#itemMain_IU2UIE3ANI9WG" in we.page_url)
def test_unavailable(self):
we = self.get_item("failed_wishlist_element_1.html")
we_json = we.jsonable()
self.assertTrue(bool(we_json["title"]))
self.assertEqual(0.0, we_json["price"])
def test_comment_with_moneysign(self):
we = self.get_item("failed_wishlist_element_2.html")
self.assertTrue(we.comment.startswith("$"))
self.assertEqual(21.59, we.price)
def test_marketplace_price(self):
we = self.get_item("failed_wishlist_element_3.html")
self.assertEqual(0.01, we.marketplace_price)
self.assertEqual(0.0, we.price)
def test_no_rating(self):
we = self.get_item("failed_wishlist_element_4.html")
self.assertEqual(0.0, we.rating)
def test_marketplace_price_thousand(self):
# test WishlistElement price with 1,140.96
we = self.get_item("failed_wishlist_element_5.html")
self.assertEqual(1424.05, we.price)
def test_unavailable_2(self):
"""test an item that has no url"""
we = self.get_item("failed_wishlist_element_6.html")
we_json = we.jsonable()
self.assertTrue(bool(we_json["title"]))
self.assertFalse(bool(we_json["url"]))
self.assertTrue(bool(we_json["image"]))
def test_external(self):
"""Make sure an item added from an external source is parsed correctly"""
we = self.get_item("failed_wishlist_element_7.html")
we_json = we.jsonable()
self.assertTrue(bool(we_json["uuid"]))
self.assertTrue(bool(we_json["url"]))
def test_external_2(self):
we = self.get_item("external_element_1.html")
self.assertEqual(159.99, we.price)
def test_external_3(self):
we1 = self.get_item("external_element_1.html")
we2 = self.get_item("external_element_2.html")
self.assertEqual(we1.uuid, we2.uuid)
def test_unavailable_3(self):
"""Make sure an unavailable item's asin value is returned as uuid"""
we = self.get_item("failed_wishlist_element_8.html")
we_json = we.jsonable()
self.assertTrue(bool(we_json["uuid"]))
def test_unavailable_4(self):
we = self.get_item("unavailable_element_1.html")
self.assertEqual(0.0, we.price)
self.assertFalse(we.in_stock())
def test_quantity_1(self):
we = self.get_item("failed_wishlist_element_1.html")
self.assertEqual((1, 0), we.quantity)
self.assertEqual(1, we.wanted_count)
self.assertEqual(0, we.has_count)
def test_quantity_2(self):
we = self.get_item("unavailable_element_3.html")
self.assertEqual((1, 0), we.quantity)
we = self.get_item("unavailable_element_4.html")
self.assertEqual((0, 0), we.quantity)
def test_is_digital(self):
we = self.get_item("failed_wishlist_element_2.html")
self.assertTrue(we.is_digital())
we = self.get_item("failed_wishlist_element_1.html")
self.assertFalse(we.is_digital())
def test_is_digital_2(self):
we = self.get_item("digital_element_1.html")
self.assertTrue(we.is_digital())
def test_range(self):
we = self.get_item("ranged_wishlist_element_1.html")
self.assertEqual(18.95, we.price)
self.assertFalse(we.is_amazon())
def test_failing_added_date(self):
"""On march 31, 2018 the date added started failing"""
we = self.get_item("failed_wishlist_element_9.html")
dt = datetime.date(2018, 3, 30)
self.assertEqual(dt, we.added)
d = we.jsonable()
self.assertTrue("added" in d)
def test_parse_error_june_2020(self):
we = self.get_item("failed_wishlist_element_10.html")
self.assertEqual(datetime.date(2020, 6, 15), we.added)
self.assertTrue(we.is_digital())
self.assertEqual("amazon", we.source)
# if no error is raised then this passes
we_json = we.jsonable()
def test_discount(self):
we = self.get_item("permalinks.html")
self.assertEqual(9, we.discount)
def test_discount_DE(self):
we = self.get_item("discount-DE.html")
self.assertEqual(3, we.discount)