Beispiel #1
0
    def test_balance_consumption(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50 - 10 * i))
                         for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts))
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
            inferred = amt - initial
            if should_use_sql_backend(self.domain):
                sql_txn = LedgerAccessors(
                    self.domain.name).get_latest_transaction(
                        self.sp.case_id, 'stock', product)
                self.assertEqual(inferred, sql_txn.delta)
            else:
                inferred_txn = StockTransaction.objects.get(
                    case_id=self.sp.case_id,
                    product_id=product,
                    subtype=stockconst.TRANSACTION_SUBTYPE_INFERRED)
                self.assertEqual(Decimal(str(inferred)), inferred_txn.quantity)
                self.assertEqual(Decimal(str(amt)), inferred_txn.stock_on_hand)
                self.assertEqual(stockconst.TRANSACTION_TYPE_CONSUMPTION,
                                 inferred_txn.type)
Beispiel #2
0
    def testArchiveLastForm(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50)) for i, p in enumerate(self.products)]
        second_form_id = self.submit_xml_form(balance_submission(final_amounts))

        def _assert_initial_state():
            self.assertEqual(1, StockReport.objects.filter(form_id=second_form_id).count())
            # 6 = 3 stockonhand and 3 inferred consumption txns
            self.assertEqual(6, StockTransaction.objects.filter(report__form_id=second_form_id).count())
            self.assertEqual(3, StockState.objects.filter(case_id=self.sp._id).count())
            for state in StockState.objects.filter(case_id=self.sp._id):
                self.assertEqual(Decimal(50), state.stock_on_hand)
                self.assertIsNotNone(state.daily_consumption)

        # check initial setup
        _assert_initial_state()

        # archive and confirm commtrack data is deleted
        form = XFormInstance.get(second_form_id)
        form.archive()
        self.assertEqual(0, StockReport.objects.filter(form_id=second_form_id).count())
        self.assertEqual(0, StockTransaction.objects.filter(report__form_id=second_form_id).count())
        self.assertEqual(3, StockState.objects.filter(case_id=self.sp._id).count())
        for state in StockState.objects.filter(case_id=self.sp._id):
            # balance should be reverted to 100 in the StockState
            self.assertEqual(Decimal(100), state.stock_on_hand)
            # consumption should be none since there will only be 1 data point
            self.assertIsNone(state.daily_consumption)

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #3
0
    def test_balance_consumption_with_date(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts), date_formatter=json_format_date)

        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts), date_formatter=json_format_date)
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
Beispiel #4
0
    def test_balance_consumption_with_date(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts), date_formatter=json_format_date)

        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts), date_formatter=json_format_date)
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
Beispiel #5
0
    def test_archive_last_form(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        self.submit_xml_form(
            balance_submission(initial_amounts),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        final_amounts = [(p._id, float(50)) for i, p in enumerate(self.products)]
        second_form_id = self.submit_xml_form(balance_submission(final_amounts))

        ledger_accessors = LedgerAccessors(self.domain.name)

        def _assert_initial_state():
            if should_use_sql_backend(self.domain):
                self.assertEqual(3, len(self._get_all_ledger_transactions(Q(form_id=second_form_id))))
            else:
                self.assertEqual(1, StockReport.objects.filter(form_id=second_form_id).count())
                # 6 = 3 stockonhand and 3 inferred consumption txns
                self.assertEqual(6, StockTransaction.objects.filter(report__form_id=second_form_id).count())

            ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for lv in ledger_values:
                self.assertEqual(50, lv.stock_on_hand)
                self.assertEqual(
                    round(float(lv.daily_consumption), 2),
                    1.67
                )

        # check initial setup
        _assert_initial_state()

        # archive and confirm commtrack data is deleted
        form = FormAccessors(self.domain.name).get_form(second_form_id)
        with process_pillow_changes('LedgerToElasticsearchPillow'):
            form.archive()

        if should_use_sql_backend(self.domain):
            self.assertEqual(0, len(self._get_all_ledger_transactions(Q(form_id=second_form_id))))
        else:
            self.assertEqual(0, StockReport.objects.filter(form_id=second_form_id).count())
            self.assertEqual(0, StockTransaction.objects.filter(report__form_id=second_form_id).count())

        ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
        self.assertEqual(3, len(ledger_values))
        for state in ledger_values:
            # balance should be reverted to 100 in the StockState
            self.assertEqual(100, int(state.stock_on_hand))
            # consumption should be none since there will only be 1 data point
            self.assertIsNone(state.daily_consumption)

        # unarchive and confirm commtrack data is restored
        with process_pillow_changes('LedgerToElasticsearchPillow'):
            form.unarchive()
        _assert_initial_state()
Beispiel #6
0
    def test_archive_last_form(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        self.submit_xml_form(
            balance_submission(initial_amounts),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        final_amounts = [(p._id, float(50)) for i, p in enumerate(self.products)]
        second_form_id = self.submit_xml_form(balance_submission(final_amounts))

        ledger_accessors = LedgerAccessors(self.domain.name)

        def _assert_initial_state():
            if should_use_sql_backend(self.domain):
                self.assertEqual(3, len(self._get_all_ledger_transactions(Q(form_id=second_form_id))))
            else:
                self.assertEqual(1, StockReport.objects.filter(form_id=second_form_id).count())
                # 6 = 3 stockonhand and 3 inferred consumption txns
                self.assertEqual(6, StockTransaction.objects.filter(report__form_id=second_form_id).count())

            ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for lv in ledger_values:
                self.assertEqual(50, lv.stock_on_hand)
                self.assertEqual(
                    round(float(lv.daily_consumption), 2),
                    1.67
                )

        # check initial setup
        _assert_initial_state()

        # archive and confirm commtrack data is deleted
        form = FormAccessors(self.domain.name).get_form(second_form_id)
        with process_pillow_changes('LedgerToElasticsearchPillow'):
            form.archive()

        if should_use_sql_backend(self.domain):
            self.assertEqual(0, len(self._get_all_ledger_transactions(Q(form_id=second_form_id))))
        else:
            self.assertEqual(0, StockReport.objects.filter(form_id=second_form_id).count())
            self.assertEqual(0, StockTransaction.objects.filter(report__form_id=second_form_id).count())

        ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
        self.assertEqual(3, len(ledger_values))
        for state in ledger_values:
            # balance should be reverted to 100 in the StockState
            self.assertEqual(100, int(state.stock_on_hand))
            # consumption should be none since there will only be 1 data point
            self.assertIsNone(state.daily_consumption)

        # unarchive and confirm commtrack data is restored
        with process_pillow_changes('LedgerToElasticsearchPillow'):
            form.unarchive()
        _assert_initial_state()
Beispiel #7
0
    def test_archive_last_form(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts),
                             timestamp=datetime.utcnow() + timedelta(-30))

        final_amounts = [(p._id, float(50))
                         for i, p in enumerate(self.products)]
        second_form_id = self.submit_xml_form(
            balance_submission(final_amounts))

        ledger_accessors = LedgerAccessors(self.domain.name)

        def _assert_initial_state():
            self.assertEqual(
                3,
                len(
                    self._get_all_ledger_transactions(
                        Q(form_id=second_form_id))))

            ledger_values = ledger_accessors.get_ledger_values_for_case(
                self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for lv in ledger_values:
                self.assertEqual(50, lv.stock_on_hand)
                self.assertEqual(round(float(lv.daily_consumption), 2), 1.67)

        # check initial setup
        _assert_initial_state()

        # archive and confirm commtrack data is deleted
        form = XFormInstance.objects.get_form(second_form_id, self.domain.name)
        with self.process_legder_changes:
            form.archive()

        self.assertEqual(
            0,
            len(self._get_all_ledger_transactions(Q(form_id=second_form_id))))

        ledger_values = ledger_accessors.get_ledger_values_for_case(
            self.sp.case_id)
        self.assertEqual(3, len(ledger_values))
        for state in ledger_values:
            # balance should be reverted to 100 in the ledger value
            self.assertEqual(100, int(state.stock_on_hand))
            # consumption should be none since there will only be 1 data point
            self.assertIsNone(state.daily_consumption)

        # unarchive and confirm commtrack data is restored
        with self.process_legder_changes:
            form.unarchive()
        _assert_initial_state()
Beispiel #8
0
    def testArchiveLastForm(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50))
                         for i, p in enumerate(self.products)]
        second_form_id = self.submit_xml_form(
            balance_submission(final_amounts))

        def _assert_initial_state():
            self.assertEqual(
                1,
                StockReport.objects.filter(form_id=second_form_id).count())
            # 6 = 3 stockonhand and 3 inferred consumption txns
            self.assertEqual(
                6,
                StockTransaction.objects.filter(
                    report__form_id=second_form_id).count())
            self.assertEqual(
                3,
                StockState.objects.filter(case_id=self.sp._id).count())
            for state in StockState.objects.filter(case_id=self.sp._id):
                self.assertEqual(Decimal(50), state.stock_on_hand)
                self.assertIsNotNone(state.daily_consumption)

        # check initial setup
        _assert_initial_state()

        # archive and confirm commtrack data is deleted
        form = XFormInstance.get(second_form_id)
        form.archive()
        self.assertEqual(
            0,
            StockReport.objects.filter(form_id=second_form_id).count())
        self.assertEqual(
            0,
            StockTransaction.objects.filter(
                report__form_id=second_form_id).count())
        self.assertEqual(
            3,
            StockState.objects.filter(case_id=self.sp._id).count())
        for state in StockState.objects.filter(case_id=self.sp._id):
            # balance should be reverted to 100 in the StockState
            self.assertEqual(Decimal(100), state.stock_on_hand)
            # consumption should be none since there will only be 1 data point
            self.assertIsNone(state.daily_consumption)

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #9
0
    def test_balance_consumption(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50 - 10 * i))
                         for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts))
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
            inferred = amt - initial
            sql_txn = LedgerAccessors(self.domain.name).get_latest_transaction(
                self.sp.case_id, 'stock', product)
            self.assertEqual(inferred, sql_txn.delta)
Beispiel #10
0
    def test_balance_consumption(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts))
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
            inferred = amt - initial
            inferred_txn = StockTransaction.objects.get(case_id=self.sp._id, product_id=product,
                                              subtype=stockconst.TRANSACTION_SUBTYPE_INFERRED)
            self.assertEqual(Decimal(str(inferred)), inferred_txn.quantity)
            self.assertEqual(Decimal(str(amt)), inferred_txn.stock_on_hand)
            self.assertEqual(stockconst.TRANSACTION_TYPE_CONSUMPTION, inferred_txn.type)
Beispiel #11
0
    def test_balance_consumption(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts))
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
            inferred = amt - initial
            inferred_txn = StockTransaction.objects.get(case_id=self.sp._id, product_id=product,
                                              subtype=stockconst.TRANSACTION_SUBTYPE_INFERRED)
            self.assertEqual(Decimal(str(inferred)), inferred_txn.quantity)
            self.assertEqual(Decimal(str(amt)), inferred_txn.stock_on_hand)
            self.assertEqual(stockconst.TRANSACTION_TYPE_CONSUMPTION, inferred_txn.type)
Beispiel #12
0
 def test_blank_product_id(self):
     initial = float(100)
     balances = [('', initial)]
     instance_id = self.submit_xml_form(balance_submission(balances))
     instance = FormAccessors(self.domain.name).get_form(instance_id)
     self.assertTrue(instance.is_error)
     self.assertTrue('MissingProductId' in instance.problem)
Beispiel #13
0
 def test_blank_product_id(self):
     initial = float(100)
     balances = [('', initial)]
     instance_id = self.submit_xml_form(balance_submission(balances))
     instance = XFormInstance.get(instance_id)
     self.assertEqual('XFormError', instance.doc_type)
     self.assertTrue('MissingProductId' in instance.problem)
Beispiel #14
0
 def test_blank_product_id(self):
     initial = float(100)
     balances = [('', initial)]
     instance_id = self.submit_xml_form(balance_submission(balances))
     instance = FormAccessors(self.domain.name).get_form(instance_id)
     self.assertTrue(instance.is_error)
     self.assertTrue('MissingProductId' in instance.problem)
Beispiel #15
0
    def test_blank_quantities(self):
        # submitting a bunch of blank data shouldn't submit transactions
        # so lets submit some initial data and make sure we don't modify it
        # or have new transactions
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        trans_count = StockTransaction.objects.all().count()

        initial_amounts = [(p._id, '') for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        self.assertEqual(trans_count, StockTransaction.objects.all().count())
        for product in self.products:
            self.check_product_stock(self.sp, product._id, 100, 0)
Beispiel #16
0
    def test_xform_id_added_to_case_xform_list(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        submissions = [balance_submission([amount]) for amount in initial_amounts]
        instance_id = self.submit_xml_form("".join(submissions), timestamp=datetime.utcnow() + timedelta(-30))

        case = CommCareCase.get(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)
Beispiel #17
0
    def testArchiveOnlyForm(self):
        # check no data in stock states
        self.assertEqual(0, StockState.objects.filter(case_id=self.sp._id).count())

        initial_amounts = [(p._id, float(100)) for p in self.products]
        form_id = self.submit_xml_form(balance_submission(initial_amounts))

        # check that we made stuff
        def _assert_initial_state():
            self.assertEqual(1, StockReport.objects.filter(form_id=form_id).count())
            self.assertEqual(3, StockTransaction.objects.filter(report__form_id=form_id).count())
            self.assertEqual(3, StockState.objects.filter(case_id=self.sp._id).count())
            for state in StockState.objects.filter(case_id=self.sp._id):
                self.assertEqual(Decimal(100), state.stock_on_hand)
        _assert_initial_state()

        # archive and confirm commtrack data is cleared
        form = XFormInstance.get(form_id)
        form.archive()
        self.assertEqual(0, StockReport.objects.filter(form_id=form_id).count())
        self.assertEqual(0, StockTransaction.objects.filter(report__form_id=form_id).count())
        self.assertEqual(0, StockState.objects.filter(case_id=self.sp._id).count())

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #18
0
 def test_blank_product_id(self):
     initial = float(100)
     balances = [("", initial)]
     instance_id = self.submit_xml_form(balance_submission(balances))
     instance = XFormInstance.get(instance_id)
     self.assertEqual("XFormError", instance.doc_type)
     self.assertTrue("MissingProductId" in instance.problem)
Beispiel #19
0
    def test_blank_quantities(self):
        # submitting a bunch of blank data shouldn't submit transactions
        # so lets submit some initial data and make sure we don't modify it
        # or have new transactions
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        trans_count = StockTransaction.objects.all().count()

        initial_amounts = [(p._id, '') for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        self.assertEqual(trans_count, StockTransaction.objects.all().count())
        for product in self.products:
            self.check_product_stock(self.sp, product._id, 100, 0)
Beispiel #20
0
    def testArchiveOnlyForm(self):
        # check no data in stock states
        self.assertEqual(0, StockState.objects.filter(case_id=self.sp._id).count())

        initial_amounts = [(p._id, float(100)) for p in self.products]
        form_id = self.submit_xml_form(balance_submission(initial_amounts))

        # check that we made stuff
        def _assert_initial_state():
            self.assertEqual(1, StockReport.objects.filter(form_id=form_id).count())
            self.assertEqual(3, StockTransaction.objects.filter(report__form_id=form_id).count())
            self.assertEqual(3, StockState.objects.filter(case_id=self.sp._id).count())
            for state in StockState.objects.filter(case_id=self.sp._id):
                self.assertEqual(Decimal(100), state.stock_on_hand)
        _assert_initial_state()

        # archive and confirm commtrack data is cleared
        form = XFormInstance.get(form_id)
        form.archive()
        self.assertEqual(0, StockReport.objects.filter(form_id=form_id).count())
        self.assertEqual(0, StockTransaction.objects.filter(report__form_id=form_id).count())
        self.assertEqual(0, StockState.objects.filter(case_id=self.sp._id).count())

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #21
0
    def test_archive_only_form(self):
        # check no data in stock states
        ledger_accessors = LedgerAccessors(self.domain.name)
        ledger_values = ledger_accessors.get_ledger_values_for_case(
            self.sp.case_id)
        self.assertEqual(0, len(ledger_values))

        initial_amounts = [(p._id, float(100)) for p in self.products]
        form_id = self.submit_xml_form(balance_submission(initial_amounts))

        # check that we made stuff
        def _assert_initial_state():
            self.assertEqual(
                3, len(self._get_all_ledger_transactions(Q(form_id=form_id))))

            ledger_values = ledger_accessors.get_ledger_values_for_case(
                self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for state in ledger_values:
                self.assertEqual(100, int(state.stock_on_hand))

        _assert_initial_state()

        # archive and confirm commtrack data is cleared
        form = XFormInstance.objects.get_form(form_id, self.domain.name)
        form.archive()
        self.assertEqual(
            0,
            len(ledger_accessors.get_ledger_values_for_case(self.sp.case_id)))
        self.assertEqual(
            0, len(self._get_all_ledger_transactions(Q(form_id=form_id))))

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #22
0
    def test_archived_product_submissions(self):
        """
        This is basically the same as above, but separated to be
        verbose about what we are checking (and to make it easy
        to change the expected behavior if the requirements change
        soon.
        """
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]

        self.submit_xml_form(balance_submission(initial_amounts))
        self.products[1].archive()
        self.submit_xml_form(balance_submission(final_amounts))

        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
Beispiel #23
0
    def test_archived_product_submissions(self):
        """
        This is basically the same as above, but separated to be
        verbose about what we are checking (and to make it easy
        to change the expected behavior if the requirements change
        soon.
        """
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]

        self.submit_xml_form(balance_submission(initial_amounts))
        self.products[1].archive()
        self.submit_xml_form(balance_submission(final_amounts))

        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
Beispiel #24
0
    def test_transfer_source_only(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        deductions = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(transfer_source_only(deductions))
        for product, amt in deductions:
            self.check_product_stock(self.sp, product, initial-amt, -amt)
Beispiel #25
0
    def test_transfer_enumerated(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        receipts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(receipts_enumerated(receipts))
        for product, amt in receipts:
            self.check_product_stock(self.sp, product, initial + amt, amt)
Beispiel #26
0
    def test_transfer_source_only(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        deductions = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(transfer_source_only(deductions))
        for product, amt in deductions:
            self.check_product_stock(self.sp, product, initial-amt, -amt)
Beispiel #27
0
    def test_transfer_enumerated(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        receipts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(receipts_enumerated(receipts))
        for product, amt in receipts:
            self.check_product_stock(self.sp, product, initial + amt, amt)
Beispiel #28
0
    def test_transfer_both(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        transfers = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(transfer_both(transfers))
        for product, amt in transfers:
            self.check_product_stock(self.sp, product, initial-amt, -amt)
            self.check_product_stock(self.sp2, product, amt, amt)
Beispiel #29
0
    def test_transfer_both(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        transfers = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(transfer_both(transfers))
        for product, amt in transfers:
            self.check_product_stock(self.sp, product, initial-amt, -amt)
            self.check_product_stock(self.sp2, product, amt, amt)
Beispiel #30
0
    def test_xform_id_added_to_case_xform_list(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        submissions = [balance_submission([amount]) for amount in initial_amounts]
        instance_id = self.submit_xml_form(
            ''.join(submissions),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        case = CaseAccessors(self.domain.name).get_case(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)
Beispiel #31
0
    def test_archive_only_form(self):
        # check no data in stock states
        ledger_accessors = LedgerAccessors(self.domain.name)
        ledger_values = ledger_accessors.get_ledger_values_for_case(
            self.sp.case_id)
        self.assertEqual(0, len(ledger_values))

        initial_amounts = [(p._id, float(100)) for p in self.products]
        form_id = self.submit_xml_form(balance_submission(initial_amounts))

        # check that we made stuff
        def _assert_initial_state():
            if should_use_sql_backend(self.domain):
                self.assertEqual(
                    3,
                    LedgerTransaction.objects.filter(form_id=form_id).count())
            else:
                self.assertEqual(
                    1,
                    StockReport.objects.filter(form_id=form_id).count())
                self.assertEqual(
                    3,
                    StockTransaction.objects.filter(
                        report__form_id=form_id).count())

            ledger_values = ledger_accessors.get_ledger_values_for_case(
                self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for state in ledger_values:
                self.assertEqual(100, int(state.stock_on_hand))

        _assert_initial_state()

        # archive and confirm commtrack data is cleared
        form = FormAccessors(self.domain.name).get_form(form_id)
        form.archive()
        self.assertEqual(
            0,
            len(ledger_accessors.get_ledger_values_for_case(self.sp.case_id)))
        if should_use_sql_backend(self.domain):
            self.assertEqual(
                0,
                LedgerTransaction.objects.filter(form_id=form_id).count())
        else:
            self.assertEqual(
                0,
                StockReport.objects.filter(form_id=form_id).count())
            self.assertEqual(
                0,
                StockTransaction.objects.filter(
                    report__form_id=form_id).count())

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #32
0
    def testStockSyncToken(self):
        # first restore should not have the updated case
        check_user_has_case(self, self.restore_user, self.sp_block, should_have=False,
                            restore_id=self.sync_log_id, version=V2)

        # submit with token
        amounts = [(p._id, float(i*10)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(amounts), last_sync_token=self.sync_log_id)
        # now restore should have the case
        check_user_has_case(self, self.restore_user, self.sp_block, should_have=True,
                            restore_id=self.sync_log_id, version=V2, line_by_line=False)
Beispiel #33
0
    def testStockSyncToken(self):
        # first restore should not have the updated case
        check_user_has_case(self, self.casexml_user, self.sp_block, should_have=False,
                            restore_id=self.sync_log_id, version=V2)

        # submit with token
        amounts = [(p._id, float(i*10)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(amounts), last_sync_token=self.sync_log_id)
        # now restore should have the case
        check_user_has_case(self, self.casexml_user, self.sp_block, should_have=True,
                            restore_id=self.sync_log_id, version=V2, line_by_line=False)
Beispiel #34
0
    def test_xform_id_added_to_case_xform_list_only_once(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        submissions = [balance_submission([amount]) for amount in initial_amounts]
        case_block = CaseBlock(create=False, case_id=self.sp.case_id, user_id="jack", update={"test": "1"}).as_string()
        instance_id = self.submit_xml_form(
            "".join([case_block] + submissions), timestamp=datetime.utcnow() + timedelta(-30)
        )

        case = CommCareCase.get(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)
        # make sure the ID only got added once
        self.assertEqual(len(case.xform_ids), len(set(case.xform_ids)))
Beispiel #35
0
    def test_balance_submit_multiple_stocks(self):
        def _random_amounts():
            return [(p._id, float(random.randint(0, 100))) for i, p in enumerate(self.products)]

        section_ids = ('stock', 'losses', 'consumption')
        stock_amounts = [(id, _random_amounts()) for id in section_ids]
        for section_id, amounts in stock_amounts:
            self.submit_xml_form(balance_submission(amounts, section_id=section_id))

        for section_id, amounts in stock_amounts:
            for product, amt in amounts:
                self.check_product_stock(self.sp, product, amt, 0, section_id)
Beispiel #36
0
    def test_balance_submit_multiple_stocks(self):
        def _random_amounts():
            return [(p._id, float(random.randint(0, 100))) for i, p in enumerate(self.products)]

        section_ids = ('stock', 'losses', 'consumption')
        stock_amounts = [(id, _random_amounts()) for id in section_ids]
        for section_id, amounts in stock_amounts:
            self.submit_xml_form(balance_submission(amounts, section_id=section_id))

        for section_id, amounts in stock_amounts:
            for product, amt in amounts:
                self.check_product_stock(self.sp, product, amt, 0, section_id)
Beispiel #37
0
    def test_balance_consumption(self):
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        final_amounts = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        self.submit_xml_form(balance_submission(final_amounts))
        for product, amt in final_amounts:
            self.check_product_stock(self.sp, product, amt, 0)
            inferred = amt - initial
            if should_use_sql_backend(self.domain.name):
                sql_txn = LedgerAccessors(self.domain.name).get_latest_transaction(
                    self.sp.case_id, 'stock', product
                )
                self.assertEqual(inferred, sql_txn.delta)
            else:
                inferred_txn = StockTransaction.objects.get(
                    case_id=self.sp.case_id, product_id=product, subtype=stockconst.TRANSACTION_SUBTYPE_INFERRED
                )
                self.assertEqual(Decimal(str(inferred)), inferred_txn.quantity)
                self.assertEqual(Decimal(str(amt)), inferred_txn.stock_on_hand)
                self.assertEqual(stockconst.TRANSACTION_TYPE_CONSUMPTION, inferred_txn.type)
Beispiel #38
0
    def test_transfer_first_doc_order(self):
        # first set to 100
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        # then mark some receipts
        transfers = [(p._id, float(50 - 10*i + 3)) for i, p in enumerate(self.products)]
        # then set to 50
        final = float(50)
        balance_amounts = [(p._id, final) for p in self.products]
        self.submit_xml_form(transfer_first(transfers, balance_amounts))
        for product, amt in transfers:
            self.check_product_stock(self.sp, product, final, 0)
Beispiel #39
0
    def test_archived_form_gets_removed_from_case_xform_ids(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        instance_id = self.submit_xml_form(
            balance_submission(initial_amounts), timestamp=datetime.utcnow() + timedelta(-30)
        )

        case = CommCareCase.get(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)

        form = XFormInstance.get(instance_id)
        form.archive()

        case = CommCareCase.get(self.sp.case_id)
        self.assertNotIn(instance_id, case.xform_ids)
Beispiel #40
0
    def test_archived_form_gets_removed_from_case_xform_ids(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        instance_id = self.submit_xml_form(balance_submission(initial_amounts),
                                           timestamp=datetime.utcnow() +
                                           timedelta(-30))

        case = CommCareCase.objects.get_case(self.sp.case_id, self.domain.name)
        self.assertIn(instance_id, case.xform_ids)

        form = XFormInstance.objects.get_form(instance_id, self.domain.name)
        form.archive()

        case = CommCareCase.objects.get_case(self.sp.case_id, self.domain.name)
        self.assertNotIn(instance_id, case.xform_ids)
Beispiel #41
0
    def test_transfer_first_doc_order(self):
        # first set to 100
        initial = float(100)
        initial_amounts = [(p._id, initial) for p in self.products]
        self.submit_xml_form(balance_submission(initial_amounts))

        # then mark some receipts
        transfers = [(p._id, float(50 - 10*i)) for i, p in enumerate(self.products)]
        # then set to 50
        final = float(50)
        balance_amounts = [(p._id, final) for p in self.products]
        self.submit_xml_form(transfer_first(transfers, balance_amounts))
        for product, amt in transfers:
            self.check_product_stock(self.sp, product, final, 0)
Beispiel #42
0
    def test_archived_form_gets_removed_from_case_xform_ids(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        instance_id = self.submit_xml_form(
            balance_submission(initial_amounts),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        case_accessors = CaseAccessors(self.domain.name)
        case = case_accessors.get_case(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)

        form = FormAccessors(self.domain.name).get_form(instance_id)
        form.archive()

        case = case_accessors.get_case(self.sp.case_id)
        self.assertNotIn(instance_id, case.xform_ids)
Beispiel #43
0
    def test_xform_id_added_to_case_xform_list_only_once(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        submissions = [balance_submission([amount]) for amount in initial_amounts]
        case_block = CaseBlock(
            create=False,
            case_id=self.sp.case_id,
            user_id='jack',
            update={'test': '1'}
        ).as_string().decode('utf-8')
        instance_id = self.submit_xml_form(
            ''.join([case_block] + submissions),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        case = CaseAccessors(self.domain.name).get_case(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)
        # make sure the ID only got added once
        self.assertEqual(len(case.xform_ids), len(set(case.xform_ids)))
Beispiel #44
0
    def test_xform_id_added_to_case_xform_list_only_once(self):
        initial_amounts = [(p._id, float(100)) for p in self.products]
        submissions = [balance_submission([amount]) for amount in initial_amounts]
        case_block = CaseBlock(
            create=False,
            case_id=self.sp.case_id,
            user_id='jack',
            update={'test': '1'}
        ).as_string()
        instance_id = self.submit_xml_form(
            ''.join([case_block] + submissions),
            timestamp=datetime.utcnow() + timedelta(-30)
        )

        case = CaseAccessors(self.domain.name).get_case(self.sp.case_id)
        self.assertIn(instance_id, case.xform_ids)
        # make sure the ID only got added once
        self.assertEqual(len(case.xform_ids), len(set(case.xform_ids)))
Beispiel #45
0
 def test_submit_bad_case_id(self):
     instance_id = uuid.uuid4().hex
     amounts = [(p._id, float(i*10)) for i, p in enumerate(self.products)]
     xml_stub = balance_submission(amounts)
     instance = submission_wrap(
         instance_id,
         self.products,
         self.user,
         'missing',
         'missing-too',
         xml_stub,
     )
     submit_form_locally(
         instance=instance,
         domain=self.domain.name,
     )
     form = XFormInstance.get(instance_id)
     self.assertEqual('XFormError', form.doc_type)
     self.assertTrue('IllegalCaseId' in form.problem)
Beispiel #46
0
    def test_archive_only_form(self):
        # check no data in stock states
        ledger_accessors = LedgerAccessors(self.domain.name)
        ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
        self.assertEqual(0, len(ledger_values))

        initial_amounts = [(p._id, float(100)) for p in self.products]
        form_id = self.submit_xml_form(balance_submission(initial_amounts))

        # check that we made stuff
        def _assert_initial_state():
            if should_use_sql_backend(self.domain.name):
                self.assertEqual(3, LedgerTransaction.objects.filter(form_id=form_id).count())
            else:
                self.assertEqual(1, StockReport.objects.filter(form_id=form_id).count())
                self.assertEqual(3, StockTransaction.objects.filter(report__form_id=form_id).count())

            ledger_values = ledger_accessors.get_ledger_values_for_case(self.sp.case_id)
            self.assertEqual(3, len(ledger_values))
            for state in ledger_values:
                self.assertEqual(100, int(state.stock_on_hand))

        _assert_initial_state()

        # archive and confirm commtrack data is cleared
        form = FormAccessors(self.domain.name).get_form(form_id)
        form.archive()
        self.assertEqual(0, len(ledger_accessors.get_ledger_values_for_case(self.sp.case_id)))
        if should_use_sql_backend(self.domain.name):
            self.assertEqual(0, LedgerTransaction.objects.filter(form_id=form_id).count())
        else:
            self.assertEqual(0, StockReport.objects.filter(form_id=form_id).count())
            self.assertEqual(0, StockTransaction.objects.filter(report__form_id=form_id).count())

        # unarchive and confirm commtrack data is restored
        form.unarchive()
        _assert_initial_state()
Beispiel #47
0
 def test_blank_product_id(self):
     initial = float(100)
     balances = [('', initial)]
     with self.assertRaises(MissingProductId):
         # todo: if we ever want to fail more gracefully we can catch this exception and change this test
         self.submit_xml_form(balance_submission(balances))
Beispiel #48
0
 def test_balance_submit_date(self):
     amounts = [(p._id, float(i * 10)) for i, p in enumerate(self.products)]
     self.submit_xml_form(balance_submission(amounts),
                          date_formatter=json_format_date)
     for product, amt in amounts:
         self.check_product_stock(self.sp, product, amt, 0)
Beispiel #49
0
 def test_balance_submit(self):
     amounts = [(p._id, float(i*10)) for i, p in enumerate(self.products)]
     self.submit_xml_form(balance_submission(amounts))
     for product, amt in amounts:
         self.check_product_stock(self.sp, product, amt, 0)