def test_queryset_visible_banners(self): parent = CategoryFactory.create() categories = CategoryFactory.create_batch(5, parent=parent) category1, category2, category3, category4, category5 = categories # category1 has a visible ImageBanner ImageBannerFactory.create(category=category1, visible=True) TextBannerFactory.create(category=category1, visible=False) FirefoxUpgradeBannerFactory.create(category=category1, visible=False) # category2 has a visible TextBanner ImageBannerFactory.create(category=category2, visible=False) TextBannerFactory.create(category=category2, visible=True) FirefoxUpgradeBannerFactory.create(category=category2, visible=False) # category3 has a visible FirefoxUpgradeBanner ImageBannerFactory.create(category=category3, visible=False) TextBannerFactory.create(category=category3, visible=False) FirefoxUpgradeBannerFactory.create(category=category3, visible=True) # category4 has no visible banners ImageBannerFactory.create(category=category4, visible=False) TextBannerFactory.create(category=category4, visible=False) FirefoxUpgradeBannerFactory.create(category=category4, visible=False) # category5 has no banners. eq_(set(Category.objects.with_visible_banners()), set([category1, category2, category3]))
def test_get_queryset_visible_banners(self): """ get_queryset should return a queryset with only categories that have visible banners. """ parent = CategoryFactory.create() category1, category2, category3, category4 = CategoryFactory.create_batch(4, parent=parent) # category1 has a visible ImageBanner ImageBannerFactory.create(category=category1, visible=True) TextBannerFactory.create(category=category1, visible=False) FirefoxUpgradeBannerFactory.create(category=category1, visible=False) # category2 has a visible TextBanner ImageBannerFactory.create(category=category2, visible=False) TextBannerFactory.create(category=category2, visible=True) FirefoxUpgradeBannerFactory.create(category=category2, visible=False) # category3 has a visible FirefoxUpgradeBanner ImageBannerFactory.create(category=category3, visible=False) TextBannerFactory.create(category=category3, visible=False) FirefoxUpgradeBannerFactory.create(category=category3, visible=True) # category4 has no visible banners ImageBannerFactory.create(category=category4, visible=False) TextBannerFactory.create(category=category4, visible=False) FirefoxUpgradeBannerFactory.create(category=category4, visible=False) eq_(set(self.view.get_queryset()), set([category1, category2, category3]))
def test_get_queryset_visible_banners(self): """ get_queryset should return a queryset with only categories that have visible banners. """ parent = CategoryFactory.create() category1, category2, category3, category4 = CategoryFactory.create_batch( 4, parent=parent) # category1 has a visible ImageBanner ImageBannerFactory.create(category=category1, visible=True) TextBannerFactory.create(category=category1, visible=False) FirefoxUpgradeBannerFactory.create(category=category1, visible=False) # category2 has a visible TextBanner ImageBannerFactory.create(category=category2, visible=False) TextBannerFactory.create(category=category2, visible=True) FirefoxUpgradeBannerFactory.create(category=category2, visible=False) # category3 has a visible FirefoxUpgradeBanner ImageBannerFactory.create(category=category3, visible=False) TextBannerFactory.create(category=category3, visible=False) FirefoxUpgradeBannerFactory.create(category=category3, visible=True) # category4 has no visible banners ImageBannerFactory.create(category=category4, visible=False) TextBannerFactory.create(category=category4, visible=False) FirefoxUpgradeBannerFactory.create(category=category4, visible=False) eq_(set(self.view.get_queryset()), set([category1, category2, category3]))
def test_category_clean(self): """ If a category is more than one layer away from its root, clean should raise a ValidationError. """ root = CategoryFactory.create() child = CategoryFactory.create(parent=root) grandchild = CategoryFactory.create(parent=child) root.clean() # No layers is fine! child.clean() # 1 layer is fine! with self.assertRaises(ValidationError): grandchild.clean() # 2 layers is no bueno!
def test_category_clean_has_children(self): """ If a category has children and is being assigned a parent category, raise a ValidationError. """ root = CategoryFactory.create() child = CategoryFactory.create() CategoryFactory.create(parent=child) # Grandchild root.clean() # Root is fine! child.parent = root with self.assertRaises(ValidationError): child.clean() # child has a grandchild, no bueno!
def test_category_clean_parent_not_root(self): """ If a category is being assigned a parent that isn't a root node, raise a ValidationError. """ root = CategoryFactory.create() child = CategoryFactory.create(parent=root) grandchild = CategoryFactory.create() root.clean() # Root is fine! child.clean() # Child is fine! grandchild.parent = child with self.assertRaises(ValidationError): grandchild.clean() # child is not a root node, no bueno!
def test_get_queryset(self): """ The list returned by get_queryset should contain image banners, text banners, and upgrade banners. """ category = CategoryFactory.create() image_banner1, image_banner2 = ImageBannerFactory.create_batch( 2, category=category, visible=True) text_banner1, text_banner2 = TextBannerFactory.create_batch( 2, category=category, visible=True) upgrade_banner1, upgrade_banner2 = FirefoxUpgradeBannerFactory.create_batch( 2, category=category, visible=True) self.view.category = category # Create banners with visible=False that should not show up. ImageBannerFactory.create(category=category, visible=False) TextBannerFactory.create(category=category, visible=False) FirefoxUpgradeBannerFactory.create(category=category, visible=False) eq_( set(self.view.get_queryset()), set([ image_banner1, image_banner2, text_banner1, text_banner2, upgrade_banner1, upgrade_banner2 ]))
def test_banners(self): category = CategoryFactory.create() image_banner1, image_banner2 = ImageBannerFactory.create_batch(2, category=category) text_banner = TextBannerFactory.create(category=category) upgrade_banner = FirefoxUpgradeBannerFactory.create(category=category) eq_(set(category.banners()), set([image_banner1, image_banner2, text_banner, upgrade_banner]))
def test_dispatch_category_exists(self): """ If a category with the given pk exists, set that category to self.category on the view. """ category = CategoryFactory.create() with patch_super(self.view, 'dispatch') as super_dispatch: response = self.view.dispatch(self.factory.get('/'), category_pk=category.pk) eq_(response, super_dispatch.return_value) eq_(self.view.category, category)
def test_links_filters(self): category = CategoryFactory.create() user = UserFactory.create() text_banner_variation = TextBannerVariationFactory.create(banner__category=category) upgrade_banner_variation = FirefoxUpgradeBannerVariationFactory.create( banner__category=category) text_link = LinkFactory.create(banner_variation=text_banner_variation, user=user) LinkFactory.create(banner_variation=upgrade_banner_variation) eq_(list(category.links(user=user)), [text_link])
def test_get_queryset(self): """ The list returned by get_queryset should contain both image banners and text banners. """ category = CategoryFactory.create() image_banner1, image_banner2 = ImageBannerFactory.create_batch(2, category=category) text_banner1, text_banner2 = TextBannerFactory.create_batch(2, category=category) self.view.category = category eq_(set(self.view.get_queryset()), set([image_banner1, image_banner2, text_banner1, text_banner2]))
def test_links(self): """ links should return a queryset of links related to any type of banner variation within the category. """ category = CategoryFactory.create() # Test against several variations, and multiple variations. image_banner_variation1 = ImageBannerVariationFactory.create(banner__category=category) image_banner_variation2 = ImageBannerVariationFactory.create(banner__category=category) text_banner_variation = TextBannerVariationFactory.create(banner__category=category) upgrade_banner_variation = FirefoxUpgradeBannerVariationFactory.create(banner__category=category) # Create links from the variations. image_link1 = LinkFactory.create(banner_variation=image_banner_variation1) image_link2 = LinkFactory.create(banner_variation=image_banner_variation1) image_link3 = LinkFactory.create(banner_variation=image_banner_variation2) text_link = LinkFactory.create(banner_variation=text_banner_variation) upgrade_link = LinkFactory.create(banner_variation=upgrade_banner_variation) eq_(set([image_link1, image_link2, image_link3, text_link, upgrade_link]), set(category.links))
def test_basic(self): category = CategoryFactory.create() banner1, banner2 = TextBannerFactory.create_batch(2, category=category) link1, link2 = LinkFactory.create_batch(2, banner_variation__banner=banner1) link3, link4 = LinkFactory.create_batch(2, banner_variation__banner=banner2) # Set datapoint totals. self._datapoint(link1, 4) self._datapoint(link1, 4) self._datapoint(link2, 3) self._datapoint(link2, 3) self._datapoint(link3, 2) self._datapoint(link3, 2) self._datapoint(link4, 1) self._datapoint(link4, 1) # Assert that data hasn't been denormalized yet. eq_(link1.link_clicks, 0) eq_(link2.link_clicks, 0) eq_(link3.link_clicks, 0) eq_(link4.link_clicks, 0) eq_(banner1.link_clicks, 0) eq_(banner2.link_clicks, 0) eq_(category.link_clicks, 0) self.command.handle() # Re-fetch data now that is has updated category = Category.objects.get(pk=category.pk) banner1, banner2 = TextBanner.objects.order_by('id') link1, link2, link3, link4 = Link.objects.order_by('id') # Assert that counts are now denormalized. eq_(link1.link_clicks, 8) eq_(link2.link_clicks, 6) eq_(link3.link_clicks, 4) eq_(link4.link_clicks, 2) eq_(banner1.link_clicks, 14) eq_(banner2.link_clicks, 6) eq_(category.link_clicks, 20)
def test_get_queryset(self): """ The list returned by get_queryset should contain image banners, text banners, and upgrade banners. """ category = CategoryFactory.create() image_banner1, image_banner2 = ImageBannerFactory.create_batch( 2, category=category, visible=True) text_banner1, text_banner2 = TextBannerFactory.create_batch( 2, category=category, visible=True) upgrade_banner1, upgrade_banner2 = FirefoxUpgradeBannerFactory.create_batch( 2, category=category, visible=True) self.view.category = category # Create banners with visible=False that should not show up. ImageBannerFactory.create(category=category, visible=False) TextBannerFactory.create(category=category, visible=False) FirefoxUpgradeBannerFactory.create(category=category, visible=False) eq_(set(self.view.get_queryset()), set([image_banner1, image_banner2, text_banner1, text_banner2, upgrade_banner1, upgrade_banner2]))
def test_links(self): """ links should return a queryset of links related to any type of banner variation within the category. """ category = CategoryFactory.create() # Test against several variations, and multiple variations. image_banner_variation1 = ImageBannerVariationFactory.create(banner__category=category) image_banner_variation2 = ImageBannerVariationFactory.create(banner__category=category) text_banner_variation = TextBannerVariationFactory.create(banner__category=category) upgrade_banner_variation = FirefoxUpgradeBannerVariationFactory.create( banner__category=category) # Create links from the variations. image_link1 = LinkFactory.create(banner_variation=image_banner_variation1) image_link2 = LinkFactory.create(banner_variation=image_banner_variation1) image_link3 = LinkFactory.create(banner_variation=image_banner_variation2) text_link = LinkFactory.create(banner_variation=text_banner_variation) upgrade_link = LinkFactory.create(banner_variation=upgrade_banner_variation) eq_(set(category.links()), set([image_link1, image_link2, image_link3, text_link, upgrade_link]))
def test_banners_filters(self): category = CategoryFactory.create() text_banner = TextBannerFactory.create(category=category, visible=True) FirefoxUpgradeBannerFactory.create(category=category, visible=False) eq_(category.banners(visible=True), [text_banner])
def test_links_no_banners(self): """If a Category has no banners, return an empty queryset.""" category = CategoryFactory.create() eq_(len(category.banners()), 0) eq_(category.links().exists(), False)
def test_link_clicks(self): category = CategoryFactory.create() mock_links = [Mock(link_clicks=5), Mock(link_clicks=13), Mock(link_clicks=7)] with patch.object(Category, "links", PropertyMock(return_value=mock_links)): eq_(category.link_clicks, 25)