

def test_web_user_report_submission(self):
    self.client.login(username=self.username5, password=self.password5)
    view_url = reverse('input_stock', kwargs={
        'domain': TEST_DOMAIN,
        'site_code': 'tsactive',
    })
    data = {
        'form-TOTAL_FORMS': 2,
        'form-INITIAL_FORMS': 2,
        'form-MAX_NUM_FORMS': 1000,
    }
    tsactive = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='tsactive')
    data['form-0-product_id'] = self.ad.product_id
    data['form-0-product'] = 'ad'
    data['form-0-stock_on_hand'] = 20
    data['form-0-receipts'] = 30
    data['form-1-product_id'] = self.al.product_id
    data['form-1-product'] = 'al'
    data['form-1-stock_on_hand'] = 14
    data['form-1-receipts'] = 17
    response = self.client.post(view_url, data=data)
    url = make_url(StockStatus, self.domain, '?location_id=%s&filter_by_program=all&startdate=&enddate=&report_type=&filter_by_product=all', (tsactive.location_id,))
    self.assertRedirects(response, url)
    messages = SMS.objects.filter(domain=TEST_DOMAIN)
    self.assertEqual(messages.count(), 1)
    self.assertEqual(messages[0].text, 'Dear test test2, thank you for reporting the commodities you have. You received ad 30 al 17.')
    stock_states = StockState.objects.filter(case_id=tsactive.supply_point_id)
    stock_transactions = StockTransaction.objects.filter(case_id=tsactive.supply_point_id)
    self.assertEqual(stock_states.count(), 2)
    self.assertEqual(stock_transactions.count(), 6)
    self.assertEqual(stock_transactions.filter(type='consumption').count(), 2)
    self.assertEqual(stock_transactions.filter(type='stockonhand').count(), 2)
    self.assertEqual(stock_transactions.filter(type='receipts').count(), 2)
    ad_consumption = stock_transactions.filter(type='consumption', product_id=self.ad.product_id)[0].quantity
    al_consumption = stock_transactions.filter(type='consumption', product_id=self.al.product_id)[0].quantity
    self.assertEqual(ad_consumption, Decimal((- 10)))
    self.assertEqual(al_consumption, Decimal((- 3)))
    al_stock_state = StockState.objects.get(case_id=tsactive.supply_point_id, product_id=self.al.product_id)
    ad_stock_state = StockState.objects.get(case_id=tsactive.supply_point_id, product_id=self.ad.product_id)
    self.assertEqual(int(ad_stock_state.stock_on_hand), 20)
    self.assertEqual(int(al_stock_state.stock_on_hand), 14)
    reports = StockReport.objects.filter(domain=TEST_DOMAIN)
    self.assertEqual(reports.count(), 2)

dataset/ETHPy150Open dimagi/commcare-hq/custom/ewsghana/tests/test_input_stock_view.py TestInputStockView.test_web_user_report_submission/original