from django.test import TestCase
from products.models import *
import datetime
from django.utils import timezone

class CounterTestCase(TestCase):
    def test_counter(self):
        count(None, 'test1')
        count(None, 'test1')
        count(None, 'test2')
        self.assertEqual(get_sum('test1'), 2)
        self.assertEqual(get_sum('test2'), 1)
        self.assertEqual(get_sum('test3'), 0)

    def test_history(self):
        begin = timezone.now()
        def ts(minutes):
            return begin + datetime.timedelta(seconds=60 * minutes)
        def add_count(i, n):
            a = Counter.objects.create(name='test1',
                                       count=n)
            a.datetime = ts(i)
            a.save()

        add_count(0, 1)
        add_count(1, 1)
        add_count(1, 1)
        add_count(3, 1)
        r = get_history_counts(None, "test1", ts(0), 60, 60 * 4)
        self.assertEqual([1, 2, 0, 1, 0], [x[1] for x in r])
