Skip to content

Conversation

@cmgoffena13
Copy link

@cmgoffena13 cmgoffena13 commented Sep 4, 2025

I'm having trouble fleshing out the test due to the path having to point to the example project. Since I have to trigger the pytest from the root it is trying to find the /tests directory in the root rather than the example project root. Any help on resolving the path issue would be appreciated so I can finish the test. (EDIT: Figured out how to switch the contexts, let me know if there's anything that needs adjustment!)

@CLAassistant
Copy link

CLAassistant commented Sep 4, 2025

CLA assistant check
All committers have signed the CLA.

@cmgoffena13
Copy link
Author

@VaggelisD @georgesittas - alright, took my first swing at it if either of you want to take a look.

  1. Added in a property method to ModelTestMetadata to get model_name from the body easily
  2. Added model_test_metadata and models_with_tests to be added when a project loads so a Set of model names is only calculated once upon load.
  3. Added self._model_test_metadata and self._models_with_tests to the context and Context::load() brings it in from loaded projects.
  4. Made sure the Context::test() utilizes the eagerly loaded test metadata instead of lazily loading it again

I did surface models_with_tests as a property in the Context to access for the linter rule.

Let me know if you want me to change anything!

Comment on lines 23 to 25
@property
def model_name(self) -> str:
return self.body["model"]
Copy link
Contributor

@georgesittas georgesittas Nov 19, 2025

Choose a reason for hiding this comment

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

We should be a bit more conservative here with the lookup. IIRC the body is validated later at runtime, when the tests are actually ran, by which time it'll be too late if the test is missing its name. We don't wanna throw a KeyError in that case.

Suggested change
@property
def model_name(self) -> str:
return self.body["model"]
@property
def model_name(self) -> str:
return self.body.get("model", "")

Copy link
Author

Choose a reason for hiding this comment

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

Gotcha, quick fix, I'll get it done

Comment on lines 67 to 68
model_test_metadata: t.List[ModelTestMetadata]
models_with_tests: t.Set[str]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this an attribute of the LoadedProject? We just need the metadata right? Then you other consumers (e.g., the context, or linter) can extract this info and use it as needed.

Copy link
Author

@cmgoffena13 cmgoffena13 Nov 20, 2025

Choose a reason for hiding this comment

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

Yeah, good catch, I can move this to the linter and keep the core code clean. Or actually, the context might be a little better, but I'm open to both

Copy link
Contributor

@georgesittas georgesittas Nov 20, 2025

Choose a reason for hiding this comment

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

Make sure this is computed once and is kept in the context so that we don't recompute it for every model. It is the same value for all models, anyway.

raise SQLMeshError(f"Gateway '{gateway}' not found in the available engine adapters.")
return self.engine_adapter

def _filter_preloaded_tests(
Copy link
Contributor

Choose a reason for hiding this comment

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

What motivated this addition? Seems like it's something that doesn't need to be included in the scope of this PR.

Copy link
Author

Choose a reason for hiding this comment

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

If I remember correctly, the test() method was setup to still lazily load the tests, so I had to implement this to ensure it maintained filtering functionality and utilized the eagerly loaded tests instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, makes sense. I suggest renaming this to _select_tests instead, then.

Copy link
Contributor

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

Btw, let's make sure that we track calls to load_model_tests and refactor as needed. For example, in magics.py, I think we should be able to skip that call since we already have the loaded context which contains all tests. Another example is in lsp/context.py.

Comment on lines 2813 to 2818
filtered_tests.extend(
[t for t in test_meta if t.path == test_path and t.test_name == test_name]
)
else:
test_path = Path(test)
filtered_tests.extend([t for t in test_meta if t.path == test_path])
Copy link
Contributor

Choose a reason for hiding this comment

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

These iterations over test_meta look costly. Can we store a mapping test_name -> test_meta instead of keeping everything in a list and having to walk it every time?

Copy link
Author

Choose a reason for hiding this comment

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

Just pushed, I added in two dictionaries, one for the fully qualified names and another for the Paths so they're just lookups. Calculated once in the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants