Report diagnostic for use of (some) Assert methods with IQuerable<T>
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
The issue refers to MSTEST0037, an existing analyzer rule that already reports some Assert calls. Start by finding that rule and how it inspects the argument type, then check whether it can be extended to IQueryable<T> and the listed methods (IsEmpty, IsNotEmpty, HasCount, ContainsSingle). Done means the listed BAD cases report a diagnostic while the GOOD Count/Any cases stay silent, with tests for both.
Written by the indexing model from the issue text.
Description
Summary
MSTEST0037 is currently not reported when the argument is an IQueryable<T> or a result of a method invoked on an IQueryable<T> (eg. IQueryable<T>.Count(...)).
This is great of course!
That said, people till tend or risk to use Assert.IsNotEmpty(...), Assert.IsEmpty(...) , Assert.ContainsSingle(...) or Assert.HasCount(...) with an IQueryable<T>.
This will of course lead to a serious performance degradation as filtering will be done client-side and counting of the number of results will be done on the dataset returned by the database (instead of just having off a SELECT COUNT(1) FROM .... to the database).
I haven't had time to review all Assert method. I'm sure this applies to other methods as well.
Would it be possible to have an analyzer rule that reports a diagnostic for at least the following cases:
private IQueryable<PERSON> GetPersons()
{
// DB stuff
}
[TestMethod]
public void SomeTest()
{
var persons = GetPersons();
Assert.AreEqual(1, persons.Count(p => p.NAME == "John")); // GOOD
Assert.HasCount(1, persons.Where(p => p.NAME == "John")); // BAD
Assert.ContainsSingle(persons.Where(p => p.NAME == "John")); // BAD
Assert.AreEqual(0, persons.Count(p => p.NAME == "John")); // GOOD
Assert.IsEmpty(persons.Where(p => p.NAME == "John")); // BAD
Assert.AreNotEqual(0, persons.Count(p => p.NAME == "John")); // GOOD
Assert.IsNotEmpty(persons.Where(p => p.NAME == "John")); // BAD
Assert.IsTrue(persons.Any(p => p.NAME == "John")); // GOOD
Assert.IsNotEmpty(persons.Where(p => p.NAME == "John")); // BAD
Assert.IsFalse(persons.Any(p => p.NAME == "John")); // GOOD
Assert.IsEmpty(persons.Where(p => p.NAME == "John")); // BAD
}
Any call to Assert.ContainsSingle(...) may be ok if caller actually uses the returned value.
- Dominant language
- C#
- Stars
- 1k
- Forks
- 314
- Avg merge
- 8h 39m
- Merged PRs (30d)
- 446
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/testfx
-
type/automation type/tech-debt
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
[file-diet] Refactor TestHostControllersTestHost.ProcessLifecycle.cs (613 lines) into focused filesPossibly taken @Evangelink claimed this 1 day ago. Opentype/automation type/tech-debt
microsoft/testfx#11905 · 1 reaction · 2 assignees ·
Maintainers usually reply within 1 day
-
area/server-mode-jsonrpc
Difficulty 5/5 Over a week Newbie friendliness 8/100
Maintainers usually reply within 1 day
-
testing type/automation type/test-gap
Difficulty 2/5 1-3 hours Newbie friendliness 20/100
Maintainers usually reply within 1 day
-
area/performance type/automation
Difficulty 5/5 Over a week Newbie friendliness 20/100
Maintainers usually reply within 1 day
All issues in microsoft/testfx
Similar issues
-
python triage
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
microsoft/semantic-kernel#14590 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
nkdAgility/azure-devops-migration-tools#3226 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Status: New
Difficulty 1/5 Under an hour Newbie friendliness 80/100
dnnsoftware/Dnn.Platform#7556 · 1 reaction ·
Maintainers usually reply within 1 day