def test_model_relations_api(self): """Test the calls used to move between the relations for the models.""" i = InterestFactory() lobbyist = LobbyistFactory.create() YEAR = 2000 try: # add an `Interest` to a `Lobbyist` annum = LobbyistAnnumFactory.create(lobbyist=lobbyist, year=YEAR) CompensationFactory.create(annum=annum, interest=i) # get all the `Interest`s for a `Lobbyist` Interest.objects.filter(compensation__annum__lobbyist=lobbyist) # get all the `Interest`s for a `Lobbyist` by year for year in lobbyist.years.all(): year.clients.all() # get all the `Interest`s for a `Lobbyist` for a year lobbyist.years.get(year=YEAR).clients.all() # get all the `Lobbyist`s for an `Interest` i.years_available.all().values('lobbyist') # get all the `Lobbyist`s for an `Interest` for a year i.years_available.filter(year=YEAR).values('lobbyist') except Exception as e: self.fail("Ha ha, %s" % e)
def test_make_stats_for_year_is_accurate(self): N = 10 # associate N `Lobbyist`s with it through `LobbyistAnnum` for i in range(N): annum = LobbyistAnnumFactory.create(year=self.year) CompensationFactory.create(annum=annum, interest=self.interest, amount_guess=i, amount_high=i * 2, amount_low=0) with self.assertNumQueries(5): # 1 to get the stats # 1 to GET # 1 to INSERT # 2 for transaction management self.interest.make_stats_for_year(self.year) stat = self.interest.stats.all().get(year=self.year) self.assertEqual(stat.guess, N * (N - 1) / 2) # math! self.assertEqual(stat.high, N * (N - 1)) self.assertEqual(stat.low, 0)
def test_make_stats_for_year_does_not_pick_up_other_years(self): N = 10 # associate N `Lobbyist`s with it through `LobbyistAnnum` for i in range(N): annum = LobbyistAnnumFactory.create(year=self.year) CompensationFactory.create(annum=annum, interest=self.interest, amount_guess=i, amount_high=i * 2, amount_low=0) self.interest.make_stats_for_year(self.year) # attempt to poison stats with extra data for i in range(N)[::2]: annum = LobbyistAnnumFactory.create(year=self.year + 1) CompensationFactory.create(annum=annum, interest=self.interest) self.interest.make_stats_for_year(self.year + 1) stat = self.interest.stats.all().get(year=self.year) self.assertEqual(stat.guess, N * (N - 1) / 2) # math! self.assertEqual(stat.high, N * (N - 1)) self.assertEqual(stat.low, 0)
def test_compensation_set_massive_works(self): c1 = CompensationFactory(interest=self.interest) c2 = CompensationFactory(interest__canonical=self.interest) self.assertIn(c1, self.interest.compensation_set.all()) self.assertNotIn(c2, self.interest.compensation_set.all()) self.assertIn(c2, self.interest.compensation_set_massive)
def test_compensation_set_massive_works_the_same_way(self): comp = CompensationFactory(interest=self.interest) with self.assertNumQueries(1): self.assertIn(comp, self.interest.compensation_set.all()) with self.assertNumQueries(1): self.assertIn(comp, self.interest.compensation_set_massive.all())
def test_get_all_addresses_is_distinct(self): a1 = AddressFactory() CompensationFactory(interest=self.interest, address=a1) CompensationFactory(interest=self.interest, address=a1) addresses = self.interest.get_all_addresses() self.assertEqual(addresses.count(), 1)