def test_interest_compensation_report_queryset(self): """Get a list of `Interest`s sorted by their bankroll of `Lobbyist`s.""" YEAR = 2000 for i in range(10): LobbyistFactory.create() InterestFactory.create() InterestFactory.create() for i in Interest.objects.all(): lobbyist = Lobbyist.objects.all().order_by('?')[0] try: annum = LobbyistAnnum.objects.get(lobbyist=lobbyist, year=YEAR) except LobbyistAnnum.DoesNotExist: annum = LobbyistAnnumFactory.create(lobbyist=lobbyist, year=YEAR) CompensationFactory(annum=annum, interest=i) # denormalize interest stats i.make_stats_for_year(YEAR) # use `__exact`, django ORM attempts to evaluate __year as a date lookup for stat in InterestStats.objects.filter( year__exact=YEAR).order_by('-guess'): # print stat stat
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_lobbyist_compensation_relation_api(self): """More tests about getting between `Lobbyist`s and `Compensation`.""" # Make a `Lobbyist` with NUM_CLIENTS clients. NUM_CLIENTS = 3 lobbyist = LobbyistFactory.create() annum = LobbyistAnnumFactory.create(lobbyist=lobbyist) for x in range(0, NUM_CLIENTS): CompensationFactory(annum=annum) try: # number of years of data we have for a lobbyist self.assertEqual(lobbyist.years.count(), 1) # number of clients a lobbyist had that year self.assertEqual(annum.clients.count(), NUM_CLIENTS) self.assertEqual(annum.compensation_set.count(), NUM_CLIENTS) # make sure we can .annotate income onto the queryset for year in lobbyist.years.all().annotate( income=Sum('compensation__amount_guess')): income = 0 for compensation in year.compensation_set.all(): income += compensation.amount_guess self.assertEqual(income, year.income) except Exception as e: self.fail("Ha ha, %s" % e)
def test_make_stats_for_year_works_with_duplicate_lobbyists(self): """ When a lobbyist lists two clients that are actually the same, they may get combined. The lobbyist's compensation should be counted twice. """ num_interests = 4 # assert we started off with 1 `Interest` (self.interest) self.assertEqual(Interest.objects.count(), 1) for __ in range(num_interests): InterestFactory(canonical=self.interest) # sanity check self.assertEqual(self.interest.aliases.count(), num_interests) self.assertEqual(Interest.objects.count(), num_interests + 1) # assign the same lobbyist to all of them lobbyist = LobbyistFactory() annum = LobbyistAnnumFactory(lobbyist=lobbyist, year=self.year) for idx, interest in enumerate(Interest.objects.all()): CompensationFactory(annum=annum, interest=interest, amount_guess=idx, amount_high=idx * 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, num_interests * (num_interests + 1) / 2) self.assertEqual(stat.high, num_interests * (num_interests + 1)) self.assertEqual(stat.low, 0)
def test_interest_compensation_report_queryset(self): """Get a list of `Interest`s sorted by their bankroll of `Lobbyist`s.""" YEAR = 2000 for i in range(10): LobbyistFactory.create() InterestFactory.create() InterestFactory.create() for i in Interest.objects.all(): lobbyist = Lobbyist.objects.all().order_by('?')[0] try: annum = LobbyistAnnum.objects.get(lobbyist=lobbyist, year=YEAR) except LobbyistAnnum.DoesNotExist: annum = LobbyistAnnumFactory.create(lobbyist=lobbyist, year=YEAR) CompensationFactory(annum=annum, interest=i) # denormalize interest stats i.make_stats_for_year(YEAR) # use `__exact`, django ORM attempts to evaluate __year as a date lookup for stat in InterestStats.objects.filter(year__exact=YEAR).order_by( '-guess'): # print stat stat
def test_lobbyist_compensation_report_queryset(self): """Get a list of `Lobbyist`s sorted by their paycheck.""" NUM_CLIENTS = 3 YEAR = 2000 for i in range(10): lobbyist = LobbyistFactory.create() annum = LobbyistAnnumFactory.create(lobbyist=lobbyist, year=YEAR) for x in range(0, NUM_CLIENTS): CompensationFactory(annum=annum) # Here's a queryset to get the best paid `Lobbyist`s in a year qs = LobbyistAnnum.objects.filter(year=YEAR).annotate( income=Sum('compensation__amount_guess')).order_by('-income') for year in qs: # print year.lobbyist, year.income year
def setUp(self): self.lobbyist = LobbyistFactory()
class LobbyistTest(TestCase): def setUp(self): self.lobbyist = LobbyistFactory() def test_address_history_trivial_case_starts_is_empty(self): history = self.lobbyist.get_address_history() self.assertEqual(len(history), 0) def test_address_history_works_simple(self): RegistrationReportFactory(lobbyist=self.lobbyist) history = self.lobbyist.get_address_history() self.assertEqual(len(history), 1) entry = history[0] self.assertIsInstance(entry.address, Address) self.assertIsInstance(entry[0], Address) self.assertIsInstance(entry.registration, RegistrationReport) self.assertIsInstance(entry[1], RegistrationReport) def test_address_history_works_advanced(self): a1 = AddressFactory() a2 = AddressFactory() a3 = AddressFactory() RegistrationReportFactory(lobbyist=self.lobbyist, address=a1, year=2000) RegistrationReportFactory(lobbyist=self.lobbyist, address=a3, year=2002) RegistrationReportFactory(lobbyist=self.lobbyist, address=a3, year=2001) RegistrationReportFactory(lobbyist=self.lobbyist, address=a2, year=2003) history = self.lobbyist.get_address_history() # assert handles repeated address by collapsing it self.assertEqual(len(history), 3) # assert pulls address in chronological order self.assertEqual(history[0].address, a1) self.assertEqual(history[1].address, a3) self.assertEqual(history[2].address, a2) def test_make_stats_does_nothing_with_no_coversheets(self): self.assertEqual(self.lobbyist.coversheets.count(), 0) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 0) def test_make_stats_is_accurate(self): CoversheetFactory(lobbyist=self.lobbyist, report_date='2000-01-15', year=2000, spent_guess=100) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 1) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 100) CoversheetFactory.create(lobbyist=self.lobbyist, report_date='2000-02-15', year=2000, spent_guess=200) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 1) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 300) CoversheetFactory.create(lobbyist=self.lobbyist, year=2001, spent_guess=400, transportation=1) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 2) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 300) self.assertEqual(self.lobbyist.stats.get(year=2001).spent_guess, 400) self.assertEqual(self.lobbyist.stats.get(year=2001).transportation, 1)
class LobbyistTest(TestCase): def setUp(self): self.lobbyist = LobbyistFactory() def test_address_history_trivial_case_starts_is_empty(self): history = self.lobbyist.get_address_history() self.assertEqual(len(history), 0) def test_address_history_works_simple(self): RegistrationReportFactory(lobbyist=self.lobbyist) history = self.lobbyist.get_address_history() self.assertEqual(len(history), 1) entry = history[0] self.assertIsInstance(entry.address, Address) self.assertIsInstance(entry[0], Address) self.assertIsInstance(entry.registration, RegistrationReport) self.assertIsInstance(entry[1], RegistrationReport) def test_address_history_works_advanced(self): a1 = AddressFactory() a2 = AddressFactory() a3 = AddressFactory() RegistrationReportFactory(lobbyist=self.lobbyist, address=a1, year=2000) RegistrationReportFactory(lobbyist=self.lobbyist, address=a3, year=2002) RegistrationReportFactory(lobbyist=self.lobbyist, address=a3, year=2001) RegistrationReportFactory(lobbyist=self.lobbyist, address=a2, year=2003) history = self.lobbyist.get_address_history() # assert handles repeated address by collapsing it self.assertEqual(len(history), 3) # assert pulls address in chronological order self.assertEqual(history[0].address, a1) self.assertEqual(history[1].address, a3) self.assertEqual(history[2].address, a2) def test_make_stats_does_nothing_with_no_coversheets(self): self.assertEqual(self.lobbyist.coversheets.count(), 0) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 0) def test_make_stats_is_accurate(self): CoversheetFactory( lobbyist=self.lobbyist, year=2000, spent_guess=100) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 1) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 100) CoversheetFactory.create(lobbyist=self.lobbyist, year=2000, spent_guess=200) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 1) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 300) CoversheetFactory.create(lobbyist=self.lobbyist, year=2001, spent_guess=400, transportation=1) self.lobbyist.make_stats() self.assertEqual(self.lobbyist.stats.count(), 2) self.assertEqual(self.lobbyist.stats.get(year=2000).spent_guess, 300) self.assertEqual(self.lobbyist.stats.get(year=2001).spent_guess, 400) self.assertEqual(self.lobbyist.stats.get(year=2001).transportation, 1)