'Tests for complex datetime fields - which can handle
microseconds without rounding.'

def test_complexdatetime_usage(self):

    class LogEntry(Document, ):
        date = ComplexDateTimeField()
    LogEntry.drop_collection()
    d1 = datetime.datetime(1950, 1, 1, 0, 0, 1, 999)
    log = LogEntry()
    log.date = d1
    log.save()
    log1 = LogEntry.objects.get(date=d1)
    self.assertEqual(log, log1)
    for i in range(1951, 2010):
        d = datetime.datetime(i, 1, 1, 0, 0, 1, 999)
        LogEntry(date=d).save()
    self.assertEqual(LogEntry.objects.count(), 60)
    logs = LogEntry.objects.order_by('date')
    i = 0
    while (i < 59):
        self.assertTrue((logs[i].date <= logs[(i + 1)].date))
        i += 1
    logs = LogEntry.objects.order_by('-date')
    i = 0
    while (i < 59):
        self.assertTrue((logs[i].date >= logs[(i + 1)].date))
        i += 1
    logs = LogEntry.objects.filter(date__gte=datetime.datetime(1980, 1, 1))
    self.assertEqual(logs.count(), 30)
    logs = LogEntry.objects.filter(date__lte=datetime.datetime(1980, 1, 1))
    self.assertEqual(logs.count(), 30)
    logs = LogEntry.objects.filter(date__lte=datetime.datetime(2011, 1, 1), date__gte=datetime.datetime(2000, 1, 1))
    self.assertEqual(logs.count(), 10)
    LogEntry.drop_collection()
    for microsecond in (99, 999, 9999, 10000):
        LogEntry(date=datetime.datetime(2015, 1, 1, 0, 0, 0, microsecond)).save()
    logs = list(LogEntry.objects.order_by('date'))
    for (next_idx, log) in enumerate(logs[:(-1)], start=1):
        next_log = logs[next_idx]
        self.assertTrue((log.date < next_log.date))
    logs = list(LogEntry.objects.order_by('-date'))
    for (next_idx, log) in enumerate(logs[:(-1)], start=1):
        next_log = logs[next_idx]
        self.assertTrue((log.date > next_log.date))
    logs = LogEntry.objects.filter(date__lte=datetime.datetime(2015, 1, 1, 0, 0, 0, 10000))
    self.assertEqual(logs.count(), 4)
