<s> from datetime import datetime <EOL> import random <EOL> import uuid <EOL> from couchdbkit import MultipleResultsFound <EOL> from django . test import TestCase <EOL> from corehq . apps . sms . handlers . form_session import get_single_open_session_or_close_multiple <EOL> from corehq . apps . smsforms . models import SQLXFormsSession , XFORMS_SESSION_TYPES , XFORMS_SESSION_SMS , XFORMS_SESSION_IVR <EOL> class SQLSessionTestCase ( TestCase ) : <EOL> def test_get_by_session_id ( self ) : <EOL> session_id = uuid . uuid4 ( ) . hex <EOL> sql_session = SQLXFormsSession . objects . create ( <EOL> session_id = session_id , <EOL> start_time = datetime . utcnow ( ) , <EOL> modified_time = datetime . utcnow ( ) , <EOL> ) <EOL> self . assertEqual ( sql_session . pk , SQLXFormsSession . by_session_id ( session_id ) . pk ) <EOL> def test_get_by_session_id_not_found ( self ) : <EOL> self . assertEqual ( None , SQLXFormsSession . by_session_id ( uuid . uuid4 ( ) . hex ) ) <EOL> def test_get_all_open_sessions_domain_mismatch ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> _make_session ( <EOL> domain = '<STR_LIT>' , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_all_open_sessions_contact_mismatch ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = '<STR_LIT>' , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_all_open_sessions_already_ended ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = datetime . utcnow ( ) , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_all_open_sessions_wrong_type ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_IVR , <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_and_close_all_open_sessions ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> sql_sessions = SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( sql_sessions ) ) <EOL> SQLXFormsSession . close_all_open_sms_sessions ( domain , contact ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_single_open_session ( self ) : <EOL> properties = _arbitrary_session_properties ( <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> session = SQLXFormsSession ( ** properties ) <EOL> session . save ( ) <EOL> ( mult , session ) = get_single_open_session_or_close_multiple ( <EOL> session . domain , session . connection_id <EOL> ) <EOL> self . assertEqual ( False , mult ) <EOL> [ session_back ] = SQLXFormsSession . get_all_open_sms_sessions ( <EOL> session . domain , session . connection_id <EOL> ) <EOL> self . assertEqual ( session . _id , session_back . couch_id ) <EOL> def test_get_single_open_session_close_multiple ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> ( mult , session ) = get_single_open_session_or_close_multiple ( domain , contact ) <EOL> self . assertEqual ( True , mult ) <EOL> self . assertEqual ( None , session ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( SQLXFormsSession . get_all_open_sms_sessions ( domain , contact ) ) ) <EOL> def test_get_open_sms_session_no_results ( self ) : <EOL> self . assertEqual ( None , SQLXFormsSession . get_open_sms_session ( uuid . uuid4 ( ) . hex , uuid . uuid4 ( ) . hex ) ) <EOL> def test_get_open_sms_session_multiple_results ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> with self . assertRaises ( MultipleResultsFound ) : <EOL> SQLXFormsSession . get_open_sms_session ( domain , contact ) <EOL> def test_get_open_sms_session_one_result ( self ) : <EOL> domain = uuid . uuid4 ( ) . hex <EOL> contact = uuid . uuid4 ( ) . hex <EOL> new_session = _make_session ( <EOL> domain = domain , <EOL> connection_id = contact , <EOL> end_time = None , <EOL> session_type = XFORMS_SESSION_SMS , <EOL> ) <EOL> session = SQLXFormsSession . get_open_sms_session ( domain , contact ) <EOL> self . assertEqual ( new_session . session_id , session . session_id ) <EOL> def _make_session ( ** kwargs ) : <EOL> properties = _arbitrary_session_properties ( ** kwargs ) <EOL> session = SQLXFormsSession ( ** properties ) <EOL> session . save ( ) <EOL> return session <EOL> def _arbitrary_session_properties ( ** kwargs ) : <EOL> def arbitrary_string ( max_len = <NUM_LIT:32> ) : <EOL> return uuid . uuid4 ( ) . hex [ : max_len ] <EOL> def arbitrary_date ( ) : <EOL> return datetime ( <EOL> random . choice ( range ( <NUM_LIT> , <NUM_LIT> ) ) , <EOL> random . choice ( range ( <NUM_LIT:1> , <NUM_LIT> ) ) , <EOL> random . choice ( range ( <NUM_LIT:1> , <NUM_LIT> ) ) , <EOL> ) <EOL> def arbitrary_bool ( ) : <EOL> return random . choice ( [ True , False ] ) <EOL> properties = { <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_date ( ) , <EOL> '<STR_LIT>' : arbitrary_date ( ) , <EOL> '<STR_LIT>' : arbitrary_date ( ) , <EOL> '<STR_LIT>' : arbitrary_bool ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> '<STR_LIT>' : random . choice ( XFORMS_SESSION_TYPES ) , <EOL> '<STR_LIT>' : arbitrary_string ( <NUM_LIT:20> ) , <EOL> '<STR_LIT>' : arbitrary_string ( ) , <EOL> } <EOL> properties . update ( kwargs ) <EOL> return properties </s>
