Skip to content

Commit

Permalink
Fixed utils test.
Browse files Browse the repository at this point in the history
  • Loading branch information
souradipp76 committed Nov 8, 2024
1 parent b3ec333 commit a34692d
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lint: ## Run pep8, black, mypy linters.
$(ENV_PREFIX)flake8 doc_generator/
$(ENV_PREFIX)black -l 79 --check doc_generator/
$(ENV_PREFIX)black -l 79 --check tests/
$(ENV_PREFIX)mypy --ignore-missing-imports doc_generator/
$(ENV_PREFIX)mypy --ignore-missing-imports --disable-error-code=arg-type doc_generator/

.PHONY: test
test: lint ## Run tests and generate coverage report.
Expand Down
4 changes: 2 additions & 2 deletions doc_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def url_validator(x):
default=f"./{name}/",
).ask()
project_url = questionary.text(
message="Project URL?[Example: \
https://github.com/username/doc_generator]",
message="Project URL?[Example: "
+ "https://github.com/username/doc_generator]",
validate=url_validator,
).ask()
output_dir = questionary.path(
Expand Down
13 changes: 8 additions & 5 deletions doc_generator/query/create_chat_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

from typing import List
from langchain.chains.conversational_retrieval.base import ChatVectorDBChain
from langchain.chains import LLMChain, StuffDocumentsChain
from langchain.chains import LLMChain
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents.stuff import (
create_stuff_documents_chain,
)
from langchain.prompts import PromptTemplate
from doc_generator.types import LLMModels
from doc_generator.utils.llm_utils import (
Expand Down Expand Up @@ -204,8 +207,8 @@ def make_qa_chain(
chat_prompt,
target_audience,
)
doc_chain = StuffDocumentsChain(
llm_chain=doc_chat_model, document_prompt=qa_prompt
doc_chain = create_stuff_documents_chain(
llm=doc_chat_model, prompt=qa_prompt
)

return ChatVectorDBChain(
Expand Down Expand Up @@ -268,8 +271,8 @@ def make_readme_chain(
chat_prompt,
target_audience,
)
doc_chain = StuffDocumentsChain(
llm_chain=doc_chat_model, document_prompt=readme_prompt
doc_chain = create_stuff_documents_chain(
llm=doc_chat_model, prompt=readme_prompt
)

return create_retrieval_chain(
Expand Down
4 changes: 2 additions & 2 deletions doc_generator/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ def print_model_details(models):
"File Count": model_details.total,
"Succeeded": model_details.succeeded,
"Failed": model_details.failed,
"Tokens": model_details.inputTokens + model_details.output_tokens,
"Tokens": model_details.input_tokens + model_details.output_tokens,
"Cost": (
(model_details.inputTokens / 1000)
(model_details.input_tokens / 1000)
* model_details.input_cost_per_1k_tokens
+ (model_details.output_tokens / 1000)
* model_details.output_cost_per_1k_tokens
Expand Down
75 changes: 64 additions & 11 deletions tests/utils/test_file_utils.py
Original file line number Diff line number Diff line change
@@ -1,105 +1,158 @@
import pytest
from doc_generator.utils.file_utils import get_file_name, github_file_url, github_folder_url
from doc_generator.utils.file_utils import (
get_file_name,
github_file_url,
github_folder_url,
)


def test_get_file_name_with_delimiter():
assert get_file_name("example.txt") == "example.md"


def test_get_file_name_without_delimiter():
assert get_file_name("example") == "example.md"


def test_get_file_name_custom_delimiter():
assert get_file_name("example-text", delimiter="-") == "example.md"


def test_get_file_name_no_delimiter_custom_extension():
assert get_file_name("example", extension=".txt") == "example.txt"


def test_get_file_name_with_delimiter_custom_extension():
assert get_file_name("example.txt", extension=".txt") == "example.txt"


def test_get_file_name_with_multiple_delimiters():
assert get_file_name("my.example.txt") == "my.example.md"


def test_get_file_name_with_no_delimiter_and_no_extension():
assert get_file_name("example", extension="") == "example"


def test_get_file_name_with_delimiter_and_no_extension():
assert get_file_name("example.txt", extension="") == "example"


def test_get_file_name_empty_input():
assert get_file_name("") == ".md"


def test_get_file_name_delimiter_not_in_input():
assert get_file_name("example", delimiter="/") == "example.md"


def test_get_file_name_delimiter_at_end():
assert get_file_name("example.", delimiter=".") == "example.md"


def test_get_file_name_delimiter_at_start():
assert get_file_name(".example", delimiter=".") == ".md"


def test_get_file_name_delimiter_multiple_occurrences():
assert get_file_name("my.file.name.txt") == "my.file.name.md"


def test_github_file_url_link_hosted_true():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
file_path = "/home/user/project/docs/file.md"
link_hosted = True
expected_url = f"{github_root}/{file_path[len(input_root)-1:]}"
assert github_file_url(github_root, input_root, file_path, link_hosted) == expected_url
assert (
github_file_url(github_root, input_root, file_path, link_hosted)
== expected_url
)


def test_github_file_url_link_hosted_false():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
file_path = "/home/user/project/docs/file.md"
link_hosted = False
expected_url = f"{github_root}/blob/master/{file_path[len(input_root)-1:]}"
assert github_file_url(github_root, input_root, file_path, link_hosted) == expected_url
assert (
github_file_url(github_root, input_root, file_path, link_hosted)
== expected_url
)


def test_github_file_url_empty_input_root():
github_root = "https://github.com/user/repo"
input_root = ""
file_path = "/docs/file.md"
link_hosted = False
expected_url = f"{github_root}/blob/master/{file_path[-1:]}"
assert github_file_url(github_root, input_root, file_path, link_hosted) == expected_url
assert (
github_file_url(github_root, input_root, file_path, link_hosted)
== expected_url
)


def test_github_file_url_empty_file_path():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
file_path = ""
link_hosted = False
expected_url = f"{github_root}/blob/master/{file_path[len(input_root)-1:]}"
assert github_file_url(github_root, input_root, file_path, link_hosted) == expected_url
assert (
github_file_url(github_root, input_root, file_path, link_hosted)
== expected_url
)


def test_github_folder_url_link_hosted_true():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
folder_path = "/home/user/project/docs/"
link_hosted = True
expected_url = f"{github_root}/{folder_path[len(input_root)-1:]}"
assert github_folder_url(github_root, input_root, folder_path, link_hosted) == expected_url
assert (
github_folder_url(github_root, input_root, folder_path, link_hosted)
== expected_url
)


def test_github_folder_url_link_hosted_false():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
folder_path = "/home/user/project/docs/"
link_hosted = False
expected_url = f"{github_root}/tree/master/{folder_path[len(input_root)-1:]}"
assert github_folder_url(github_root, input_root, folder_path, link_hosted) == expected_url
expected_url = (
f"{github_root}/tree/master/{folder_path[len(input_root)-1:]}"
)
assert (
github_folder_url(github_root, input_root, folder_path, link_hosted)
== expected_url
)


def test_github_folder_url_empty_input_root():
github_root = "https://github.com/user/repo"
input_root = ""
folder_path = "/docs/"
link_hosted = False
expected_url = f"{github_root}/tree/master/{folder_path[-1:]}"
assert github_folder_url(github_root, input_root, folder_path, link_hosted) == expected_url
assert (
github_folder_url(github_root, input_root, folder_path, link_hosted)
== expected_url
)


def test_github_folder_url_empty_folder_path():
github_root = "https://github.com/user/repo"
input_root = "/home/user/project"
folder_path = ""
link_hosted = False
expected_url = f"{github_root}/tree/master/{folder_path[len(input_root)-1:]}"
assert github_folder_url(github_root, input_root, folder_path, link_hosted) == expected_url
expected_url = (
f"{github_root}/tree/master/{folder_path[len(input_root)-1:]}"
)
assert (
github_folder_url(github_root, input_root, folder_path, link_hosted)
== expected_url
)
Loading

0 comments on commit a34692d

Please sign in to comment.