def test_all_products_stocked(self): """No notification if all products are stocked.""" assign_products_to_location(self.facility, [self.product]) create_stock_report(self.facility, {'tp': 10}) generated = list(OnGoingStockouts(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 0)
def test_product_type_filter(self): """User can recieve missing notifications for only certain product type.""" bootstrap_web_user(username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.district, password='******', email='*****@*****.**', user_data={}, program_id=self.program.get_id) other_product = Product(domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each') other_product2 = Product(domain=self.TEST_DOMAIN, name='Test Product3', code_='tp3', unit='each') other_product.save() other_product2.save() assign_products_to_location( self.facility, [self.product, other_product, other_product2]) generated = list( OnGoingNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_reminders(self): products_reported, products_not_reported = report_status(self.facility.sql_location, days_until_late=1) self.assertEqual(len(products_reported), 0) self.assertEqual(len(products_not_reported), 0) assign_products_to_location(self.facility, [self.commodity]) products_reported, products_not_reported = report_status(self.facility.sql_location, days_until_late=1) self.assertEqual(len(products_reported), 0) sql_commodity = SQLProduct.objects.get(product_id=self.commodity.get_id) self.assertEqual(products_not_reported[0], sql_commodity) sql_commodity2 = SQLProduct.objects.get(product_id=self.commodity2.get_id) create_stock_report(self.facility, {'ab': 10}) products_reported, products_not_reported = report_status(self.facility.sql_location, days_until_late=1) self.assertEqual(products_reported[0], sql_commodity) self.assertEqual(len(products_not_reported), 0) assign_products_to_location(self.facility, [self.commodity, self.commodity2]) products_reported, products_not_reported = report_status(self.facility.sql_location, days_until_late=1) self.assertEqual(products_reported[0], sql_commodity) self.assertEqual(products_not_reported[0], sql_commodity2) create_stock_report(self.facility, {'cd': 10}) products_reported, products_not_reported = report_status(self.facility.sql_location, days_until_late=1) self.assertTrue(sql_commodity in products_reported) self.assertTrue(sql_commodity2 in products_reported) self.assertEqual(len(products_not_reported), 0)
def test_incomplete_with_filter(self): """ User with product type filter will get notification all products of that type are missing reports. """ self.user.get_domain_membership( self.TEST_DOMAIN).program_id = self.program.get_id self.user.save() self.product.program_id = self.program.get_id self.product.save() create_stock_report(self.facility, {'tp': 100}, date=datetime.utcnow() - timedelta(days=365)) other_product = Product(domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each', program_id=self.program.get_id) other_product.save() assign_products_to_location(self.facility, [self.product, other_product]) generated = list( OnGoingNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_simple_stockout(self): """Single product, single report with 0 quantity.""" assign_products_to_location(self.facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) generated = list(OnGoingStockouts(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_partial_duration_stockout(self): """Some reports indicate a stockout but did not last the entire period. No notification.""" assign_products_to_location(self.facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) create_stock_report(self.facility, {'tp': 1}) generated = list(OnGoingStockouts(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 0)
def test_incomplete_report2(self): create_stock_report(self.facility, {'tp': 100}, date=datetime.utcnow()) self.product.program_id = self.program2.get_id self.product.save() other_product = Product( domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each', program_id=self.program.get_id ) other_product.save() assign_products_to_location(self.facility, [self.product, other_product]) generated = list(OnGoingNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 0)
def test_partial_product_stockout(self): """Multiple products but only one is stocked out. Should be reported.""" other_product = Product(domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each') other_product.save() assign_products_to_location(self.facility, [self.product, other_product]) create_stock_report(self.facility, {'tp': 0}) create_stock_report(self.facility, {'tp2': 10}) generated = list(OnGoingStockouts(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_partial_stock_coverage(self): """ Handle the case when not all facilities are expected to have stocked a given product. i.e. if only one facility is expected to have a certain product and it is stocked out then that is an urgent stockout. """ assign_products_to_location(self.facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) generated = list(UrgentStockoutAlert(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_multiple_users(self): """Each user will get their own notification.""" assign_products_to_location(self.facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) other_user = bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.district, password='******', email='*****@*****.**', user_data={} ) generated = list(OnGoingStockouts(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 2) self.assertEqual({generated[0].user.get_id, generated[1].user.get_id}, {self.user.get_id, other_user.get_id})
def test_product_type_filter(self): """User can recieve missing notifications for only certain product type.""" bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.district, password='******', email='*****@*****.**', user_data={}, program_id=self.program.get_id ) other_product = Product(domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each') other_product2 = Product(domain=self.TEST_DOMAIN, name='Test Product3', code_='tp3', unit='each') other_product.save() other_product2.save() assign_products_to_location(self.facility, [self.product, other_product, other_product2]) generated = list(OnGoingNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_product_type_filter(self): """ Notifications will not be sent if the stockout is a product type does not interest the user. """ self.user.get_domain_membership(self.TEST_DOMAIN).program_id = self.program.get_id self.user.save() program = Program(domain=self.TEST_DOMAIN, name='Test Program 2') program.save() bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.district, password='******', email='*****@*****.**', user_data={}, program_id=program.get_id ) assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) create_stock_report(self.other_facility, {'tp': 0}) create_stock_report(self.last_facility, {'tp': 0}) generated = list(UrgentStockoutAlert(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_product_type_filter(self): """ Notifications will not be sent if the stockout is a product type does not interest the user. """ self.user.get_domain_membership(self.TEST_DOMAIN).program_id = self.program.get_id self.user.save() program = Program(domain=self.TEST_DOMAIN, name='Test Program 2') program.save() other_user = bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.region, password='******', email='*****@*****.**', user_data={} ) assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) generated = list(UrgentNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 2) self.assertEqual({generated[0].user.get_id, generated[1].user.get_id}, {self.user.get_id, other_user.get_id})
def test_incomplete_with_filter(self): """ User with product type filter will get notification all products of that type are missing reports. """ self.user.get_domain_membership(self.TEST_DOMAIN).program_id = self.program.get_id self.user.save() self.product.program_id = self.program.get_id self.product.save() create_stock_report(self.facility, {'tp': 100}, date=datetime.utcnow() - timedelta(days=365)) other_product = Product( domain=self.TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each', program_id=self.program.get_id ) other_product.save() assign_products_to_location(self.facility, [self.product, other_product]) generated = list(OnGoingNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def setUpClass(cls): super(EWSTestCase, cls).setUpClass() cls.backend, cls.sms_backend_mapping = setup_default_sms_test_backend() cls.domain = prepare_domain(TEST_DOMAIN) cls.national = make_loc(code='national', name='National', type='country', domain=TEST_DOMAIN) cls.region = make_loc(code="region", name="Test Region", type="region", domain=TEST_DOMAIN, parent=cls.national) cls.rms = make_loc(code="rms", name="Test Medical Store", type="Regional Medical Store", domain=TEST_DOMAIN, parent=cls.region) cls.rms2 = make_loc(code="rms2", name="Test Medical Store 2", type="Regional Medical Store", domain=TEST_DOMAIN, parent=cls.region) cls.district = make_loc(code="district", name="Test District", type="district", domain=TEST_DOMAIN) cls.loc1 = make_loc(code="tf", name="Test Facility", type="Hospital", domain=TEST_DOMAIN, parent=cls.district) cls.loc2 = make_loc(code="tf2", name="Test Facility 2", type="Hospital", domain=TEST_DOMAIN, parent=cls.district) cls.user1 = bootstrap_web_user( username='******', phone_number='1111', location=cls.district, domain=TEST_DOMAIN, first_name='test', last_name='test1', user_data={ 'role': [] }, email='*****@*****.**', password='******' ) set_sms_notifications(TEST_DOMAIN, cls.user1, True) cls.national_user = bootstrap_web_user( username='******', phone_number='2222', location=cls.national, domain=TEST_DOMAIN, first_name='test', last_name='test2', user_data={ 'role': [] }, email='*****@*****.**', password='******' ) set_sms_notifications(TEST_DOMAIN, cls.national_user, True) cls.regional_user = bootstrap_web_user( username='******', phone_number='4444', location=cls.region, domain=TEST_DOMAIN, first_name='test', last_name='test4', user_data={ 'role': [] }, email='*****@*****.**', password='******' ) set_sms_notifications(TEST_DOMAIN, cls.regional_user, True) cls.product = Product(domain=TEST_DOMAIN, name='Test Product', code_='tp', unit='each') cls.product.save() cls.product2 = Product(domain=TEST_DOMAIN, name='Test Product2', code_='tp2', unit='each') cls.product2.save() assign_products_to_location(cls.loc1, [cls.product]) assign_products_to_location(cls.loc2, [cls.product, cls.product2]) assign_products_to_location(cls.rms, [cls.product, cls.product2])
def test_all_facility_not_report(self): """Send a notification because all facilities don't send report.""" assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) generated = list(UrgentNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_minority_facility_stockout(self): """No notification because < 50% of the facilities don't send report.""" assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) create_stock_report(self.other_facility, {'tp': 10}) create_stock_report(self.last_facility, {'tp': 10}) generated = list(UrgentStockoutAlert(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 0)
def test_majority_facility_stockout(self): """Send a notification because > 50% of the facilities are stocked out of a product.""" assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) create_stock_report(self.facility, {'tp': 0}) create_stock_report(self.other_facility, {'tp': 0}) create_stock_report(self.last_facility, {'tp': 10}) generated = list(UrgentStockoutAlert(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 1) self.assertEqual(generated[0].user.get_id, self.user.get_id)
def test_multiple_users(self): """Each user will get their own urgent stockout notification.""" other_user = bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.region, password='******', email='*****@*****.**', user_data={} ) assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) generated = list(UrgentNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 2) self.assertEqual({generated[0].user.get_id, generated[1].user.get_id}, {self.user.get_id, other_user.get_id})
def test_country_user(self): """Country as well as region users should get notifications.""" other_user = bootstrap_web_user( username='******', domain=self.TEST_DOMAIN, phone_number='+44445', location=self.country, password='******', email='*****@*****.**', user_data={} ) assign_products_to_location(self.facility, [self.product]) assign_products_to_location(self.other_facility, [self.product]) assign_products_to_location(self.last_facility, [self.product]) generated = list(UrgentNonReporting(self.TEST_DOMAIN).get_notifications()) self.assertEqual(len(generated), 2) self.assertEqual( {generated[0].user.get_id, generated[1].user.get_id}, {self.user.get_id, other_user.get_id} )
def setUpClass(cls): cls.domain = prepare_domain(TEST_DOMAIN) cls.sms_backend_mapping, cls.backend = create_backend() cls.national = make_loc(code="national", name="National", type="country", domain=TEST_DOMAIN) cls.region = make_loc(code="region", name="Test Region", type="region", domain=TEST_DOMAIN, parent=cls.national) cls.rms = make_loc( code="rms", name="Test Medical Store", type="Regional Medical Store", domain=TEST_DOMAIN, parent=cls.region ) cls.rms2 = make_loc( code="rms2", name="Test Medical Store 2", type="Regional Medical Store", domain=TEST_DOMAIN, parent=cls.region, ) cls.district = make_loc(code="district", name="Test District", type="district", domain=TEST_DOMAIN) cls.loc1 = make_loc(code="tf", name="Test Facility", type="Hospital", domain=TEST_DOMAIN, parent=cls.district) cls.loc2 = make_loc( code="tf2", name="Test Facility 2", type="Hospital", domain=TEST_DOMAIN, parent=cls.district ) cls.user1 = bootstrap_web_user( username="******", phone_number="1111", location=cls.district, domain=TEST_DOMAIN, first_name="test", last_name="test1", user_data={"role": []}, email="*****@*****.**", password="******", ) set_sms_notifications(TEST_DOMAIN, cls.user1, True) cls.national_user = bootstrap_web_user( username="******", phone_number="2222", location=cls.national, domain=TEST_DOMAIN, first_name="test", last_name="test2", user_data={"role": []}, email="*****@*****.**", password="******", ) set_sms_notifications(TEST_DOMAIN, cls.national_user, True) cls.regional_user = bootstrap_web_user( username="******", phone_number="4444", location=cls.region, domain=TEST_DOMAIN, first_name="test", last_name="test4", user_data={"role": []}, email="*****@*****.**", password="******", ) set_sms_notifications(TEST_DOMAIN, cls.regional_user, True) cls.product = Product(domain=TEST_DOMAIN, name="Test Product", code_="tp", unit="each") cls.product.save() cls.product2 = Product(domain=TEST_DOMAIN, name="Test Product2", code_="tp2", unit="each") cls.product2.save() assign_products_to_location(cls.loc1, [cls.product]) assign_products_to_location(cls.loc2, [cls.product, cls.product2]) assign_products_to_location(cls.rms, [cls.product, cls.product2])