Example #1
0
 def test_mix_date_and_not_date(self):
     with self.assertRaises(AssertionError):
         rule_intermediary = CombinedQuerysetIntermediary(Rule.objects.all(), "date_added")
         rule_intermediary2 = CombinedQuerysetIntermediary(Rule.objects.all(), "label")
         CombinedQuerysetPaginator(
             intermediaries=[rule_intermediary, rule_intermediary2],
         )
Example #2
0
 def test_order_by_invalid_key(self):
     with self.assertRaises(AssertionError):
         rule_intermediary = CombinedQuerysetIntermediary(
             Rule.objects.all(), "dontexist")
         CombinedQuerysetPaginator(intermediaries=[rule_intermediary], )
Example #3
0
    def test_simple(self):
        Rule.objects.all().delete()

        alert_rule0 = self.create_alert_rule(name="alertrule0")
        alert_rule1 = self.create_alert_rule(name="alertrule1")
        rule1 = Rule.objects.create(label="rule1", project=self.project)
        alert_rule2 = self.create_alert_rule(name="alertrule2")
        alert_rule3 = self.create_alert_rule(name="alertrule3")
        rule2 = Rule.objects.create(label="rule2", project=self.project)
        rule3 = Rule.objects.create(label="rule3", project=self.project)

        alert_rule_intermediary = CombinedQuerysetIntermediary(
            AlertRule.objects.all(), "date_added")
        rule_intermediary = CombinedQuerysetIntermediary(
            Rule.objects.all(), "date_added")
        paginator = CombinedQuerysetPaginator(
            intermediaries=[alert_rule_intermediary, rule_intermediary],
            desc=True,
        )

        result = paginator.get_result(limit=3, cursor=None)
        assert len(result) == 3
        page1_results = list(result)
        assert page1_results[0].id == rule3.id
        assert page1_results[1].id == rule2.id
        assert page1_results[2].id == alert_rule3.id

        next_cursor = result.next
        result = paginator.get_result(limit=3, cursor=next_cursor)
        page2_results = list(result)
        assert len(result) == 3
        assert page2_results[0].id == alert_rule2.id
        assert page2_results[1].id == rule1.id
        assert page2_results[2].id == alert_rule1.id

        next_cursor = result.next
        prev_cursor = result.prev
        result = paginator.get_result(limit=3, cursor=next_cursor)
        page3_results = list(result)
        assert len(result) == 2
        assert page3_results[0].id == alert_rule0.id

        result = paginator.get_result(limit=3, cursor=prev_cursor)
        assert list(result) == page1_results

        # Test reverse sorting:
        paginator = CombinedQuerysetPaginator(
            intermediaries=[alert_rule_intermediary, rule_intermediary], )
        result = paginator.get_result(limit=3, cursor=None)
        assert len(result) == 3
        page1_results = list(result)
        assert page1_results[0].id == Rule.objects.all().first().id
        assert page1_results[1].id == alert_rule0.id
        assert page1_results[2].id == alert_rule1.id

        next_cursor = result.next
        result = paginator.get_result(limit=3, cursor=next_cursor)
        page2_results = list(result)
        assert len(result) == 3
        assert page2_results[0].id == rule1.id
        assert page2_results[1].id == alert_rule2.id
        assert page2_results[2].id == alert_rule3.id

        next_cursor = result.next
        prev_cursor = result.prev
        result = paginator.get_result(limit=3, cursor=next_cursor)
        page3_results = list(result)
        assert len(result) == 2
        assert page3_results[0].id == rule2.id
        assert page3_results[1].id == rule3.id

        result = paginator.get_result(limit=3, cursor=prev_cursor)
        assert list(result) == page1_results