def setUp(self, *args, **kwargs): super(CreditSubmissionHistoryViewTest, self).setUp(*args, **kwargs) category = CategoryFactory(creditset=self.submission.creditset) category.save() category_submission = CategorySubmissionFactory( category=category, submissionset=self.submission) self.category_abbreviation = category.abbreviation subcategory = SubcategoryFactory(category=category) subcategory.save() subcategory_submission = SubcategorySubmissionFactory( subcategory=subcategory, category_submission=category_submission) self.subcategory_slug = subcategory.slug credit = CreditFactory(subcategory=subcategory) credit.save() credit_submission = CreditUserSubmissionFactory( credit=credit, subcategory_submission=subcategory_submission) self.credit_identifier = credit.identifier # Some history to show: documentation_field = DocumentationFieldFactory(credit=credit) documentation_field.save() documentation_field_submission = DocumentationFieldSubmissionFactory( documentation_field=documentation_field, credit_submission=credit_submission) documentation_field_submission.save()
def test_get_formula_terms_empty_formula(self): """Does get_formula_terms handle an empty formula?""" calculated_field = DocumentationFieldFactory(credit=self.credit, type='calculated') calculated_field.formula = '' formula_terms = calculated_field.get_formula_terms() self.assertItemsEqual([], formula_terms)
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_get_formula_terms_unparseable_formula(self): """Does get_formula_terms handle an unparseable formula?""" calculated_field = DocumentationFieldFactory(credit=self.credit, type='calculated') calculated_field.formula = 'value = (bogus }[' formula_terms = calculated_field.get_formula_terms() self.assertItemsEqual([], formula_terms)
def test_migrate_creditset(self): """Does migrate_creditset make a faithful copy of a Creditset? """ # One CreditSet. cs = CreditSetFactory() # Three Categories. first_category = CategoryFactory(creditset=cs) second_category = CategoryFactory(creditset=cs) CategoryFactory(creditset=cs) # Three Subcategories. first_subcategory = SubcategoryFactory(category=first_category) second_subcategory = SubcategoryFactory(category=second_category) SubcategoryFactory(category=second_category) # Two Credits. first_credit = CreditFactory(subcategory=first_subcategory) second_credit = CreditFactory(subcategory=second_subcategory) # Four Documentation Fields. DocumentationFieldFactory(credit=first_credit) DocumentationFieldFactory(credit=first_credit) DocumentationFieldFactory(credit=second_credit) DocumentationFieldFactory(credit=second_credit) new_cs = migrate_creditset(cs, "99.0", date.today()) self.assertEqual(new_cs.category_set.count(), cs.category_set.count()) self.assertEqual( Subcategory.objects.filter(category__creditset=new_cs).count(), Subcategory.objects.filter(category__creditset=cs).count()) self.assertEqual( Credit.objects.filter( subcategory__category__creditset=new_cs).count(), Credit.objects.filter( subcategory__category__creditset=cs).count()) self.assertEqual( ApplicabilityReason.objects.filter( credit__subcategory__category__creditset=new_cs).count(), ApplicabilityReason.objects.filter( credit__subcategory__category__creditset=cs).count()) self.assertEqual( DocumentationField.objects.filter( credit__subcategory__category__creditset=new_cs).count(), DocumentationField.objects.filter( credit__subcategory__category__creditset=cs).count()) self.assertEqual( Choice.objects.filter( documentation_field__credit__subcategory__category__creditset=new_cs).count(), Choice.objects.filter( documentation_field__credit__subcategory__category__creditset=cs).count()) self.assertEqual( CreditTestSubmission.objects.filter( credit__subcategory__category__creditset=new_cs).count(), CreditTestSubmission.objects.filter( credit__subcategory__category__creditset=cs).count())
def test_get_formula_terms_calculated_field(self): """Does get_formula_terms handle calculated fields?""" calculated_field = DocumentationFieldFactory(credit=self.credit, type='calculated') calculated_field.formula = 'value = A + B + C / D * (E / F)' formula_terms = calculated_field.get_formula_terms() self.assertItemsEqual(['value', 'A', 'B', 'C', 'D', 'E', 'F'], formula_terms)
def test_get_fields_for_terms(self): """Does get_fields_for_terms work on a sunny day?""" calculated_field = DocumentationFieldFactory(credit=self.credit, type='calculated') fields_for_terms = make_numeric_fields(self.credit, 3) fields_for_terms_identifiers = [ df.identifier for df in fields_for_terms ] fields = calculated_field.get_fields_for_terms( fields_for_terms_identifiers) self.assertItemsEqual(fields_for_terms, fields)
def setUp(self): super(SRT87Test, self).setUp() # initialize the creditset and institution self.institution = InstitutionFactory() self.account = StarsAccountFactory(institution=self.institution, user_level='admin') self.request.user = self.account.user self.field = DocumentationFieldFactory(type='numeric', required=True, min_range=0) # Set up titles for the url self.field.credit.identifier = u"CR-1" self.field.credit.validation_rules = ( "if A < 0: errors['B'] = 'you have errors'") self.field.credit.save() self.credit = self.field.credit self.subcategory = self.credit.subcategory self.subcategory.title = u"subcategory" self.subcategory.save() self.subcategory.category.abbreviation = "CAT" self.subcategory.category.save() self.category = self.subcategory.category tabular_field_dict = { "numRows": 1, "fields": [['']], "rowHeadings": ["Row 1"], "colHeadings": ["Col 1"], "numCols": 1 } self.tabular_field = DocumentationFieldFactory( type='tabular', required=True, tabular_fields=tabular_field_dict, credit=self.credit) # create a submission set to work with self.submission = init_submissionset(self.institution, self.account.user) self.rp = ResponsiblePartyFactory(institution=self.institution) self.view_kwargs = { "institution_slug": self.institution.slug, "submissionset": str(self.submission.id), "category_abbreviation": self.category.abbreviation, "subcategory_slug": self.subcategory.slug, "credit_identifier": self.credit.identifier }
def setUp(self): # create an initial creditset and submission self.cs1 = CreditSetFactory( version="2.0", release_date=date.today() - timedelta(days=2)) self.cat1 = CategoryFactory(creditset=self.cs1) self.sub1 = SubcategoryFactory(category=self.cat1) self.cred1 = CreditFactory(subcategory=self.sub1) self.df1 = DocumentationFieldFactory( credit=self.cred1, type='numeric', title="doc field 1") self.ss1 = SubmissionSetFactory(creditset=self.cs1) self.m_unit = UnitFactory( name="met", equivalent=self.df1.units, is_metric=True ) self.df1.units.name = "imp" self.df1.units.equivalent = self.m_unit self.df1.units.save() # SubmissionSetFactory doesn't seem to create a numeric submission # self.assertEqual(NumericSubmission.objects.count(), 1) # fails self.ns1 = NumericSubmissionFactory( documentation_field=self.df1, credit_submission=CreditSubmission.objects.all()[0] ) # Now created a migrated version of both self.cs2 = migrate_creditset( self.cs1, '2.1', date.today() - timedelta(days=1)) self.ss2 = create_ss_mirror( self.ss1, new_creditset=self.cs2) # create some old one that won't have versions with others # create an initial creditset and submission self.cs0 = CreditSetFactory( version="1.2", release_date=date.today() - timedelta(days=3)) self.cat0 = CategoryFactory(creditset=self.cs0) self.sub0 = SubcategoryFactory(category=self.cat0) self.cred0 = CreditFactory(subcategory=self.sub0) self.df0 = DocumentationFieldFactory( credit=self.cred0, type='numeric', title="doc field 1") self.ss0 = SubmissionSetFactory(creditset=self.cs0) self.ns0 = NumericSubmissionFactory( documentation_field=self.df0, credit_submission=CreditSubmission.objects.all()[2] ) self.setUpScores()
def setUp(self): self.us_units = Unit(is_metric=False, ratio=.25) self.us_units.save() self.metric_units = Unit(is_metric=True, ratio=4, equivalent=self.us_units) self.metric_units.save() self.us_units.equivalent = self.metric_units self.us_units.save() self.credit_user_submission = CreditUserSubmissionFactory() self.documentation_field = DocumentationFieldFactory()
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_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)
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 setUp(self): super(ValueErrorTest, self).setUp() self.institution = InstitutionFactory() self.account = StarsAccountFactory(institution=self.institution, user_level='admin') self.request.user = self.account.user self.field = DocumentationFieldFactory(type='boolean') self.submission = init_submissionset(self.institution, self.account.user) self.field.credit.identifier = u"CR-1" self.field.credit.save() self.credit = self.field.credit self.subcategory = self.credit.subcategory self.subcategory.title = u"subcategory" self.subcategory.save() self.subcategory.category.abbreviation = "CAT" self.subcategory.category.save() self.category = self.subcategory.category self.rp = ResponsiblePartyFactory(institution=self.institution) self.view_kwargs = { "institution_slug": self.institution.slug, "submissionset": str(self.submission.id), "category_abbreviation": self.category.abbreviation, "subcategory_slug": self.subcategory.slug, "credit_identifier": self.credit.identifier }
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 make_numeric_fields(credit, num): fields = [] for i in xrange(num): fields.append(DocumentationFieldFactory(credit=credit, type='numeric')) return fields
def test_get_fields_for_terms_empty_terms_list(self): """Does get_fields_for_terms work with an empty terms list?""" calculated_field = DocumentationFieldFactory(credit=self.credit, type='calculated') fields = calculated_field.get_fields_for_terms(terms_list=[]) self.assertEqual(0, fields.count())
class NumericSubmissionTest(TestCase): def setUp(self): self.us_units = Unit(is_metric=False, ratio=.25) self.us_units.save() self.metric_units = Unit(is_metric=True, ratio=4, equivalent=self.us_units) self.metric_units.save() self.us_units.equivalent = self.metric_units self.us_units.save() self.credit_user_submission = CreditUserSubmissionFactory() self.documentation_field = DocumentationFieldFactory() def set_institution_prefers_metric_system(self, what): institution = self.credit_user_submission.get_institution() institution.prefers_metric_system = what institution.save() def test_save_store_us_value_when_using_metric(self): """Does save() store US value when using metric? """ self.set_institution_prefers_metric_system(True) self.documentation_field.units = self.metric_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, metric_value=100) numeric_submission.save() self.assertEqual(400, numeric_submission.value) def test_save_store_metric_value_when_not_using_metric(self): """Does save() store metric value when not using metric? """ self.set_institution_prefers_metric_system(False) self.documentation_field.units = self.us_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, value=100) numeric_submission.save() self.assertEqual(25, numeric_submission.metric_value) def test_save_handle_metric_value_is_none_using_metric(self): """Does save() handle it when the metric_value is None? (Using metric system.) """ self.set_institution_prefers_metric_system(True) self.documentation_field.units = self.metric_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, metric_value=None) numeric_submission.save() self.assertEqual(None, numeric_submission.metric_value) def test_save_handle_metric_value_is_none_not_using_metric(self): """Does save() handle it when the metric_value is None? (Not using metric system.) """ self.set_institution_prefers_metric_system(False) self.documentation_field.units = self.us_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, metric_value=None) numeric_submission.save() self.assertEqual(None, numeric_submission.metric_value) def test_save_handle_value_is_none_using_metric(self): """Does save() handle it when the value is None? (Using metric system.) """ self.set_institution_prefers_metric_system(True) self.documentation_field.units = self.metric_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, value=None) numeric_submission.save() self.assertEqual(None, numeric_submission.value) def test_save_handle_value_is_none_not_using_metric(self): """Does save() handle it when the value is None? (Not using metric system.) """ self.set_institution_prefers_metric_system(False) self.documentation_field.units = self.us_units self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, value=None) numeric_submission.save() self.assertEqual(None, numeric_submission.value) def test_save_without_units_succeeds(self): """Is save() ok when the doc field has no units? """ self.documentation_field.units = None self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, value=100) numeric_submission.save() self.assertEqual(100, numeric_submission.value) def test_save_without_units_succeeds_using_metric(self): """Is save() ok when the doc field has no units and using metric? """ self.set_institution_prefers_metric_system(True) self.documentation_field.units = None self.documentation_field.save() numeric_submission = NumericSubmission( documentation_field=self.documentation_field, credit_submission=self.credit_user_submission, metric_value=100) numeric_submission.save() self.assertEqual(100, numeric_submission.metric_value)
def test_get_formula_terms_not_calculated_field(self): """Does get_formula_terms handle non-calculated fields?""" non_calculated_field = DocumentationFieldFactory(credit=self.credit) formula_terms = non_calculated_field.get_formula_terms() self.assertItemsEqual([], formula_terms)