Skip to content

Commit

Permalink
refactor: Refactor Code Embedder test
Browse files Browse the repository at this point in the history
  • Loading branch information
kvankova committed Nov 5, 2024
1 parent 2274417 commit 8fceb9f
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions tests/test_code_embedding.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
from pathlib import Path

import pytest

from src.code_embedding import CodeEmbedder
from src.script_content_reader import ScriptContentReader
from src.script_metadata_extractor import ScriptMetadataExtractor


def test_code_embedder(tmp_path) -> None:
original_paths = [
"tests/data/readme0.md",
"tests/data/readme1.md",
"tests/data/readme2.md",
"tests/data/readme3.md",
]
expected_paths = [
"tests/data/expected_readme0.md",
"tests/data/expected_readme1.md",
"tests/data/expected_readme2.md",
"tests/data/expected_readme3.md",
]

@pytest.mark.parametrize(
"before_code_embedding_path, after_code_embedding_path",
[
# Full path and missing path
(
"tests/data/readme0.md",
"tests/data/expected_readme0.md",
),
# Section
(
"tests/data/readme1.md",
"tests/data/expected_readme1.md",
),
# Empty readme
(
"tests/data/readme2.md",
"tests/data/expected_readme2.md",
),
# Objects
(
"tests/data/readme3.md",
"tests/data/expected_readme3.md",
),
],
ids=[
"full_path_missing_path",
"section",
"empty_readme",
"objects",
],
)
def test_code_embedder(
before_code_embedding_path: str, after_code_embedding_path: str, tmp_path: Path
) -> None:
# Create a temporary copy of the original file
temp_readme_paths = [tmp_path / f"readme{i}.md" for i in range(len(original_paths))]
for original_path, temp_readme_path in zip(original_paths, temp_readme_paths):
with open(original_path) as readme_file:
temp_readme_path.write_text(readme_file.read())
temp_readme_path = tmp_path / "readme.md"
with open(before_code_embedding_path) as readme_file:
temp_readme_path.write_text(readme_file.read())

code_embedder = CodeEmbedder(
readme_paths=[str(temp_readme_path) for temp_readme_path in temp_readme_paths],
readme_paths=[str(temp_readme_path)],
script_metadata_extractor=ScriptMetadataExtractor(),
script_content_reader=ScriptContentReader(),
)

code_embedder()

for expected_path, temp_readme_path in zip(expected_paths, temp_readme_paths):
with open(expected_path) as expected_file:
expected_readme_content = expected_file.readlines()
with open(after_code_embedding_path) as expected_file:
expected_readme_content = expected_file.readlines()

with open(temp_readme_path) as updated_file:
updated_readme_content = updated_file.readlines()
with open(temp_readme_path) as updated_file:
updated_readme_content = updated_file.readlines()

assert updated_readme_content == expected_readme_content
assert updated_readme_content == expected_readme_content

0 comments on commit 8fceb9f

Please sign in to comment.