Ejemplo n.º 1
0
    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_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(
            self.casexml_user,
            version=V2,
            stock_settings=self.ota_settings,
        )
        self.sync_log_id = synclog_id_from_restore_payload(restore_config.get_payload())
Ejemplo n.º 2
0
    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_monthly_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_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))
Ejemplo n.º 3
0
    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_monthly_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)
        # 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
        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]))
Ejemplo n.º 4
0
    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_monthly_consumption_for_domain(self.domain.name, 5 * DAYS_IN_MONTH)
        self._save_settings_and_clear_cache()

        amounts = [(p._id, i * 10) for i, p in enumerate(self.products)]
        report = _report_soh(amounts, self.sp.case_id, "stock")
        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=json_format_datetime(report.date)),
            stock_block,
        )
        check_xml_line_by_line(
            self,
            balance_ota_block(
                self.sp,
                "consumption",
                [(p._id, 150) for p in self.products],
                datestring=json_format_datetime(report.date),
            ),
            consumption_block,
        )
Ejemplo n.º 5
0
    def test_defaults_set_after_report(self):
        self.report(25, 0)
        set_default_monthly_consumption_for_domain(self.domain.name, 5 * 30)

        state = StockState.objects.get(section_id="stock", case_id=self.sp._id, product_id=self.products[0]._id)

        self.assertEqual(5, float(state.get_daily_consumption()))
Ejemplo n.º 6
0
 def testSetForDomain(self):
     self.assertEqual(None, DefaultConsumption.get_domain_default(domain))
     default = set_default_monthly_consumption_for_domain(domain, 50)
     self.assertEqual(50, DefaultConsumption.get_domain_default(domain).default_consumption)
     self.assertEqual(1, _count_consumptions())
     updated = set_default_monthly_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())
Ejemplo n.º 7
0
    def test_pre_set_defaults(self):
        set_default_monthly_consumption_for_domain(self.domain.name, 5 * 30)
        with process_kafka_changes('LedgerToElasticsearchPillow', topics.LEDGER):
            self.report(25, 0)
        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp.case_id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(5, float(state.get_daily_consumption()))
Ejemplo n.º 8
0
    def test_ota_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 * 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)
        ]
        report_date = _report_soh(amounts, self.sp.case_id, self.domain.name)
        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,
        )