I modified the test to run against UnitTest since I dont have nose  
installed, and all tests pass for me.   My version is attached.  Make  
sure you're actually running 0.5 since the test seems to be testing  
behavior that didn't work in older 0.4 versions of SQLA (although the  
other tests seem to be testing the same thing, so maybe thats not it).



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

from datetime import datetime, timedelta
from sqlalchemy import create_engine, MetaData, Table, Column, ForeignKey
from sqlalchemy.types import Integer, DateTime, Text
from sqlalchemy.orm import mapper, relation
from sqlalchemy.orm import scoped_session, sessionmaker
import unittest

def assert_identical (obj1, obj2):
    assert obj1 is obj2, "%r is not %r" % (obj1, obj2)

def assert_equal(x, y):
    assert x== y
    
metadata = MetaData()

bars = Table("bars", metadata,
    Column("id", Integer, primary_key=True))

foos = Table("foos", metadata,
    Column("id", Integer, primary_key=True),
    Column("bar_id", None, ForeignKey("bars.id"), nullable=False))

class Bar (object):
    def __init__ (self, id_):
        self.id = id_

class Foo (object):
    def __init__ (self, bar):
        self.id = None
        self.bar = bar

mapper(Bar, bars, properties={
    'id':bars.c.id})

mapper(Foo, foos, properties={
    'id':foos.c.id,
    'bar':relation(Bar, backref="foos")})

Session = sessionmaker()

class TestMerge (unittest.TestCase):

    def setUp (self):
        metadata.bind = create_engine("sqlite:///:memory:", echo=True)
        metadata.create_all()

    def tearDown (self):
        metadata.drop_all()
        metadata.bind = None

    def test_normal_merge (self):
        s = Session()
        assert_identical(s.merge(Bar(1)), s.merge(Bar(1)))

    def test_merge_flush (self):
        s = Session()
        bar = s.merge(Bar(1))
        before_id = id(bar)
        s.flush()
        after_id = id(bar)
        assert_equal(before_id, after_id)

    def test_merge_related_flush (self):
        s = Session()
        bar = s.merge(Bar(1))
        before_id = id(bar)
        foo = Foo(bar)
        s.flush()
        after_id = id(bar)
        related_id = id(foo.bar)
        assert_equal(before_id, after_id)
        assert_equal(before_id, related_id)

    def test_double_merge_flush (self):
        s = Session()
        bar1 = s.merge(Bar(1))
        bar2 = s.merge(Bar(1))
        s.flush()
        assert_identical(bar1, bar2)

    def test_merge_bug (self):
        s = Session()
        foo1 = Foo(s.merge(Bar(1)))
        before_id = id(foo1.bar)
        foo2 = Foo(s.merge(Bar(1)))
        after_id = id(foo1.bar)
        other_id = id(foo2.bar)
        assert_equal(before_id, other_id)
        assert_equal(after_id, other_id)
        assert_equal(before_id, after_id)
        assert_identical(foo1.bar, foo2.bar)
        
if __name__ == '__main__':
    unittest.main()


On Dec 12, 2008, at 3:23 PM, Jonathon Anderson wrote:

>
> I am experiencing strange behavior in with Session.merge in 0.5.0rc4,
> where a flush cause merged objects on related entities to change
> object identity. I think this is a bug, but I might be missing
> something about Session.merge. (I've never used it before.)
>
> My test case can be viewed on pastebin at http://pastebin.com/ 
> m72d6885b
> (only TestMerge.test_merge_bug should fail).
>
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google  
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en
> -~----------~----~----~----~------~----~------~--~---
>

Reply via email to