def test_calculate_immunization_window(self): pregnancy_products = get_immunization_products(self.domain, 'pregnancy') child_products = get_immunization_products(self.domain, 'child') self.assertEqual( calculate_immunization_window( self.tasks_pregnancy, date(2017, 8, 1), get_map(pregnancy_products, 'code')['anc_1'], pregnancy_products, []), (date(2016, 10, 31), date(2018, 1, 28))) self.assertEqual( calculate_immunization_window( self.tasks_child, date(2017, 8, 1), get_map(child_products, 'code')['3g_dpt_3'], child_products, []), (None, date(2019, 8, 1))) self.assertEqual( calculate_immunization_window( self.tasks_child, date(2017, 8, 1), get_map(child_products, 'code')['3g_dpt_3'], child_products, [LedgerValue(entry_id=self.dpt2.get_id, balance=17390)]), (date(2017, 11, 7), date(2019, 8, 1))) self.assertEqual( calculate_immunization_window( self.tasks_child, date(2017, 8, 1), get_map(child_products, 'code')['3g_dpt_3'], child_products, [LedgerValue(entry_id=self.dpt2.get_id, balance=17501)]), (date(2017, 12, 29), date(2019, 8, 1)))
def get_models_to_update(self, stock_report_helper, ledger_db=None): latest_values = {} for stock_trans in stock_report_helper.transactions: def _lazy_original_balance(): # needs to be in closures because it's zero-argument. # see compute_ledger_values for more information if stock_trans.ledger_reference in latest_values: return latest_values[stock_trans.ledger_reference] else: return self.get_current_ledger_value(stock_trans.ledger_reference) new_ledger_values = compute_ledger_values( _lazy_original_balance, stock_report_helper.report_type, stock_trans.relative_quantity ) latest_values[stock_trans.ledger_reference] = new_ledger_values.balance to_save = [] for touched_ledger_reference, quantity in latest_values.items(): try: ledger_value = LedgerAccessorSQL.get_ledger_value(**touched_ledger_reference._asdict()) except LedgerValue.DoesNotExist: ledger_value = LedgerValue(**touched_ledger_reference._asdict()) ledger_value.balance = quantity to_save.append(ledger_value) return StockModelUpdateResult(to_save=to_save)
def test_immunization_is_due(self, todays_date): pregnancy_products = get_immunization_products(self.domain, 'pregnancy') child_products = get_immunization_products(self.domain, 'child') # Immunization is due when it has a predecessor which is completed and its date range is valid todays_date.return_value = date(2018, 1, 1) self.assertTrue( immunization_is_due( self.tasks_child, date(2017, 8, 1), get_map(child_products, 'code')['3g_dpt_3'], child_products, [LedgerValue(entry_id=self.dpt2.get_id, balance=17501)])) # Immunization is not due when it has a predecessor and its predecessor is not completed todays_date.return_value = date(2018, 1, 1) self.assertFalse( immunization_is_due(self.tasks_child, date(2017, 8, 1), get_map(child_products, 'code')['3g_dpt_3'], child_products, [])) # Immunization is due when it has a schedule_flag which is applied to the tasks case # and its date range is valid todays_date.return_value = date(2017, 9, 1) self.assertTrue( immunization_is_due(self.tasks_pregnancy, date(2017, 8, 1), get_map(pregnancy_products, 'code')['tt_1'], pregnancy_products, [])) # Immunization is not due when it has a schedule_flag which is not applied to the tasks case # even though its date range is valid todays_date.return_value = date(2017, 9, 1) self.assertFalse( immunization_is_due( self.tasks_pregnancy, date(2017, 8, 1), get_map(pregnancy_products, 'code')['tt_booster'], pregnancy_products, [])) # Immunization is not due when its date range is not valid todays_date.return_value = date(2020, 9, 1) self.assertFalse( immunization_is_due(self.tasks_pregnancy, date(2017, 8, 1), get_map(pregnancy_products, 'code')['tt_1'], pregnancy_products, [])) # Immunization is not due when it has already been completed todays_date.return_value = date(2017, 9, 1) self.assertFalse( immunization_is_due( self.tasks_pregnancy, date(2017, 8, 1), get_map(pregnancy_products, 'code')['tt_1'], pregnancy_products, [LedgerValue(entry_id=self.tt1.get_id, balance=17390)]))
def _process_transaction(stock_report_helper, stock_trans, ledger_db): def _lazy_original_balance(): # needs to be in closures because it's zero-argument. # see compute_ledger_values for more information reference = stock_trans.ledger_reference return ledger_db.get_current_ledger_value(reference) new_ledger_values = compute_ledger_values( _lazy_original_balance, stock_report_helper.report_type, stock_trans.relative_quantity) ledger_value = ledger_db.get_ledger(stock_trans.ledger_reference) if not ledger_value: ledger_value = LedgerValue( **stock_trans.ledger_reference._asdict()) ledger_value.domain = stock_report_helper.domain ledger_db.set_ledger(ledger_value) transaction = _get_ledger_transaction(_lazy_original_balance, stock_report_helper, stock_trans, new_ledger_values.balance) ledger_value.track_create(transaction) # only do this after we've created the transaction otherwise we'll get the wrong delta ledger_value.balance = new_ledger_values.balance ledger_value.last_modified = stock_report_helper.server_date # form.received_on ledger_value.last_modified_form_id = stock_report_helper.form_id return ledger_value
def _create_ledger(domain, entry_id, balance, case_id=None, section_id='stock'): user_id = 'user1' utcnow = datetime.utcnow() case_id = case_id or uuid.uuid4().hex case = CommCareCaseSQL( case_id=case_id, domain=domain, type='', owner_id=user_id, opened_on=utcnow, modified_on=utcnow, modified_by=user_id, server_modified_on=utcnow, ) CaseAccessorSQL.save_case(case) ledger = LedgerValue(domain=domain, case_id=case_id, section_id=section_id, entry_id=entry_id, balance=balance, last_modified=utcnow) LedgerAccessorSQL.save_ledger_values([ledger]) return ledger
def _process_transaction(stock_report_helper, stock_trans, ledger_db): def _lazy_original_balance(): # needs to be in closures because it's zero-argument. # see compute_ledger_values for more information reference = stock_trans.ledger_reference return ledger_db.get_current_ledger_value(reference) new_ledger_values = compute_ledger_values( _lazy_original_balance, stock_report_helper.report_type, stock_trans.relative_quantity ) ledger_value = ledger_db.get_ledger(stock_trans.ledger_reference) if not ledger_value: ledger_value = LedgerValue(**stock_trans.ledger_reference._asdict()) ledger_value.domain = stock_report_helper.domain ledger_db.set_ledger(ledger_value) transaction = _get_ledger_transaction( _lazy_original_balance, stock_report_helper, stock_trans, new_ledger_values.balance ) ledger_value.track_create(transaction) # only do this after we've created the transaction otherwise we'll get the wrong delta ledger_value.balance = new_ledger_values.balance ledger_value.last_modified = stock_report_helper.server_date # form.received_on ledger_value.last_modified_form_id = stock_report_helper.form_id return ledger_value
def setUpClass(cls): ensure_index_deleted(LEDGER_INDEX_INFO.index) es = get_es_new() initialize_index_and_mapping(es, LEDGER_INDEX_INFO) cls.expected_combos = { ('stock', 'product_a'), ('stock', 'product_b'), ('consumption', 'product_a'), ('consumption', 'product_c'), } for section, entry in cls.expected_combos: ledger = LedgerValue( domain=cls.domain, case_id=uuid.uuid4().hex, section_id=section, entry_id=entry, ) ledger_json = ledger.to_json() ledger_json['_id'] = ledger.ledger_reference.as_id() send_to_elasticsearch('ledgers', doc=ledger_json) es.indices.refresh(LEDGER_INDEX_INFO.index)
def setUpClass(cls): ensure_index_deleted(LEDGER_INDEX_INFO.index) es = get_es_new() initialize_index_and_mapping(es, LEDGER_INDEX_INFO) cls.expected_combos = { ('stock', 'product_a'), ('stock', 'product_b'), ('consumption', 'product_a'), ('consumption', 'product_c'), } for section, entry in cls.expected_combos: ledger = LedgerValue( domain=cls.domain, case_id=uuid.uuid4().hex, section_id=section, entry_id=entry, ) ledger_json = ledger.to_json(include_location_id=False) ledger_json['_id'] = ledger.ledger_reference.as_id() send_to_elasticsearch('ledgers', doc=ledger_json) es.indices.refresh(LEDGER_INDEX_INFO.index)
def delete_ledger_values(case_id, section_id=None, entry_id=None): """ Delete LedgerValues marching passed in args :param case_id: ID of the case :param section_id: section ID or None :param entry_id: entry ID or None :return: number of values deleted """ try: with LedgerValue.get_plproxy_cursor() as cursor: cursor.execute( "SELECT delete_ledger_values(%s, %s, %s) as deleted_count", [case_id, section_id, entry_id]) results = fetchall_as_namedtuple(cursor) return sum([result.deleted_count for result in results]) except InternalError as e: raise LedgerSaveError(e)