方法模拟和sqlalchemy(psycopg2)

时间:2017-04-12 18:30:23

标签: python unit-testing sqlalchemy mocking psycopg2

我有方法,它做了一些工作,返回一些值并将其传递给sqlalchemy查询。 在测试的情况下,我尝试模拟这个方法,但得到例外:#! /bin/bash DATA_FOLDER="/path/to/local/data/folder" REMOTE_TEMP_FOLDER="/linux/system/path/to/temp/folder/to/save/compressed /file" # you should have created it REMOTE_HDFS_DATA_FOLDER="hdfs/data/folder" for fpath in $DATA_FOLDER/*; do fname=`basename "$fpath"` # Keep the original file and create a compressed copy gzip -c $DATA_FOLDER/$fname > $fname.gz echo "Compressed \"$fname\" and created a local copy." # Upload compressed the file to remote server scp $fname.gz namenode:$REMOTE_TEMP_FOLDER/$fname.gz echo "Uploaded \"$fname\" to the namenode:$REMOTE_TEMP_FOLDER." # Delete the local copy of the compressed file rm $fname.gz echo "Deleted local copy of \"$fname.gz\"." # Decompress file and put in HDFS on the remote server # Each csv data file will be placed in its own directory (creating Hive external table requires it) ssh namenode ". ~/.bash_profile; hdfs dfs -mkdir $REMOTE_HDFS_DATA_FOLDER/$fname.dir" ssh namenode ". ~/.bash_profile; gunzip -c $REMOTE_TEMP_FOLDER/$fname.gz | hdfs dfs -put - $REMOTE_HDFS_DATA_FOLDER/$fname.dir/$fname" echo "Copied \"$fname\" to HDFS." ssh namenode ". ~/.bash_profile; hdfs dfs -ls -h $REMOTE_HDFS_DATA_FOLDER/$fname.dir/$fname" # Remove the compressed copy on the remote server ssh namenode "rm $REMOTE_TEMP_FOLDER/$fname.gz" echo "Deleted remote copy of \"$fname.gz\"." done

我有代码,如下所示:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'MagicMock'

调用此类代码后,我得到了这样的追溯:

from mock import patch
from sqlalchemy import Integer, Column

from pr.core.model import Base
from pr.core.model import Session


class SomeModel(Base):
    __tablename__ = 'table'
    id = Column(Integer, primary_key=True)


class SomeObj(object):
    def some_method(self):
        return 1

    def call(self):
        return Session.query(SomeModel).filter(SomeModel.id == self.some_method()).all()


def main():
    with patch('__main__.SomeObj.some_method') as mocked:
        SomeObj().call()
        assert mocked.called
if __name__ == '__main__':
    main()

有没有办法正确模拟方法?

0 个答案:

没有答案