I am writing a unit test for SQLAlchemy model class with one-to-many relationship but am not able to add a mocked object into the collection.
Classes under test:
class PCLRun(Base):
__tablename__ = 'pcl_runs'
id = Column(Integer, primary_key=True)
...
files = relationship("PCLOutputFile", backref='pcl_run')
class PCLOutputFile(Base):
__tablename__ = 'pcl_output_files'
id = Column(Integer, primary_key=True)
...
pcl_run_id = Column(Integer, ForeignKey('pcl_runs.id'))
Test code:
class PCLRunTests(unittest.TestCase):
def test_foo(self):
file_mock = mock.Mock()
pcl_run = PCLRun()
pcl_run.files.append(file_mock)
...
Appending the mock object raises an exception:
TypeError: 'Mock' object has no attribute '__getitem__'
Is there any way to unit test the class containing the relationship by adding mocks into it while keeping the collection behave like a simple list?
I'm using mock 1.0.1 and sqlalchemy 0.8.2.