Ejemplo n.º 1
0
def test_new_unsupported_language(db):
    # Explicit mode
    with pytest.raises(models.ProgrammingLanguage.DoesNotExist):
        models.programming_language('foolang')

    # Silent mode
    lang = models.programming_language('foolang', raises=False)
    assert lang.ref == 'foolang'
    assert lang.name == 'Foolang'
    assert lang.is_supported is False
    assert lang.is_language is True
    assert lang.is_binary is False
Ejemplo n.º 2
0
    def load_markio(cls, source):
        """
        Creates a CodingIoQuestion object from a Markio object or source
        string and saves the resulting question in the database.

        This function can run without touching the database if the markio file
        does not define any information that should be saved in an answer key.

        Args:
            source:
                A string with the Markio source code.

        Returns:
            question:
                A question object.
        """

        import markio

        if isinstance(source, markio.Markio):
            data = source
        else:
            data = markio.parse_string(source)

        # Create question object from parsed markio data
        question = CodingIoQuestion.objects.create(
            title=data.title,
            author_name=data.author,
            timeout=data.timeout,
            short_description=data.short_description,
            long_description=data.description,
            iospec_source=data.tests,
        )

        # Add answer keys
        answer_keys = {}
        for (lang, answer_key) in data.answer_key.items():
            language = programming_language(lang)
            key = question.answer_keys.create(language=language,
                                              source=answer_key)
            answer_keys[lang] = key
        for (lang, placeholder) in data.placeholder.items():
            if placeholder is None:
                continue
            try:
                answer_keys[lang].placeholder = placeholder
                answer_keys[lang].save(update_fields=['placeholder'])
            except KeyError:
                language = ProgrammingLanguage.objects.get(lang)
                question.answer_keys.create(language=language,
                                            placeholder=placeholder)
        return question
Ejemplo n.º 3
0
    def respond_route(self, client, source=None, language=None, **kwargs):
        """
        Handles student responses via AJAX and a srvice program.
        """

        if not language:
            client.dialog('<p>Please select the correct language</p>')
            return

        # Bug with <ace-editor>?
        if not source or source == '\x01\x01':
            client.dialog('<p>Internal error: please send it again!</p>')
            return

        language = programming_language(language)
        self.bind(client.request, language=language, **kwargs)
        response = self.register_response_item(source, autograde=True)
        html = render_html(response.feedback)
        client.dialog(html)
Ejemplo n.º 4
0
 def __init__(self, *args, **kwargs):
     if 'language' in kwargs and isinstance(kwargs['language'], str):
         kwargs['language'] = programming_language(kwargs['language'])
     super().__init__(*args, **kwargs)
Ejemplo n.º 5
0
class CodingIoAnswerKeyFactory(factory.DjangoModelFactory):
    class Meta:
        model = models.AnswerKeyItem

    language = factory.LazyAttribute(lambda x: programming_language('python'))
    source = "print('hello', input('who? '))"
Ejemplo n.º 6
0
    def from_markio(cls, source, commit=None, return_keys=False):
        """Creates a CodingIoQuestion object from a Markio object r source
        string and saves the resulting question in the database.

        This function can run without touching the database if the markio file
        does not define any information that should be saved in an answer key.

        Args:
            source:
                A string with the Markio source code.
            commit (bool):
                If True (default), saves resulting question in the database.
            return_keys (bool):
                If True, also return a dictionary mapping language references
                to answer keys.

        Returns:
            question:
                A question object.
            [answer_keys]:
                A map from language references to :class:`AnswerKeyItem`
                objects.
        """

        import markio

        if isinstance(source, markio.Markio):
            data = source
        else:
            data = markio.parse_string(source)

        # Create question object from parsed markio data
        question = CodingIoQuestion(
            title=data.title,
            author_name=data.author,
            timeout=data.timeout,
            short_description=data.short_description,
            long_description=data.description,
            iospec_source=data.tests,
        )
        saving(question, commit)

        # Add answer keys
        answer_keys = {}
        for (lang, answer_key) in data.answer_key.items():
            language = programming_language(lang)
            key = saving(CodingIoAnswerKey(question=question,
                                           language=language,
                                           source=answer_key), commit)
            answer_keys[lang] = key
        for (lang, placeholder) in data.placeholder.items():
            if placeholder is None:
                continue
            try:
                answer_keys[lang].placeholder = placeholder
                saving(answer_keys[lang], commit, update_fields=['placeholder'])
            except KeyError:
                language = ProgrammingLanguage.objects.get(lang)
                key = CodingIoAnswerKey(question=question,
                                        language=language,
                                        placeholder=placeholder)
                saving(key, commit)

        # Question is done!
        if return_keys:
            answer_keys = {key.language.ref: key
                           for key in answer_keys.values()}
            return question, answer_keys
        return question
Ejemplo n.º 7
0
def test_get_language_support_most_common_languages(db):
    assert models.programming_language('python').name == 'Python 3.5'
    assert models.programming_language('python2').name == 'Python 2.7'
Ejemplo n.º 8
0
 def language(self, value):
     self.response_data['language'] = programming_language(value).ref
Ejemplo n.º 9
0
 def _language(self, language=None, raises=True):
     # Shortcut used internally to normalize the given language
     if language is None:
         return self.language or self.default_language
     return programming_language(language, raises)
Ejemplo n.º 10
0
 def language(self, value):
     self._language_bind = programming_language(value, raises=False)
Ejemplo n.º 11
0
import pytest
from codeschool.tests import *
from cs_core.factories import *
from cs_core import models
from cs_core.models import programming_language


@pytest.fixture
def user_with_profile():
    user = factory.UserFactory.create()
    user.profile.school_id = '12345'
    user.profile.gender = 'male'
    user.profile.about_me = 'i am ok'
    return (user, user.profile)


# Programming language fixtures
python = pytest.fixture(lambda: programming_language('python'))
c = pytest.fixture(lambda: programming_language('c'))
cpp = pytest.fixture(lambda: programming_language('cpp'))
ruby = pytest.fixture(lambda: programming_language('ruby'))