Beispiel #1
0
 def test_save_recalculates_invalid_credit_user_submissions(self):
     """Does save recalculate submissions when formula goes invalid?"""
     credit_submission = CreditUserSubmissionFactory(credit=self.credit)
     fields = make_numeric_fields(self.credit, 3)
     field_submissions = []
     for i in xrange(len(fields)):
         field_submissions.append(
             NumericDocumentationFieldSubmissionFactory(
                 documentation_field=fields[i],
                 credit_submission=credit_submission,
                 value=i))
     calculated_field = DocumentationField.objects.create(
         credit=self.credit, type='calculated', formula='value = A + B + C')
     calculated_field_submission = (
         NumericDocumentationFieldSubmissionFactory(
             documentation_field=calculated_field,
             credit_submission=credit_submission))
     calculated_field_submission.calculate()
     self.assertEqual(3, calculated_field_submission.value)
     calculated_field.formula = 'value = bo-o-o-o-gus'
     calculated_field.save()
     # We have a dirty read.
     calculated_field_submission = NumericSubmission.objects.get(
         pk=calculated_field_submission.pk)
     self.assertEqual(None, calculated_field_submission.value)
Beispiel #2
0
    def test_calculated_field_as_formula_term_recalculates(self):
        """Does a calculated field in a formula recalculate?"""
        # test that calculated fields acting as formula terms are calculated
        # appropriately -- CF1 is term in CF2, T1 is term in CF1, test that
        # update of T1.value causes value of CF2 to be recalculated.
        second_calculated_field = DocumentationFieldFactory(
            credit=self.credit,
            type="calculated",
            formula="value = {}".format(self.calculated_field.identifier))

        second_calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=second_calculated_field,
                credit_submission=self.credit_user_submission,
                value=5000))

        # Updating self.numeric_submission.value should cause
        # self.calculated_submission to calculate, which should
        # cause second_calculated_submission to calculate.

        self.numeric_submission.value += 100

        self.numeric_submission.save()

        self.assertEqual(
            NumericSubmission.objects.get(pk=self.numeric_submission.pk).value,
            NumericSubmission.objects.get(
                pk=second_calculated_submission.pk).value)
Beispiel #3
0
    def test_all_related_calculated_fields_recalculate(self):
        """Do all related calculated fields recalculate when self.value changes?
        """
        second_calculated_field = DocumentationFieldFactory(
            credit=self.credit,
            type="calculated",
            formula=self.calculated_field.formula)

        second_calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=second_calculated_field,
                credit_submission=self.credit_user_submission,
                value=self.calculated_submission.value))

        self.numeric_submission.value += 100
        self.numeric_submission.save()

        fresh_calculated_submission = NumericSubmission.objects.get(
            pk=self.calculated_submission.pk)

        self.assertEqual(self.numeric_submission.value,
                         fresh_calculated_submission.value)

        fresh_second_calculated_submission = NumericSubmission.objects.get(
            pk=second_calculated_submission.pk)

        self.assertEqual(self.numeric_submission.value,
                         fresh_second_calculated_submission.value)
Beispiel #4
0
 def test_save_recalculates_credit_test_submissions(self):
     """Does save recalculate submissions when formula changes?"""
     credit_submission = CreditTestSubmissionFactory(credit=self.credit)
     make_numeric_fields(self.credit, 3)
     # CreditSubmission.get_submission_fields() has a nasty side
     # effect -- it creates DocumentationFieldSubmissions for every
     # DocumentationField associated with CreditSubmission.Credit.
     # It's nasty because it gets called by the CreditSubmission's
     # __unicode__ method, which can get called anywhere, say, in
     # a log message, as it does if we try to just create a bunch
     # of NumericSubmissions for each DocumentationField, as we
     # do above, with CreditUserSubmission objects.  So we surrender.
     # Call the frickin' method here, and let it create the
     # NumericSubmissions.
     field_submissions = credit_submission.get_submission_fields()
     # Can't set the values of the DocumentationFieldSubmissions
     # created by get_submission_fields(), though, so we'll do that
     # here.
     for i in xrange(len(field_submissions)):
         field_submissions[i].value = i
         field_submissions[i].save()
     calculated_field = DocumentationField.objects.create(
         credit=self.credit, type='calculated', formula='value = A + B + C')
     calculated_field_submission = (
         NumericDocumentationFieldSubmissionFactory(
             documentation_field=calculated_field,
             credit_submission=credit_submission))
     calculated_field_submission.calculate()
     self.assertEqual(3, calculated_field_submission.value)
     calculated_field.formula = 'value = C - B - A'
     calculated_field.save()
     # We have a dirty read.
     calculated_field_submission = NumericSubmission.objects.get(
         pk=calculated_field_submission.pk)
     self.assertEqual(1, calculated_field_submission.value)
Beispiel #5
0
    def test_calculated_field_is_calculated_when_term_changes(self):
        credit = CreditFactory()
        first_term_field = DocumentationFieldFactory(credit=credit,
                                                     type='numeric',
                                                     identifier='AA')
        second_term_field = DocumentationFieldFactory(credit=credit,
                                                      type='numeric',
                                                      identifier='AB')
        calculated_field = DocumentationField.objects.create(
            credit=credit,
            type='calculated',
            formula='value = (AA or 0) + (AB or 0)')
        credit_submission = CreditUserSubmissionFactory(credit=credit)
        first_term_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=first_term_field,
            value=1)
        NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=second_term_field,
            value=2)
        calculated_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=calculated_field,
            value=None)

        first_term_submission.value = 10
        first_term_submission.save()

        # We have a dirty read.
        calculated_submission = NumericSubmission.objects.get(
            pk=calculated_submission.pk)

        self.assertEqual(12, calculated_submission.value)
Beispiel #6
0
    def setUp(self):
        self.credit_user_submission = CreditUserSubmissionFactory()
        self.credit = self.credit_user_submission.credit

        numeric_field = DocumentationFieldFactory(credit=self.credit,
                                                  type="numeric",
                                                  identifier="A")
        self.calculated_field = DocumentationFieldFactory(credit=self.credit,
                                                          type="calculated",
                                                          formula="value = A")

        self.calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=self.calculated_field,
                credit_submission=self.credit_user_submission,
                value=999))

        self.numeric_submission = NumericDocumentationFieldSubmissionFactory(
            documentation_field=numeric_field,
            credit_submission=self.credit_user_submission,
            value=0)
Beispiel #7
0
    def test_calculated_field_is_cleared_when_term_goes_invalid(self):
        credit = CreditFactory()
        term_field = DocumentationFieldFactory(credit=credit,
                                               type='numeric',
                                               identifier='BA')
        calculated_field = DocumentationField.objects.create(
            credit=credit, type='calculated', formula='value = BA')
        credit_submission = CreditUserSubmissionFactory(credit=credit)
        term_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=term_field,
            value=1)
        calculated_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=calculated_field,
            value=100)

        term_submission.value = None
        term_submission.save()

        # We have a dirty read.
        calculated_submission = NumericSubmission.objects.get(
            pk=calculated_submission.pk)

        self.assertEqual(None, calculated_submission.value)
Beispiel #8
0
class CalculatedFieldTest(TestCase):
    def setUp(self):
        self.credit_user_submission = CreditUserSubmissionFactory()
        self.credit = self.credit_user_submission.credit

        numeric_field = DocumentationFieldFactory(credit=self.credit,
                                                  type="numeric",
                                                  identifier="A")
        self.calculated_field = DocumentationFieldFactory(credit=self.credit,
                                                          type="calculated",
                                                          formula="value = A")

        self.calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=self.calculated_field,
                credit_submission=self.credit_user_submission,
                value=999))

        self.numeric_submission = NumericDocumentationFieldSubmissionFactory(
            documentation_field=numeric_field,
            credit_submission=self.credit_user_submission,
            value=0)

    def test_save_of_calculated_field(self):
        """Does saving a calculated field change its value?
        It shouldn't.
        """
        self.calculated_submission.save()
        fresh_calculated_submission = NumericSubmission.objects.get(
            id=self.calculated_submission.id)
        self.assertEqual(999, fresh_calculated_submission.value)

    def test_save_of_noncalculated_field(self):
        """Does saving a noncalculated field change a related calculated field?
        It should.
        """
        new_numeric_submission_value = self.numeric_submission.value + 10

        self.numeric_submission.value = new_numeric_submission_value
        self.numeric_submission.save()

        fresh_calculated_submission = NumericSubmission.objects.get(
            pk=self.calculated_submission.pk)

        self.assertEqual(new_numeric_submission_value,
                         fresh_calculated_submission.value)

    def test_calculated_field_is_calculated_when_term_changes(self):
        credit = CreditFactory()
        first_term_field = DocumentationFieldFactory(credit=credit,
                                                     type='numeric',
                                                     identifier='AA')
        second_term_field = DocumentationFieldFactory(credit=credit,
                                                      type='numeric',
                                                      identifier='AB')
        calculated_field = DocumentationField.objects.create(
            credit=credit,
            type='calculated',
            formula='value = (AA or 0) + (AB or 0)')
        credit_submission = CreditUserSubmissionFactory(credit=credit)
        first_term_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=first_term_field,
            value=1)
        NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=second_term_field,
            value=2)
        calculated_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=calculated_field,
            value=None)

        first_term_submission.value = 10
        first_term_submission.save()

        # We have a dirty read.
        calculated_submission = NumericSubmission.objects.get(
            pk=calculated_submission.pk)

        self.assertEqual(12, calculated_submission.value)

    def test_calculated_field_is_cleared_when_term_goes_invalid(self):
        credit = CreditFactory()
        term_field = DocumentationFieldFactory(credit=credit,
                                               type='numeric',
                                               identifier='BA')
        calculated_field = DocumentationField.objects.create(
            credit=credit, type='calculated', formula='value = BA')
        credit_submission = CreditUserSubmissionFactory(credit=credit)
        term_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=term_field,
            value=1)
        calculated_submission = NumericDocumentationFieldSubmissionFactory(
            credit_submission=credit_submission,
            documentation_field=calculated_field,
            value=100)

        term_submission.value = None
        term_submission.save()

        # We have a dirty read.
        calculated_submission = NumericSubmission.objects.get(
            pk=calculated_submission.pk)

        self.assertEqual(None, calculated_submission.value)

    def test_all_related_calculated_fields_recalculate(self):
        """Do all related calculated fields recalculate when self.value changes?
        """
        second_calculated_field = DocumentationFieldFactory(
            credit=self.credit,
            type="calculated",
            formula=self.calculated_field.formula)

        second_calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=second_calculated_field,
                credit_submission=self.credit_user_submission,
                value=self.calculated_submission.value))

        self.numeric_submission.value += 100
        self.numeric_submission.save()

        fresh_calculated_submission = NumericSubmission.objects.get(
            pk=self.calculated_submission.pk)

        self.assertEqual(self.numeric_submission.value,
                         fresh_calculated_submission.value)

        fresh_second_calculated_submission = NumericSubmission.objects.get(
            pk=second_calculated_submission.pk)

        self.assertEqual(self.numeric_submission.value,
                         fresh_second_calculated_submission.value)

    def test_calculated_field_as_formula_term_recalculates(self):
        """Does a calculated field in a formula recalculate?"""
        # test that calculated fields acting as formula terms are calculated
        # appropriately -- CF1 is term in CF2, T1 is term in CF1, test that
        # update of T1.value causes value of CF2 to be recalculated.
        second_calculated_field = DocumentationFieldFactory(
            credit=self.credit,
            type="calculated",
            formula="value = {}".format(self.calculated_field.identifier))

        second_calculated_submission = (
            NumericDocumentationFieldSubmissionFactory(
                documentation_field=second_calculated_field,
                credit_submission=self.credit_user_submission,
                value=5000))

        # Updating self.numeric_submission.value should cause
        # self.calculated_submission to calculate, which should
        # cause second_calculated_submission to calculate.

        self.numeric_submission.value += 100

        self.numeric_submission.save()

        self.assertEqual(
            NumericSubmission.objects.get(pk=self.numeric_submission.pk).value,
            NumericSubmission.objects.get(
                pk=second_calculated_submission.pk).value)