Exemplo n.º 1
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
Exemplo n.º 2
0
 def test_make_stats_for_year_does_nothing_for_noncanonical_interests(self):
     interest = InterestFactory(canonical=self.interest)
     year2000 = LobbyistAnnumFactory.create(year=2000)
     CompensationFactory(annum=year2000, interest=interest,
         amount_low=2000, amount_guess=2000, amount_high=2000)
     interest.make_stats_for_year(2000)
     # assert noncanonical interest did not have stats generated
     self.assertEqual(0, interest.stats.count())
     # assert canonical interest got the stats instead
     self.assertEqual(1, self.interest.stats.count())
Exemplo n.º 3
0
 def test_make_stats_for_year_does_nothing_for_noncanonical_interests(self):
     interest = InterestFactory(canonical=self.interest)
     year2000 = LobbyistAnnumFactory.create(year=2000)
     CompensationFactory(annum=year2000,
                         interest=interest,
                         amount_low=2000,
                         amount_guess=2000,
                         amount_high=2000)
     interest.make_stats_for_year(2000)
     # assert noncanonical interest did not have stats generated
     self.assertEqual(0, interest.stats.count())
     # assert canonical interest got the stats instead
     self.assertEqual(1, self.interest.stats.count())
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    def test_make_stats_sets_latest_address(self):
        old_address = AddressFactory(address1=u'old')
        new_address = AddressFactory(address1=u'new')
        interest = InterestFactory(address=old_address)

        CompensationFactory(annum__year=2000, interest=interest,
            address=old_address)
        CompensationFactory(annum__year=2001, interest=interest,
            address=new_address)

        # sanity check
        self.assertEqual(interest.address, old_address)
        with self.assertNumQueries(15):
            interest.make_stats()
        # assert interest got new address
        self.assertEqual(interest.address, new_address)
Exemplo n.º 7
0
    def test_make_stats_sets_latest_address(self):
        old_address = AddressFactory(address1=u'old')
        new_address = AddressFactory(address1=u'new')
        interest = InterestFactory(address=old_address)

        CompensationFactory(annum__year=2000,
                            interest=interest,
                            address=old_address)
        CompensationFactory(annum__year=2001,
                            interest=interest,
                            address=new_address)

        # sanity check
        self.assertEqual(interest.address, old_address)
        with self.assertNumQueries(15):
            interest.make_stats()
        # assert interest got new address
        self.assertEqual(interest.address, new_address)
Exemplo n.º 8
0
 def test_get_all_addresses_can_get_aliases(self):
     interest = InterestFactory(canonical=self.interest)
     a1 = AddressFactory()
     a2 = AddressFactory()
     CompensationFactory(interest=self.interest, address=a1)
     CompensationFactory(interest=interest, address=a2)
     addresses = self.interest.get_all_addresses(include_aliases=True)
     self.assertIn(a1, addresses)
     self.assertIn(a2, addresses)
     # property version too
     self.assertIn(a1, self.interest.address_set_massive)
     self.assertNotIn(a2, self.interest.address_set)
     self.assertIn(a2, self.interest.address_set_massive)
Exemplo n.º 9
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
Exemplo n.º 10
0
    def test_make_stats_for_year_takes_aliases_into_account(self):
        """
        This is just like test_make_stats_for_year_is_accurate, except we
        spread the lobbyists to a pool of interests that are all really the
        same interest.
        """
        num_lobbyists = random.randint(7, 13)
        num_interests = random.randint(2, 3)

        # assert we started off with 1 `Interest` (self.interest)
        self.assertEqual(Interest.objects.count(), 1)
        for i in range(num_interests):
            InterestFactory(canonical=self.interest)
        # sanity check
        self.assertEqual(self.interest.aliases.count(), num_interests)

        # associate num_lobbyists `Lobbyist`s with it through `LobbyistAnnum`
        for i in range(num_lobbyists):
            year = LobbyistAnnumFactory.create(year=self.year)
            interest = Interest.objects.all().order_by('?')[0]
            CompensationFactory(annum=year,
                                interest=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,
                         num_lobbyists * (num_lobbyists - 1) / 2)  # math!
        self.assertEqual(stat.high, num_lobbyists * (num_lobbyists - 1))
        self.assertEqual(stat.low, 0)
Exemplo n.º 11
0
 def test_canonical_field_works(self):
     a1 = InterestFactory(canonical=self.interest)
     self.assertIn(a1, self.interest.aliases.all())
Exemplo n.º 12
0
 def setUp(self):
     self.interest = InterestFactory()
     self.year = 2000
Exemplo n.º 13
0
class InterestTest(TestCase):
    def setUp(self):
        self.interest = InterestFactory()
        self.year = 2000

    def test_canonical_field_works(self):
        a1 = InterestFactory(canonical=self.interest)
        self.assertIn(a1, self.interest.aliases.all())

    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_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_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_make_stats_for_year_takes_aliases_into_account(self):
        """
        This is just like test_make_stats_for_year_is_accurate, except we
        spread the lobbyists to a pool of interests that are all really the
        same interest.
        """
        num_lobbyists = random.randint(7, 13)
        num_interests = random.randint(2, 3)

        # assert we started off with 1 `Interest` (self.interest)
        self.assertEqual(Interest.objects.count(), 1)
        for i in range(num_interests):
            InterestFactory(canonical=self.interest)
        # sanity check
        self.assertEqual(self.interest.aliases.count(), num_interests)

        # associate num_lobbyists `Lobbyist`s with it through `LobbyistAnnum`
        for i in range(num_lobbyists):
            year = LobbyistAnnumFactory.create(year=self.year)
            interest = Interest.objects.all().order_by('?')[0]
            CompensationFactory(annum=year,
                                interest=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,
                         num_lobbyists * (num_lobbyists - 1) / 2)  # math!
        self.assertEqual(stat.high, num_lobbyists * (num_lobbyists - 1))
        self.assertEqual(stat.low, 0)

    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_make_stats_for_year_does_nothing_for_noncanonical_interests(self):
        interest = InterestFactory(canonical=self.interest)
        year2000 = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000,
                            interest=interest,
                            amount_low=2000,
                            amount_guess=2000,
                            amount_high=2000)
        interest.make_stats_for_year(2000)
        # assert noncanonical interest did not have stats generated
        self.assertEqual(0, interest.stats.count())
        # assert canonical interest got the stats instead
        self.assertEqual(1, self.interest.stats.count())

    def test_make_stats_trivial_case_works(self):
        LobbyistAnnumFactory.create(year=self.year)
        # making stats for an interest with no compensation
        self.interest.make_stats()
        # assert interest did not have stats generated
        self.assertEqual(0, self.interest.stats.count())

    def test_make_stats_finds_all_years(self):
        year2000 = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000,
                            interest=self.interest,
                            amount_low=2000,
                            amount_guess=2000,
                            amount_high=2000)
        year2000b = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000b,
                            interest=self.interest,
                            amount_low=2000,
                            amount_guess=2000,
                            amount_high=2000)
        year2001 = LobbyistAnnumFactory.create(year=2001)
        CompensationFactory(annum=year2001,
                            interest=self.interest,
                            amount_low=2001,
                            amount_guess=2001,
                            amount_high=2001)
        year2004 = LobbyistAnnumFactory.create(year=2004)
        CompensationFactory(annum=year2004,
                            interest=self.interest,
                            amount_low=2004,
                            amount_guess=2004,
                            amount_high=2004)
        with self.assertNumQueries(20):
            self.interest.make_stats()
        # assert stats are generated
        self.assertFalse(self.interest.stats.filter(year=1999).exists())
        self.assertTrue(self.interest.stats.filter(year=2000).exists())
        self.assertTrue(self.interest.stats.filter(year=2001).exists())
        self.assertFalse(self.interest.stats.filter(year=2002).exists())
        self.assertFalse(self.interest.stats.filter(year=2003).exists())
        self.assertTrue(self.interest.stats.filter(year=2004).exists())
        self.assertFalse(self.interest.stats.filter(year=2005).exists())

    def test_make_stats_sets_latest_address(self):
        old_address = AddressFactory(address1=u'old')
        new_address = AddressFactory(address1=u'new')
        interest = InterestFactory(address=old_address)

        CompensationFactory(annum__year=2000,
                            interest=interest,
                            address=old_address)
        CompensationFactory(annum__year=2001,
                            interest=interest,
                            address=new_address)

        # sanity check
        self.assertEqual(interest.address, old_address)
        with self.assertNumQueries(15):
            interest.make_stats()
        # assert interest got new address
        self.assertEqual(interest.address, new_address)

    def test_get_all_addresses_works(self):
        a1 = AddressFactory()
        a2 = AddressFactory()
        CompensationFactory(interest=self.interest, address=a1)
        CompensationFactory(interest=self.interest, address=a2)
        addresses = self.interest.get_all_addresses()
        self.assertIn(a1, addresses)
        self.assertIn(a2, addresses)
        # property version too
        self.assertIn(a1, self.interest.address_set)
        self.assertIn(a2, self.interest.address_set)

    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)

    def test_get_all_addresses_can_get_aliases(self):
        interest = InterestFactory(canonical=self.interest)
        a1 = AddressFactory()
        a2 = AddressFactory()
        CompensationFactory(interest=self.interest, address=a1)
        CompensationFactory(interest=interest, address=a2)
        addresses = self.interest.get_all_addresses(include_aliases=True)
        self.assertIn(a1, addresses)
        self.assertIn(a2, addresses)
        # property version too
        self.assertIn(a1, self.interest.address_set_massive)
        self.assertNotIn(a2, self.interest.address_set)
        self.assertIn(a2, self.interest.address_set_massive)
Exemplo n.º 14
0
 def setUp(self):
     self.interest = InterestFactory()
     self.year = 2000
Exemplo n.º 15
0
class InterestTest(TestCase):
    def setUp(self):
        self.interest = InterestFactory()
        self.year = 2000

    def test_canonical_field_works(self):
        a1 = InterestFactory(canonical=self.interest)
        self.assertIn(a1, self.interest.aliases.all())

    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_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_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_make_stats_for_year_takes_aliases_into_account(self):
        """
        This is just like test_make_stats_for_year_is_accurate, except we
        spread the lobbyists to a pool of interests that are all really the
        same interest.
        """
        num_lobbyists = random.randint(7, 13)
        num_interests = random.randint(2, 3)

        # assert we started off with 1 `Interest` (self.interest)
        self.assertEqual(Interest.objects.count(), 1)
        for i in range(num_interests):
            InterestFactory(canonical=self.interest)
        # sanity check
        self.assertEqual(self.interest.aliases.count(), num_interests)

        # associate num_lobbyists `Lobbyist`s with it through `LobbyistAnnum`
        for i in range(num_lobbyists):
            year = LobbyistAnnumFactory.create(year=self.year)
            interest = Interest.objects.all().order_by('?')[0]
            CompensationFactory(annum=year, interest=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, num_lobbyists * (num_lobbyists - 1) / 2)  # math!
        self.assertEqual(stat.high, num_lobbyists * (num_lobbyists - 1))
        self.assertEqual(stat.low, 0)

    def test_make_stats_for_year_does_nothing_for_noncanonical_interests(self):
        interest = InterestFactory(canonical=self.interest)
        year2000 = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000, interest=interest,
            amount_low=2000, amount_guess=2000, amount_high=2000)
        interest.make_stats_for_year(2000)
        # assert noncanonical interest did not have stats generated
        self.assertEqual(0, interest.stats.count())
        # assert canonical interest got the stats instead
        self.assertEqual(1, self.interest.stats.count())

    def test_make_stats_finds_all_years(self):
        year2000 = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000, interest=self.interest,
            amount_low=2000, amount_guess=2000, amount_high=2000)
        year2000b = LobbyistAnnumFactory.create(year=2000)
        CompensationFactory(annum=year2000b, interest=self.interest,
            amount_low=2000, amount_guess=2000, amount_high=2000)
        year2001 = LobbyistAnnumFactory.create(year=2001)
        CompensationFactory(annum=year2001, interest=self.interest,
            amount_low=2001, amount_guess=2001, amount_high=2001)
        year2004 = LobbyistAnnumFactory.create(year=2004)
        CompensationFactory(annum=year2004, interest=self.interest,
            amount_low=2004, amount_guess=2004, amount_high=2004)
        with self.assertNumQueries(20):
            self.interest.make_stats()
        # assert stats are generated
        self.assertFalse(self.interest.stats.filter(year=1999).exists())
        self.assertTrue(self.interest.stats.filter(year=2000).exists())
        self.assertTrue(self.interest.stats.filter(year=2001).exists())
        self.assertFalse(self.interest.stats.filter(year=2002).exists())
        self.assertFalse(self.interest.stats.filter(year=2003).exists())
        self.assertTrue(self.interest.stats.filter(year=2004).exists())
        self.assertFalse(self.interest.stats.filter(year=2005).exists())

    def test_make_stats_sets_latest_address(self):
        old_address = AddressFactory(address1=u'old')
        new_address = AddressFactory(address1=u'new')
        interest = InterestFactory(address=old_address)

        CompensationFactory(annum__year=2000, interest=interest,
            address=old_address)
        CompensationFactory(annum__year=2001, interest=interest,
            address=new_address)

        # sanity check
        self.assertEqual(interest.address, old_address)
        with self.assertNumQueries(15):
            interest.make_stats()
        # assert interest got new address
        self.assertEqual(interest.address, new_address)

    def test_get_all_addresses_works(self):
        a1 = AddressFactory()
        a2 = AddressFactory()
        CompensationFactory(interest=self.interest, address=a1)
        CompensationFactory(interest=self.interest, address=a2)
        addresses = self.interest.get_all_addresses()
        self.assertIn(a1, addresses)
        self.assertIn(a2, addresses)
        # property version too
        self.assertIn(a1, self.interest.address_set)
        self.assertIn(a2, self.interest.address_set)

    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)

    def test_get_all_addresses_can_get_aliases(self):
        interest = InterestFactory(canonical=self.interest)
        a1 = AddressFactory()
        a2 = AddressFactory()
        CompensationFactory(interest=self.interest, address=a1)
        CompensationFactory(interest=interest, address=a2)
        addresses = self.interest.get_all_addresses(include_aliases=True)
        self.assertIn(a1, addresses)
        self.assertIn(a2, addresses)
        # property version too
        self.assertIn(a1, self.interest.address_set_massive)
        self.assertNotIn(a2, self.interest.address_set)
        self.assertIn(a2, self.interest.address_set_massive)