# Path: doj/tests/db/models.py
# class TestModel(models.Model):
# 
#     field_1 = models.BigIntegerField(default=9223372036854775807)
#     field_2 = models.BinaryField(default="010101010101010101010101")  # Causes problems with SQLite
#     field_3 = models.BooleanField(default=True)
#     field_4 = models.CharField(max_length=256, default="abc")
#     field_5 = models.DateField(auto_now_add=True)
#     field_6 = models.DateTimeField(auto_now_add=True)
#     field_7 = models.DecimalField(max_digits=5, decimal_places=2, default=123.45)
#     field_8 = models.EmailField(default='test@email.at')
#     field_9 = models.FloatField(default=12.34)
#     field_10 = models.IntegerField(default=1234)
#     field_11 = models.GenericIPAddressField(default='192.0.2.30')
#     field_12 = models.NullBooleanField(default=None)
#     field_13 = models.PositiveIntegerField(default=1234)
#     field_14 = models.PositiveSmallIntegerField(default=1234)
#     field_15 = models.SmallIntegerField(default=1234)
#     field_16 = models.TextField(default="abc")
#     field_18 = models.TimeField(auto_now_add=True)
#     field_19 = models.ForeignKey('TestModelRelation', related_name='test_models', null=True, blank=True)
# 
# class TestModelRelation(models.Model):
# 
#     field_1 = models.BigIntegerField(default=1234)


from datetime import datetime
from django.test import TestCase
from django.db import transaction
from django.db.models import Count, Min, Max, Avg, Sum
from django.db.models.query import EmptyQuerySet
from doj.tests.db.models import TestModel, TestModelRelation
            test_model.save()

        first = TestModel.objects.order_by('id').first()
        last = TestModel.objects.order_by('id').last()

        self.assertIsInstance(first, TestModel)
        self.assertIsInstance(last, TestModel)

    def test_aggregate(self):
        counter = 1
        counters = []
        for _ in range(0, DBTestCase.NUMBER_OF_RECORDS):
            test_model = TestModel(field_10=counter)
            test_model.save()

            counters.append(counter)
            counter += 1

        count_result = TestModel.objects.aggregate(Count('field_10'))
        avg_result = TestModel.objects.aggregate(Avg('field_10'))
        min_result = TestModel.objects.aggregate(Min('field_10'))
        max_result = TestModel.objects.aggregate(Max('field_10'))
        sum_result = TestModel.objects.aggregate(Sum('field_10'))

        self.assertEqual(count_result, {'field_10__count': DBTestCase.NUMBER_OF_RECORDS})
        self.assertEqual(min_result, {'field_10__min': 1})
        self.assertEqual(max_result, {'field_10__max': DBTestCase.NUMBER_OF_RECORDS})
        self.assertEqual(avg_result, {'field_10__avg': float(sum(counters))/len(counters)})
        self.assertEqual(sum_result, {'field_10__sum': sum(counters)})


# Path: doj/tests/db/models.py
# class TestModel(models.Model):
# 
#     field_1 = models.BigIntegerField(default=9223372036854775807)
#     field_2 = models.BinaryField(default="010101010101010101010101")  # Causes problems with SQLite
#     field_3 = models.BooleanField(default=True)
#     field_4 = models.CharField(max_length=256, default="abc")
#     field_5 = models.DateField(auto_now_add=True)
#     field_6 = models.DateTimeField(auto_now_add=True)
#     field_7 = models.DecimalField(max_digits=5, decimal_places=2, default=123.45)
#     field_8 = models.EmailField(default='test@email.at')
#     field_9 = models.FloatField(default=12.34)
#     field_10 = models.IntegerField(default=1234)
#     field_11 = models.GenericIPAddressField(default='192.0.2.30')
#     field_12 = models.NullBooleanField(default=None)
#     field_13 = models.PositiveIntegerField(default=1234)
#     field_14 = models.PositiveSmallIntegerField(default=1234)
#     field_15 = models.SmallIntegerField(default=1234)
#     field_16 = models.TextField(default="abc")
#     field_18 = models.TimeField(auto_now_add=True)
#     field_19 = models.ForeignKey('TestModelRelation', related_name='test_models', null=True, blank=True)
# 
# class TestModelRelation(models.Model):
# 
#     field_1 = models.BigIntegerField(default=1234)


# Path: doj/tests/db/tests.py
from datetime import datetime
from django.test import TestCase
from django.db import transaction
from django.db.models import Count, Min, Max, Avg, Sum
from django.db.models.query import EmptyQuerySet
from doj.tests.db.models import TestModel, TestModelRelation

            test_model.save()

        first = TestModel.objects.order_by('id').first()
        last = TestModel.objects.order_by('id').last()

        self.assertIsInstance(first, TestModel)
        self.assertIsInstance(last, TestModel)

    def test_aggregate(self):
        counter = 1
        counters = []
        for _ in range(0, DBTestCase.NUMBER_OF_RECORDS):
            test_model = TestModel(field_10=counter)
            test_model.save()

            counters.append(counter)
            counter += 1

        count_result = TestModel.objects.aggregate(Count('field_10'))
        avg_result = TestModel.objects.aggregate(Avg('field_10'))
        min_result = TestModel.objects.aggregate(Min('field_10'))
        max_result = TestModel.objects.aggregate(Max('field_10'))
        sum_result = TestModel.objects.aggregate(Sum('field_10'))

        self.assertEqual(count_result, {'field_10__count': DBTestCase.NUMBER_OF_RECORDS})
        self.assertEqual(min_result, {'field_10__min': 1})
        self.assertEqual(max_result, {'field_10__max': DBTestCase.NUMBER_OF_RECORDS})
        self.assertEqual(avg_result, {'field_10__avg': float(sum(counters))/len(counters)})
        self.assertEqual(sum_result, {'field_10__sum': sum(counters)})


    def test_aggregate_join(self):