11from zt_backend .models .state .notebook_state import NotebookState
2- from zt_backend .models import notebook
3- import rtoml
42import subprocess
53from pathlib import Path
64import os
5+ import importlib .util
76
8- notebook_state = NotebookState ()
7+ IPYNB_PATH = Path ("test_file.ipynb" ).resolve ()
8+ OUTPUT_PATH = Path ("notebook.py" ).resolve ()
9+ NOTEBOOK_PATH = Path ("test_notebook.py" ).resolve ()
910
10- IPYNB_PATH = Path ("zt_backend/tests/test_file.ipynb" ).resolve ()
11- OUTPUT_PATH = Path ("zt_backend/tests/notebook.py" ).resolve ()
12- NOTEBOOK_PATH = Path ("zt_backend/tests/test_notebook.py" ).resolve ()
11+ def dynamic_import (module_path ):
12+ spec = importlib .util .spec_from_file_location ("notebook_module" , module_path )
13+ module = importlib .util .module_from_spec (spec )
14+ spec .loader .exec_module (module )
15+ return module
1316
1417def test_ipynb_to_ztnb ():
1518
19+ # Step 1: Convert the IPYNB file to a Python file
1620 convert = subprocess .Popen (
1721 ["zero-true" , "jupyter-convert" , IPYNB_PATH , OUTPUT_PATH ]
1822 )
1923 convert .wait ()
2024
21- with open (NOTEBOOK_PATH , "r" , encoding = "utf-8" ) as file :
22- expected_data = rtoml .loads (file .read ().replace ("\\ " , "\\ \\ " ))
23-
24- expected_notebook_data = {
25- "notebookId" : "test_id" ,
26- "notebookName" : expected_data .get ("notebookName" , "Zero True" ),
27- "userId" : "" ,
28- "cells" : {
29- f"{ index } " : notebook .CodeCell (id = f"{ index } " , ** cell_data , output = "" )
30- for index , (cell_id , cell_data ) in enumerate (expected_data ["cells" ].items ())
31- },
32- }
33- expected_notebook = notebook .Notebook (** expected_notebook_data )
34-
35- with open (OUTPUT_PATH , "r" , encoding = "utf-8" ) as file :
36- output_data = rtoml .loads (file .read ().replace ("\\ " , "\\ \\ " ))
37-
38- output_notebook_data = {
39- "notebookId" : "test_id" ,
40- "notebookName" : output_data .get ("notebookName" , "Zero True" ),
41- "userId" : "" ,
42- "cells" : {
43- f"{ index } " : notebook .CodeCell (id = f"{ index } " , ** cell_data , output = "" )
44- for index , (cell_id , cell_data ) in enumerate (output_data ["cells" ].items ())
45- },
46- }
47- output_notebook = notebook .Notebook (** output_notebook_data )
48-
49- assert expected_notebook == output_notebook
50-
51- os .remove (OUTPUT_PATH )
25+ # Step 2: Import the expected notebook from test_notebook.py
26+ from test_notebook import notebook as expected_notebook
27+ output_module = dynamic_import (OUTPUT_PATH )
28+ output_notebook = output_module .notebook
29+ # Step 3: Import the generated notebook from notebook.py
30+ expected_notebook .notebookId = '0'
31+ output_notebook .notebookId = '0'
32+ # Step 4: Assert that the generated notebook matches the expected notebook
33+ assert output_notebook == expected_notebook
34+
35+ # Step 5: Clean up the generated file
36+ os .remove (OUTPUT_PATH )
0 commit comments