<s> from datetime import datetime , timedelta <EOL> from decimal import Decimal <EOL> from casexml . apps . stock . models import StockTransaction , StockReport <EOL> from corehq . apps . commtrack . models import StockState <EOL> from corehq . apps . locations . tests . util import make_loc <EOL> from corehq . apps . products . models import SQLProduct , Product <EOL> from corehq . apps . sms . models import SMS <EOL> from corehq . apps . sms . tests . util import setup_default_sms_test_backend , delete_domain_phone_numbers <EOL> from custom . ewsghana . models import FacilityInCharge , EWSExtension <EOL> from custom . ewsghana . reminders import STOCK_ON_HAND_REMINDER , SECOND_STOCK_ON_HAND_REMINDER , SECOND_INCOMPLETE_SOH_REMINDER , STOCKOUT_REPORT , THIRD_STOCK_ON_HAND_REMINDER , INCOMPLETE_SOH_TO_SUPER <EOL> from custom . ewsghana . reminders . second_soh_reminder import SecondSOHReminder <EOL> from custom . ewsghana . tasks import first_soh_reminder , second_soh_reminder , third_soh_to_super , stockout_notification_to_web_supers , reminder_to_visit_website , reminder_to_submit_rrirv <EOL> from custom . ewsghana . tests . handlers . utils import EWSTestCase <EOL> from custom . ewsghana . utils import prepare_domain , bootstrap_user , bootstrap_web_user , set_sms_notifications <EOL> TEST_DOMAIN = '<STR_LIT>' <EOL> def create_stock_report ( location , products_quantities , date = datetime . utcnow ( ) ) : <EOL> sql_location = location . sql_location <EOL> report = StockReport . objects . create ( <EOL> form_id = '<STR_LIT>' , <EOL> domain = sql_location . domain , <EOL> type = '<STR_LIT>' , <EOL> date = date <EOL> ) <EOL> for product_code , quantity in products_quantities . iteritems ( ) : <EOL> StockTransaction ( <EOL> stock_on_hand = Decimal ( quantity ) , <EOL> report = report , <EOL> type = '<STR_LIT>' , <EOL> section_id = '<STR_LIT>' , <EOL> case_id = sql_location . supply_point_id , <EOL> product_id = SQLProduct . objects . get ( domain = sql_location . domain , code = product_code ) . product_id <EOL> ) . save ( ) <EOL> class TestReminders ( EWSTestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . backend , cls . sms_backend_mapping = setup_default_sms_test_backend ( ) <EOL> cls . domain = prepare_domain ( TEST_DOMAIN ) <EOL> cls . loc1 = make_loc ( code = "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT>" , domain = TEST_DOMAIN ) <EOL> cls . loc2 = make_loc ( code = "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT>" , domain = TEST_DOMAIN ) <EOL> cls . region = make_loc ( code = "<STR_LIT>" , name = "<STR_LIT>" , type = "<STR_LIT>" , domain = TEST_DOMAIN ) <EOL> cls . user1 = bootstrap_user ( <EOL> username = '<STR_LIT>' , phone_number = '<STR_LIT>' , home_loc = cls . loc2 , domain = TEST_DOMAIN , <EOL> first_name = '<STR_LIT:test>' , last_name = '<STR_LIT>' , <EOL> user_data = { <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> ) <EOL> cls . user2 = bootstrap_user ( <EOL> username = '<STR_LIT>' , phone_number = '<STR_LIT>' , home_loc = cls . loc1 , domain = TEST_DOMAIN , <EOL> first_name = '<STR_LIT:test>' , last_name = '<STR_LIT>' , <EOL> user_data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : "<STR_LIT:False>" <EOL> } <EOL> ) <EOL> cls . user3 = bootstrap_user ( <EOL> username = '<STR_LIT>' , phone_number = '<STR_LIT>' , home_loc = cls . loc2 , domain = TEST_DOMAIN , <EOL> first_name = '<STR_LIT:test>' , last_name = '<STR_LIT>' , <EOL> user_data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : "<STR_LIT:True>" <EOL> } <EOL> ) <EOL> cls . in_charge = bootstrap_user ( <EOL> username = '<STR_LIT>' , phone_number = '<STR_LIT>' , home_loc = cls . loc2 , domain = TEST_DOMAIN , <EOL> first_name = '<STR_LIT:test>' , last_name = '<STR_LIT>' , <EOL> user_data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> cls . web_user = bootstrap_web_user ( <EOL> domain = TEST_DOMAIN , <EOL> username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> location = cls . loc2 , <EOL> phone_number = '<STR_LIT>' <EOL> ) <EOL> EWSExtension . objects . create ( <EOL> domain = TEST_DOMAIN , <EOL> user_id = cls . web_user . get_id , <EOL> sms_notifications = True , <EOL> location_id = cls . loc2 . get_id <EOL> ) <EOL> cls . web_user2 = bootstrap_web_user ( <EOL> domain = TEST_DOMAIN , <EOL> username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> location = cls . region , <EOL> phone_number = '<STR_LIT>' <EOL> ) <EOL> set_sms_notifications ( TEST_DOMAIN , cls . web_user2 , True ) <EOL> FacilityInCharge . objects . create ( <EOL> user_id = cls . in_charge . get_id , <EOL> location = cls . loc2 . sql_location <EOL> ) <EOL> cls . product = Product ( <EOL> domain = TEST_DOMAIN , <EOL> name = '<STR_LIT>' , <EOL> code_ = '<STR_LIT>' , <EOL> unit = '<STR_LIT>' <EOL> ) <EOL> cls . product . save ( ) <EOL> cls . product2 = Product ( <EOL> domain = TEST_DOMAIN , <EOL> name = '<STR_LIT>' , <EOL> code_ = '<STR_LIT>' , <EOL> unit = '<STR_LIT>' <EOL> ) <EOL> cls . product2 . save ( ) <EOL> sql_product = SQLProduct . objects . get ( product_id = cls . product . get_id ) <EOL> sql_product2 = SQLProduct . objects . get ( product_id = cls . product2 . get_id ) <EOL> sql_location1 = cls . loc1 . sql_location <EOL> sql_location2 = cls . loc2 . sql_location <EOL> sql_location1 . products = [ sql_product ] <EOL> sql_location2 . products = [ sql_product , sql_product2 ] <EOL> sql_location1 . save ( ) <EOL> sql_location2 . save ( ) <EOL> def tearDown ( self ) : <EOL> SMS . objects . all ( ) . delete ( ) <EOL> StockState . objects . all ( ) . delete ( ) <EOL> StockReport . objects . all ( ) . delete ( ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> delete_domain_phone_numbers ( TEST_DOMAIN ) <EOL> cls . user1 . delete ( ) <EOL> cls . user2 . delete ( ) <EOL> cls . user3 . delete ( ) <EOL> cls . domain . delete ( ) <EOL> FacilityInCharge . objects . all ( ) . delete ( ) <EOL> super ( TestReminders , cls ) . tearDownClass ( ) <EOL> def test_first_soh_reminder ( self ) : <EOL> first_soh_reminder ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> STOCK_ON_HAND_REMINDER % { '<STR_LIT:name>' : self . user3 . full_name } <EOL> ) <EOL> def test_second_soh_reminder ( self ) : <EOL> second_soh_reminder ( ) <EOL> smses = SMS . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> SECOND_STOCK_ON_HAND_REMINDER % { '<STR_LIT:name>' : self . user3 . full_name } <EOL> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:1> ] . text , <EOL> SECOND_STOCK_ON_HAND_REMINDER % { '<STR_LIT:name>' : self . user2 . full_name } <EOL> ) <EOL> create_stock_report ( self . loc1 , { <EOL> '<STR_LIT>' : <NUM_LIT:100> <EOL> } ) <EOL> now = datetime . utcnow ( ) <EOL> second_soh_reminder ( ) <EOL> smses = smses . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> SECOND_STOCK_ON_HAND_REMINDER % { '<STR_LIT:name>' : self . user3 . full_name } <EOL> ) <EOL> create_stock_report ( self . loc2 , { <EOL> '<STR_LIT>' : <NUM_LIT:100> <EOL> } ) <EOL> now = datetime . utcnow ( ) <EOL> second_soh_reminder ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> SECOND_INCOMPLETE_SOH_REMINDER % { '<STR_LIT:name>' : self . user3 . full_name , '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> create_stock_report ( self . loc2 , { <EOL> '<STR_LIT>' : <NUM_LIT:100> <EOL> } ) <EOL> now = datetime . utcnow ( ) <EOL> SecondSOHReminder ( TEST_DOMAIN ) . send ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) <EOL> def test_third_soh_reminder ( self ) : <EOL> third_soh_to_super ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( smses [ <NUM_LIT:0> ] . text , THIRD_STOCK_ON_HAND_REMINDER % { <EOL> '<STR_LIT:name>' : self . web_user2 . full_name , <EOL> '<STR_LIT>' : self . loc2 . name , <EOL> } ) <EOL> self . assertEqual ( smses [ <NUM_LIT:1> ] . text , THIRD_STOCK_ON_HAND_REMINDER % { <EOL> '<STR_LIT:name>' : self . in_charge . full_name , <EOL> '<STR_LIT>' : self . loc2 . name , <EOL> } ) <EOL> create_stock_report ( self . loc2 , { <EOL> '<STR_LIT>' : <NUM_LIT:100> <EOL> } ) <EOL> now = datetime . utcnow ( ) <EOL> third_soh_to_super ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> INCOMPLETE_SOH_TO_SUPER % { <EOL> '<STR_LIT:name>' : self . web_user2 . full_name , <EOL> '<STR_LIT>' : self . loc2 . name , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:1> ] . text , <EOL> INCOMPLETE_SOH_TO_SUPER % { <EOL> '<STR_LIT:name>' : self . in_charge . full_name , <EOL> '<STR_LIT>' : self . loc2 . name , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> create_stock_report ( self . loc2 , { <EOL> '<STR_LIT>' : <NUM_LIT:100> <EOL> } ) <EOL> now = datetime . utcnow ( ) <EOL> third_soh_to_super ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) <EOL> def test_stockout_reminder ( self ) : <EOL> stockout_notification_to_web_supers ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) <EOL> create_stock_report ( <EOL> self . loc2 , { <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> ) <EOL> stockout_notification_to_web_supers ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:1> ) <EOL> last_modified_date = StockState . objects . latest ( '<STR_LIT>' ) . last_modified_date . strftime ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> smses [ <NUM_LIT:0> ] . text , <EOL> STOCKOUT_REPORT % { <EOL> '<STR_LIT:name>' : self . web_user . full_name , <EOL> '<STR_LIT>' : self . loc2 . name , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : last_modified_date <EOL> } <EOL> ) <EOL> set_sms_notifications ( TEST_DOMAIN , self . web_user , False ) <EOL> self . web_user . save ( ) <EOL> now = datetime . utcnow ( ) <EOL> stockout_notification_to_web_supers ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) <EOL> def test_rrirv_reminder ( self ) : <EOL> reminder_to_submit_rrirv ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:2> ) <EOL> def test_visit_reminder ( self ) : <EOL> reminder_to_visit_website ( ) <EOL> smses = SMS . objects . all ( ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) <EOL> now = datetime . utcnow ( ) <EOL> self . web_user2 . last_login = now - timedelta ( weeks = <NUM_LIT> ) <EOL> self . web_user2 . save ( ) <EOL> reminder_to_visit_website ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:1> ) <EOL> set_sms_notifications ( TEST_DOMAIN , self . web_user2 , False ) <EOL> self . web_user2 . save ( ) <EOL> now = datetime . utcnow ( ) <EOL> reminder_to_visit_website ( ) <EOL> smses = SMS . objects . filter ( date__gte = now ) <EOL> self . assertEqual ( smses . count ( ) , <NUM_LIT:0> ) </s>
