def setUp(self): super(PriceTest, self).setUp() book1 = self.objects[0] book2 = self.objects[1] self.price_sample = { 'mph': { "book": book1, "source": 'mph', "price": 80.5 }, 'mudah': { "book": book2, "source": 'mudah', "state": 1, "posted": datetime.date.today(), "price": 40.25, "url": 'http://example.com/' } } self.price_objects = models.store_prices(self.price_sample.values())
def testUpdate(self): price = self.price_sample['mudah'] new = { "book": price['book'], "source": price['source'], "state": price['state'], "posted": price['posted'], # changed: "price": 43.25, "url": price['url'] + 'book/123/' } updated = models.store_prices([new], update=True) returned = len(updated) self.assertEqual(returned, 1, 'returned %d entities, expected 1' % returned) for k, v in new.iteritems(): self.assertEqual(new[k], getattr(updated[0], k), 'price[%s] != obj.%s' % (k, k)) all_prices = models.Price.all().count() expected = len(self.price_sample) self.assertEqual(all_prices, expected, 'expected %d prices, got %d' % (expected, all_prices))
def _get(self): isbn = self.request.get('isbn').strip() if not models.Book.ISBN13_REGEX.match(isbn): raise ValueError(u'Invalid ISBN') shop = self.request.get('shop').strip() if not shop: raise ValueError(u'Shop name not given') title = self.request.get('title').strip() author = self.request.get('author').strip() defaults = {"isbn13":isbn, "title":title, "authors":[author]} # get/create book object book = models.Book.get_by_isbn(isbn, defaults=defaults) # find in memcache/db first... prices = models.Price.find_for_book(book, shop) if prices: return prices # none found, so do actual page/scrape lookup prices = bookshop.find(shop, defaults) objects = [] if prices: # bookshop.find might return actual Price entities or just faux # dicts, separate the dicts and store them to get the # corresponding entities dicts = [] for p in prices: if isinstance(p, models.Price): objects.append(p) else: p['book'] = book dicts.append(p) objects.extend(models.store_prices(dicts, update=True)) return objects