<s> from __future__ import absolute_import , division <EOL> from twisted . internet import defer <EOL> from twisted . trial import unittest <EOL> import txmongo <EOL> mongo_host = "<STR_LIT:localhost>" <EOL> mongo_port = <NUM_LIT> <EOL> class TestFindAndModify ( unittest . TestCase ) : <EOL> @ defer . inlineCallbacks <EOL> def setUp ( self ) : <EOL> self . conn = yield txmongo . MongoConnection ( mongo_host , mongo_port ) <EOL> self . coll = self . conn . mydb . mycol <EOL> @ defer . inlineCallbacks <EOL> def test_Update ( self ) : <EOL> yield self . coll . insert ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT> } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT> } ] , safe = True ) <EOL> res = yield self . coll . find_one ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( res [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> res = yield self . coll . find_and_modify ( { "<STR_LIT>" : "<STR_LIT>" } , { "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:1> } } ) <EOL> self . assertEqual ( res , None ) <EOL> res = yield self . coll . find_and_modify ( { "<STR_LIT>" : "<STR_LIT>" } , { "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:1> } } ) <EOL> self . assertEqual ( res [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> res = yield self . coll . find_and_modify ( { "<STR_LIT>" : "<STR_LIT>" } , { "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:1> } } , new = True ) <EOL> self . assertEqual ( res [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> res = yield self . coll . find_one ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( res [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> def test_InvalidOptions ( self ) : <EOL> self . assertFailure ( self . coll . find_and_modify ( ) , ValueError ) <EOL> self . assertFailure ( self . coll . find_and_modify ( update = { "<STR_LIT>" : { '<STR_LIT:x>' : <NUM_LIT> } } , remove = True ) , <EOL> ValueError ) <EOL> @ defer . inlineCallbacks <EOL> def test_Remove ( self ) : <EOL> yield self . coll . insert ( { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> doc = yield self . coll . find_and_modify ( remove = True ) <EOL> self . assertEqual ( doc [ '<STR_LIT:x>' ] , <NUM_LIT> ) <EOL> cnt = yield self . coll . count ( ) <EOL> self . assertEqual ( cnt , <NUM_LIT:0> ) <EOL> @ defer . inlineCallbacks <EOL> def test_Upsert ( self ) : <EOL> yield self . coll . find_and_modify ( { '<STR_LIT:x>' : <NUM_LIT> } , update = { "<STR_LIT>" : { '<STR_LIT:y>' : <NUM_LIT> } } , upsert = True ) <EOL> docs = yield self . coll . find ( fields = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( docs , [ { '<STR_LIT:x>' : <NUM_LIT> , '<STR_LIT:y>' : <NUM_LIT> } ] ) <EOL> @ defer . inlineCallbacks <EOL> def tearDown ( self ) : <EOL> yield self . coll . drop ( ) <EOL> yield self . conn . disconnect ( ) </s>
