Vinz
2012-05-18 16:08:03 UTC
Hi all,
I'm writing some tests for my SqlAlchemy DB using the fixtures module,
and I'm currently trying to populate a one to many relationship.
http://farmdev.com/projects/fixture/using-dataset.html#referencing-foreign-dataset-classes
We have a relationship between Author and Address : (very much like
the SA doc)
------------ model.py ---------------
class Address(Base):
author_id = Column(Integer, ForeignKey('author.id'))
author = relationship("Author")
------------------------------------
Then I defined some data (fixture specific) :
------------ modelData.py --------------
class AuthorDate(DataSet):
class foo:
name = "foo"
class AddressData(DataSet):
class my_address:
name = "my address"
author_id = AuthorData.foo.ref('id')
author = [AuthorData.foo,] # this is handy with the fixtures
-------------
Then we have a file with the tests :
-------- testAuthor.py
import model
from modelData import *
# call to __init__.py and Base.metadata.bind = engine, sessionmaker
# definition of dbfixture
class TestAuthor(DataTestCase, unittest.TestCase):
#datasets = [AuthorData, etc]
#call to Base.metadata.create_all()
self.data = dbfixture.data(AuthorData, etc)
self.setup() # <--------- supposed to load all datasets and
populate self.data <---- raises the error
---------------------
And I get the following error when I run my tests :
Traceback (most recent call last):
File "tests/testCard.py", line 88, in setUp
self.data.setup()
File "/usr/local/lib/python2.6/dist-packages/fixture/base.py", line
71, in setup
[ bla bla ... ]
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
attributes.py", line 654, in get_all_pending
ret = [(instance_state(current), current)]
LoadError: AttributeError: 'list' object has no attribute
'_sa_instance_state' (with 'my_address' of
'<fixture.dataset.dataset.my_address object at 0x26a64d0>' in
<AddressData at 0x22b3850 with keys ['my_address']>)
Do you have any idea about what my problem is ??!
Where can I find some documentation about _sa_instance_state ? (didn't
find what that is)
Also, I don't quite understand the following warning from the fixture-
s doc :
"
... class two_worlds:
... title = "Man of Two Worlds"
... authors = [Authors.frank_herbert, Authors.brian_herbert]
However, in some cases you may need to reference an attribute that
does not have a value until it is loaded, like a serial ID column.
(****Note that this is not supported by the SQLAlchemy data layer when
using sessions****.) To facilitate this, each inner class of a DataSet
gets decorated with a special method, ref(), that can be used to
reference a column value before it exists, i.e.:
"
It may be related (though I understand they are talking about
ref('id') and that isn't my concern (yet) (I used it in other tests
and had no error messages).
Hope I was clear.
Many thanks in advance !
Vincent, not much experienced in SA and python
I'm writing some tests for my SqlAlchemy DB using the fixtures module,
and I'm currently trying to populate a one to many relationship.
http://farmdev.com/projects/fixture/using-dataset.html#referencing-foreign-dataset-classes
We have a relationship between Author and Address : (very much like
the SA doc)
------------ model.py ---------------
class Address(Base):
author_id = Column(Integer, ForeignKey('author.id'))
author = relationship("Author")
------------------------------------
Then I defined some data (fixture specific) :
------------ modelData.py --------------
class AuthorDate(DataSet):
class foo:
name = "foo"
class AddressData(DataSet):
class my_address:
name = "my address"
author_id = AuthorData.foo.ref('id')
author = [AuthorData.foo,] # this is handy with the fixtures
-------------
Then we have a file with the tests :
-------- testAuthor.py
import model
from modelData import *
# call to __init__.py and Base.metadata.bind = engine, sessionmaker
# definition of dbfixture
class TestAuthor(DataTestCase, unittest.TestCase):
#datasets = [AuthorData, etc]
#call to Base.metadata.create_all()
self.data = dbfixture.data(AuthorData, etc)
self.setup() # <--------- supposed to load all datasets and
populate self.data <---- raises the error
---------------------
And I get the following error when I run my tests :
Traceback (most recent call last):
File "tests/testCard.py", line 88, in setUp
self.data.setup()
File "/usr/local/lib/python2.6/dist-packages/fixture/base.py", line
71, in setup
[ bla bla ... ]
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
attributes.py", line 654, in get_all_pending
ret = [(instance_state(current), current)]
LoadError: AttributeError: 'list' object has no attribute
'_sa_instance_state' (with 'my_address' of
'<fixture.dataset.dataset.my_address object at 0x26a64d0>' in
<AddressData at 0x22b3850 with keys ['my_address']>)
Do you have any idea about what my problem is ??!
Where can I find some documentation about _sa_instance_state ? (didn't
find what that is)
Also, I don't quite understand the following warning from the fixture-
s doc :
"
... class two_worlds:
... title = "Man of Two Worlds"
... authors = [Authors.frank_herbert, Authors.brian_herbert]
However, in some cases you may need to reference an attribute that
does not have a value until it is loaded, like a serial ID column.
(****Note that this is not supported by the SQLAlchemy data layer when
using sessions****.) To facilitate this, each inner class of a DataSet
gets decorated with a special method, ref(), that can be used to
reference a column value before it exists, i.e.:
"
It may be related (though I understand they are talking about
ref('id') and that isn't my concern (yet) (I used it in other tests
and had no error messages).
Hope I was clear.
Many thanks in advance !
Vincent, not much experienced in SA and python
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.