<commit_before># -*- coding: utf-8 -*-
"""
Tests for leaderboard handlers in Open Assessment XBlock.
"""
import json
import mock
from django.core.cache import cache
from submissions import api as sub_api
from .base import XBlockHandlerTransactionTestCase, scenario
from django.test.utils import override_settings
from moto import mock_s3
import boto
from boto.s3.key import Key
from openassessment.fileupload import api

class TestLeaderboardRender(XBlockHandlerTransactionTestCase):

    @scenario('data/basic_scenario.xml')
    def test_no_leaderboard(self, xblock):
        # Since there's no leaderboard set in the problem XML,
        # it should not be visible
        self._assert_leaderboard_visible(xblock, False)

    @scenario('data/leaderboard_unavailable.xml')
    def test_unavailable(self, xblock):
        # Start date is in the future for this scenario
        self._assert_path_and_context(
            xblock,
            'openassessmentblock/leaderboard/oa_leaderboard_waiting.html',
            {}
        )
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_show_no_submissions(self, xblock):
        # No submissions created yet, so the leaderboard shouldn't display any scores
        self._assert_scores(xblock, [])
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_show_submissions(self, xblock):

        # Create some submissions (but fewer than the max that can be shown)
        self._create_submissions_and_scores(xblock, [
            ("test answer 1", 1),
            ("test answer 2", 2)
        ])
        self._assert_scores(xblock, [
            {"content": "test answer 2", "score": 2},
            {"content": "test answer 1", "score": 1}
        ])
        self._assert_leaderboard_visible(xblock, True)

        # Since leaderboard results are cached, we need to clear
        # the cache in order to see the new scores.
        cache.clear()

        # Create more submissions than the max
        self._create_submissions_and_scores(xblock, [
            ("test answer 3", 0),
            ("test answer 4", 10),
            ("test answer 5", 3)
        ])
        self._assert_scores(xblock, [
            {"content": "test answer 4", "score": 10},
            {"content": "test answer 5", "score": 3},
            {"content": "test answer 2", "score": 2}
        ])
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_no_text_key_submission(self, xblock):
        # Instead of using the default submission as a dict with "text",
        # make the submission a string.
        self._create_submissions_and_scores(xblock, [("test answer", 1)], submission_key=None)

        # It should still work
        self._assert_scores(xblock, [
            {"content": "test answer", "score": 1}
        ])

    @scenario('data/leaderboard_show.xml')
    def test_non_text_submission(self, xblock):
        # Create a non-text submission (the submission dict doesn't contain "text")
        self._create_submissions_and_scores(xblock, [("s3key", 1)], submission_key="file_key")

        # Expect that we default to an empty string for content
        self._assert_scores(xblock, [
            {"content": "", "score": 1, "file": ""}
        ])

    @mock_s3
    @override_settings(
        AWS_ACCESS_KEY_ID='foobar',
        AWS_SECRET_ACCESS_KEY='bizbaz',
        FILE_UPLOAD_STORAGE_BUCKET_NAME="mybucket"
    )
    @scenario('data/leaderboard_show_allowfiles.xml')
    def test_image_and_text_submission(self, xblock):
        # Create a file and get the download URL<commit_msg>Update leaderboard test for no text submission<commit_after># -*- coding: utf-8 -*-
"""
Tests for leaderboard handlers in Open Assessment XBlock.
"""
import json
import mock
from django.core.cache import cache
from submissions import api as sub_api
from .base import XBlockHandlerTransactionTestCase, scenario
from django.test.utils import override_settings
from moto import mock_s3
import boto
from boto.s3.key import Key
from openassessment.fileupload import api

class TestLeaderboardRender(XBlockHandlerTransactionTestCase):

    @scenario('data/basic_scenario.xml')
    def test_no_leaderboard(self, xblock):
        # Since there's no leaderboard set in the problem XML,
        # it should not be visible
        self._assert_leaderboard_visible(xblock, False)

    @scenario('data/leaderboard_unavailable.xml')
    def test_unavailable(self, xblock):
        # Start date is in the future for this scenario
        self._assert_path_and_context(
            xblock,
            'openassessmentblock/leaderboard/oa_leaderboard_waiting.html',
            {}
        )
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_show_no_submissions(self, xblock):
        # No submissions created yet, so the leaderboard shouldn't display any scores
        self._assert_scores(xblock, [])
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_show_submissions(self, xblock):

        # Create some submissions (but fewer than the max that can be shown)
        self._create_submissions_and_scores(xblock, [
            ("test answer 1", 1),
            ("test answer 2", 2)
        ])
        self._assert_scores(xblock, [
            {"content": "test answer 2", "score": 2},
            {"content": "test answer 1", "score": 1}
        ])
        self._assert_leaderboard_visible(xblock, True)

        # Since leaderboard results are cached, we need to clear
        # the cache in order to see the new scores.
        cache.clear()

        # Create more submissions than the max
        self._create_submissions_and_scores(xblock, [
            ("test answer 3", 0),
            ("test answer 4", 10),
            ("test answer 5", 3)
        ])
        self._assert_scores(xblock, [
            {"content": "test answer 4", "score": 10},
            {"content": "test answer 5", "score": 3},
            {"content": "test answer 2", "score": 2}
        ])
        self._assert_leaderboard_visible(xblock, True)

    @scenario('data/leaderboard_show.xml')
    def test_no_text_key_submission(self, xblock):
        # Instead of using the default submission as a dict with "text",
        # make the submission a string.
        self._create_submissions_and_scores(xblock, [("test answer", 1)], submission_key=None)

        # It should still work
        self._assert_scores(xblock, [
            {"content": "test answer", "score": 1}
        ])

    @mock_s3
    @override_settings(
        AWS_ACCESS_KEY_ID='foobar',
        AWS_SECRET_ACCESS_KEY='bizbaz',
        FILE_UPLOAD_STORAGE_BUCKET_NAME="mybucket"
    )
    @scenario('data/leaderboard_show.xml')
    def test_non_text_submission(self, xblock):
        # Create a mock bucket
        conn = boto.connect_s3()
        bucket = conn.create_bucket('mybucket')
        # Create a non-text submission (the submission dict doesn't contain "text")
        self._create_submissions_and_scores(xblock, [("s3key", 1)], submission_key="file_key")

        # Expect that we default to an empty string for content
        self._assert_scores(xblock, [
            {"content": "", "score": 1, "file": ""}
        ])

    @mock_s3
    @override_settings(
        AWS_ACCESS_KEY_ID='foobar',
        AWS_SECRET_ACCESS_KEY='bizbaz',
        FILE_UPLOAD_STORAGE_BUCKET_NAME="mybucket"
    )
    @scenario('data/leaderboard_show_allowfiles.xml')
    def test_image_and_text_submission(self, xblock):
        # Create a file and get the download URL
{'fraction_non_alphanumeric': 0.10035005834305717, 'fraction_numerical': 0.008297679242836769, 'mean_word_length': 2.860860860860861, 'pattern_counts': {'":': 30, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816900', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2218, 'n_words': 598}