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_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_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_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_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_variation_queryset(self): """ The variation field queryset should be limited to variations from the text banner passed in the constructor. """ banner = TextBannerFactory.create() variation1, variation2 = TextBannerVariationFactory.create_batch(2, banner=banner) ok_(CustomizeTextBannerForm(banner, {'variation': variation1.pk}).is_valid()) ok_(CustomizeTextBannerForm(banner, {'variation': variation2.pk}).is_valid()) non_matching_variation = TextBannerVariationFactory.create() invalid_form = CustomizeTextBannerForm(banner, {'variation': non_matching_variation.pk}) ok_(not invalid_form.is_valid())
def test_get_context_data_variations(self): view = views.CustomizeTextBannerView() view.banner = TextBannerFactory.create() variation1, variation2 = TextBannerVariationFactory.create_batch(2, banner=view.banner) ctx = view.get_context_data(foo='bar', baz=1) eq_(ctx['foo'], 'bar') eq_(ctx['baz'], 1) variations = json.loads(ctx['variations_text_json']) eq_(variations, { unicode(variation1.pk): variation1.text, unicode(variation2.pk): variation2.text, })
def test_get_context_data_variations(self): view = views.CustomizeTextBannerView() view.banner = TextBannerFactory.create() variation1, variation2 = TextBannerVariationFactory.create_batch( 2, banner=view.banner) ctx = view.get_context_data(foo='bar', baz=1) eq_(ctx['foo'], 'bar') eq_(ctx['baz'], 1) variations = json.loads(ctx['variations_text_json']) eq_( variations, { unicode(variation1.pk): variation1.text, unicode(variation2.pk): variation2.text, })
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_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_dispatch_invisible(self): self.view.banner_class = TextBanner banner = TextBannerFactory.create(visible=False) with self.assertRaises(Http404): self.view.dispatch(pk=banner.pk)
def test_generate_banner_code(self): banner = TextBannerFactory() variation = TextBannerVariationFactory(text='Test') eq_(banner.generate_banner_code(variation=variation), '<a href="{href}">Test</a>')
def test_links(self): banner = TextBannerFactory.create() link1, link2 = LinkFactory.create_batch(2, banner_variation__banner=banner) eq_(set(banner.links), set([link1, link2]))