1 対多の関係を持つ SQLAlchemy モデル クラスの単体テストを作成していますが、モック オブジェクトをコレクションに追加できません。
テスト中のクラス:
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'))
テストコード:
class PCLRunTests(unittest.TestCase):
def test_foo(self):
file_mock = mock.Mock()
pcl_run = PCLRun()
pcl_run.files.append(file_mock)
...
モック オブジェクトを追加すると、例外が発生します。
TypeError: 'Mock' object has no attribute '__getitem__'
コレクションを単純なリストのように動作させながら、モックを追加してリレーションシップを含むクラスを単体テストする方法はありますか?
私はモック 1.0.1 と sqlalchemy 0.8.2 を使用しています。