Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/prompt to generate tests #16

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

sungchun12
Copy link
Owner

No description provided.

import re
import os

def generate_test_yaml(model_name, column_names, active_file_path, tests_data):
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@sungchun12
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants