def setUp(self): super(CommTrackSyncTest, self).setUp() # reused stuff self.casexml_user = self.user.to_casexml_user() self.sp_block = CaseBlock( case_id=self.sp._id, version=V2, ).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_consumption_for_domain(self.domain.name, 5) self.ota_settings = self.ct_settings.get_ota_restore_settings() # get initial restore token restore_config = RestoreConfig( self.casexml_user, version=V2, stock_settings=self.ota_settings, ) self.sync_log_id = synclog_id_from_restore_payload(restore_config.get_payload())
def testOTASettings(self): domain = bootstrap_domain() ct_settings = CommtrackConfig.for_domain(domain.name) 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_consumption_for_domain(domain.name, 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_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 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 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 setUp(self): super(CommTrackSyncTest, self).setUp() # reused stuff self.casexml_user = self.user.to_casexml_user() self.sp_block = CaseBlock( case_id=self.sp._id, version=V2, ).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_consumption_for_domain(self.domain.name, 5) self.ota_settings = self.ct_settings.get_ota_restore_settings() # get initial restore token restore_config = RestoreConfig( self.casexml_user, version=V2, stock_settings=self.ota_settings, ) self.sync_log_id = synclog_id_from_restore_payload( restore_config.get_payload())
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 testSetForDomain(self): self.assertEqual(None, DefaultConsumption.get_domain_default(domain)) default = set_default_consumption_for_domain(domain, 50) self.assertEqual(50, DefaultConsumption.get_domain_default(domain).default_consumption) self.assertEqual(1, _count_consumptions()) updated = set_default_consumption_for_domain(domain, 40) self.assertEqual(default._id, updated._id) self.assertEqual(40, DefaultConsumption.get_domain_default(domain).default_consumption) self.assertEqual(1, _count_consumptions())
def test_defaults_set_after_report(self): self.report(25, 0) set_default_consumption_for_domain(self.domain.name, 50) state = StockState.objects.get( section_id='stock', case_id=self.sp._id, product_id=self.products[0]._id, ) self.assertEqual(50 / DAYS_IN_MONTH, state.get_consumption())
def test_pre_set_defaults(self): set_default_consumption_for_domain(self.domain.name, 50) self.report(25, 0) state = StockState.objects.get( section_id='stock', case_id=self.sp._id, product_id=self.products[0]._id, ) self.assertEqual(50, float(state.get_consumption()))
def test_defaults_set_after_report(self): self.report(25, 0) set_default_consumption_for_domain(self.domain.name, 50) state = StockState.objects.get( section_id='stock', case_id=self.sp._id, product_id=self.products[0]._id, ) self.assertEqual(50, state.get_consumption())
def testOTASettings(self): domain = bootstrap_domain() ct_settings = CommtrackConfig.for_domain(domain.name) 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_consumption_for_domain(domain.name, 5) 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( 5, restore_settings.consumption_config.default_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 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, )