~sidnei/storm/mssql-support

« back to all changes in this revision

Viewing changes to tests/store/base.py

  • Committer: Gustavo Niemeyer
  • Date: 2007-08-07 23:15:09 UTC
  • mfrom: (156.1.8 preset-primary-key)
  • Revision ID: gustavo@niemeyer.net-20070807231509-iw58uiygzuef5wth
Merged preset-primary-key branch [r=radix,jkakar] [f=126080]

This branch introduces a new hook which allows backends to modify
variables which are part of the primary key of an object before
they are inserted in the database.  Also, lazy values may now be
used in the primary key, which means that it's possible to set these
primary variables to SQL expressions if needed.

Other minor changes include an optimization in Result: it will
preset the raw cursor's arraysize to a better value if it's found
to be 1.  Result.__iter__() was also fixed to parse rows through
the from_database hook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1029
1029
        self.store.reload(bar)
1030
1030
        self.assertEquals(bar.title, "Title 500")
1031
1031
 
 
1032
    def test_add_completely_undefined(self):
 
1033
        foo = Foo()
 
1034
        self.store.add(foo)
 
1035
        self.store.flush()
 
1036
        self.assertEquals(type(foo.id), int)
 
1037
        self.assertEquals(foo.title, u"Default Title")
 
1038
 
1032
1039
    def test_remove_commit(self):
1033
1040
        foo = self.store.get(Foo, 20)
1034
1041
        self.store.remove(foo)
3999
4006
        result = self.store.find(Money, value=decimal.Decimal("12.3456"))
4000
4007
        self.assertEquals(result.one(), money)
4001
4008
 
4002
 
    # XXX Lazy expressions not supported on primary keys yet.
4003
 
    #def test_fill_missing_primary_key_with_lazy_value(self):
4004
 
    #    foo = self.store.get(Foo, 10)
4005
 
    #    foo.id = SQL("40")
4006
 
    #    self.store.flush()
4007
 
    #    self.assertEquals(foo.id, 40)
4008
 
    #    self.assertEquals(self.store.get(Foo, 10), None)
4009
 
    #    self.assertEquals(self.store.get(Foo, 40), foo)
 
4009
    def test_fill_missing_primary_key_with_lazy_value(self):
 
4010
        foo = self.store.get(Foo, 10)
 
4011
        foo.id = SQL("40")
 
4012
        self.store.flush()
 
4013
        self.assertEquals(foo.id, 40)
 
4014
        self.assertEquals(self.store.get(Foo, 10), None)
 
4015
        self.assertEquals(self.store.get(Foo, 40), foo)
 
4016
 
 
4017
    def test_fill_missing_primary_key_with_lazy_value_on_creation(self):
 
4018
        foo = Foo()
 
4019
        foo.id = SQL("40")
 
4020
        self.store.add(foo)
 
4021
        self.store.flush()
 
4022
        self.assertEquals(foo.id, 40)
 
4023
        self.assertEquals(self.store.get(Foo, 40), foo)
 
4024
 
 
4025
    def test_preset_primary_key(self):
 
4026
        check = []
 
4027
        def preset_primary_key(primary_columns, primary_variables):
 
4028
            check.append([(variable.is_defined(), variable.get_lazy())
 
4029
                          for variable in primary_variables])
 
4030
            check.append([column.name for column in primary_columns])
 
4031
            primary_variables[0].set(SQL("40"))
 
4032
 
 
4033
        class DatabaseWrapper(object):
 
4034
            """Wrapper to inject our custom preset_primary_key hook."""
 
4035
 
 
4036
            def __init__(self, database):
 
4037
                self.database = database
 
4038
 
 
4039
            def connect(self):
 
4040
                connection = self.database.connect()
 
4041
                connection.preset_primary_key = preset_primary_key
 
4042
                return connection
 
4043
 
 
4044
        store = Store(DatabaseWrapper(self.database))
 
4045
 
 
4046
        foo = store.add(Foo())
 
4047
 
 
4048
        store.flush()
 
4049
        try:
 
4050
            self.assertEquals(check, [[(False, None)], ["id"]])
 
4051
            self.assertEquals(foo.id, 40)
 
4052
        finally:
 
4053
            store.close()
4010
4054
 
4011
4055
 
4012
4056
class EmptyResultSetTest(object):