Skip to content

Commit

Permalink
fixed failing tests and new cells not adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Red Giuliano committed Jan 28, 2025
1 parent f36c1a0 commit abeef31
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
6 changes: 3 additions & 3 deletions zt_backend/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import os
import importlib.util

IPYNB_PATH = Path("test_file.ipynb").resolve()
OUTPUT_PATH = Path("notebook.py").resolve()
NOTEBOOK_PATH = Path("test_notebook.py").resolve()
IPYNB_PATH = Path("zt_backend/tests/test_file.ipynb").resolve()
OUTPUT_PATH = Path("zt_backend/tests/notebook.py").resolve()
NOTEBOOK_PATH = Path("zt_backend/tests/test_notebook.py").resolve()

def dynamic_import(module_path):
spec = importlib.util.spec_from_file_location("notebook_module", module_path)
Expand Down
25 changes: 16 additions & 9 deletions zt_backend/tests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import subprocess
import textwrap
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
Expand All @@ -14,22 +15,27 @@

notebook_id = str(uuid.uuid4())

expected_code = """
import zero_true as zt
import time
time.sleep(2)
slider = zt.Slider(id='slide')
zt.TextInput(id='text')"""


# Define the expected Python code for the notebook
notebook_str = f"""
import zero_true as zt
import time
def cell_57fbbd59_8f30_415c_87bf_8caae0374070():
time.sleep(2)
slider = zt.Slider(id='slide')
zt.TextInput(id='text')
def cell_0():
"""+textwrap.indent(expected_code,' ')+"""
notebook = zt.notebook(
id="{notebook_id}",
name="Zero True",
cells=[
zt.cell(cell_57fbbd59_8f30_415c_87bf_8caae0374070, type="code")
zt.cell(cell_0, type="code")
]
)
"""
Expand Down Expand Up @@ -76,6 +82,7 @@ def find_code_cells(driver):
EC.presence_of_element_located((By.XPATH, "//div[contains(@id, 'codeCard')]"))
)
code_cells = driver.find_elements(By.XPATH, "//div[contains(@id, 'codeCard')]")
print(code_cells)
return code_cells

def find_el_by_id_w_exception(driver,element_id):
Expand Down Expand Up @@ -163,7 +170,7 @@ def test_notebook_loading(driver):

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

def test_intial_code_cell_content(driver):
code_cells = find_code_cells(driver)
Expand Down Expand Up @@ -221,7 +228,7 @@ def test_adding_new_code_cell(driver):

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

def test_execution_of_new_code_cell(driver):
code_cells = find_code_cells(driver)
Expand Down Expand Up @@ -334,7 +341,7 @@ def test_app_mode(driver):

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

cell_id_0 = '57fbbd59-8f30-415c-87bf-8caae0374070'
cell_id_0 = 'cell_0'

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

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

# test hiding code cell

Expand Down
11 changes: 9 additions & 2 deletions zt_backend/utils/pyfile_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ def build_cell_code_block(fn_name, cell_obj, def_line):
original_lines = [import_line, "\n"]

original_source = "".join(original_lines)
print("ORIGINAL:\n", original_source)

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

Expand Down Expand Up @@ -391,6 +390,15 @@ def build_cell_code_block(fn_name, cell_obj, def_line):
current_idx += 1
i += 1

# Handle new cells that are not already handled
for cid, cell_obj in notebook_obj.cells.items():
if cid not in handled_cells:
maybe_add_blank_line(updated_lines)
def_line = f"def {cid}():"
new_block = build_cell_code_block(cid, cell_obj, def_line)
for ln in new_block:
updated_lines.append(ln + "\n")

# copy leftover lines
while current_idx < len(original_lines):
updated_lines.append(original_lines[current_idx])
Expand Down Expand Up @@ -521,7 +529,6 @@ def format_cell_call(fn_name, cobj):
existing_imports.add(imp + "\n") # Track the added import


print("UPDATED:\n", "".join(updated_lines))
filepath_temp = str(filepath).replace('.py', str(uuid4()) + '.py')
with open(filepath_temp, 'w') as f:
f.writelines(updated_lines)
Expand Down

0 comments on commit abeef31

Please sign in to comment.