Repository: dbt-labs/dbt-core

Issue:
Variables not picked up by tests
### Describe the bug
Compilation fails when using variables defined in dbt_project.yaml in tests.

This was working prior to the upgrade to 0.20.0

### Steps To Reproduce
Variable in dbt_project.toml

```yaml
vars:
  signal_dbt:
    # the number of days we are legally allowed to retain data
    tracking_event_retention_period_days: 1460
```

Test:

```yaml
        tests:
          - not_null:
              where: 'day >= CURRENT_DATE - interval ''{{ var("tracking_event_retention_period_days") }} days'''
```

### Expected behavior
The variable to be used from dbt_project.toml

### Screenshots and log output

```
Running with dbt=0.20.0
Encountered an error:
Compilation Error
  Invalid test config given in models/marts/core/core.yml:
  	Required var 'tracking_event_retention_period_days' not found in config:
  Vars supplied to <Configuration> = {}
  	@: UnparsedNodeUpdate(original_file_path='model...ne)
```

### System information
**Which database are you using dbt with?**
- [ ] postgres
- [x] redshift
- [ ] bigquery
- [ ] snowflake
- [ ] other (specify: ____________)


**The output of `dbt --version`:**
```
installed version: 0.20.0
   latest version: 0.20.0

Up to date!

Plugins:
  - redshift: 0.20.0
  - postgres: 0.20.0

```

**The operating system you're using:**

Mac OS big Sur 11.2.3

**The output of `python --version`:**

3.8.5



Discussion:
Thanks for the quick report @Limess! I was able to reproduce this locally.

Given that this works if I pass the `var` from the CLI:
```
dbt test --vars 'tracking_event_retention_period_days: 1460'
```

This appears to be a bug with the rendering context used for test arguments here:
https://github.com/dbt-labs/dbt/blob/f460d275baa4df8cf8713f27e198a3577d6ea71c/core/dbt/parser/schema_test_builders.py#L231

That comes from:
https://github.com/dbt-labs/dbt/blob/f460d275baa4df8cf8713f27e198a3577d6ea71c/core/dbt/parser/schemas.py#L291-L293

It looks like we're pulling in `self.root_project.cli_vars` there. At the same time, `self.root_project.vars.vars` does contain `{'signal_dbt': {'tracking_event_retention_period_days': 1460}}`, so I'm not sure why it's missing in the resulting context.

I'm hopeful that this is a quick fix, and something we could easily include in a patch release.
We are experiencing the same issue when testing an upgrade to dbt .20 as it's very common behavior for us to use vars defined in dbt_project.yml in our tests.

An example of a test that is failing for us:

`
      - dbt_utils.expression_is_true:
          severity: warn
          tags:
            - known_fail_or_warn
          expression: "parent_classification IN ('Revenue', 'Expense')"
          condition: "dim_financial_account_key <> {{ var('unknown_key') }}"
`

Sorry, it doesn't seem to be preserving the appropriate indentation, but the indentation is correct in the .yml file.
@kwigley could you please look into this and see what might have caused the regression?

Resolving patch:
diff --git a/core/dbt/parser/schemas.py b/core/dbt/parser/schemas.py
--- a/core/dbt/parser/schemas.py
+++ b/core/dbt/parser/schemas.py
@@ -171,15 +171,15 @@ def __init__(
             self.project.config_version == 2
         )
         if all_v_2:
-            ctx = generate_schema_yml(
+            self.render_ctx = generate_schema_yml(
                 self.root_project, self.project.project_name
             )
         else:
-            ctx = generate_target_context(
+            self.render_ctx = generate_target_context(
                 self.root_project, self.root_project.cli_vars
             )
 
-        self.raw_renderer = SchemaYamlRenderer(ctx)
+        self.raw_renderer = SchemaYamlRenderer(self.render_ctx)
 
         internal_package_names = get_adapter_package_names(
             self.root_project.credentials.type
@@ -287,17 +287,13 @@ def _parse_generic_test(
         tags: List[str],
         column_name: Optional[str],
     ) -> ParsedSchemaTestNode:
-
-        render_ctx = generate_target_context(
-            self.root_project, self.root_project.cli_vars
-        )
         try:
             builder = TestBuilder(
                 test=test,
                 target=target,
                 column_name=column_name,
                 package_name=target.package_name,
-                render_ctx=render_ctx,
+                render_ctx=self.render_ctx,
             )
         except CompilationException as exc:
             context = _trimmed(str(target))


Test patch:
diff --git a/test/integration/008_schema_tests_test/models-v2/render_test_arg_models/model.sql b/test/integration/008_schema_tests_test/models-v2/render_test_cli_arg_models/model.sql
similarity index 100%
rename from test/integration/008_schema_tests_test/models-v2/render_test_arg_models/model.sql
rename to test/integration/008_schema_tests_test/models-v2/render_test_cli_arg_models/model.sql
diff --git a/test/integration/008_schema_tests_test/models-v2/render_test_arg_models/schema.yml b/test/integration/008_schema_tests_test/models-v2/render_test_cli_arg_models/schema.yml
similarity index 100%
rename from test/integration/008_schema_tests_test/models-v2/render_test_arg_models/schema.yml
rename to test/integration/008_schema_tests_test/models-v2/render_test_cli_arg_models/schema.yml
diff --git a/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/model.sql b/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/model.sql
new file mode 100644
--- /dev/null
+++ b/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/model.sql
@@ -0,0 +1 @@
+select 1 as id
diff --git a/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/schema.yml b/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/schema.yml
new file mode 100644
--- /dev/null
+++ b/test/integration/008_schema_tests_test/models-v2/render_test_configured_arg_models/schema.yml
@@ -0,0 +1,7 @@
+version: 2
+
+models:
+  - name: model
+    tests:
+      - equivalent:
+          value: "{{ var('myvar', 'baz') }}-bar"
diff --git a/test/integration/008_schema_tests_test/test_schema_v2_tests.py b/test/integration/008_schema_tests_test/test_schema_v2_tests.py
--- a/test/integration/008_schema_tests_test/test_schema_v2_tests.py
+++ b/test/integration/008_schema_tests_test/test_schema_v2_tests.py
@@ -331,14 +331,14 @@ def test_postgres_quote_required_column(self):
         self.assertEqual(len(results), 2)
 
 
-class TestVarsSchemaTests(DBTIntegrationTest):
+class TestCliVarsSchemaTests(DBTIntegrationTest):
     @property
     def schema(self):
         return "schema_tests_008"
 
     @property
     def models(self):
-        return "models-v2/render_test_arg_models"
+        return "models-v2/render_test_cli_arg_models"
 
     @property
     def project_config(self):
@@ -356,6 +356,32 @@ def test_postgres_argument_rendering(self):
         self.run_dbt(['test'], expect_pass=False)
 
 
+class TestConfiguredVarsSchemaTests(DBTIntegrationTest):
+    @property
+    def schema(self):
+        return "schema_tests_008"
+
+    @property
+    def models(self):
+        return "models-v2/render_test_configured_arg_models"
+
+    @property
+    def project_config(self):
+        return {
+            'config-version': 2,
+            "macro-paths": ["macros-v2/macros"],
+            'vars': {
+                'myvar': 'foo'
+            }
+        }
+
+    @use_profile('postgres')
+    def test_postgres_argument_rendering(self):
+        results = self.run_dbt()
+        self.assertEqual(len(results), 1)
+        results = self.run_dbt(['test'])
+        self.assertEqual(len(results), 1)
+
 class TestSchemaCaseInsensitive(DBTIntegrationTest):
     @property
     def schema(self):