Feature/prompt to generate tests#16
Draft
sungchun12 wants to merge 22 commits into
Draft
Conversation
…r/modular-design
…riptions without tests.
sungchun12
commented
Mar 28, 2023
| import re | ||
| import os | ||
|
|
||
| def generate_test_yaml(model_name, column_names, active_file_path, tests_data): |
Owner
Author
There was a problem hiding this comment.
There are more efficient ways to do this. Example code from GPT.
import os
import re
import yaml
from typing import List, Dict
def generate_test_yaml(model_name: str, column_names: List[str], active_file_path: str, tests_data: List[Dict[str, str]]) -> str:
yaml_files = {}
def add_tests_to_existing_columns(existing_column: Dict[str, List[str]], tests_to_add: List[str]) -> None:
tests = existing_column.get('tests', [])
for test in tests_to_add:
if test not in tests:
tests.append(test)
existing_column['tests'] = tests
def add_tests_to_new_columns(schema_yml_path: str, model_name: str, column: str, tests_to_add: List[str]) -> None:
with open(schema_yml_path, "r") as f:
schema_yml_data = yaml.safe_load(f)
for model in schema_yml_data.get("models", []):
if model["name"] == model_name:
if "columns" not in model:
model["columns"] = []
new_column = {
"name": column,
"description": f"A placeholder description for {column}",
"tests": tests_to_add,
}
model["columns"].append(new_column)
break
with open(schema_yml_path, "w") as f:
yaml.dump(schema_yml_data, f)
for column in column_names:
tests_to_add = ["unique", "not_null"] if re.search(r"(_id|_ID)$", column) else []
existing_tests = [data for data in tests_data if data['column'] == column]
if existing_tests:
for test_data in existing_tests:
yaml_file = test_data['file']
if yaml_file not in yaml_files:
with open(yaml_file, 'r') as f:
yaml_files[yaml_file] = yaml.safe_load(f)
models = yaml_files[yaml_file].get('models', [])
columns_data = [model.get('columns', []) for model in models if model['name'] == model_name]
existing_columns = [column_data for columns in columns_data for column_data in columns if column_data['name'] == column]
for existing_column in existing_columns:
add_tests_to_existing_columns(existing_column, tests_to_add)
else:
schema_yml_path = os.path.join(os.path.dirname(active_file_path), "schema.yml")
if os.path.exists(schema_yml_path):
add_tests_to_new_columns(schema_yml_path, model_name, column, tests_to_add)
return schema_yml_path
return next(iter(yaml_files))
Collaborator
There was a problem hiding this comment.
that's a good idea to separate the methods indeed!
I've been a bit weary of overusing GPTs code directly as it tends to be spaghetti complicated, but I think we'll always be able to handle when there's issues
Owner
Author
|
@xtomflo Let's hold off on this feature until we get the general look and feel in a great place. This is a nice to have in the context of v1 I'm realizing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.