def test_overall_quality(self): popularity = self._popularity(59) rating = self._rating(4) irrelevant = self._measurement("Some other quantity", 42, self.source, 1) pop = popularity.normalized_value rat = rating.normalized_value assert 0.5 == pop assert 1.0 / 3 == rat l = [popularity, rating, irrelevant] quality = Measurement.overall_quality(l) assert (0.7 * rat) + (0.3 * pop) == quality # Mess with the weights. assert (0.5 * rat) + (0.5 * pop) == Measurement.overall_quality( l, 0.5, 0.5) # Adding a non-popularity measurement that is _equated_ to # popularity via a percentile scale modifies the # normalized value -- we don't care exactly how, only that # it's taken into account. oclc = DataSource.lookup(self._db, DataSource.OCLC) popularityish = self._measurement(Measurement.HOLDINGS, 400, oclc, 10) new_quality = Measurement.overall_quality(l + [popularityish]) assert quality != new_quality
def test_overall_quality_with_popularity_and_quality_but_not_rating(self): pop = self._popularity(4) qual = self._quality(0.5) # We would expect the final quality score to be 1/2 of the quality # score we got from the metadata wrangler, and 1/2 of the normalized # value of the 4-star rating. expect = (pop.normalized_value / 2) + (0.5 / 2) assert expect == Measurement.overall_quality([pop, qual], 0.5, 0.5)
def test_overall_quality_with_popularity_quality_and_rating(self): pop = self._popularity(4) rat = self._rating(4) quality_score = 0.66 qual = self._quality(quality_score) # The popularity and rating are scaled appropriately and # added together. expect_1 = (pop.normalized_value * 0.75) + (rat.normalized_value * 0.25) # Then the whole thing is divided in half and added to half of the # quality score expect_total = expect_1 / 2 + (quality_score / 2) assert expect_total == Measurement.overall_quality([pop, rat, qual], 0.75, 0.25)
def test_overall_quality_is_zero_if_no_relevant_measurements(self): irrelevant = self._measurement("Some other quantity", 42, self.source, 1) assert 0 == Measurement.overall_quality([irrelevant])
def test_overall_quality_takes_weights_into_account(self): rating1 = self._rating(10, weight=10) rating2 = self._rating(1, weight=1) assert 0.91 == round(Measurement.overall_quality([rating1, rating2]), 2)
def test_overall_quality_based_solely_on_popularity_if_no_rating(self): pop = self._popularity(59) assert 0.5 == Measurement.overall_quality([pop])
def _measurement(self, quantity, value, source, weight): source = source or self.source return Measurement(data_source=source, quantity_measured=quantity, value=value, weight=weight)