<commit_before>from corehq.apps.commtrack.models import StockState
from casexml.apps.stock.models import DocDomainMapping
from casexml.apps.stock.tests.base import _stock_report
from corehq.apps.commtrack.tests.util import CommTrackTest
from corehq.apps.commtrack.const import DAYS_IN_MONTH
from datetime import datetime
from corehq.apps.consumption.shortcuts import set_default_monthly_consumption_for_domain


class StockStateTest(CommTrackTest):
    def report(self, amount, days_ago):
        return _stock_report(
            self.sp._id,
            self.products[0]._id,
            amount,
            days_ago
        )


class StockStateBehaviorTest(StockStateTest):
    def test_stock_state(self):

        self.report(25, 5)
        self.report(10, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(10, state.stock_on_hand)
        self.assertEqual(3.0, state.get_daily_consumption())

    def test_domain_mapping(self):
        # make sure there's a fake case setup for this
        with self.assertRaises(DocDomainMapping.DoesNotExist):
            DocDomainMapping.objects.get(doc_id=self.sp._id)

        StockState(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
            last_modified_date=datetime.now(),
        ).save()

        self.assertEqual(
            self.domain.name,
            DocDomainMapping.objects.get(doc_id=self.sp._id).domain_name
        )


class StockStateConsumptionTest(StockStateTest):
    def test_none_with_no_defaults(self):
        # need to submit something to have a state initialized
        self.report(25, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(None, state.get_daily_consumption())

    def test_pre_set_defaults(self):
        set_default_monthly_consumption_for_domain(self.domain.name, 50)
        self.report(25, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(50, float(state.get_daily_consumption()))

    def test_defaults_set_after_report(self):
        self.report(25, 0)
        set_default_monthly_consumption_for_domain(self.domain.name, 50)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(50, float(state.get_daily_consumption()))<commit_msg>fix test that should have caught consumption issue<commit_after>from corehq.apps.commtrack.models import StockState
from casexml.apps.stock.models import DocDomainMapping
from casexml.apps.stock.tests.base import _stock_report
from corehq.apps.commtrack.tests.util import CommTrackTest
from corehq.apps.commtrack.const import DAYS_IN_MONTH
from datetime import datetime
from corehq.apps.consumption.shortcuts import set_default_monthly_consumption_for_domain


class StockStateTest(CommTrackTest):
    def report(self, amount, days_ago):
        return _stock_report(
            self.sp._id,
            self.products[0]._id,
            amount,
            days_ago
        )


class StockStateBehaviorTest(StockStateTest):
    def test_stock_state(self):

        self.report(25, 5)
        self.report(10, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(10, state.stock_on_hand)
        self.assertEqual(3.0, state.get_daily_consumption())

    def test_domain_mapping(self):
        # make sure there's a fake case setup for this
        with self.assertRaises(DocDomainMapping.DoesNotExist):
            DocDomainMapping.objects.get(doc_id=self.sp._id)

        StockState(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
            last_modified_date=datetime.now(),
        ).save()

        self.assertEqual(
            self.domain.name,
            DocDomainMapping.objects.get(doc_id=self.sp._id).domain_name
        )


class StockStateConsumptionTest(StockStateTest):
    def test_none_with_no_defaults(self):
        # need to submit something to have a state initialized
        self.report(25, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(None, state.get_daily_consumption())

    def test_pre_set_defaults(self):
        set_default_monthly_consumption_for_domain(self.domain.name, 5 * 30)
        self.report(25, 0)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(5, float(state.get_daily_consumption()))

    def test_defaults_set_after_report(self):
        self.report(25, 0)
        set_default_monthly_consumption_for_domain(self.domain.name, 5 * 30)

        state = StockState.objects.get(
            section_id='stock',
            case_id=self.sp._id,
            product_id=self.products[0]._id,
        )

        self.assertEqual(5, float(state.get_daily_consumption()))
{'fraction_non_alphanumeric': 0.09424083769633508, 'fraction_numerical': 0.011915508214479148, 'mean_word_length': 2.5286624203821657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7241432', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1651, 'n_words': 264}