Ejemplo n.º 1
0
 def _setup_db(self):
     dbpath = self._init_db()
     gb = Gradebook(dbpath)
     gb.add_assignment("ps1", duedate="2015-02-02 14:58:23.948203 PST")
     gb.add_student("foo")
     gb.add_student("bar")
     return dbpath
Ejemplo n.º 2
0
def gradebook(request, tempdir):
    # create a "class files" directory
    origdir = os.getcwd()
    os.mkdir("class_files")
    os.chdir("class_files")

    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "source", "user_guide", "source")
    submitted_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "source", "user_guide", "submitted")

    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), submitted_path), "submitted")

    # create the gradebook
    gb = Gradebook("sqlite:///gradebook.db")
    gb.add_assignment("Problem Set 1")
    gb.add_student("Bitdiddle", first_name="Ben", last_name="B")
    gb.add_student("Hacker", first_name="Alyssa", last_name="H")
    gb.add_student("Reasoner", first_name="Louis", last_name="R")

    # run nbgrader assign
    run_command([
        "nbgrader", "assign", "Problem Set 1",
        "--IncludeHeaderFooter.header=source/header.ipynb"
    ])

    # run the autograder
    run_command(["nbgrader", "autograde", "Problem Set 1"])

    def fin():
        os.chdir(origdir)
        shutil.rmtree("class_files")
    request.addfinalizer(fin)

    return gb
Ejemplo n.º 3
0
def test_find_nonexistant_notebook(gradebook: Gradebook) -> None:
    # check that it doesn't find it when there is nothing in the db
    with pytest.raises(MissingEntry):
        gradebook.find_notebook('p1', 'foo')

    # check that it doesn't find it even if the assignment exists
    gradebook.add_assignment('foo')
    with pytest.raises(MissingEntry):
        gradebook.find_notebook('p1', 'foo')
Ejemplo n.º 4
0
def test_update_or_create_solution_cell(gradebook: Gradebook) -> None:
    # first test creating it
    gradebook.add_assignment('foo')
    gradebook.add_notebook('p1', 'foo')
    sc1 = gradebook.update_or_create_solution_cell('test1', 'p1', 'foo')
    assert gradebook.find_solution_cell('test1', 'p1', 'foo') == sc1

    # now test finding/updating it
    sc2 = gradebook.update_or_create_solution_cell('test1', 'p1', 'foo')
    assert sc1 == sc2
Ejemplo n.º 5
0
    def test_save_cells(self, db):
        """Ensure cells are saved into the database"""
        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")

        gb = Gradebook(db)
        gb.add_assignment("ps1")

        run_command('nbgrader assign ps1 --db="{}"'.format(db))

        notebook = gb.find_notebook("test", "ps1")
        assert len(notebook.grade_cells) == 8
Ejemplo n.º 6
0
def assignment(gradebook: Gradebook) -> Gradebook:
    gradebook.add_assignment('foo')
    gradebook.add_notebook('p1', 'foo')
    gradebook.add_grade_cell('test1', 'p1', 'foo', max_score=1, cell_type='code')
    gradebook.add_grade_cell('test2', 'p1', 'foo', max_score=2, cell_type='markdown')
    gradebook.add_solution_cell('solution1', 'p1', 'foo')
    gradebook.add_solution_cell('test2', 'p1', 'foo')
    gradebook.add_source_cell('test1', 'p1', 'foo', cell_type='code')
    gradebook.add_source_cell('test2', 'p1', 'foo', cell_type='markdown')
    gradebook.add_source_cell('solution1', 'p1', 'foo', cell_type='code')
    return gradebook
    def test_save_cells(self, db):
        """Ensure cells are saved into the database"""
        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")

        gb = Gradebook(db)
        gb.add_assignment("ps1")

        run_command(["nbgrader", "assign", "ps1", "--db", db])

        notebook = gb.find_notebook("test", "ps1")
        assert len(notebook.grade_cells) == 6
Ejemplo n.º 8
0
    def init_assignment(self, assignment_id, student_id):
        super(AssignApp, self).init_assignment(assignment_id, student_id)

        # try to get the assignment from the database, and throw an error if it
        # doesn't exist
        gb = Gradebook(self.db_url)
        try:
            gb.find_assignment(assignment_id)
        except MissingEntry:
            if self.create_assignment:
                self.log.warning("Creating assignment '%s'", assignment_id)
                gb.add_assignment(assignment_id)
            else:
                self.fail("No assignment called '%s' exists in the database", assignment_id)
Ejemplo n.º 9
0
    def test_save_cells(self):
        """Ensure cells are saved into the database"""
        with self._temp_cwd(["files/test.ipynb"]):
            os.makedirs('source/ps1')
            shutil.move("test.ipynb", "source/ps1/test.ipynb")

            dbpath = self._init_db()
            gb = Gradebook(dbpath)
            gb.add_assignment("ps1")

            self._run_command('nbgrader assign ps1 --db="{}"'.format(dbpath))

            notebook = gb.find_notebook("test", "ps1")
            assert_equal(len(notebook.grade_cells), 8)
Ejemplo n.º 10
0
    def test_add_remove_extra_notebooks(self, db):
        """Are extra notebooks added and removed?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        notebook1 = gb.find_notebook("test", "ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test2.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db, "--force"])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 2
        gb.db.refresh(notebook1)
        notebook2 = gb.find_notebook("test2", "ps1")

        os.remove("source/ps1/test2.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db, "--force"])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        gb.db.refresh(notebook1)
        with pytest.raises(InvalidRequestError):
            gb.db.refresh(notebook2)
Ejemplo n.º 11
0
    def test_add_remove_extra_notebooks(self, db):
        """Are extra notebooks added and removed?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command('nbgrader assign ps1 --db="{}"'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        notebook1 = gb.find_notebook("test", "ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test2.ipynb")
        run_command('nbgrader assign ps1 --db="{}" --force'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 2
        gb.db.refresh(notebook1)
        notebook2 = gb.find_notebook("test2", "ps1")

        os.remove("source/ps1/test2.ipynb")
        run_command('nbgrader assign ps1 --db="{}" --force'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        gb.db.refresh(notebook1)
        with pytest.raises(InvalidRequestError):
            gb.db.refresh(notebook2)
Ejemplo n.º 12
0
def assignmentWithTask(gradebook: Gradebook) -> Gradebook:
    for f in ['foo', 'foo2']:
        gradebook.add_assignment(f)
        for n in ['p1', 'p2']:
            gradebook.add_notebook(n, f)
            gradebook.add_solution_cell('solution1', n, f)
            gradebook.add_solution_cell('test2', n, f)
            gradebook.add_source_cell('test1', n, f, cell_type='code')
            gradebook.add_source_cell('test2', n, f, cell_type='markdown')
            gradebook.add_source_cell('solution1', n, f, cell_type='code')
            gradebook.add_grade_cell('grade_code1', n, f, cell_type='code', max_score=1)
            gradebook.add_grade_cell('grade_code2', n, f, cell_type='code', max_score=10)
            gradebook.add_grade_cell('grade_written1', n, f, cell_type='markdown', max_score=1)
            gradebook.add_grade_cell('grade_written2', n, f, cell_type='markdown', max_score=10)
            gradebook.add_task_cell('task1', n, f, cell_type='markdown', max_score=2)
            gradebook.add_task_cell('task2', n, f, cell_type='markdown', max_score=20)

    return gradebook
Ejemplo n.º 13
0
    def init_assignment(self, assignment_id, student_id):
        super(AssignApp, self).init_assignment(assignment_id, student_id)

        # try to get the assignment from the database, and throw an error if it
        # doesn't exist
        gb = Gradebook(self.db_url)
        try:
            gb.find_assignment(assignment_id)
        except MissingEntry:
            if self.create_assignment:
                self.log.warning("Creating assignment '%s'", assignment_id)
                gb.add_assignment(assignment_id)
            else:
                self.fail("No assignment called '%s' exists in the database", assignment_id)
        else:
            # check if there are any extra notebooks in the db that are no longer
            # part of the assignment, and if so, remove them
            if self.notebook_id == "*":
                self._clean_old_notebooks(assignment_id, student_id)
Ejemplo n.º 14
0
def class_files(request, tempdir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), "source")

    # create a fake ps1
    os.mkdir(os.path.join("source", "ps1"))
    with open(os.path.join("source", "ps1", "problem 1.ipynb"), "w") as fh:
        write_nb(new_notebook(), fh, 4)

    # create the gradebook
    gb = Gradebook("sqlite:///gradebook.db")
    gb.add_assignment("Problem Set 1")
    gb.add_assignment("ps1")
    gb.add_student("Bitdiddle", first_name="Ben", last_name="B")
    gb.add_student("Hacker", first_name="Alyssa", last_name="H")
    gb.add_student("Reasoner", first_name="Louis", last_name="R")

    return tempdir
Ejemplo n.º 15
0
    def init_assignment(self, assignment_id, student_id):
        super(AssignApp, self).init_assignment(assignment_id, student_id)

        # try to get the assignment from the database, and throw an error if it
        # doesn't exist
        gb = Gradebook(self.db_url)
        try:
            gb.find_assignment(assignment_id)
        except MissingEntry:
            if self.create_assignment:
                self.log.warning("Creating assignment '%s'", assignment_id)
                gb.add_assignment(assignment_id)
            else:
                self.fail("No assignment called '%s' exists in the database",
                          assignment_id)
        else:
            # check if there are any extra notebooks in the db that are no longer
            # part of the assignment, and if so, remove them
            if self.notebook_id == "*":
                self._clean_old_notebooks(assignment_id, student_id)
Ejemplo n.º 16
0
def gradebook(request, tempdir):
    # create a "class files" directory
    origdir = os.getcwd()
    os.mkdir("class_files")
    os.chdir("class_files")

    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                               "docs", "source", "user_guide", "source")
    submitted_path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                                  "docs", "source", "user_guide", "submitted")

    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path),
                    "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), submitted_path),
                    "submitted")

    # create the gradebook
    gb = Gradebook("sqlite:///gradebook.db")
    gb.add_assignment("Problem Set 1")
    gb.add_student("Bitdiddle", first_name="Ben", last_name="B")
    gb.add_student("Hacker", first_name="Alyssa", last_name="H")
    gb.add_student("Reasoner", first_name="Louis", last_name="R")

    # run nbgrader assign
    run_command([
        "nbgrader", "assign", "Problem Set 1",
        "--IncludeHeaderFooter.header=source/header.ipynb"
    ])

    # run the autograder
    run_command(["nbgrader", "autograde", "Problem Set 1"])

    def fin():
        os.chdir(origdir)
        shutil.rmtree("class_files")

    request.addfinalizer(fin)

    return gb
Ejemplo n.º 17
0
    def test_add_extra_notebooks_with_submissions(self, db):
        """Is an error thrown when new notebooks are added and there are existing submissions?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command('nbgrader assign ps1 --db="{}"'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1

        gb.add_student("hacker123")
        gb.add_submission("ps1", "hacker123")

        self._copy_file("files/test.ipynb", "source/ps1/test2.ipynb")
        run_command('nbgrader assign ps1 --db="{}" --force'.format(db), retcode=1)
Ejemplo n.º 18
0
    def test_add_extra_notebooks_with_submissions(self, db):
        """Is an error thrown when new notebooks are added and there are existing submissions?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1

        gb.add_student("hacker123")
        gb.add_submission("ps1", "hacker123")

        self._copy_file("files/test.ipynb", "source/ps1/test2.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db, "--force"],
                    retcode=1)
Ejemplo n.º 19
0
    def test_remove_extra_notebooks_with_submissions(self, db):
        """Is an error thrown when notebooks are removed and there are existing submissions?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        self._copy_file("files/test.ipynb", "source/ps1/test2.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 2

        gb.add_student("hacker123")
        gb.add_submission("ps1", "hacker123")

        os.remove("source/ps1/test2.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db, "--force"], retcode=1)
Ejemplo n.º 20
0
    def test_same_notebooks_with_submissions(self, db):
        """Is it ok to run nbgrader assign with the same notebooks and existing submissions?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command('nbgrader assign ps1 --db="{}"'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        notebook = assignment.notebooks[0]

        gb.add_student("hacker123")
        submission = gb.add_submission("ps1", "hacker123")
        submission_notebook = submission.notebooks[0]

        run_command('nbgrader assign ps1 --db="{}" --force'.format(db))

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        gb.db.refresh(notebook)
        gb.db.refresh(submission)
        gb.db.refresh(submission_notebook)
Ejemplo n.º 21
0
    def test_same_notebooks_with_submissions(self, db):
        """Is it ok to run nbgrader assign with the same notebooks and existing submissions?"""
        gb = Gradebook(db)
        assignment = gb.add_assignment("ps1")

        self._copy_file("files/test.ipynb", "source/ps1/test.ipynb")
        run_command(["nbgrader", "assign", "ps1", "--db", db])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        notebook = assignment.notebooks[0]

        gb.add_student("hacker123")
        submission = gb.add_submission("ps1", "hacker123")
        submission_notebook = submission.notebooks[0]

        run_command(["nbgrader", "assign", "ps1", "--db", db, "--force"])

        gb.db.refresh(assignment)
        assert len(assignment.notebooks) == 1
        gb.db.refresh(notebook)
        gb.db.refresh(submission)
        gb.db.refresh(submission_notebook)
Ejemplo n.º 22
0
def gradebook(db):
    gb = Gradebook(db)
    gb.add_assignment("ps0")
    gb.add_student("bar")
    return gb
Ejemplo n.º 23
0
def gradebook(db):
    gb = Gradebook(db)
    gb.add_assignment("ps1", duedate="2015-02-02 14:58:23.948203 PST")
    gb.add_student("foo")
    gb.add_student("bar")
    return db
Ejemplo n.º 24
0
#! /usr/bin/env python3

import os

# remove an existing database
if os.path.exists("gradebook.db"):
    os.remove("gradebook.db")

# create a connection to the db using the nbgrader API
from nbgrader.api import Gradebook
gb = Gradebook("sqlite:///gradebook.db")

# add the assignment to the database and specify a due date
gb.add_assignment("ProblemSet1")#, duedate="2015-02-01 15:00:00.000000 PST")
Ejemplo n.º 25
0
def gradebook(db):
    gb = Gradebook(db)
    gb.add_assignment("ps0")
    return gb
Ejemplo n.º 26
0
def gradebook(db):
    gb = Gradebook(db)
    gb.add_assignment("ps1", duedate="2015-02-02 14:58:23.948203 PST")
    gb.add_student("foo")
    gb.add_student("bar")
    return db
Ejemplo n.º 27
0
class TestSaveAutoGrades(TestBase):

    def setup(self):
        super(TestSaveAutoGrades, self).setup()
        db_url = self._init_db()
        self.gb = Gradebook(db_url)
        self.gb.add_assignment("ps0")
        self.gb.add_student("bar")

        self.preprocessor1 = SaveCells()
        self.preprocessor2 = SaveAutoGrades()
        self.resources = {
            "nbgrader": {
                "db_url": db_url,
                "assignment": "ps0",
                "notebook": "test",
                "student": "bar"
            }
        }

    def test_grade_correct_code(self):
        """Is a passing code cell correctly graded?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        self.preprocessor2.preprocess(nb, self.resources)

        grade_cell = self.gb.find_grade("foo", "test", "ps0", "bar")
        assert_equal(grade_cell.score, 1)
        assert_equal(grade_cell.max_score, 1)
        assert_equal(grade_cell.auto_score, 1)
        assert_equal(grade_cell.manual_score, None)
        assert not grade_cell.needs_manual_grade

    def test_grade_incorrect_code(self):
        """Is a failing code cell correctly graded?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        cell.outputs = [new_output('error', ename="NotImplementedError", evalue="", traceback=["error"])]
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        self.preprocessor2.preprocess(nb, self.resources)

        grade_cell = self.gb.find_grade("foo", "test", "ps0", "bar")
        assert_equal(grade_cell.score, 0)
        assert_equal(grade_cell.max_score, 1)
        assert_equal(grade_cell.auto_score, 0)
        assert_equal(grade_cell.manual_score, None)
        assert not grade_cell.needs_manual_grade

    def test_grade_unchanged_markdown(self):
        """Is an unchanged markdown cell correctly graded?"""
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        self.preprocessor2.preprocess(nb, self.resources)

        grade_cell = self.gb.find_grade("foo", "test", "ps0", "bar")
        assert_equal(grade_cell.score, 0)
        assert_equal(grade_cell.max_score, 1)
        assert_equal(grade_cell.auto_score, 0)
        assert_equal(grade_cell.manual_score, None)
        assert not grade_cell.needs_manual_grade

    def test_grade_changed_markdown(self):
        """Is a changed markdown cell correctly graded?"""
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        cell.source = "hello!"
        self.preprocessor2.preprocess(nb, self.resources)

        grade_cell = self.gb.find_grade("foo", "test", "ps0", "bar")
        assert_equal(grade_cell.score, 0)
        assert_equal(grade_cell.max_score, 1)
        assert_equal(grade_cell.auto_score, None)
        assert_equal(grade_cell.manual_score, None)
        assert grade_cell.needs_manual_grade

    def test_comment_unchanged_code(self):
        """Is an unchanged code cell given the correct comment?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        self.preprocessor2.preprocess(nb, self.resources)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, "No response.")

    def test_comment_changed_code(self):
        """Is a changed code cell given the correct comment?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        cell.source = "hello!"
        self.preprocessor2.preprocess(nb, self.resources)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, None)

    def test_comment_unchanged_markdown(self):
        """Is an unchanged markdown cell given the correct comment?"""
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        self.preprocessor2.preprocess(nb, self.resources)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, "No response.")

    def test_comment_changed_markdown(self):
        """Is a changed markdown cell given the correct comment?"""
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        self.preprocessor1.preprocess(nb, self.resources)
        self.gb.add_submission("ps0", "bar")
        cell.source = "hello!"
        self.preprocessor2.preprocess(nb, self.resources)

        comment = self.gb.find_comment(0, "test", "ps0", "bar")
        assert_equal(comment.comment, None)
Ejemplo n.º 28
0
class TestOverwriteCells(TestBase):

    def setup(self):
        super(TestOverwriteCells, self).setup()
        db_url = self._init_db()
        self.gb = Gradebook(db_url)
        self.gb.add_assignment("ps0")

        self.preprocessor1 = SaveCells()
        self.preprocessor2 = OverwriteCells()
        self.resources = {
            "nbgrader": {
                "db_url": db_url,
                "assignment": "ps0",
                "notebook": "test"
            }
        }

    def test_overwrite_points(self):
        """Are points overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.metadata.nbgrader["points"] = 2
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.metadata.nbgrader["points"], 1)

    def test_overwrite_grade_source(self):
        """Is the source overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.source, "hello")

    def test_dont_overwrite_grade_and_solution_source(self):
        """Is the source not overwritten for grade+solution cells?"""
        cell = self._create_grade_and_solution_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

    def test_dont_overwrite_solution_source(self):
        """Is the source not overwritten for solution cells?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.source = "hello!"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.source, "hello!")

    def test_overwrite_grade_cell_type(self):
        """Is the cell type overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.cell_type = "markdown"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.cell_type, "code")

    def test_overwrite_grade_cell_type(self):
        """Is the cell type overwritten for solution cells?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.cell_type = "markdown"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.cell_type, "code")

    def test_overwrite_grade_checksum(self):
        """Is the checksum overwritten for grade cells?"""
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.metadata.nbgrader["checksum"] = "1234"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.metadata.nbgrader["checksum"], compute_checksum(cell))

    def test_overwrite_solution_checksum(self):
        """Is the checksum overwritten for solution cells?"""
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)
        nb, resources = self.preprocessor1.preprocess(nb, self.resources)

        cell.metadata.nbgrader["checksum"] = "1234"
        nb, resources = self.preprocessor2.preprocess(nb, self.resources)

        assert_equal(cell.metadata.nbgrader["checksum"], compute_checksum(cell))
Ejemplo n.º 29
0
 def _setup_db(self):
     dbpath = self._init_db()
     gb = Gradebook(dbpath)
     gb.add_assignment("ps1")
     gb.add_student("foo")
     return dbpath
Ejemplo n.º 30
0
class TestSaveCells(TestBase):

    def setup(self):
        super(TestSaveCells, self).setup()
        db_url = self._init_db()
        self.gb = Gradebook(db_url)
        self.gb.add_assignment("ps0")

        self.preprocessor = SaveCells()
        self.resources = {
            "nbgrader": {
                "db_url": db_url,
                "assignment": "ps0",
                "notebook": "test"
            }
        }

    def test_save_code_grade_cell(self):
        cell = self._create_grade_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        assert_equal(grade_cell.max_score, 1, "max_score is incorrect")
        assert_equal(grade_cell.source, "hello", "source is incorrect")
        assert_equal(grade_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(grade_cell.cell_type, "code", "cell_type is incorrect")

    def test_save_markdown_grade_cell(self):
        cell = self._create_grade_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        assert_equal(grade_cell.max_score, 1, "max_score is incorrect")
        assert_equal(grade_cell.source, "hello", "source is incorrect")
        assert_equal(grade_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(grade_cell.cell_type, "markdown", "cell_type is incorrect")

    def test_save_code_solution_cell(self):
        cell = self._create_solution_cell("hello", "code")
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        solution_cell = gb.find_solution_cell(0, "test", "ps0")
        assert_equal(solution_cell.source, "hello", "source is incorrect")
        assert_equal(solution_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(solution_cell.cell_type, "code", "cell_type is incorrect")

    def test_save_markdown_solution_cell(self):
        cell = self._create_solution_cell("hello", "markdown")
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        solution_cell = gb.find_solution_cell(0, "test", "ps0")
        assert_equal(solution_cell.source, "hello", "source is incorrect")
        assert_equal(solution_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(solution_cell.cell_type, "markdown", "cell_type is incorrect")

    def test_save_code_grade_and_solution_cell(self):
        cell = self._create_grade_and_solution_cell("hello", "code", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        assert_equal(grade_cell.max_score, 1, "max_score is incorrect")
        assert_equal(grade_cell.source, "hello", "source is incorrect")
        assert_equal(grade_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(grade_cell.cell_type, "code", "cell_type is incorrect")

        solution_cell = gb.find_solution_cell(0, "test", "ps0")
        assert_equal(solution_cell.source, "hello", "source is incorrect")
        assert_equal(solution_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(solution_cell.cell_type, "code", "cell_type is incorrect")

    def test_save_markdown_grade_and_solution_cell(self):
        cell = self._create_grade_and_solution_cell("hello", "markdown", "foo", 1)
        nb = new_notebook()
        nb.cells.append(cell)

        nb, resources = self.preprocessor.preprocess(nb, self.resources)

        gb = self.preprocessor.gradebook
        grade_cell = gb.find_grade_cell("foo", "test", "ps0")
        assert_equal(grade_cell.max_score, 1, "max_score is incorrect")
        assert_equal(grade_cell.source, "hello", "source is incorrect")
        assert_equal(grade_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(grade_cell.cell_type, "markdown", "cell_type is incorrect")
        
        solution_cell = gb.find_solution_cell(0, "test", "ps0")
        assert_equal(solution_cell.source, "hello", "source is incorrect")
        assert_equal(solution_cell.checksum, cell.metadata.nbgrader["checksum"], "checksum is incorrect")
        assert_equal(solution_cell.cell_type, "markdown", "cell_type is incorrect")