Skip to content

Commit

Permalink
Add styling checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravpanda committed Oct 3, 2024
1 parent 6fd0e0f commit d4b0b9a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
7 changes: 3 additions & 4 deletions examples/api_test/api-main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
generator = APITestGenerator()

generator.generate_tests(
file_path="./examples/api_test/api-schema.json",
file_path="./examples/api_test/api-schema.json",
base_url="http://api.weatherbit.io/v2.0/",
enable_critique=True,
enable_critique=True,
verbose=True,
max_critique=1
max_critique=1,
)

test_results = generator.run_tests()
Expand All @@ -21,4 +21,3 @@
print(f" Failures: {result.get('failures', 'N/A')}")
print(f" Errors: {result.get('errors', 'N/A')}")
print()

1 change: 0 additions & 1 deletion kaizen/actors/api_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,3 @@ def run_command(self, command, cwd=None):
except subprocess.TimeoutExpired:
self.logger.error(f"Command timed out after {self.timeout} seconds")
return 124, "", f"Command timed out after {self.timeout} seconds"

37 changes: 22 additions & 15 deletions kaizen/generator/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from kaizen.actors.api_test_runner import APITestRunner
from kaizen.llms.prompts.API_tests_prompts import (
API_TEST_SYSTEM_PROMPT,
API_METHOD_PROMPT
API_METHOD_PROMPT,
)


Expand Down Expand Up @@ -73,7 +73,7 @@ def generate_tests(

def _read_file_content(self, file_path):
try:
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
api_schema = json.load(file)
except UnicodeDecodeError:
print(f"Error reading file: {file_path}")
Expand All @@ -83,15 +83,17 @@ def generate_test_files(self, file_data, file_path):
folder_path = "/".join(file_path.split("/")[:-1])
test_files = {}
actions_used = 0
for path, path_item in tqdm(file_data['paths'].items(), desc="Processing Endpoints(Paths)", unit="paths"):
for path, path_item in tqdm(
file_data["paths"].items(), desc="Processing Endpoints(Paths)", unit="paths"
):
try:
test_code, count = self._process_item(
path, path_item, folder_path
)
test_code, count = self._process_item(path, path_item, folder_path)
test_files[file_path] = test_code
actions_used += count
except Exception as e:
self.logger.error(f"Failed to generate test case for item: {path}. Error: {str(e)}")
self.logger.error(
f"Failed to generate test case for item: {path}. Error: {str(e)}"
)
break

print(
Expand All @@ -101,13 +103,15 @@ def generate_test_files(self, file_data, file_path):

def _process_item(self, path, path_item, folder_path):
print(f"\n{'=' * 50}\nProcessing Item: {path}\n{'=' * 50}")
file_path = path.replace('/', '_').replace('{', '').replace('}', '')
file_path = path.replace("/", "_").replace("{", "").replace("}", "")
test_file_path = self._prepare_test_file_path(file_path, folder_path)
test_code = ""
totalcount = 0
for method, method_code in path_item.items():
print("Processing method:", method)
individual_test_code, count = self.generate_ai_tests(path, method, method_code)
individual_test_code, count = self.generate_ai_tests(
path, method, method_code
)
test_code += individual_test_code
totalcount += count

Expand All @@ -128,14 +132,18 @@ def _prepare_test_file_path(self, path, folder_path):
return test_file_path

def generate_ai_tests(self, path, method, method_code):
print(f"• Generating AI tests for {method.upper()} {path} ...")
test_generation_prompt = API_METHOD_PROMPT.format(path=path, method=method, method_code=method_code, base_url=self.base_url)
response, usage = self.provider.chat_completion_with_json(test_generation_prompt, model="default")
print(f"• Generating AI tests for {method.upper()} {path} ...")
test_generation_prompt = API_METHOD_PROMPT.format(
path=path, method=method, method_code=method_code, base_url=self.base_url
)
response, usage = self.provider.chat_completion_with_json(
test_generation_prompt, model="default"
)
self.update_usage(usage)
test_code = extract_code_from_markdown(response)
print(f" ✓ AI tests generated successfully for {method.upper()} {path}")
self.log_step("Generate AI tests", f"Generated test code:\n{response}")
return test_code, 1
return test_code, 1

def run_tests(self, test_file=None):
runner = APITestRunner(self.output_folder)
Expand Down Expand Up @@ -185,7 +193,7 @@ def log_step(self, step_name, data):

def _write_test_file(self, test_file_path, test_code):
print("• Writing test file...")
try:
try:
with open(test_file_path, "w") as test_file:
test_file.write(test_code)
print(" ✓ Test file written successfully")
Expand All @@ -196,4 +204,3 @@ def _write_test_file(self, test_file_path, test_code):
def update_usage(self, usage):
self.total_usage = self.provider.update_usage(self.total_usage, usage)
print(f"@ Token usage: current_step: {usage}, total: {self.total_usage}")

4 changes: 1 addition & 3 deletions kaizen/generator/code_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def fix_code(
)

if not self.provider.is_inside_token_limit(PROMPT=fix_prompt):
self.logger.warning(
f"Fix prompt for issue exceeds token limit. Skipping."
)
self.logger.warning(f"Fix prompt for issue exceeds token limit. Skipping.")
raise Exception("File Size too big!")

resp, usage = self.provider.chat_completion_with_json(
Expand Down
2 changes: 0 additions & 2 deletions kaizen/generator/e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ def generate_module_tests(self, web_content: str, test_modules: dict, web_url: s
return ui_tests, self.total_usage

def store_tests_files(self, json_tests: list, folder_path: str = ""):

if not folder_path:
folder_path = output.get_parent_folder()

Expand All @@ -151,7 +150,6 @@ def store_tests_files(self, json_tests: list, folder_path: str = ""):
self.logger.info("Successfully store the files")

def store_module_files(self, module_data: list, folder_path: str = ""):

if not folder_path:
folder_path = output.get_parent_folder()

Expand Down
1 change: 0 additions & 1 deletion kaizen/generator/pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def _process_files_generator(
filename.split(".")[-1] not in parser.EXCLUDED_FILETYPES
and patch_details is not None
):

diff_parts.append(
f"\n---->\nFile Name: {filename}\nPatch Details: \n{patch_details}"
)
Expand Down
6 changes: 6 additions & 0 deletions kaizen/reviewer/code_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,15 @@ def review_pull_request(
ignore_deletions=False,
custom_context: str = "",
check_sensetive: bool = False,
custom_rules: str = "",
) -> ReviewOutput:
self.ignore_deletions = ignore_deletions
self.files_processed = 0
self.provider.system_prompt = (
CODE_REVIEW_SYSTEM_PROMPT
+ "\nAlways mark the issues which following rule at high and above with 8+ severity.\n"
+ custom_rules
)
prompt = (
CODE_REVIEW_PROMPT.format(
CODE_DIFF=parser.patch_to_combined_chunks(
Expand Down

0 comments on commit d4b0b9a

Please sign in to comment.