def test_presense_in_references(self): milk = ProductCategory.fetch('milk', self.keeper) diary = Category.fetch('diary', self.keeper) self.assertEqual('diary', diary.title) self.assertEqual(4, len(milk.products)) self.assertIn(64.3, milk.get_prices()) report1 = PriceReport.fetch(self.report1_key, self.keeper) self.assertEqual(55.6, report1.normalized_price_value) self.assertIs(milk, report1.product.category) self.assertEqual(u'Московский магазин', report1.merchant.title) self.assertEqual('Jack', report1.reporter.name) self.assertIs(diary, milk.category) self.assertIn(milk, diary.categories)
def test_presense_in_references(self): milk = ProductCategory.fetch("milk", self.keeper) diary = Category.fetch("diary", self.keeper) self.assertEqual("diary", diary.title) self.assertEqual(4, len(milk.products)) self.assertIn(64.3, milk.get_prices()) report1 = PriceReport.fetch(self.report1_key, self.keeper) self.assertEqual(55.6, report1.normalized_price_value) self.assertIs(milk, report1.product.category) self.assertEqual(u"Московский магазин", report1.merchant.title) self.assertEqual("Jack", report1.reporter.name) self.assertIs(diary, milk.category) self.assertIn(milk, diary.categories)
def cycle(entity_class_name_, keeper): """Perform all needed routines on an `entity_class`""" print(cyan('{} check...'.format(entity_class_name_))) entity_class = globals()[entity_class_name_] instances = entity_class.fetch_all(keeper, objects_only=False) for key in instances.keys(): instance = instances[key] if entity_class is ProductCategory: if not hasattr(instance, 'category') or not instance.category: category_key = instance.get_category_key() category = Category.acquire(category_key, keeper) instance.category = category category.add(instance) print(green(u'Added {} to {}'.format(instance, category))) for product in instance.products: if product.category is not instance: instance.remove_product(product) print(yellow(u'Removed ' u'`{}` from `{}`...'.format(product, instance))) if len(product.reports) == 0: instance.remove_product(product) print(yellow(u'Removed stale ' u'`{}` from `{}`...'.format(product, instance))) if product.key not in keeper[product.namespace]: try: instance.remove_product(product) print(yellow(u'Removed `{}` from `{}` ' u'as its not ' u'registered...'.format(product, instance))) except ValueError: pass if entity_class is Product: if type(instance.reports) is not list: print(yellow(u'Fixing product report ' u'list for `{}`...'.format(instance))) instance.reports = list(instance.reports.values()) if type(instance.merchants) is not list: print(yellow(u'Fixing product merchant ' u'list for `{}`...'.format(instance))) instance.merchants = list(instance.merchants.values()) if len(instance.reports) == 0: print(yellow(u'Removing stale `{}`...'.format(instance))) instance.delete_from(keeper) # check category try: cat_key = instance.get_category_key() category = ProductCategory.fetch(cat_key, keeper) if instance.category is not category: print(yellow(u'Adding `{}` to ' u'`{}`...'.format(instance, category))) category.add_product(instance) instance.category = category except CategoryLookupError: print(yellow(u'Removing `{}` as no ' u'category found...'.format(instance))) instance.delete_from(keeper) # check key if key != instance.key: print(yellow(u'Fixing key for `{}`...'.format(key))) keeper.register(instance) keeper.delete_key(instance.namespace, key) if entity_class is Merchant: if type(instance.products) is not list: instance.products = list(instance.products.values()) for product in instance.products: if len(product.reports) == 0: print(yellow(u'Deleting `{}` ' u'from `{}`...'.format(product, instance))) instance.remove_product(product) for report in product.reports: if type(report) is str: print(yellow('Removing product with str report ...')) product.delete_from(keeper) if entity_class is PriceReport: if instance.product.category is None: print(yellow(u'Removing report ' u'for {}'.format(instance.product))) instance.delete_from(keeper) break try: correct_package_key = instance.product.get_package_key() except PackageLookupError, e: print(e.message) else: if instance.product.package.key != correct_package_key: correct_package = ProductPackage.acquire( correct_package_key, keeper) product = Product.fetch(instance.product.key, keeper) print(yellow(u'Fixing package for {}: {}-->{}'.format( instance.product, product.package, correct_package))) product.package = correct_package instance.product = product old_norm_price = instance.normalized_price_value correct_norm_price = instance._get_normalized_price( instance.price_value) if old_norm_price != correct_norm_price: print(yellow( u'Fixing normal price ' u'for {} report ({}-->{})'.format( instance.product, old_norm_price, correct_norm_price))) instance.normalized_price_value = correct_norm_price
def cycle(entity_class_name_, keeper): """Perform all needed routines on an `entity_class`""" print(cyan('{} check...'.format(entity_class_name_))) entity_class = globals()[entity_class_name_] instances = entity_class.fetch_all(keeper, objects_only=False) for key in instances.keys(): instance = instances[key] if entity_class is ProductCategory: if not hasattr(instance, 'category') or not instance.category: category_key = instance.get_category_key() category = Category.acquire(category_key, keeper) instance.category = category category.add(instance) print(green(u'Added {} to {}'.format(instance, category))) for product in instance.products: if product.category is not instance: instance.remove_product(product) print( yellow(u'Removed ' u'`{}` from `{}`...'.format( product, instance))) if len(product.reports) == 0: instance.remove_product(product) print( yellow(u'Removed stale ' u'`{}` from `{}`...'.format( product, instance))) if product.key not in keeper[product.namespace]: try: instance.remove_product(product) print( yellow(u'Removed `{}` from `{}` ' u'as its not ' u'registered...'.format( product, instance))) except ValueError: pass if entity_class is Product: if type(instance.reports) is not list: print( yellow(u'Fixing product report ' u'list for `{}`...'.format(instance))) instance.reports = list(instance.reports.values()) if type(instance.merchants) is not list: print( yellow(u'Fixing product merchant ' u'list for `{}`...'.format(instance))) instance.merchants = list(instance.merchants.values()) if len(instance.reports) == 0: print(yellow(u'Removing stale `{}`...'.format(instance))) instance.delete_from(keeper) # check category try: cat_key = instance.get_category_key() category = ProductCategory.fetch(cat_key, keeper) if instance.category is not category: print( yellow(u'Adding `{}` to ' u'`{}`...'.format(instance, category))) category.add_product(instance) instance.category = category except CategoryLookupError: print( yellow(u'Removing `{}` as no ' u'category found...'.format(instance))) instance.delete_from(keeper) # check key if key != instance.key: print(yellow(u'Fixing key for `{}`...'.format(key))) keeper.register(instance) keeper.delete_key(instance.namespace, key) if entity_class is Merchant: if type(instance.products) is not list: instance.products = list(instance.products.values()) for product in instance.products: if len(product.reports) == 0: print( yellow(u'Deleting `{}` ' u'from `{}`...'.format(product, instance))) instance.remove_product(product) for report in product.reports: if type(report) is str: print( yellow('Removing product with str report ...')) product.delete_from(keeper) if entity_class is PriceReport: if instance.product.category is None: print( yellow(u'Removing report ' u'for {}'.format(instance.product))) instance.delete_from(keeper) break try: correct_package_key = instance.product.get_package_key() except PackageLookupError, e: print(e.message) else: if instance.product.package.key != correct_package_key: correct_package = ProductPackage.acquire( correct_package_key, keeper) product = Product.fetch(instance.product.key, keeper) print( yellow(u'Fixing package for {}: {}-->{}'.format( instance.product, product.package, correct_package))) product.package = correct_package instance.product = product old_norm_price = instance.normalized_price_value correct_norm_price = instance._get_normalized_price( instance.price_value) if old_norm_price != correct_norm_price: print( yellow(u'Fixing normal price ' u'for {} report ({}-->{})'.format( instance.product, old_norm_price, correct_norm_price))) instance.normalized_price_value = correct_norm_price