예제 #1
0
    def test_filter_by_unrestricted_compound_from_memory (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')

        q = Query(Person)

        self.assertEqual(q.filter_by(Person.firstname == 'Homer',
                                     Person.surname == 'Simpson').one(), p1)
        self.assertEqual(q.filter_by(Person.firstname == 'Marge',
                                     Person.surname == 'Simpson').one(), None)
        self.assertEqual(q.filter_by(Person.firstname == 'Homer',
                                     Person.surname == 'Flanders').one(), None)

        self.assertEqual(q.filter_by((Person.firstname == 'Homer') &
                                     (Person.surname == 'Simpson')).one(), p1)
        self.assertEqual(q.filter_by((Person.firstname == 'Marge') &
                                     (Person.surname == 'Simpson')).one(), None)
        self.assertEqual(q.filter_by((Person.firstname == 'Homer') &
                                     (Person.surname == 'Flanders')).one(), None)
        self.assertEqual(q.filter_by((Person.firstname == 'Homer') |
                                     (Person.surname == 'Flanders')).one(), p1)
        self.assertEqual(q.filter_by((Person.firstname == 'Homer') &
                                     ~(Person.surname == 'Flanders')).one(), p1)
        self.assertEqual(q.filter_by(~(Person.firstname == 'Homer') &
                                     ~(Person.surname == 'Flanders')).one(), p2)
예제 #2
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])
예제 #3
0
    def test_get_unrestricted_from_memory (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')

        q = Query(Person)

        self.assertEqual(q.get(1), p1)
        self.assertEqual(q.get(2), p2)
        self.assertEqual(q.get(3), None)

        self.assertTrue(p1 in q.all())
        self.assertTrue(p2 in q.all())
        self.assertEqual(len(q.all()), 2)
예제 #4
0
    def test_filter_by_unrestricted_integer (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')

        Session.default.store(p1)
        Session.default.store(p2)
        Session.default.expunge()

        q = Query(Person)

        self.assertTrue('__eq__' in dir(Person.pk))
        self.assertTrue('__ne__' in dir(Person.pk))
        self.assertTrue('__lt__' in dir(Person.pk))
        self.assertTrue('__le__' in dir(Person.pk))
        self.assertTrue('__ge__' in dir(Person.pk))
        self.assertTrue('__gt__' in dir(Person.pk))

        self.assertFalse('__lt__' in dir(Person.firstname))
        self.assertFalse('__le__' in dir(Person.firstname))
        self.assertFalse('__ge__' in dir(Person.firstname))
        self.assertFalse('__gt__' in dir(Person.firstname))

        self.assertEqual(q.filter_by(Person.pk == 1).one(), p1)
        self.assertEqual(q.filter_by(Person.pk == 2).one(), p2)
        self.assertEqual(q.filter_by(Person.pk == 3).one(), None)

        self.assertEqual(q.filter_by(Person.pk < 1).one(), None)
        self.assertEqual(q.filter_by(Person.pk < 2).one(), p1)

        self.assertEqual(q.filter_by(Person.pk <= 0).one(), None)
        self.assertEqual(q.filter_by(Person.pk <= 1).one(), p1)

        self.assertEqual(q.filter_by(Person.pk >= 2).one(), p2)
        self.assertEqual(q.filter_by(Person.pk >= 3).one(), None)

        self.assertEqual(q.filter_by(Person.pk > 1).one(), p2)
        self.assertEqual(q.filter_by(Person.pk > 2).one(), None)

        self.assertEqual(q.filter_by((Person.pk >= 1) & (Person.pk <= 1)).one(), p1)
        self.assertEqual(q.filter_by((Person.pk >= 1) & ~(Person.pk > 1)).one(), p1)
예제 #5
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])
예제 #6
0
    def test_filter_by_unrestricted_from_memory (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')

        q = Query(Person)

        self.assertEqual(q.filter_by(Person.pk == 1).one(), p1)
        self.assertEqual(q.filter_by(Person.pk == 1).all(), [p1])
        self.assertEqual(q.filter_by(Person.pk == 2).one(), p2)
        self.assertEqual(q.filter_by(Person.pk == 2).all(), [p2])
        self.assertEqual(q.filter_by(Person.pk == 3).one(), None)
        self.assertEqual(q.filter_by(Person.pk == 3).all(), [])

        self.assertEqual(q.filter_by(Person.firstname == 'Homer').one(), p1)
        self.assertEqual(q.filter_by(Person.firstname == 'Bart').one(), p2)
        self.assertEqual(q.filter_by(Person.firstname == 'Marge').one(), None)

        self.assertTrue(p1 in q.filter_by(Person.surname == 'Simpson').all())
        self.assertTrue(p2 in q.filter_by(Person.surname == 'Simpson').all())
        self.assertEqual(len(q.filter_by(Person.surname == 'Simpson').all()), 2)

        self.assertEqual(q.filter_by(Person.firstname == 'Homer',
                                     Person.surname == 'Simpson').one(), p1)
        self.assertEqual(q.filter_by(Person.firstname == 'Marge',
                                     Person.surname == 'Simpson').one(), None)
        self.assertEqual(q.filter_by(Person.firstname == 'Homer',
                                     Person.surname == 'Flanders').one(), None)
예제 #7
0
    def test_filter_by_unrestricted_string (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')

        Session.default.store(p1)
        Session.default.store(p2)
        Session.default.expunge()

        q = Query(Person)

        self.assertTrue('__eq__' in dir(Person.firstname))
        self.assertTrue('__ne__' in dir(Person.firstname))
        self.assertTrue('startswith' in dir(Person.firstname))
        self.assertTrue('contains' in dir(Person.firstname))
        self.assertTrue('endswith' in dir(Person.firstname))

        self.assertFalse('startswith' in dir(Person.pk))
        self.assertFalse('contains' in dir(Person.pk))
        self.assertFalse('endswith' in dir(Person.pk))

        self.assertEqual(q.filter_by(Person.firstname == 'Homer').one(), p1)
        self.assertEqual(q.filter_by(Person.firstname == 'Bart').one(), p2)
        self.assertEqual(q.filter_by(Person.firstname == 'Marge').one(), None)

        self.assertEqual(q.filter_by(Person.firstname.startswith('H')).one(), p1)
        self.assertEqual(q.filter_by(Person.firstname.startswith('Bar')).one(), p2)
        self.assertEqual(q.filter_by(Person.firstname.startswith('M')).one(), None)

        self.assertEqual(q.filter_by(Person.firstname.contains('m')).one(), p1)
        self.assertEqual(q.filter_by(Person.firstname.contains('ar')).one(), p2)
        self.assertEqual(q.filter_by(Person.firstname.contains('g')).one(), None)

        self.assertEqual(q.filter_by(Person.firstname.endswith('r')).one(), p1)
        self.assertEqual(q.filter_by(Person.firstname.endswith('rt')).one(), p2)
        self.assertEqual(q.filter_by(Person.firstname.endswith('e')).one(), None)