Skip to content

Commit

Permalink
Adds validation for the examples
Browse files Browse the repository at this point in the history
All examples must adhere to a strict format. We'll be adding a directory
with "unstructured" examples that don't adhere.
  • Loading branch information
elijahbenizzy committed Mar 29, 2024
1 parent dd2740d commit 8aa989d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ jobs:
- name: Run tests
run: |
python -m pytest tests
validate-examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- name: Install dependencies
run: |
python -m pip install -e ".[tests]"
- name: Validate examples
working-directory: ./examples
run: |
python -m pytest validate_examples.py
Empty file added examples/blog_post/.demo.db
Empty file.
Binary file added examples/blog_post/.demo2.db
Binary file not shown.
Binary file added examples/blog_post/.sqlite.db
Binary file not shown.
14 changes: 14 additions & 0 deletions examples/blog_post/digraph_safe
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
digraph {
graph [compound=false concentrate=false rankdir=TB ranksep=0.4]
human_input [label=human_input shape=box style=rounded]
input__prompt [label="input: prompt" shape=oval style=dashed]
input__prompt -> human_input
ai_response [label=ai_response shape=box style=rounded]
safety_check [label=safety_check shape=box style=rounded]
unsafe_response [label=unsafe_response shape=box style=rounded]
human_input -> safety_check [style=solid]
safety_check -> unsafe_response [label="safe=False" style=dashed]
safety_check -> ai_response [label="safe=True" style=dashed]
unsafe_response -> human_input [style=solid]
ai_response -> human_input [style=solid]
}
Binary file added examples/blog_post/sqlite.db
Binary file not shown.
43 changes: 43 additions & 0 deletions examples/validate_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os

EXAMPLES_DIR = "."
REQUIRED_FILES = ["notebook.ipynb", "application.py", "README.md", "digraph.png"]

FILTERLIST = []


def should_validate(directory: str) -> bool:
"""Return True if the given directory is an example directory."""
return (
os.path.isdir(os.path.join(EXAMPLES_DIR, directory))
and not directory.startswith(".")
and not directory.startswith("_")
and directory not in FILTERLIST
)


def get_directories(base_path: str) -> list[str]:
"""Return a list of directories under the given base_path."""
return [d for d in os.listdir(base_path) if should_validate(d)]


def check_files_exist(directory, files):
"""Check if each file in 'files' exists in 'directory'."""
missing_files = []
for file in files:
if not os.path.exists(os.path.join(EXAMPLES_DIR, directory, file)):
missing_files.append(file)
return missing_files


# Use pytest_generate_tests to dynamically parameterize the fixture
def pytest_generate_tests(metafunc):
# if "directory" in metafunc.fixturenames:
directories = get_directories(EXAMPLES_DIR)
metafunc.parametrize("directory", directories, scope="module")


def test_directory_contents(directory):
required_files = ["notebook.ipynb", "application.py", "README.md", "digraph.png"]
missing_files = check_files_exist(directory, required_files)
assert not missing_files, f"Missing files in '{directory}': {', '.join(missing_files)}"

0 comments on commit 8aa989d

Please sign in to comment.