Example #1
0
 def setUp(self):
     self.service = ScoringService(0)
Example #2
0
 def setUp(self):
     self.service = ScoringService(0)
Example #3
0
class TestScoringService(unittest.TestCase):

    def setUp(self):
        self.service = ScoringService(0)

    # Testing new_evaluation.

    def test_new_evaluation(self):
        """One submission is scored.

        """
        score_info = self.new_score_info()
        sr = TestScoringService.new_sr_to_score()
        score_type = Mock()
        score_type.compute_score.return_value = score_info
        TestScoringService.set_up_db([sr], score_type)

        self.service.new_evaluation(123, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == [call(sr)]
        assert (sr.score,
                sr.score_details,
                sr.public_score,
                sr.public_score_details,
                sr.ranking_score_details) == score_info

    def test_new_evaluation_two(self):
        """More than one submissions in the queue.

        """
        score_type = Mock()
        score_type.compute_score.return_value = (1, "1", 2, "2", ["1", "2"])
        sr_a = TestScoringService.new_sr_to_score()
        sr_b = TestScoringService.new_sr_to_score()
        TestScoringService.set_up_db([sr_a, sr_b], score_type)

        self.service.new_evaluation(123, 456)
        self.service.new_evaluation(124, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == [call(sr_a), call(sr_b)]

    def test_new_evaluation_already_scored(self):
        """One submission is not re-scored if already scored.

        """
        sr = TestScoringService.new_sr_scored()
        score_type = Mock()
        score_type.compute_score.return_value = (1, "1", 2, "2", ["1", "2"])
        TestScoringService.set_up_db([sr], score_type)

        self.service.new_evaluation(123, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == []

    @staticmethod
    def new_sr_to_score():
        sr = Mock()
        sr.needs_scoring.return_value = True
        sr.scored.return_value = False
        return sr

    @staticmethod
    def new_sr_scored():
        sr = Mock()
        sr.needs_scoring.return_value = False
        sr.scored.return_value = True
        return sr

    @staticmethod
    def new_score_info():
        return (
            random.randint(1, 1000),
            str(random.randint(1, 1000)),
            random.randint(1, 1000),
            str(random.randint(1, 1000)),
            [str(random.randint(1, 1000)), str(random.randint(1, 1000))]
        )

    @staticmethod
    def set_up_db(srs, score_type):
        submission = Mock()
        submission.get_result = Mock(side_effect=srs)
        cms.service.ScoringService.Submission.get_from_id = \
            Mock(return_value=submission)
        cms.service.ScoringService.Dataset.get_from_id = \
            Mock(return_value=Mock())
        cms.service.ScoringService.get_score_type = \
            Mock(return_value=score_type)
Example #4
0
class TestScoringService(unittest.TestCase):

    def setUp(self):
        self.service = ScoringService(0)

    # Testing new_evaluation.

    def test_new_evaluation(self):
        """One submission is scored.

        """
        score_info = self.new_score_info()
        sr = TestScoringService.new_sr_to_score()
        score_type = Mock()
        score_type.compute_score.return_value = score_info
        TestScoringService.set_up_db([sr], score_type)

        self.service.new_evaluation(123, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == [call(sr)]
        assert (sr.score,
                sr.score_details,
                sr.public_score,
                sr.public_score_details,
                sr.ranking_score_details) == score_info

    def test_new_evaluation_two(self):
        """More than one submissions in the queue.

        """
        score_type = Mock()
        score_type.compute_score.return_value = (1, 1, 2, 2, ["1", "2"])
        sr_a = TestScoringService.new_sr_to_score()
        sr_b = TestScoringService.new_sr_to_score()
        TestScoringService.set_up_db([sr_a, sr_b], score_type)

        self.service.new_evaluation(123, 456)
        self.service.new_evaluation(124, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == [call(sr_a), call(sr_b)]

    def test_new_evaluation_already_scored(self):
        """One submission is not re-scored if already scored.

        """
        sr = TestScoringService.new_sr_scored()
        score_type = Mock()
        score_type.compute_score.return_value = (1, 1, 2, 2, ["1", "2"])
        TestScoringService.set_up_db([sr], score_type)

        self.service.new_evaluation(123, 456)

        gevent.sleep(0)  # Needed to trigger the score loop.
        # Asserts that compute_score was called.
        assert score_type.compute_score.mock_calls == []

    @staticmethod
    def new_sr_to_score():
        sr = Mock()
        sr.needs_scoring.return_value = True
        sr.scored.return_value = False
        return sr

    @staticmethod
    def new_sr_scored():
        sr = Mock()
        sr.needs_scoring.return_value = False
        sr.scored.return_value = True
        return sr

    @staticmethod
    def new_score_info():
        return (
            random.randint(1, 1000),
            random.randint(1, 1000),
            random.randint(1, 1000),
            random.randint(1, 1000),
            ["%d" % random.randint(1, 1000), "%d" % random.randint(1, 1000)]
        )

    @staticmethod
    def set_up_db(srs, score_type):
        submission = Mock()
        submission.get_result = Mock(side_effect=srs)
        cms.service.ScoringService.Submission.get_from_id = \
            Mock(return_value=submission)
        cms.service.ScoringService.Dataset.get_from_id = \
            Mock(return_value=Mock())
        cms.service.ScoringService.get_score_type = \
            Mock(return_value=score_type)