Exemplo n.º 1
0
    def test_sorted_by_unrestricted_mixed(self):
        class Person(Entity):
            pk = Field(Integer, primary_key=True)
            firstname = Field(String)
            surname = Field(String)

        p1 = Person(pk=1, firstname="Homer", surname="Simpson")

        Session.default.commit()
        Session.default.expunge()

        p2 = Person(pk=2, firstname="Bart", surname="Simpson")
        p3 = Person(pk=3, firstname="Carl", surname="Carlson")

        q = Query(Person)

        self.assertEqual(q.sort_by(Person.pk.ascending).all(), [p1, p2, p3])
        self.assertEqual(q.sort_by(Person.pk.descending).all(), [p3, p2, p1])

        self.assertEqual(q.sort_by(Person.firstname.ascending).all(), [p2, p3, p1])
        self.assertEqual(q.sort_by(Person.firstname.descending).all(), [p1, p3, p2])

        self.assertEqual(q.sort_by(Person.surname.ascending, Person.firstname.ascending).all(), [p3, p2, p1])
        self.assertEqual(q.sort_by(Person.surname.ascending, Person.firstname.descending).all(), [p3, p1, p2])
        self.assertEqual(q.sort_by(Person.surname.descending, Person.firstname.ascending).all(), [p2, p1, p3])
        self.assertEqual(q.sort_by(Person.surname.descending, Person.firstname.descending).all(), [p1, p2, p3])
Exemplo n.º 2
0
    def test_range_from_disk (self):
        class Person (Entity):
            pk = Field(Integer, primary_key=True)
            firstname = Field(String)
            surname = Field(String)

        p1 = Person(pk=1, firstname='Homer', surname='Simpson')
        p2 = Person(pk=2, firstname='Bart', surname='Simpson')
        p3 = Person(pk=3, firstname='Carl', surname='Carlson')

        Session.default.commit()
        Session.default.expunge()

        q = Query(Person)

        self.assertEqual(q.sort_by(Person.pk.ascending).one(), p1)
        self.assertEqual(q.sort_by(Person.pk.descending).one(), p3)

        self.assertEqual(q.sort_by(Person.pk.ascending).all(), [p1, p2, p3])
        self.assertEqual(q.sort_by(Person.pk.descending).all(), [p3, p2, p1])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(start=1),
                         [p2, p3])
        self.assertEqual(q.sort_by(Person.pk.descending).range(start=1),
                         [p2, p1])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(stop=2),
                         [p1, p2])
        self.assertEqual(q.sort_by(Person.pk.descending).range(stop=2),
                         [p3, p2])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(step=2),
                         [p1, p3])
        self.assertEqual(q.sort_by(Person.pk.descending).range(step=2),
                         [p3, p1])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(start=1,
                         stop=2), [p2])
        self.assertEqual(q.sort_by(Person.pk.descending).range(start=1,
                         stop=2), [p2])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(start=1,
                         step=2), [p2])
        self.assertEqual(q.sort_by(Person.pk.descending).range(start=1,
                         step=2), [p2])

        self.assertEqual(q.sort_by(Person.pk.ascending).range(start=1,
                         stop=3, step=2), [p2])
        self.assertEqual(q.sort_by(Person.pk.descending).range(start=1,
                         stop=2, step=2), [p2])