def test_force_consumption(self): self.ct_settings.sync_consumption_fixtures = True self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}, ) set_default_monthly_consumption_for_domain(self.domain.name, 5) self._save_settings_and_clear_cache() balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(0, len(balance_blocks)) self.ct_settings.ota_restore_config.force_consumption_case_types = [ const.SUPPLY_POINT_CASE_TYPE ] self._save_settings_and_clear_cache() balance_blocks = _get_ota_balance_blocks(self.domain, self.user) # with no data, there should be no consumption block self.assertEqual(0, len(balance_blocks)) self.ct_settings.ota_restore_config.use_dynamic_product_list = True self._save_settings_and_clear_cache() balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(1, len(balance_blocks)) [balance_block] = balance_blocks element = etree.fromstring(balance_block) self.assertEqual(3, len([child for child in element]))
def testOTASettings(self): ct_settings = CommtrackConfig.for_domain(self.domain) ct_settings.consumption_config = ConsumptionConfig( min_transactions=10, min_window=20, optimal_window=60, ) ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}, ) set_default_monthly_consumption_for_domain(self.domain, 5 * DAYS_IN_MONTH) restore_settings = ct_settings.get_ota_restore_settings() self.assertEqual(1, len(restore_settings.section_to_consumption_types)) self.assertEqual('consumption', restore_settings.section_to_consumption_types['stock']) self.assertEqual(10, restore_settings.consumption_config.min_periods) self.assertEqual(20, restore_settings.consumption_config.min_window) self.assertEqual(60, restore_settings.consumption_config.max_window) self.assertEqual(150, restore_settings.consumption_config.default_monthly_consumption_function('foo', 'bar')) self.assertFalse(restore_settings.force_consumption_case_filter(CommCareCase(type='force-type'))) self.assertEqual(0, len(restore_settings.default_product_list)) ct_settings.ota_restore_config.force_consumption_case_types=['force-type'] ct_settings.ota_restore_config.use_dynamic_product_list=True restore_settings = ct_settings.get_ota_restore_settings() self.assertTrue(restore_settings.force_consumption_case_filter(CommCareCase(type='force-type'))) self.assertEqual(3, len(restore_settings.default_product_list))
def setUp(self): super(CommTrackSyncTest, self).setUp() # reused stuff self.restore_user = self.user.to_ota_restore_user() self.sp_block = CaseBlock(case_id=self.sp.case_id, ).as_xml() # bootstrap ota stuff self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}) set_default_monthly_consumption_for_domain(self.domain.name, 5) self.ota_settings = self.ct_settings.get_ota_restore_settings() # get initial restore token restore_config = RestoreConfig( project=self.domain, restore_user=self.restore_user, params=RestoreParams(version=V2), ) self.sync_log_id = synclog_id_from_restore_payload( restore_config.get_payload().as_string())
def test_ota_consumption(self): self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}) set_default_consumption_for_domain(self.domain.name, 5) amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] report = _report_soh(amounts, self.sp._id, 'stock') balance_blocks = _get_ota_balance_blocks(self.ct_settings, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=json_format_datetime(report.date), ), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, 'consumption', [(p._id, 5) for p in self.products], datestring=json_format_datetime(report.date), ), consumption_block, )
def test_force_consumption(self): self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}, ) set_default_consumption_for_domain(self.domain.name, 5) balance_blocks = _get_ota_balance_blocks(self.ct_settings, self.user) self.assertEqual(0, len(balance_blocks)) # self.ct_settings.ota_restore_config.use_dynamic_product_list = True self.ct_settings.ota_restore_config.force_consumption_case_types = [ const.SUPPLY_POINT_CASE_TYPE ] balance_blocks = _get_ota_balance_blocks(self.ct_settings, self.user) self.assertEqual(1, len(balance_blocks)) [balance_block] = balance_blocks element = etree.fromstring(balance_block) self.assertEqual(0, len([child for child in element])) self.ct_settings.ota_restore_config.use_dynamic_product_list = True balance_blocks = _get_ota_balance_blocks(self.ct_settings, self.user) self.assertEqual(1, len(balance_blocks)) [balance_block] = balance_blocks element = etree.fromstring(balance_block) self.assertEqual(3, len([child for child in element]))
def set_ota_restore_config(self): """ If the checkbox for syncing consumption fixtures is checked, then we build the restore config with appropriate special properties, otherwise just clear the object. If there becomes a way to tweak these on the UI, this should be done differently. """ from corehq.apps.commtrack.models import StockRestoreConfig if self.commtrack_settings.sync_consumption_fixtures: self.domain_object.commtrack_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={ 'stock': 'consumption' }, force_consumption_case_types=[ 'supply-point' ], use_dynamic_product_list=True, ) else: self.domain_object.commtrack_settings.ota_restore_config = StockRestoreConfig()
def _create_unsynced_couch(self): """ Create a CommtrackConfig matching the one created by _create_unsynced_sql """ couch = CommtrackConfig( domain='my_project', use_auto_emergency_levels=False, sync_consumption_fixtures=False, use_auto_consumption=False, individual_consumption_defaults=True, ota_restore_config=StockRestoreConfig( section_to_consumption_types={'s1': 'c1'}, force_consumption_case_types=['type1'], use_dynamic_product_list=True, ), alert_config=AlertConfig( stock_out_facilities=True, stock_out_commodities=True, stock_out_rates=True, non_report=True, ), actions=[ CommtrackActionConfig( action='receipts', subaction='sub-receipts', _keyword='one', caption='first action', ), CommtrackActionConfig( action='receipts', subaction='sub-receipts', _keyword='two', caption='second action', ), ], consumption_config=ConsumptionConfig( min_transactions=1, min_window=2, optimal_window=3, use_supply_point_type_default_consumption=True, exclude_invalid_periods=False, ), stock_levels_config=StockLevelsConfig( emergency_level=0.5, understock_threshold=1.5, overstock_threshold=3, ) ) couch.save(sync_to_sql=False) return couch
def test_ota_consumption(self): self.ct_settings.sync_consumption_fixtures = True self.ct_settings.consumptionconfig = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.stockrestoreconfig = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}) set_default_monthly_consumption_for_domain(self.domain.name, 5 * DAYS_IN_MONTH) self._save_settings_and_clear_cache() amounts = [ SohReport(section_id='stock', product_id=p._id, amount=i * 10) for i, p in enumerate(self.products) ] form_id, report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) self.addCleanup(self.delete_ledger_transactions, form_id) balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=report_date, ), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, 'consumption', [ SohReport(section_id='', product_id=p._id, amount=150) for p in self.products ], datestring=report_date, ), consumption_block, )
def _create_commtrack_config_if_needed(domain): if CommtrackConfig.for_domain(domain): return config = CommtrackConfig(domain=domain) config.save() # must be saved before submodels can be saved AlertConfig(commtrack_config=config).save() ConsumptionConfig(commtrack_config=config).save() StockLevelsConfig(commtrack_config=config).save() StockRestoreConfig(commtrack_config=config).save() config.set_actions([ ActionConfig( action='receipts', keyword='r', caption='Received', ), ActionConfig( action='consumption', keyword='c', caption='Consumed', ), ActionConfig( action='consumption', subaction='loss', keyword='l', caption='Losses', ), ActionConfig( action='stockonhand', keyword='soh', caption='Stock on hand', ), ActionConfig( action='stockout', keyword='so', caption='Stock-out', ), ]) config.save() # save actions, and sync couch