'Ensure that a query returns a valid set of results.'

def test_scalar_cursor_behaviour(self):

    person1 = self.Person(name='User    A', age=20)
    person1.save()
    person2 = self.Person(name='User    B', age=30)
    person2.save()
    people = self.Person.objects.scalar('name')
    self.assertEqual(people.count(), 2)
    results = list(people)
    self.assertEqual(results[0], 'User    A')
    self.assertEqual(results[1], 'User    B')
    people = self.Person.objects(age=20).scalar('name')
    self.assertEqual(people.count(), 1)
    person = people.next()
    self.assertEqual(person, 'User    A')
    people = list(self.Person.objects.limit(1).scalar('name'))
    self.assertEqual(len(people), 1)
    self.assertEqual(people[0], 'User    A')
    people = list(self.Person.objects.skip(1).scalar('name'))
    self.assertEqual(len(people), 1)
    self.assertEqual(people[0], 'User    B')
    person3 = self.Person(name='User    C', age=40)
    person3.save()
    people = list(self.Person.objects[:2].scalar('name'))
    self.assertEqual(len(people), 2)
    self.assertEqual(people[0], 'User    A')
    self.assertEqual(people[1], 'User    B')
    people = list(self.Person.objects[1:].scalar('name'))
    self.assertEqual(len(people), 2)
    self.assertEqual(people[0], 'User    B')
    self.assertEqual(people[1], 'User    C')
    people = list(self.Person.objects[1:2].scalar('name'))
    self.assertEqual(len(people), 1)
    self.assertEqual(people[0], 'User    B')
    people = list(self.Person.objects[1:1].scalar('name'))
    self.assertEqual(len(people), 0)
    people = list(self.Person.objects.scalar('name')[80000:80001])
    self.assertEqual(len(people), 0)
    self.Person.objects.delete()
    for i in range(55):
        self.Person(name=('A%s' % i), age=i).save()
    self.assertEqual(self.Person.objects.scalar('name').count(), 55)
    self.assertEqual('A0', ('%s' % self.Person.objects.order_by('name').scalar('name').first()))
    self.assertEqual('A0', ('%s' % self.Person.objects.scalar('name').order_by('name')[0]))
    if six.PY3:
        self.assertEqual("['A1',    'A2']", ('%s' % self.Person.objects.order_by('age').scalar('name')[1:3]))
        self.assertEqual("['A51',    'A52']", ('%s' % self.Person.objects.order_by('age').scalar('name')[51:53]))
    else:
        self.assertEqual("[u'A1',    u'A2']", ('%s' % self.Person.objects.order_by('age').scalar('name')[1:3]))
        self.assertEqual("[u'A51',    u'A52']", ('%s' % self.Person.objects.order_by('age').scalar('name')[51:53]))
    person = self.Person.objects.order_by('name').first()
    self.assertEqual('A0', ('%s' % self.Person.objects.scalar('name').with_id(person.id)))
    pks = self.Person.objects.order_by('age').scalar('pk')[1:3]
    names = self.Person.objects.scalar('name').in_bulk(list(pks)).values()
    if six.PY3:
        expected = "['A1',    'A2']"
    else:
        expected = "[u'A1',    u'A2']"
    self.assertEqual(expected, ('%s' % sorted(names)))
