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

Consider having AssertTranslation that tests both predicate and projection #35318

Open
roji opened this issue Dec 11, 2024 · 0 comments
Open

Comments

@roji
Copy link
Member

roji commented Dec 11, 2024

When testing e.g. a function translation, for full coverage we need to check both predicate translation (with the function in Where()), and projection (with the function in Select()).

  • Testing Select() is insufficient because EF client evaluates final select (and so translation isn't even tested); even without this, Select() wouldn't check that the translation returns the correct relational value, only that what it returns can be projected out and read.
  • Testing Where() ensures that actual translation occurs, and also that the proper relational value is produced (since we compare the translation result to e.g. some constant). However, it does not check that e.g. the correct CLR type is set on the resulting SqlExpression; thus, a correctly translated function can fail when projected out of the database.

In the base, we've sometimes done both Where and Select tests for the same function translation. We've done this very sporadically and non-systematically, with very few translations getting both and most getting only one or the other.

To do this more systematically, we could have an AssertTranslation, which accepts a query base, a lambda to be tested, and an expected result value for the Where test; AssertTranslation would then do AssertQuery twice internally, once for Where and once for Select. For example:

await AssertTranslation(async, ss => ss.Set<BasicTypesEntity>(), b => Math.Cos(b.Double), b => 8);

This would internally perform the following two things:

await AssertQuery(async, ss => ss.Set<BasicTypesEntity>().Where(b => Math.Cos(b.Double) == 8);
await AssertQuery(async, ss => ss.Set<BasicTypesEntity>().Select(b => Math.Cos(b.Double));

In the meantime, where we think it's important to test both Where and Select, we should simply combine both into the same test; so the single test that covers the translation for Func() can call AssertQuery twice. If and when we have AssertTranslation, it would basically do the same.

/cc @maumar

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

No branches or pull requests

1 participant