def test_order(self): em = Email("foo") uuid = testdata.get_ascii(16) body = { "url": "http://foo.com", "image": "http://foo.com/bar.jpg", } body.update({ "title": "expensive", "price": 100.00, }) it = Item(uuid=uuid, body=dict(body), price=body["price"]) em.cheaper_items.append(it) body.update({ "title": "cheaper", "price": 10.00, }) it = Item(uuid=uuid, body=dict(body), price=body["price"]) em.cheaper_items.append(it) body.update({ "title": "cheapest", "price": 1.00, }) it = Item(uuid=uuid, body=dict(body), price=body["price"]) em.cheaper_items.append(it) html = em.body_html self.assertTrue( html.index("cheapest") < html.index("cheaper") < html.index( "expensive"))
def test_is_richest(self): uuid = testdata.get_hash() it = WatchlistItem.create(price=100, body={}, uuid=uuid) it = WatchlistItem.create(price=10, body={}, uuid=uuid) it = Item(price=1000, body={}, uuid=uuid) self.assertFalse(it.is_cheapest()) self.assertTrue(it.is_richest())
def test_new_old_price(self): uuid = testdata.get_hash() body = { "url": testdata.get_url(), "title": testdata.get_words(), } oit = WatchlistItem.create(price=10, body=dict(body), uuid=uuid) it = Item(price=1, body=dict(body), uuid=uuid) s = it.html_detail() self.assertTrue("<b>$1.00</b>" in s) self.assertTrue("was <b>$10.00</b>" in s)
def test_is_newest(self): uuid = testdata.get_hash() it = Item(price=1000, body={}, uuid=uuid) self.assertTrue(it.is_newest()) it.save() it = Item(price=100, body={}, uuid=uuid) self.assertFalse(it.is_newest())
def main(name, dry_run): """go through and check wishlist against previous entries""" echo.out( "{}. Starting on wishlist {}", datetime.datetime.utcnow(), name, ) # Let's flush out any problems connecting to the DB before getting into the loop WatchlistItem.interface.connect() name = name[0] email = Email(name) item_count = 1 try: for item_count, we in enumerate(Wishlist(name), item_count): try: echo.out("{}. (p{}) {}", item_count, we.page, we.title) item = Item( uuid=we.uuid, body=we.jsonable(), price=we.price, element=we, ) add_item(email, item, dry_run) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() email.errors.append((e, (exc_type, exc_value, exc_traceback))) echo.err("{}. Failed!", item_count) echo.exception(e) # bail if we've had a lot of errors or the first N items # have all resulted in an error total_errors = len(email.errors) if total_errors > 25 or (total_errors > 10 and total_errors == item_count): break echo.out( "{}. Done with wishlist, {} total items, {} changes", datetime.datetime.utcnow(), item_count, len(email), ) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() email.errors.append((e, (exc_type, exc_value, exc_traceback))) echo.exception(e) if not dry_run: email.send(item_count=item_count)
def test_richest(self): uuid = testdata.get_hash() it = WatchlistItem.create(price=10, body={}, uuid=uuid) it = WatchlistItem.create(price=1, body={}, uuid=uuid) it = WatchlistItem.create(price=1000, body={}, uuid=uuid) it = WatchlistItem.create(price=100, body={}, uuid=uuid) it = Item(price=0, body={}, uuid=uuid) richest = it.richest self.assertEqual(1000, richest.price)
def test_digital(self): nit = Item(price=100, body={ "url": testdata.get_url(), "title": testdata.get_words(), "digital": True, "price": 1.0 }, uuid="foo") self.assertTrue(" (digital)" in nit.title)
def test_body(self): uuid = testdata.get_ascii(16) body = { "url": "http://example.com", "title": "this is the title", "image": "http://example.com/image.jpg", "price": 0.0, } new_body = dict(body) new_body["price"] = 1.00 new_item = Item(uuid=uuid, body=new_body, price=1.00) old_body = dict(body) old_body["price"] = 10.00 old_item = Item(uuid=uuid, body=body, price=10.00) old_item.save() em = Email("wishlist-name") em.cheaper_items.append(new_item)
def test_last_newest_in_db(self): uuid = testdata.get_hash() oit1 = WatchlistItem.create(price=10, body={}, uuid=uuid) oit2 = WatchlistItem.create(price=100, body={}, uuid=uuid) it = Item(price=1000, body={}, uuid=uuid) it.save() self.assertEqual(it.last.pk, oit2.pk) it = Item(price=1000, body={}, uuid=uuid) it.newest = oit2 self.assertEqual(it.last.pk, oit1.pk)
def test_email_unicode(self): em = Email("foo") body = { "url": "http://foo.com", #"title": "foo", "title": "\u2713", "image": "http://foo.com/bar.jpg", "price": 12.34 } it = Item(uuid=testdata.get_ascii(16), body=body, price=body["price"]) em.cheaper_items.append(it) with self.assertRaises(UnicodeEncodeError): str(em.body_html) str(em.body_html.encode("utf8"))
def get_item(item=None, **kwargs): if item: body = dict(item.newest.body) body.update(kwargs) body.setdefault("uuid", item.uuid) kwargs = body price = kwargs.pop("price", testdata.get_int(1000)) uuid = kwargs.pop("uuid", testdata.get_hash()) kwargs.setdefault("url", testdata.get_url()) kwargs.setdefault("digital", testdata.get_bool()) kwargs.setdefault("image", testdata.get_url()) kwargs.setdefault("title", testdata.get_words()) if isinstance(price, float): kwargs["price"] = price price = int(price * 100.0) else: kwargs["price"] = float(price) * 0.01 it = Item(price=price, body=kwargs, uuid=uuid) return it
def test_equality(self): uuid = testdata.get_hash() price = 10 it = Item(price=price, body={}, uuid=uuid) # make sure when there is non of this item in the db it acts as expected self.assertFalse(it.is_cheaper()) self.assertFalse(it.is_richer()) self.assertTrue(it.is_newest()) self.assertTrue(it.is_cheapest()) self.assertTrue(it.is_stocked()) self.assertTrue(it.is_richest()) # now make sure it acts as expected when there is another item wit = WatchlistItem.create(price=price, body={}, uuid=uuid) self.assertFalse(it.is_cheaper()) self.assertFalse(it.is_richer()) self.assertFalse(it.is_newest()) self.assertTrue(it.is_cheapest()) self.assertTrue(it.is_stocked()) self.assertTrue(it.is_richest())
def test_subject_total(self): em = Email("foo") it = Item(uuid=testdata.get_ascii(16), body={}, price=1) it = Item(uuid=testdata.get_ascii(16), body={}, price=2) it.save() em.cheaper_items.append(Item(uuid=it.uuid, body={}, price=1)) it = Item(uuid=testdata.get_ascii(16), body={}, price=1) it.save() em.cheaper_items.append(Item(uuid=it.uuid, body={}, price=2)) self.assertFalse("/2" in em.subject) em.kwargs["item_count"] = 2 self.assertTrue("/2" in em.subject)