-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_notebook_manager.py
66 lines (53 loc) · 3.34 KB
/
test_notebook_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
from unittest.mock import patch, mock_open, MagicMock
import nbformat
from notebook_manager import NotebookManager
class TestNotebookManager(unittest.TestCase):
def setUp(self):
self.notebook_path = 'test_notebook.ipynb'
with patch('notebook_manager.config.NOTEBOOK_PATH', self.notebook_path):
self.notebook_manager = NotebookManager()
@patch('nbformat.writes')
def test_save_notebook(self, mock_writes):
mock_writes.return_value = '{"cells": []}'
with patch('builtins.open', mock_open()) as mock_file:
self.notebook_manager.save_notebook()
mock_writes.assert_called_once_with(self.notebook_manager.notebook)
mock_file.assert_called_once_with(self.notebook_path, 'w', encoding='utf-8')
mock_file().write.assert_called_once_with('{"cells": []}')
def test_add_markdown_cell(self):
initial_cell_count = len(self.notebook_manager.notebook.cells)
markdown_content = "Test Markdown"
self.notebook_manager.add_markdown_cell(markdown_content)
self.assertEqual(len(self.notebook_manager.notebook.cells), initial_cell_count + 1)
self.assertEqual(self.notebook_manager.notebook.cells[-1].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-1].source, f"## {markdown_content}")
def test_add_code_cell(self):
initial_cell_count = len(self.notebook_manager.notebook.cells)
code_content = "print('Hello, World!')"
self.notebook_manager.add_code_cell(code_content)
self.assertEqual(len(self.notebook_manager.notebook.cells), initial_cell_count + 1)
self.assertEqual(self.notebook_manager.notebook.cells[-1].cell_type, 'code')
self.assertEqual(self.notebook_manager.notebook.cells[-1].source, code_content)
def test_add_analysis_step(self):
initial_cell_count = len(self.notebook_manager.notebook.cells)
step_name = "Test Step"
step_description = "This is a test step"
step_goals = "To test the add_analysis_step method"
code = "print('Test code')"
self.notebook_manager.add_analysis_step(step_name, step_description, step_goals, code)
self.assertEqual(len(self.notebook_manager.notebook.cells), initial_cell_count + 6)
self.assertEqual(self.notebook_manager.notebook.cells[-6].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-5].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-4].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-3].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-2].cell_type, 'markdown')
self.assertEqual(self.notebook_manager.notebook.cells[-1].cell_type, 'code')
self.assertIn(step_name, self.notebook_manager.notebook.cells[-6].source)
self.assertIn("Description:", self.notebook_manager.notebook.cells[-5].source)
self.assertIn(step_description, self.notebook_manager.notebook.cells[-4].source)
self.assertIn("Goals:", self.notebook_manager.notebook.cells[-3].source)
self.assertIn(step_goals, self.notebook_manager.notebook.cells[-2].source)
self.assertEqual(self.notebook_manager.notebook.cells[-1].source, code)
if __name__ == '__main__':
unittest.main()