class StockTestBase(TestCase): @classmethod def setUpClass(cls): super(StockTestBase, cls).setUpClass() cls.domain = create_domain("stock-report-test") @classmethod def tearDownClass(cls): cls.domain.delete() super(StockTestBase, cls).tearDownClass() def setUp(self): # create case case = CaseFactory(domain=self.domain.name).create_case() self.case_id = case.case_id self.product_id = uuid.uuid4().hex SQLProduct(product_id=self.product_id, domain=self.domain.name).save() self._stock_report = functools.partial(_stock_report, self.domain.name, self.case_id, self.product_id) self._receipt_report = functools.partial(_receipt_report, self.domain.name, self.case_id, self.product_id) self._test_config = ConsumptionConfiguration.test_config() self._compute_consumption = functools.partial( compute_daily_consumption, self.domain.name, self.case_id, self.product_id, now, configuration=self._test_config)
def _default_consumption_entry(self, case_id, product_id): configuration = self.stock_settings.consumption_config or ConsumptionConfiguration() default_value = compute_default_monthly_consumption( case_id, product_id, configuration, ) if default_value is not None: return self._entry_xml(product_id, default_value)
def mock_consumption(txdata, window, params=None): default_params = {'min_window': 0, 'min_periods': 0} params = params or {} default_params.update(params) config = ConsumptionConfiguration(**default_params) return compute_daily_consumption_from_transactions( txdata, ago(window), config, )
def testDefaultValue(self): self._stock_report(25, 5) self._stock_report(10, 0) self.assertEqual(None, compute_daily_consumption( self.domain.name, self.case_id, self.product_id, now, configuration=ConsumptionConfiguration(min_periods=4)) ) _ten = lambda case_id, product_id: 10 self.assertAlmostEqual(10., compute_consumption_or_default( self.domain.name, self.case_id, self.product_id, now, configuration=ConsumptionConfiguration( min_periods=4, default_monthly_consumption_function=_ten ) ))
def setUp(self): # create case self.case_id = uuid.uuid4().hex CommCareCase(_id=self.case_id, domain="fakedomain").save() self.product_id = uuid.uuid4().hex self._stock_report = functools.partial(_stock_report, self.case_id, self.product_id) self._receipt_report = functools.partial(_receipt_report, self.case_id, self.product_id) self._test_config = ConsumptionConfiguration.test_config() self._compute_consumption = functools.partial( compute_consumption, self.case_id, self.product_id, now, configuration=self._test_config )
def testDefaultValue(self): self._stock_report(25, 5) self._stock_report(10, 0) self.assertEqual( None, compute_daily_consumption( self.domain.name, self.case_id, self.product_id, now, configuration=ConsumptionConfiguration(min_periods=4)))
def _stock_report(case_id, product_id, amount, days_ago): report = StockReport.objects.create(form_id=uuid.uuid4().hex, date=ago(days_ago), type=const.REPORT_TYPE_BALANCE) txn = StockTransaction( report=report, section_id=const.SECTION_TYPE_STOCK, type=const.TRANSACTION_TYPE_STOCKONHAND, case_id=case_id, product_id=product_id, stock_on_hand=Decimal(amount), ) txn._test_config = ConsumptionConfiguration.test_config() txn.quantity = 0 txn.save()
def setUp(self): # create case case = CaseFactory(domain=self.domain.name).create_case() self.case_id = case.case_id self.product_id = uuid.uuid4().hex SQLProduct(product_id=self.product_id, domain=self.domain.name).save() self._stock_report = functools.partial(_stock_report, self.domain.name, self.case_id, self.product_id) self._receipt_report = functools.partial(_receipt_report, self.domain.name, self.case_id, self.product_id) self._test_config = ConsumptionConfiguration.test_config() self._compute_consumption = functools.partial( compute_daily_consumption, self.domain.name, self.case_id, self.product_id, now, configuration=self._test_config )
def setUp(self): # create case self.case_id = uuid.uuid4().hex CommCareCase( _id=self.case_id, domain='fakedomain', ).save() self.product_id = uuid.uuid4().hex self._stock_report = functools.partial(_stock_report, self.case_id, self.product_id) self._receipt_report = functools.partial(_receipt_report, self.case_id, self.product_id) self._test_config = ConsumptionConfiguration.test_config() self._compute_consumption = functools.partial(compute_consumption, self.case_id, self.product_id, now, configuration=self._test_config)
def get_consumption_config(self): def _default_monthly_consumption(case_id, product_id): # note: for now as an optimization hack, per-supply point type is not supported # unless explicitly configured, because it will require looking up the case facility_type = None if self.consumption_config.use_supply_point_type_default_consumption: try: supply_point = SupplyInterface(self.domain).get_supply_point(case_id) facility_type = supply_point.sql_location.location_type_name except ResourceNotFound: pass return get_default_monthly_consumption(self.domain, product_id, facility_type, case_id) return ConsumptionConfiguration( min_periods=self.consumption_config.min_transactions, min_window=self.consumption_config.min_window, max_window=self.consumption_config.optimal_window, default_monthly_consumption_function=_default_monthly_consumption, exclude_invalid_periods=self.consumption_config.exclude_invalid_periods )