Skip to content

Commit

Permalink
Move skip to the test ; add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
classiqdor committed Nov 14, 2024
1 parent ea0894e commit dc53040
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions tests/notebooks/algorithms/test_bernstein_vazirani.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import pytest
from tests.utils.utils_for_testbook import (
assert_unchanged_size,
assert_valid_model,
execute_testbook_with_timeout,
)
from tests.utils.utils_for_tests import should_skip_notebook
from testbook.client import TestbookNotebookClient # type: ignore[import]


@pytest.mark.skipif(should_skip_notebook("bernstein_vazirani"), reason="Didn't change")
@execute_testbook_with_timeout("bernstein_vazirani", timeout_seconds=20)
def test_bernstein_vazirani(tb: TestbookNotebookClient) -> None:
# test models
Expand Down
6 changes: 1 addition & 5 deletions tests/utils/utils_for_testbook.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import base64
import json
import os
import pytest
import pickle
import re
from typing import Any, Callable

from testbook import testbook # type: ignore[import]
from testbook.client import TestbookNotebookClient # type: ignore[import]
from tests.utils.utils_for_tests import resolve_notebook_path, should_test_notebook
from tests.utils.utils_for_tests import resolve_notebook_path

from classiq.interface.generator.quantum_program import QuantumProgram

Expand All @@ -19,9 +18,6 @@ def execute_testbook_with_timeout(
def inner_decorator(func: Callable) -> Any:
notebook_path = resolve_notebook_path(notebook_name)

if not should_test_notebook(notebook_path):
pytest.skip("Skipped")

for decorator in [
testbook(notebook_path, execute=True, timeout=timeout_seconds),
_build_cd_decorator(notebook_path),
Expand Down
19 changes: 13 additions & 6 deletions tests/utils/utils_for_tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
from functools import lru_cache
from collections.abc import Iterable
from pathlib import Path

ROOT_DIRECTORY = Path(__file__).parents[1]
ROOT_DIRECTORY = Path(__file__).parents[2]


def iterate_notebooks() -> Iterable[str]:
Expand All @@ -14,17 +15,22 @@ def iterate_notebooks() -> Iterable[str]:
return notebooks_to_test


@lru_cache
def _get_all_notebooks(directory: Path = ROOT_DIRECTORY) -> Iterable[str]:
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".ipynb"):
yield os.path.join(root, file)
return [
file
for root, _, files in os.walk(directory)
for file in files
if file.endswith(".ipynb")
]


def should_test_notebook(notebook_path: str) -> bool:
def should_skip_notebook(notebook_name: str) -> bool:
notebook_path = resolve_notebook_path(notebook_name)
return notebook_path in list(iterate_notebooks())


@lru_cache
def resolve_notebook_path(notebook_name: str) -> str:
notebook_name_lower = notebook_name.lower()
if not notebook_name_lower.endswith(".ipynb"):
Expand All @@ -34,3 +40,4 @@ def resolve_notebook_path(notebook_name: str) -> str:
for file in files:
if file.lower() == notebook_name_lower:
return os.path.join(root, file)
raise LookupError(f"Notebook not found: {notebook_name}")

0 comments on commit dc53040

Please sign in to comment.