AltimateAI / vscode-dbt-power-user

This extension makes vscode seamlessly work with dbt™: Auto-complete, preview, column lineage, AI docs generation, health checks, cost estimation etc
https://www.altimate.ai
MIT License
427 stars 84 forks source link

Documentation editor mess up tests when same test exist with different configs #1219

Closed saravmajestic closed 1 week ago

saravmajestic commented 2 weeks ago

Expected behavior

Actual behavior

Steps To Reproduce

I have two accepted values tests with two different configs but the Documentation Editor changed them to give them the same config.

before

tests:
          - accepted_values:
              values: ['deep_dish', 'detroit', 'thin_crust']
              config:
                severity: 'warn'
                where: "menu_item = 'pizza'"
          - accepted_values:
              values: ['gyro', 'sub', 'footlong']
              config:
                severity: 'warn'
                where: "menu_item = 'sandwich'"

after

- accepted_values:
              values:
                - deep_dish
                - detroit
                - thin_crust
              config: &a1
                severity: warn
                where: menu_item = 'pizza'
          - accepted_values:
              values:
                - gyro
                - sub
                - footlong
              config: *a1

Log output/Screenshots

No response

Operating System

MacOs

dbt version

1.7

dbt Adapter

any

dbt Power User version

any

Are you willing to submit PR?

saravmajestic commented 2 weeks ago

Sweep: In src\webview_provider\docsEditPanel.ts, function: getTestDataByColumn, fix the tests which has same test name with different config to be saved properly in yaml file

sweep-ai[bot] commented 2 weeks ago

🚀 Here's the PR! #1222

💎 Sweep Pro: You have unlimited Sweep issues

Actions

Step 1: 🔎 Searching

(Click to expand) Here are the code search results. I'm now analyzing these search results to write the PR. https://github.com/AltimateAI/vscode-dbt-power-user/blob/98595e706480dd59adf3061a41afcdfcfae4d99f/src/webview_provider/docsEditPanel.ts#L1-L868 https://github.com/AltimateAI/vscode-dbt-power-user/blob/98595e706480dd59adf3061a41afcdfcfae4d99f/src/services/dbtTestService.ts#L1-L317 https://github.com/AltimateAI/vscode-dbt-power-user/blob/98595e706480dd59adf3061a41afcdfcfae4d99f/README.md#L1-L102

Step 2: ⌨️ Coding

I'm going to follow the following steps to help you solve the GitHub issue:

  1. Modify the getTestDataByColumn method in docsEditPanel.ts to preserve existing test configurations when updating column tests in the documentation editor.
  2. Update the saveDocumentation case in the setupWebviewHooks method of docsEditPanel.ts to use the updated getTestDataByColumn method when saving column documentation.

Here are the changes we decided to make. I'm done making edits and now I'm just validating the changes using a linter to catch any mistakes like syntax errors or undefined variables:

src/webview_provider/docsEditPanel.ts

Modify the `getTestDataByColumn` method to preserve existing test configurations when updating column tests in the documentation editor.
--- 
+++ 
@@ -36,35 +36,10 @@
         return key === fullName;
       });

-      // If relationships test, set field and to
-      if (this.isRelationship(kwargs)) {
-        const { to, field } = kwargs;
-        return {
-          relationships: {
-            ...existingConfig?.["relationships"],
-            field,
-            to,
-          },
-        };
+      if (existingConfig) {
+        return existingConfig;
       }

-      // set values if test is accepted_values
-      if (this.isAcceptedValues(kwargs)) {
-        return {
-          accepted_values: {
-            ...existingConfig?.["accepted_values"],
-            values: kwargs.values,
-          },
-        };
-      }
-
-      if (existingConfig?.[fullName]) {
-        return {
-          [fullName]: existingConfig?.[fullName],
-        };
-      }
-
-      // Add extra config from external packages or test macros
       // Add extra config from external packages or test macros
       const testMetaKwargs = this.getTestMetadataKwArgs(kwargs, fullName);
       return testMetaKwargs || fullName;

src/webview_provider/docsEditPanel.ts

Update the `saveDocumentation` case in the `setupWebviewHooks` method to use the updated `getTestDataByColumn` method when saving column documentation.
--- 
+++ 
@@ -15,13 +15,11 @@
                                 isColumnNameEqual(yamlColumn.name, column.name),
                               );
                             if (existingColumn !== undefined) {
-                              // ignore tests from existing column, as it will be recreated in `getTestDataByColumn`
-                              const { tests, ...rest } = existingColumn;
                               return {
-                                ...rest,
+                                ...existingColumn,
                                 name: existingColumn.name,
                                 data_type: (
-                                  rest.data_type || column.type
+                                  existingColumn.data_type || column.type
                                 )?.toLowerCase(),
                                 description: column.description || undefined,
                                 ...this.getTestDataByColumn(

Step 3: 🔄️ Validating

Your changes have been successfully made to the branch sweep/documentation_editor_mess_up_tests_when. I have validated these changes using a syntax checker and a linter.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.