Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions bioptim/examples/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,14 @@ def applySearchHighlight(self, text):
self.setFormat(start, length, style)


def unnestedDict(exDict):
"""Converts a dict-of-dicts to a singly nested dict for non-recursive parsing"""
def unnestedDict(exDict, root_dir=""):
"""Convert an example tree to a title-to-relative-path dictionary."""
out = {}
for kk, vv in exDict.items():
if isinstance(vv, dict):
out.update(unnestedDict(vv))
out.update(unnestedDict(vv, os.path.join(root_dir, kk)))
else:
out[kk] = vv
out[kk] = os.path.join(root_dir, vv)
return out


Expand Down Expand Up @@ -575,19 +575,15 @@ def filterByContent(self, text=None):
self.hl.setDocument(self.ui.codeView.document())
text = text.lower()
titles = []
for key, val in examples_.items():
if isinstance(val, OrderedDict):
root_dir = key
checkDict = unnestedDict(val)
for kk, vv in checkDict.items():
path = os.getcwd() + "/" + root_dir
filename = os.path.join(path, vv)
contents = self.getExampleContent(filename).lower()
if text in contents:
titles.append(kk)
else:
pass
self.showExamplesByTitle(titles)
for root_dir, examples in examples_.items():
if not isinstance(examples, dict):
continue
for title, relative_filename in unnestedDict(examples, root_dir).items():
filename = os.path.join(path, relative_filename)
contents = self.getExampleContent(filename).lower()
if text in contents:
titles.append(title)
self.showExamplesByTitle(titles)

def getMatchingTitles(self, text, exDict=None, acceptAll=False):
if exDict is None:
Expand Down
71 changes: 71 additions & 0 deletions tests/shard1/test__run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,80 @@
Test for file IO
"""

import os
from sys import platform


def test_example_paths_are_preserved_when_flattening(monkeypatch):
if platform == "linux":
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")

from bioptim.examples.__main__ import examples_, path, unnestedDict

example_paths = unnestedDict(examples_["toy_examples/acados"], "toy_examples/acados")

assert os.path.normpath(example_paths["Static arm"]) == os.path.normpath("toy_examples/acados/static_arm.py")
for root_dir, examples in examples_.items():
for relative_path in unnestedDict(examples, root_dir).values():
assert os.path.isfile(os.path.join(path, relative_path))


def test_filter_by_content_uses_example_package_path(monkeypatch, tmp_path):
if platform == "linux":
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")

import bioptim.examples.__main__ as examples_module

example_directory = tmp_path / "nested"
example_directory.mkdir()
(example_directory / "matching.py").write_text("the content needle is here")
(example_directory / "other.py").write_text("unrelated content")

monkeypatch.setattr(
examples_module,
"examples_",
{
"nested": {
"Matching example": "matching.py",
"Other example": "other.py",
},
"metadata": "ignored",
},
)
monkeypatch.setattr(examples_module, "path", str(tmp_path))

class Highlighter:
searchText = None

@staticmethod
def setDocument(_):
pass

class CodeView:
@staticmethod
def document():
return None

class Loader:
hl = Highlighter()
ui = type("Ui", (), {"codeView": CodeView()})()
matching_titles = None

@staticmethod
def getExampleContent(filename):
with open(filename) as example_file:
return example_file.read()

def showExamplesByTitle(self, titles):
self.matching_titles = titles

loader = Loader()
examples_module.ExampleLoader.filterByContent(loader, "NEEDLE")

assert loader.hl.searchText == "NEEDLE"
assert loader.matching_titles == ["Matching example"]


def test_run_examples():
if platform == "linux": # AppVeyor and GitHub action cannot work with graphic interface on Linux
return
Expand Down
Loading