class TestMixin(object):
    def create_users(self):
        self.user_model = get_user_model()

        self.user = self.user_model.objects.create_user(
            username='******', email='*****@*****.**', password='******')
        self.user2 = self.user_model.objects.create_user(
            username='******', email='*****@*****.**', password='******')

        self.user.save()
        self.user2.save()

    def create_promoter_scores(self):
        self.now = datetime.now()

        self.ps1 = PromoterScore(user=self.user, score=8)
        self.ps1.save()

        self.ps2 = PromoterScore(user=self.user, score=9)
        self.ps2.save()
        self.ps2.created_at = self.now - timedelta(4 * 365 / 12)
        self.ps2.save()

        self.ps3 = PromoterScore(user=self.user2, score=9)
        self.ps3.save()
        self.ps3.created_at = self.now - timedelta(7 * 365 / 12)
        self.ps3.save()

    def delete_users(self):
        self.user_model.objects.all().delete()

    def delete_promoter_scores(self):
        PromoterScore.objects.all().delete()
    def test_survey_view_update_reason(self):
        reason = 'Awesome!'

        promoter_score = PromoterScore(user=self.user, score=10)
        promoter_score.save()

        data = json.dumps({'id': promoter_score.pk, 'reason': reason})
        response = self.client.post(reverse('survey'), data, content_type='application/json')
        print response.content

        promoter_score = PromoterScore.objects.get(pk=promoter_score.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(promoter_score.reason, reason)
    def test_survey_view_update_reason(self):
        reason = 'Awesome!'

        promoter_score = PromoterScore(user=self.user, score=10)
        promoter_score.save()

        data = json.dumps({'id': promoter_score.pk, 'reason': reason})
        response = self.client.post(reverse('survey'),
                                    data,
                                    content_type='application/json')
        print response.content

        promoter_score = PromoterScore.objects.get(pk=promoter_score.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(promoter_score.reason, reason)
    def create_promoter_scores(self):
        self.now = datetime.now()

        self.ps1 = PromoterScore(user=self.user, score=8)
        self.ps1.save()

        self.ps2 = PromoterScore(user=self.user, score=9)
        self.ps2.save()
        self.ps2.created_at = self.now - timedelta(4*365/12)
        self.ps2.save()

        self.ps3 = PromoterScore(user=self.user2, score=9)
        self.ps3.save()
        self.ps3.created_at = self.now - timedelta(7*365/12)
        self.ps3.save()
    def test_promoter_score_returned_for_user_with_score_6_months_later(self):
        ps = PromoterScore(user=self.user, score=None)
        ps.save()
        ps.created_at = ps.created_at - datetime.timedelta(7 * 30)
        ps.save()

        response = self.client.get(reverse('survey'))
        data = json.loads(response.content)

        self.assertEqual(data['survey_is_needed'], True)
class TestMixin(object):

    def create_users(self):
        self.user_model = get_user_model()

        self.user = self.user_model.objects.create_user(
            username='******', email='*****@*****.**', password='******'
        )
        self.user2 = self.user_model.objects.create_user(
            username='******', email='*****@*****.**', password='******'
        )

        self.user.save()
        self.user2.save()

    def create_promoter_scores(self):
        self.now = datetime.now()

        self.ps1 = PromoterScore(user=self.user, score=8)
        self.ps1.save()

        self.ps2 = PromoterScore(user=self.user, score=9)
        self.ps2.save()
        self.ps2.created_at = self.now - timedelta(4*365/12)
        self.ps2.save()

        self.ps3 = PromoterScore(user=self.user2, score=9)
        self.ps3.save()
        self.ps3.created_at = self.now - timedelta(7*365/12)
        self.ps3.save()

    def delete_users(self):
        self.user_model.objects.all().delete()

    def delete_promoter_scores(self):
        PromoterScore.objects.all().delete()
    def test_promoter_score_returned_for_user_with_score_6_months_later(self):
        ps = PromoterScore(user=self.user, score=None)
        ps.save()
        ps.created_at = ps.created_at - datetime.timedelta(7*30)
        ps.save()

        response = self.client.get(reverse('survey'))
        data = json.loads(response.content)

        self.assertEqual(data['survey_is_needed'], True)
    def create_promoter_scores(self):
        self.now = datetime.now()

        self.ps1 = PromoterScore(user=self.user, score=8)
        self.ps1.save()

        self.ps2 = PromoterScore(user=self.user, score=9)
        self.ps2.save()
        self.ps2.created_at = self.now - timedelta(4 * 365 / 12)
        self.ps2.save()

        self.ps3 = PromoterScore(user=self.user2, score=9)
        self.ps3.save()
        self.ps3.created_at = self.now - timedelta(7 * 365 / 12)
        self.ps3.save()