def test_kwargs(): # Test can run in given directory. with JupyterKernel('ir') as rk: output = _stripped(rk.run_code('getwd()')[0]) pth = PROMPT_STR_RE.search(output['content']).groups()[0] assert op.realpath(pth) == op.realpath(os.getcwd()) data_path = op.join(op.dirname(__file__), 'data') with JupyterKernel('ir', cwd=data_path) as rk: output = _stripped(rk.run_code('getwd()')[0]) pth = PROMPT_STR_RE.search(output['content']).groups()[0] assert op.realpath(pth) == op.realpath(data_path)
def test_report(): runner = NBRunner() nb_fileobj0 = StringIO(""" Text ```{r} first_var <- 1 ``` More text ```{r} first_var ``` """) nb_fileobj1 = StringIO(""" Text ```{r} ``` More text ```{r} first_var <- 2 ``` """) with JupyterKernel('ir') as rk: results0 = runner.run(nb_fileobj0, rk) results1 = runner.run(nb_fileobj1, rk) assert (report(results0) == ' 0: first_var <- 1 - None\n 1: first_var - [1] 1') assert (report(results1) == ' 0: (no code) - None\n 1: first_var <- 2 - None')
def rebuild(self): # Rebuild solution notebooks if not isdir(self.out_dir): makedirs(self.out_dir) with JupyterKernel('ir', timeout=self.timeout) as rk: solution = self.runner.run(StringIO(self.notebook_text), rk) self._store_solution(solution) return solution
def test_smoke(): solution_rmd = pjoin(DATA, 'py_solution.Rmd') runner = NBRunner() with JupyterKernel('python3', cwd=DATA) as pk: results0 = runner.run(solution_rmd, pk) contents = [ c.results[0]['content'] if c.results else None for c in results0 ] assert contents[0] is None assert contents[1] == "['speed', 'dist']" assert contents[2] == '(50, 2)'
def grade_notebook(self, fileish, answers=None): answers = self.make_check_answers() if answers is None else answers with JupyterKernel('ir') as rk: ev_chunks = self.runner.run(fileish, rk) adjustments = self.calc_adjustments(rk) # Remove any not-answer chunks ev_chunks = self.clear_not_answers(ev_chunks) # Get adjustments from markup markups = sum(self.mark_markups(fileish)) grid = full_grid(answers, ev_chunks) names = [a.name if a.name else 'unnamed' for a in answers] names += ['adjustments', 'markups'] return pd.Series(list(max_multi(grid)) + [adjustments, markups], names)
def test_error_report(): nb = StringIO(""" Some text. ```{r} a <- 1 a ``` More text. ```{r} b ``` """) runner = NBRunner() with pytest.raises(NotebookError): with JupyterKernel('ir') as rk: runner.run(nb, rk)
def test_chunk_is_answer(): runner = NBRunner() nb_text = """ Text ```{r} #- A question first_var <- 99 ``` More text ```{r} first_var ``` ```{r} #- Actual answer first_var ``` """ nb_fobj = StringIO(nb_text) with JupyterKernel('ir') as rk: chunks = runner.run(nb_fobj, rk) g = Grader() assert_seq_equal(chunks, g.clear_not_answers(chunks)) # Second chunk has results. assert len(chunks[1].results) == 1 # First check case where not-answer present raises error nb_fobj.seek(0) class MyG(Grader): solution_rmds = (nb_fobj,) total = 5 def make_answers(self): self._chk_answer(RegexAnswer( 5, OPTIONAL_PROMPT + r'99'), 2) g = MyG() # Doesn't clear any chunks. assert_seq_equal(chunks, g.clear_not_answers(chunks)) assert len(chunks[1].results) == 1 # Therefore errors for duplicate outputs with pytest.raises(NotebookError): g.grade_notebook(StringIO(nb_text)) # Check case with answer removed. class MyG2(MyG): solution_rmds = (nb_fobj,) def chunk_is_answer(self, chunk): return chunk.chunk.code != 'first_var\n' g2 = MyG2() # Clears output for not-answer chunk. cleared = deepcopy(chunks[1]) cleared.results = [] assert_seq_equal([chunks[0], cleared, chunks[2]], g2.clear_not_answers(chunks)) # Answers now not duplicated. assert np.all(np.array(g2.grade_notebook(StringIO(nb_text))) == [5, 0, 0])
def test_context_manager(): with JupyterKernel('ir') as rk: output, = rk.run_code('a = 1\na') assert _stripped(output) == dict(type='text', content='[1] 1')
def rkernel(): # Inject kernel into test function namespace with JupyterKernel('ir') as rk: yield rk