Skip to content

Commit abeef31

Browse files
author
Red Giuliano
committed
fixed failing tests and new cells not adding
1 parent f36c1a0 commit abeef31

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

zt_backend/tests/test_convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import os
55
import importlib.util
66

7-
IPYNB_PATH = Path("test_file.ipynb").resolve()
8-
OUTPUT_PATH = Path("notebook.py").resolve()
9-
NOTEBOOK_PATH = Path("test_notebook.py").resolve()
7+
IPYNB_PATH = Path("zt_backend/tests/test_file.ipynb").resolve()
8+
OUTPUT_PATH = Path("zt_backend/tests/notebook.py").resolve()
9+
NOTEBOOK_PATH = Path("zt_backend/tests/test_notebook.py").resolve()
1010

1111
def dynamic_import(module_path):
1212
spec = importlib.util.spec_from_file_location("notebook_module", module_path)

zt_backend/tests/test_e2e.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import subprocess
3+
import textwrap
34
import os
45
from selenium import webdriver
56
from selenium.webdriver.common.by import By
@@ -14,22 +15,27 @@
1415

1516
notebook_id = str(uuid.uuid4())
1617

18+
expected_code = """
19+
import zero_true as zt
20+
import time
21+
time.sleep(2)
22+
slider = zt.Slider(id='slide')
23+
zt.TextInput(id='text')"""
24+
1725

1826
# Define the expected Python code for the notebook
1927
notebook_str = f"""
2028
import zero_true as zt
2129
import time
2230
23-
def cell_57fbbd59_8f30_415c_87bf_8caae0374070():
24-
time.sleep(2)
25-
slider = zt.Slider(id='slide')
26-
zt.TextInput(id='text')
31+
def cell_0():
32+
"""+textwrap.indent(expected_code,' ')+"""
2733
2834
notebook = zt.notebook(
2935
id="{notebook_id}",
3036
name="Zero True",
3137
cells=[
32-
zt.cell(cell_57fbbd59_8f30_415c_87bf_8caae0374070, type="code")
38+
zt.cell(cell_0, type="code")
3339
]
3440
)
3541
"""
@@ -76,6 +82,7 @@ def find_code_cells(driver):
7682
EC.presence_of_element_located((By.XPATH, "//div[contains(@id, 'codeCard')]"))
7783
)
7884
code_cells = driver.find_elements(By.XPATH, "//div[contains(@id, 'codeCard')]")
85+
print(code_cells)
7986
return code_cells
8087

8188
def find_el_by_id_w_exception(driver,element_id):
@@ -163,7 +170,7 @@ def test_notebook_loading(driver):
163170

164171
def test_initial_code_cell(driver):
165172
code_cells = find_code_cells(driver)
166-
assert len(code_cells) == 1 and code_cells[0].get_attribute('id') == 'codeCard57fbbd59-8f30-415c-87bf-8caae0374070', "Expected code cell not found."
173+
assert len(code_cells) == 1 and code_cells[0].get_attribute('id') == 'codeCardcell_0', "Expected code cell not found."
167174

168175
def test_intial_code_cell_content(driver):
169176
code_cells = find_code_cells(driver)
@@ -221,7 +228,7 @@ def test_adding_new_code_cell(driver):
221228

222229
code_cells = find_code_cells(driver)
223230
assert len(code_cells) == 2, "New code cell was not added"
224-
assert code_cells[0].get_attribute('id') == 'codeCard57fbbd59-8f30-415c-87bf-8caae0374070', "Expected code cell not found"
231+
assert code_cells[0].get_attribute('id') == 'codeCardcell_0', "Expected code cell not found"
225232

226233
def test_execution_of_new_code_cell(driver):
227234
code_cells = find_code_cells(driver)
@@ -334,7 +341,7 @@ def test_app_mode(driver):
334341

335342
#assert that there is only cell in app mode because the second cell was hidden
336343

337-
cell_id_0 = '57fbbd59-8f30-415c-87bf-8caae0374070'
344+
cell_id_0 = 'cell_0'
338345

339346
assert len(code_cells) == 1 and code_cells[0].get_attribute('id') == f'codeCard{cell_id_0}', "Expected code cell not found."
340347

@@ -380,7 +387,7 @@ def test_deletion_of_new_code_cell(driver):
380387
delete_btn.click()
381388
time.sleep(2)
382389
code_cells = find_code_cells(driver)
383-
assert len(code_cells) == 1 and code_cells[0].get_attribute('id') == 'codeCard57fbbd59-8f30-415c-87bf-8caae0374070', "Expected code cell not found."
390+
assert len(code_cells) == 1 and code_cells[0].get_attribute('id') == 'codeCardcell_0', "Expected code cell not found."
384391

385392
# test hiding code cell
386393

zt_backend/utils/pyfile_parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ def build_cell_code_block(fn_name, cell_obj, def_line):
290290
original_lines = [import_line, "\n"]
291291

292292
original_source = "".join(original_lines)
293-
print("ORIGINAL:\n", original_source)
294293

295294
# 1a) Gather old_cell_ids by scanning lines for 'notebook=zt.notebook(..., cells=[zt.cell(...), ...])'
296295

@@ -391,6 +390,15 @@ def build_cell_code_block(fn_name, cell_obj, def_line):
391390
current_idx += 1
392391
i += 1
393392

393+
# Handle new cells that are not already handled
394+
for cid, cell_obj in notebook_obj.cells.items():
395+
if cid not in handled_cells:
396+
maybe_add_blank_line(updated_lines)
397+
def_line = f"def {cid}():"
398+
new_block = build_cell_code_block(cid, cell_obj, def_line)
399+
for ln in new_block:
400+
updated_lines.append(ln + "\n")
401+
394402
# copy leftover lines
395403
while current_idx < len(original_lines):
396404
updated_lines.append(original_lines[current_idx])
@@ -521,7 +529,6 @@ def format_cell_call(fn_name, cobj):
521529
existing_imports.add(imp + "\n") # Track the added import
522530

523531

524-
print("UPDATED:\n", "".join(updated_lines))
525532
filepath_temp = str(filepath).replace('.py', str(uuid4()) + '.py')
526533
with open(filepath_temp, 'w') as f:
527534
f.writelines(updated_lines)

0 commit comments

Comments
 (0)