test_lazy.py :  » Database » SQLObject » SQLObject-0.12.4 » sqlobject » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLObject 
SQLObject » SQLObject 0.12.4 » sqlobject » tests » test_lazy.py
from sqlobject import *
from sqlobject.tests.dbtest import *

########################################
## Lazy updates
########################################

class Lazy(SQLObject):

    class sqlmeta:
        lazyUpdate = True
    name = StringCol()
    other = StringCol(default='nothing')
    third = StringCol(default='third')

class TestLazyTest:

    def setup_method(self, meth):
        # All this stuff is so that we can track when the connection
        # does an actual update; we put in a new _SO_update method
        # that calls the original and sets an instance variable that
        # we can later check.
        setupClass(Lazy)
        self.conn = Lazy._connection
        self.conn.didUpdate = False
        self._oldUpdate = self.conn._SO_update
        newUpdate = (
            lambda so, values, s=self, c=self.conn, o=self._oldUpdate:
            self._alternateUpdate(so, values, c, o))
        self.conn._SO_update = newUpdate

    def teardown_method(self, meth):
        self.conn._SO_update = self._oldUpdate
        del self._oldUpdate

    def _alternateUpdate(self, so, values, conn, oldUpdate):
        conn.didUpdate = True
        return oldUpdate(so, values)

    def test_lazy(self):
        assert not self.conn.didUpdate
        obj = Lazy(name='tim')
        # We just did an insert, but not an update:
        assert not self.conn.didUpdate
        obj.set(name='joe')
        assert obj.dirty
        assert obj.name == 'joe'
        assert not self.conn.didUpdate
        obj.syncUpdate()
        assert obj.name == 'joe'
        assert self.conn.didUpdate
        assert not obj.dirty
        assert obj.name == 'joe'
        self.conn.didUpdate = False

        obj = Lazy(name='frank')
        obj.name = 'joe'
        assert not self.conn.didUpdate
        assert obj.dirty
        assert obj.name == 'joe'
        obj.name = 'joe2'
        assert not self.conn.didUpdate
        assert obj.dirty
        assert obj.name == 'joe2'
        obj.syncUpdate()
        assert obj.name == 'joe2'
        assert not obj.dirty
        assert self.conn.didUpdate
        self.conn.didUpdate = False

        obj = Lazy(name='loaded')
        assert not obj.dirty
        assert not self.conn.didUpdate
        assert obj.name == 'loaded'
        obj.name = 'unloaded'
        assert obj.dirty
        assert obj.name == 'unloaded'
        assert not self.conn.didUpdate
        obj.sync()
        assert not obj.dirty
        assert obj.name == 'unloaded'
        assert self.conn.didUpdate
        self.conn.didUpdate = False
        obj.name = 'whatever'
        assert obj.dirty
        assert obj.name == 'whatever'
        assert not self.conn.didUpdate
        obj._SO_loadValue('name')
        assert obj.dirty
        assert obj.name == 'whatever'
        assert not self.conn.didUpdate
        obj._SO_loadValue('other')
        assert obj.name == 'whatever'
        assert not self.conn.didUpdate
        obj.syncUpdate()
        assert self.conn.didUpdate
        self.conn.didUpdate = False

        # Now, check that get() doesn't screw
        # cached objects' validator state.
        obj_id = obj.id
        old_state = obj._SO_validatorState
        obj = Lazy.get(obj_id)
        assert not obj.dirty
        assert not self.conn.didUpdate
        assert obj._SO_validatorState is old_state
        assert obj.name == 'whatever'
        obj.name = 'unloaded'
        assert obj.name == 'unloaded'
        assert obj.dirty
        assert not self.conn.didUpdate
        # Fetch the object again with get() and
        # make sure dirty is still set, as the
        # object should come from the cache.
        obj = Lazy.get(obj_id)
        assert obj.dirty
        assert not self.conn.didUpdate
        assert obj.name == 'unloaded'
        obj.syncUpdate()
        assert self.conn.didUpdate
        assert not obj.dirty
        self.conn.didUpdate = False

        # Then clear the cache, and try a get()
        # again, to make sure stuf like _SO_createdValues
        # is properly initialized.
        self.conn.cache.clear()
        obj = Lazy.get(obj_id)
        assert not obj.dirty
        assert not self.conn.didUpdate
        assert obj.name == 'unloaded'
        obj.name = 'spongebob'
        assert obj.name == 'spongebob'
        assert obj.dirty
        assert not self.conn.didUpdate
        obj.syncUpdate()
        assert self.conn.didUpdate
        assert not obj.dirty
        self.conn.didUpdate = False

        obj = Lazy(name='last')
        assert not obj.dirty
        obj.syncUpdate()
        assert not self.conn.didUpdate
        assert not obj.dirty
        # Check that setting multiple values
        # actually works. This was broken
        # and just worked because we were testing
        # only one value at a time, so 'name'
        # had the right value after the for loop *wink*
        # Also, check that passing a name that is not
        # a valid column doesn't break, but instead
        # just does a plain setattr.
        obj.set(name='first', other='who', third='yes')
        assert obj.name == 'first'
        assert obj.other == 'who'
        assert obj.third == 'yes'
        assert obj.dirty
        assert not self.conn.didUpdate
        obj.syncUpdate()
        assert self.conn.didUpdate
        assert not obj.dirty
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.