<s> from unittest import TestCase <EOL> from mock import patch , MagicMock <EOL> import sqlalchemy as sa <EOL> from sqlalchemy . orm import Session <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from crate . client . cursor import Cursor <EOL> fake_cursor = MagicMock ( name = '<STR_LIT>' ) <EOL> FakeCursor = MagicMock ( name = '<STR_LIT>' , spec = Cursor ) <EOL> FakeCursor . return_value = fake_cursor <EOL> class SqlAlchemyBulkTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . engine = sa . create_engine ( '<STR_LIT>' ) <EOL> Base = declarative_base ( bind = self . engine ) <EOL> class Character ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> name = sa . Column ( sa . String , primary_key = True ) <EOL> age = sa . Column ( sa . Integer ) <EOL> self . character = Character <EOL> self . session = Session ( ) <EOL> @ patch ( '<STR_LIT>' , FakeCursor ) <EOL> def test_bulk_save ( self ) : <EOL> chars = [ <EOL> self . character ( name = '<STR_LIT>' , age = <NUM_LIT> ) , <EOL> self . character ( name = '<STR_LIT>' , age = <NUM_LIT> ) , <EOL> self . character ( name = '<STR_LIT>' , age = <NUM_LIT> ) , <EOL> ] <EOL> fake_cursor . description = ( ) <EOL> fake_cursor . rowcount = len ( chars ) <EOL> fake_cursor . executemany . return_value = [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> } , <EOL> ] <EOL> self . session . bulk_save_objects ( chars ) <EOL> ( stmt , bulk_args ) , _kwargs = fake_cursor . executemany . call_args <EOL> expected_stmt = "<STR_LIT>" <EOL> self . assertEqual ( expected_stmt , stmt ) <EOL> expected_bulk_args = ( <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ) <EOL> self . assertEqual ( expected_bulk_args , bulk_args ) </s>
