Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
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)