def test_first_update_with_errors(self, p_urlopen, mock_logging): def mocked_urlopen(request): raise IOError('foo') p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': ''} event.save() eq_(event_hit_stats.update(), 0) mock_logging.warn.assert_called_with( 'Event %r does not have a Vid.ly tag', event.title ) event.template_environment = {'tag': 'abc123'} event.save() self.assertRaises( IOError, event_hit_stats.update ) eq_(event_hit_stats.update(swallow_errors=True), 0) mock_logging.error.assert_called_with( 'Unable to download statistics for %r (tag: %s)', event.title, 'abc123' )
def test_first_update_with_errors(self, p_urlopen, mock_logging): def mocked_urlopen(request): raise IOError('foo') p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': ''} event.save() eq_(event_hit_stats.update(), 0) mock_logging.warn.assert_called_with( 'Event %r does not have a Vid.ly tag', event.title) event.template_environment = {'tag': 'abc123'} event.save() self.assertRaises(IOError, event_hit_stats.update) eq_(event_hit_stats.update(swallow_errors=True), 0) mock_logging.error.assert_called_with( 'Unable to download statistics for %r (tag: %s)', event.title, 'abc123')
def test_update_removed_tag(self, p_urlopen): def mocked_urlopen(request): assert 'xyz987' in request.data return StringIO((SAMPLE_STATISTICS_XML % (10,)).strip()) p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() stat = EventHitStats.objects.create( event=event, shortcode='abc123', total_hits=5, ) event.template_environment = {'foo': 'bar'} event.save() # set them back two days now = datetime.datetime.utcnow().replace(tzinfo=utc) days_ago = now - datetime.timedelta(hours=24 * 2, seconds=1) non_signal_save(stat, modified=days_ago) non_signal_save( event, modified=days_ago + datetime.timedelta(seconds=1) ) eq_(event_hit_stats.update(), 0) ok_(not EventHitStats.objects.all().count())
def test_update_with_errors(self, p_urlopen, mock_logging): def mocked_urlopen(request): raise IOError('boo!') p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() stat = EventHitStats.objects.create( event=event, shortcode='abc123', total_hits=5, ) # set them back two days now = timezone.now() days_ago = now - datetime.timedelta(hours=24 * 2, seconds=1) non_signal_save(stat, modified=days_ago) non_signal_save(event, modified=days_ago + datetime.timedelta(seconds=1)) self.assertRaises(IOError, event_hit_stats.update) eq_(event_hit_stats.update(swallow_errors=True), 0) mock_logging.error.assert_called_with( 'Unable to download statistics for %r (tag: %s)', event.title, 'abc123')
def test_update_removed_tag(self, p_urlopen): def mocked_urlopen(request): assert 'xyz987' in request.data return StringIO((SAMPLE_STATISTICS_XML % (10, )).strip()) p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() stat = EventHitStats.objects.create( event=event, shortcode='abc123', total_hits=5, ) event.template_environment = {'foo': 'bar'} event.save() # set them back two days now = timezone.now() days_ago = now - datetime.timedelta(hours=24 * 2, seconds=1) non_signal_save(stat, modified=days_ago) non_signal_save(event, modified=days_ago + datetime.timedelta(seconds=1)) eq_(event_hit_stats.update(), 0) ok_(not EventHitStats.objects.all().count())
def test_update_new_tag(self, p_urlopen): def mocked_urlopen(request): assert 'xyz987' in request.data return StringIO((SAMPLE_STATISTICS_XML % (10,)).strip()) p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() stat = EventHitStats.objects.create( event=event, shortcode='abc123', total_hits=5, ) event.template_environment = {'tag': 'xyz987'} event.save() # set them back two days now = timezone.now() days_ago = now - datetime.timedelta(hours=24 * 2, seconds=1) non_signal_save(stat, modified=days_ago) non_signal_save( event, modified=days_ago + datetime.timedelta(seconds=1) ) eq_(event_hit_stats.update(), 1) stat = EventHitStats.objects.get(pk=stat.pk) eq_(stat.total_hits, 10) eq_(stat.shortcode, 'xyz987')
def test_update_with_errors(self, p_urlopen, mock_logging): def mocked_urlopen(request): raise IOError('boo!') p_urlopen.side_effect = mocked_urlopen vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() stat = EventHitStats.objects.create( event=event, shortcode='abc123', total_hits=5, ) # set them back two days now = datetime.datetime.utcnow().replace(tzinfo=utc) days_ago = now - datetime.timedelta(hours=24 * 2, seconds=1) non_signal_save(stat, modified=days_ago) non_signal_save( event, modified=days_ago + datetime.timedelta(seconds=1) ) self.assertRaises( IOError, event_hit_stats.update ) eq_(event_hit_stats.update(swallow_errors=True), 0) mock_logging.error.assert_called_with( 'Unable to download statistics for %r (tag: %s)', event.title, 'abc123' )
def test_update(self, p_urlopen): calls = [] def mocked_urlopen(request): calls.append(1) assert 'abc123' in request.data return StringIO((SAMPLE_STATISTICS_XML % (10,)).strip()) p_urlopen.side_effect = mocked_urlopen assert not EventHitStats.objects.count() assert Event.objects.all() assert Event.objects.archived().all() eq_(event_hit_stats.update(), 0) assert not EventHitStats.objects.count() vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() eq_(event_hit_stats.update(), 1) assert EventHitStats.objects.count() stat, = EventHitStats.objects.all() eq_(stat.event, event) eq_(stat.total_hits, 10) eq_(stat.shortcode, 'abc123') eq_(len(calls), 1) # do it again and nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # let's pretend the event is half an hour old now = datetime.datetime.utcnow().replace(tzinfo=utc) half_hour_ago = now - datetime.timedelta(minutes=30) #event.update(modified=event.modified - half_hour_ago) #non_signal_save(event, modified=half_hour_ago) eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # ...because the EventHitStats was modified too recently non_signal_save(stat, modified=half_hour_ago) non_signal_save(event, modified=half_hour_ago) eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # it needs to be at least one hour old hour_ago = now - datetime.timedelta(minutes=60, seconds=1) non_signal_save(stat, modified=hour_ago) non_signal_save(event, modified=hour_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 2) # a second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 2) # let's pretend it's even older day_ago = now - datetime.timedelta(hours=24, seconds=1) non_signal_save(stat, modified=day_ago) non_signal_save(event, modified=day_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 3) # second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 3) # even older still week_ago = now - datetime.timedelta(days=7, seconds=1) non_signal_save(stat, modified=week_ago) non_signal_save(event, modified=week_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 4) # second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 4)
def test_update(self, p_urlopen): calls = [] def mocked_urlopen(request): calls.append(1) assert 'abc123' in request.data return StringIO((SAMPLE_STATISTICS_XML % (10, )).strip()) p_urlopen.side_effect = mocked_urlopen assert not EventHitStats.objects.count() assert Event.objects.all() assert Event.objects.archived().all() eq_(event_hit_stats.update(), 0) assert not EventHitStats.objects.count() vidly_template = Template.objects.create(name='Vid.ly Template') event, = Event.objects.archived().all() event.template = vidly_template event.template_environment = {'tag': 'abc123'} event.save() eq_(event_hit_stats.update(), 1) assert EventHitStats.objects.count() stat, = EventHitStats.objects.all() eq_(stat.event, event) eq_(stat.total_hits, 10) eq_(stat.shortcode, 'abc123') eq_(len(calls), 1) # do it again and nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # let's pretend the event is half an hour old now = timezone.now() half_hour_ago = now - datetime.timedelta(minutes=30) # event.update(modified=event.modified - half_hour_ago) # non_signal_save(event, modified=half_hour_ago) eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # ...because the EventHitStats was modified too recently non_signal_save(stat, modified=half_hour_ago) non_signal_save(event, modified=half_hour_ago) eq_(event_hit_stats.update(), 0) eq_(len(calls), 1) # it needs to be at least one hour old hour_ago = now - datetime.timedelta(minutes=60, seconds=1) non_signal_save(stat, modified=hour_ago) non_signal_save(event, modified=hour_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 2) # a second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 2) # let's pretend it's even older day_ago = now - datetime.timedelta(hours=24, seconds=1) non_signal_save(stat, modified=day_ago) non_signal_save(event, modified=day_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 3) # second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 3) # even older still week_ago = now - datetime.timedelta(days=7, seconds=1) non_signal_save(stat, modified=week_ago) non_signal_save(event, modified=week_ago) eq_(event_hit_stats.update(), 1) eq_(len(calls), 4) # second time, nothing should happen eq_(event_hit_stats.update(), 0) eq_(len(calls), 4)