<s> from decimal import Decimal <EOL> from casexml . apps . stock . models import StockReport , StockTransaction <EOL> from corehq . apps . commtrack . models import StockState <EOL> from corehq . apps . commtrack . tests . util import CommTrackTest , FIXED_USER , ROAMING_USER <EOL> from corehq . apps . commtrack . sms import handle <EOL> from corehq . toggles import STOCK_AND_RECEIPT_SMS_HANDLER , NAMESPACE_DOMAIN <EOL> from couchforms . dbaccessors import get_commtrack_forms <EOL> class SMSTests ( CommTrackTest ) : <EOL> def setUp ( self ) : <EOL> super ( SMSTests , self ) . setUp ( ) <EOL> def check_stock ( self , code , amount , case_id = None , section_id = '<STR_LIT>' ) : <EOL> if not case_id : <EOL> case_id = self . sp . case_id <EOL> [ product ] = filter ( lambda p : p . code_ == code , self . products ) <EOL> try : <EOL> state = StockState . objects . get ( <EOL> product_id = product . _id , <EOL> case_id = case_id , <EOL> section_id = section_id <EOL> ) <EOL> self . assertEqual ( amount , state . stock_on_hand ) <EOL> except StockState . DoesNotExist : <EOL> if amount != <NUM_LIT:0> : <EOL> raise Exception ( <EOL> '<STR_LIT>' % section_id <EOL> ) <EOL> class StockReportTest ( SMSTests ) : <EOL> user_definitions = [ ROAMING_USER , FIXED_USER ] <EOL> def testStockReportRoaming ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , len ( get_commtrack_forms ( self . domain . name ) ) ) <EOL> amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> } <EOL> handled = handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in amounts . items ( ) ) <EOL> ) ) <EOL> self . assertTrue ( handled ) <EOL> forms = list ( get_commtrack_forms ( self . domain . name ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( forms ) ) <EOL> self . assertEqual ( _get_location_from_sp ( self . sp ) , _get_location_from_form ( forms [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , StockReport . objects . count ( ) ) <EOL> report = StockReport . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( forms [ <NUM_LIT:0> ] . _id , report . form_id ) <EOL> self . assertEqual ( '<STR_LIT>' , report . type ) <EOL> self . assertEqual ( <NUM_LIT:3> , report . stocktransaction_set . count ( ) ) <EOL> for code , amt in amounts . items ( ) : <EOL> [ product ] = filter ( lambda p : p . code_ == code , self . products ) <EOL> trans = StockTransaction . objects . get ( product_id = product . _id ) <EOL> self . assertEqual ( self . sp . case_id , trans . case_id ) <EOL> self . assertEqual ( <NUM_LIT:0> , trans . quantity ) <EOL> self . assertEqual ( amt , trans . stock_on_hand ) <EOL> def testStockReportFixed ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , len ( get_commtrack_forms ( self . domain . name ) ) ) <EOL> amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> } <EOL> handled = handle ( self . users [ <NUM_LIT:1> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in amounts . items ( ) ) <EOL> ) ) <EOL> self . assertTrue ( handled ) <EOL> forms = list ( get_commtrack_forms ( self . domain . name ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( forms ) ) <EOL> self . assertEqual ( _get_location_from_sp ( self . sp ) , _get_location_from_form ( forms [ <NUM_LIT:0> ] ) ) <EOL> for code , amt in amounts . items ( ) : <EOL> [ product ] = filter ( lambda p : p . code_ == code , self . products ) <EOL> trans = StockTransaction . objects . get ( product_id = product . _id ) <EOL> self . assertEqual ( self . sp . case_id , trans . case_id ) <EOL> self . assertEqual ( <NUM_LIT:0> , trans . quantity ) <EOL> self . assertEqual ( amt , trans . stock_on_hand ) <EOL> def check_form_type ( self , expected_type , subtype = None ) : <EOL> [ report ] = StockReport . objects . filter ( type = '<STR_LIT>' ) <EOL> transaction = report . stocktransaction_set . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> transaction . type , <EOL> expected_type <EOL> ) <EOL> if subtype : <EOL> self . assertEqual ( <EOL> transaction . subtype , <EOL> subtype <EOL> ) <EOL> def testStockReceipt ( self ) : <EOL> original_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> } <EOL> handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in original_amounts . items ( ) ) <EOL> ) ) <EOL> received_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> handled = handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in received_amounts . items ( ) ) <EOL> ) ) <EOL> self . assertTrue ( handled ) <EOL> self . check_form_type ( '<STR_LIT>' ) <EOL> for code in original_amounts . keys ( ) : <EOL> expected_amount = original_amounts [ code ] + received_amounts [ code ] <EOL> self . check_stock ( code , expected_amount ) <EOL> def testStockLosses ( self ) : <EOL> original_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> } <EOL> handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in original_amounts . items ( ) ) <EOL> ) ) <EOL> lost_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> handled = handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in lost_amounts . items ( ) ) <EOL> ) ) <EOL> self . assertTrue ( handled ) <EOL> self . check_form_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for code in original_amounts . keys ( ) : <EOL> expected_amount = original_amounts [ code ] - lost_amounts [ code ] <EOL> self . check_stock ( code , expected_amount ) <EOL> def testStockConsumption ( self ) : <EOL> original_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:20> , <EOL> '<STR_LIT>' : <NUM_LIT:30> , <EOL> } <EOL> handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in original_amounts . items ( ) ) <EOL> ) ) <EOL> lost_amounts = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> handled = handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' . format ( <EOL> loc = '<STR_LIT>' , <EOL> report = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % ( k , v ) for k , v in lost_amounts . items ( ) ) <EOL> ) ) <EOL> self . assertTrue ( handled ) <EOL> self . check_form_type ( '<STR_LIT>' ) <EOL> for code in original_amounts . keys ( ) : <EOL> expected_amount = original_amounts [ code ] - lost_amounts [ code ] <EOL> self . check_stock ( code , expected_amount ) <EOL> class StockAndReceiptTest ( SMSTests ) : <EOL> user_definitions = [ FIXED_USER ] <EOL> def setUp ( self ) : <EOL> super ( StockAndReceiptTest , self ) . setUp ( ) <EOL> STOCK_AND_RECEIPT_SMS_HANDLER . set ( self . domain , True , NAMESPACE_DOMAIN ) <EOL> def test_soh_and_receipt ( self ) : <EOL> handled = handle ( self . users [ <NUM_LIT:0> ] . get_verified_number ( ) , '<STR_LIT>' ) <EOL> self . assertTrue ( handled ) <EOL> self . check_stock ( '<STR_LIT>' , Decimal ( <NUM_LIT:20> ) ) <EOL> def tearDown ( self ) : <EOL> super ( StockAndReceiptTest , self ) . tearDown ( ) <EOL> STOCK_AND_RECEIPT_SMS_HANDLER . set ( self . domain , False , NAMESPACE_DOMAIN ) <EOL> def _get_location_from_form ( form ) : <EOL> return form . form [ '<STR_LIT:location>' ] <EOL> def _get_location_from_sp ( sp ) : <EOL> return sp . location_id </s>
