'Verify stored visit counts from mocked data.'

@mock.patch.object(models, 'close_old_connections')
@mock.patch.object(googleanalytics, 'pageviews_by_question')
def test_visit_count_from_analytics(self, pageviews_by_question, close_old_connections):

    q1 = QuestionFactory()
    q2 = QuestionFactory()
    q3 = QuestionFactory()
    pageviews_by_question.return_value = {q1.id: 42, q2.id: 27, q3.id: 1337, 123459: 3}
    QuestionVisits.reload_from_analytics()
    eq_(3, QuestionVisits.objects.count())
    eq_(42, QuestionVisits.objects.get(question_id=q1.id).visits)
    eq_(27, QuestionVisits.objects.get(question_id=q2.id).visits)
    eq_(1337, QuestionVisits.objects.get(question_id=q3.id).visits)
    pageviews_by_question.return_value = {q1.id: 100, q2.id: 200, q3.id: 300}
    QuestionVisits.reload_from_analytics()
    eq_(3, QuestionVisits.objects.count())
    eq_(100, QuestionVisits.objects.get(question_id=q1.id).visits)
    eq_(200, QuestionVisits.objects.get(question_id=q2.id).visits)
    eq_(300, QuestionVisits.objects.get(question_id=q3.id).visits)
