<s> import re <EOL> from django . test . testcases import TestCase <EOL> from corehq . apps . sms . api import incoming <EOL> from corehq . apps . sms . mixin import VerifiedNumber <EOL> from corehq . apps . sms . models import SMS , OUTGOING <EOL> from corehq . apps . sms . util import strip_plus <EOL> class TestScript ( TestCase ) : <EOL> def get_last_outbound_sms ( self , doc_type , contact_id ) : <EOL> return SMS . get_last_log_for_recipient ( <EOL> doc_type , <EOL> contact_id , <EOL> direction = OUTGOING <EOL> ) <EOL> def parse_script ( self , script ) : <EOL> lines = [ line . strip ( ) for line in script . split ( '<STR_LIT:\n>' ) ] <EOL> commands = [ ] <EOL> for line in lines : <EOL> if not line : <EOL> continue <EOL> tokens = re . split ( r'<STR_LIT>' , line , <NUM_LIT:1> ) <EOL> phone_number , direction , text = [ x . strip ( ) for x in tokens ] <EOL> commands . append ( <EOL> { <EOL> '<STR_LIT>' : phone_number , <EOL> '<STR_LIT>' : direction , <EOL> '<STR_LIT:text>' : text <EOL> } <EOL> ) <EOL> return commands <EOL> def run_script ( self , script ) : <EOL> commands = self . parse_script ( script ) <EOL> for command in commands : <EOL> phone_number = command [ '<STR_LIT>' ] <EOL> v = VerifiedNumber . by_phone ( phone_number ) <EOL> if command [ '<STR_LIT>' ] == '<STR_LIT:>>' : <EOL> incoming ( phone_number , command [ '<STR_LIT:text>' ] , v . backend_id , domain_scope = v . domain ) <EOL> else : <EOL> msg = self . get_last_outbound_sms ( v . owner_doc_type , v . owner_id ) <EOL> self . assertEqual ( msg . text , unicode ( command [ '<STR_LIT:text>' ] ) ) <EOL> self . assertEqual ( strip_plus ( msg . phone_number ) , strip_plus ( phone_number ) ) <EOL> msg . delete ( ) <EOL> class TestParser ( TestScript ) : <EOL> def test_parse_script ( self ) : <EOL> script = """<STR_LIT>""" <EOL> commands = self . parse_script ( script ) <EOL> self . assertEqual ( len ( commands ) , <NUM_LIT:3> ) <EOL> self . assertDictEqual ( commands [ <NUM_LIT:0> ] , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:>>' , <EOL> '<STR_LIT:text>' : '<STR_LIT:test>' <EOL> } ) <EOL> self . assertDictEqual ( commands [ <NUM_LIT:1> ] , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:<>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' <EOL> } ) <EOL> self . assertDictEqual ( commands [ <NUM_LIT:2> ] , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:>>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' <EOL> } ) </s>
