Exemple #1
0
    def test_three_best_answers_one_poll_two_plain_conflict(self):
        """three threads with best answer, thread with poll and two plain threads conflict"""
        best_answers = [self.create_best_answer_thread() for i in range(3)]
        polls = [self.create_poll_thread()]
        threads = [
            self.create_plain_thread(),
            self.create_plain_thread(),
        ] + best_answers + polls

        merge_conflict = MergeConflict(threads=threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer'])

        # without choice, conflict lists resolutions
        try:
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer'])
            self.assertEqual(e.detail, {
                'best_answers': [['0', 'Unmark all best answers']] + [
                    [
                        str(thread.id),
                        thread.title,
                    ] for thread in best_answers
                ]
            })

        # conflict validates choice
        try:
            merge_conflict = MergeConflict({'best_answer': threads[0].id}, threads)
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(e.detail, {'best_answer': ['Invalid choice.']})

        # conflict returns selected resolution
        merge_conflict = MergeConflict({'best_answer': best_answers[0].id}, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': best_answers[0],
            'poll': polls[0].poll,
        })

        # conflict returns no-choice resolution
        merge_conflict = MergeConflict({'best_answer': 0}, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': None,
            'poll': polls[0].poll,
        })
Exemple #2
0
 def test_one_best_answer_one_poll(self):
     """thread with best answer and thread with poll don't conflict"""
     threads = [
         self.create_poll_thread(),
         self.create_best_answer_thread(),
     ]
     merge_conflict = MergeConflict(threads=threads)
     self.assertFalse(merge_conflict.is_merge_conflict())
Exemple #3
0
    def test_plain_threads_no_conflicts(self):
        """threads without items of interest don't conflict"""
        threads = [self.create_plain_thread() for i in range(10)]
        merge_conflict = MergeConflict(threads=threads)
        self.assertFalse(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), [])

        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {})
Exemple #4
0
    def test_one_poll_one_plain(self):
        """thread with poll and plain thread don't conflict"""
        threads = [
            self.create_poll_thread(),
            self.create_plain_thread(),
        ]
        merge_conflict = MergeConflict(threads=threads)
        self.assertFalse(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), [])

        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'poll': threads[0].poll,
        })
Exemple #5
0
    def test_three_best_answers_three_polls_two_plain_conflict(self):
        """multiple conflict is handled"""
        best_answers = [self.create_best_answer_thread() for i in range(3)]
        polls = [self.create_poll_thread() for i in range(3)]
        threads = [
            self.create_plain_thread(),
            self.create_plain_thread(),
        ] + best_answers + polls

        merge_conflict = MergeConflict(threads=threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])

        # without choice, conflict lists all resolutions
        try:
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])
            self.assertEqual(e.detail, {
                'best_answers': [['0', 'Unmark all best answers']] + [
                    [
                        str(thread.id),
                        thread.title,
                    ] for thread in best_answers
                ],
                'polls': [['0', 'Delete all polls']] + [
                    [
                        str(thread.poll.id),
                        u'{} ({})'.format(thread.poll.question, thread.title),
                    ] for thread in polls
                ]
            })

        # conflict validates all choices if single choice was given
        try:
            merge_conflict = MergeConflict({'best_answer': threads[0].id}, threads)
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(e.detail, {
                'best_answer': ['Invalid choice.'],
                'poll': ['Invalid choice.'],
            })

        try:
            merge_conflict = MergeConflict({'poll': threads[0].id}, threads)
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(e.detail, {
                'best_answer': ['Invalid choice.'],
                'poll': ['Invalid choice.'],
            })

        # conflict validates all choices if all choices were given
        try:
            merge_conflict = MergeConflict({
                'best_answer': threads[0].id,
                'poll': threads[0].id,
            }, threads)
            merge_conflict.is_valid(raise_exception=True)
            self.fail("merge_conflict.is_valid() should raise ValidationError")
        except ValidationError as e:
            self.assertTrue(merge_conflict.is_merge_conflict())
            self.assertEqual(e.detail, {
                'best_answer': ['Invalid choice.'],
                'poll': ['Invalid choice.'],
            })

        # conflict returns selected resolutions
        valid_choices = {'best_answer': best_answers[0].id, 'poll': polls[0].poll.id}
        merge_conflict = MergeConflict(valid_choices, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': best_answers[0],
            'poll': polls[0].poll,
        })

        # conflict returns no-choice resolution
        merge_conflict = MergeConflict({'best_answer': 0, 'poll': 0}, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': None,
            'poll': None,
        })

        # conflict allows mixing no-choice with choice
        merge_conflict = MergeConflict({'best_answer': best_answers[0].id, 'poll': 0}, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': best_answers[0],
            'poll': None,
        })

        merge_conflict = MergeConflict({'best_answer': 0, 'poll': polls[0].poll.id}, threads)
        self.assertTrue(merge_conflict.is_merge_conflict())
        self.assertEqual(merge_conflict.get_conflicting_fields(), ['best_answer', 'poll'])
        merge_conflict.is_valid(raise_exception=True)
        self.assertEqual(merge_conflict.get_resolution(), {
            'best_answer': None,
            'poll': polls[0].poll,
        })