def test_rebuild_for_domain(self):
        # 5 days, 1 transaction
        self.report(25, 5)
        self.report(10, 0)
        expected_result = Decimal(3)

        commtrack_settings = self.domain_obj.commtrack_settings

        def _update_consumption_config(min_transactions, min_window,
                                       optimal_window):
            if not hasattr(commtrack_settings, 'sqlconsumptionconfig'):
                commtrack_settings.sqlconsumptionconfig = SQLConsumptionConfig(
                )
                commtrack_settings.sqlconsumptionconfig.commtrack_settings = commtrack_settings
            commtrack_settings.sqlconsumptionconfig.min_transactions = min_transactions
            commtrack_settings.sqlconsumptionconfig.min_window = min_window
            commtrack_settings.sqlconsumptionconfig.optimal_window = optimal_window
            commtrack_settings.sqlconsumptionconfig.save()

        _reset = functools.partial(_update_consumption_config, 0, 3,
                                   100)  # should fall in range

        tests = (
            ((0, 3, 100), expected_result),  # normal result
            ((2, 3, 100), None),  # not enough transactions
            ((2, 3, 100), None),  # not enough transactions
            ((0, 6, 100), None),  # too much time
            ((2, 3, 4), None),  # not enough time
        )
        for consumption_params, test_result in tests:
            _reset()
            recalculate_domain_consumption(self.domain)
            state = StockState.objects.get(section_id='stock',
                                           case_id=self.sp.case_id,
                                           product_id=self.products[0]._id)
            self.assertEqual(
                expected_result, state.daily_consumption,
                'reset state failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption))

            # just changing the config shouldn't change the state
            _update_consumption_config(*consumption_params)
            state = StockState.objects.get(section_id='stock',
                                           case_id=self.sp.case_id,
                                           product_id=self.products[0]._id)
            self.assertEqual(
                expected_result, state.daily_consumption,
                'update config failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption))

            # recalculating should though
            recalculate_domain_consumption(self.domain)
            state = StockState.objects.get(section_id='stock',
                                           case_id=self.sp.case_id,
                                           product_id=self.products[0]._id)
            self.assertEqual(
                test_result, state.daily_consumption,
                'test failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption))
    def test_rebuild_for_domain(self):
        # 5 days, 1 transaction
        self.report(25, 5)
        self.report(10, 0)
        expected_result = Decimal(3)

        commtrack_settings = self.domain.commtrack_settings

        def _update_consumption_config(min_transactions, min_window, optimal_window):
            commtrack_settings.consumption_config.min_transactions = min_transactions
            commtrack_settings.consumption_config.min_window = min_window
            commtrack_settings.consumption_config.optimal_window = optimal_window
            commtrack_settings.save()

        _reset = functools.partial(_update_consumption_config, 0, 3, 100)  # should fall in range

        tests = (
            ((0, 3, 100), expected_result),  # normal result
            ((2, 3, 100), None),   # not enough transactions
            ((2, 3, 100), None),   # not enough transactions
            ((0, 6, 100), None),   # too much time
            ((2, 3, 4), None),   # not enough time
        )
        for consumption_params, test_result in tests:
            _reset()
            recalculate_domain_consumption(self.domain.name)
            state = StockState.objects.get(section_id='stock', case_id=self.sp.case_id, product_id=self.products[0]._id)
            self.assertEqual(
                expected_result,
                state.daily_consumption,
                'reset state failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption)
            )

            # just changing the config shouldn't change the state
            _update_consumption_config(*consumption_params)
            state = StockState.objects.get(section_id='stock', case_id=self.sp.case_id, product_id=self.products[0]._id)
            self.assertEqual(
                expected_result,
                state.daily_consumption,
                'update config failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption)
            )

            # recalculating should though
            recalculate_domain_consumption(self.domain.name)
            state = StockState.objects.get(section_id='stock', case_id=self.sp.case_id, product_id=self.products[0]._id)
            self.assertEqual(
                test_result,
                state.daily_consumption,
                'test failed for arguments: {}: {} != {}'.format(
                    consumption_params, test_result, state.daily_consumption)
            )
Beispiel #3
0
def recalculate_domain_consumption_task(domain):
    recalculate_domain_consumption(domain)
Beispiel #4
0
def recalculate_domain_consumption_task(domain):
    recalculate_domain_consumption(domain)