你将如何测试一个可以为查询添加数十个不同过滤器的类?

I'm trying to migrate a page from an old system to a new one were we try to unit test everything.

The page contains around 40 advanced search filters to search inside our products table and its related table.

Some of them are simple field search, but others create new joins or sub queries.

I'm at a loss is how to unit test the repository, the method receives an array of user provided filters and should return the corresponding products, inside it uses Doctrine ORM and DQL to generate all the queries.

I though of the following:

  • Unit test filters one by one
    • Issue: Takes a lot of tests and does not cover filters mixing together.
  • Compare outputted MySQL code to expected output
    • Issue: Not flexible, and does not test the actual result.

So how would you go about testing this?

Unless you can guarantee the order of the code, then checking the MySQL code is as expected will take a long time to check. Instead you could:

  1. Add each filter individually. Check the query is what is expected, and that it will execute. Ideally check the records for an expected record
  2. Add multiple items together. Check that the query at least contains the code you expect to be added (so you don't have to guarantee the order it is added), and that the query executes.

For part 2, if you can automate this to run the checks, then even better. Checking all possible additions is going to be near impossible, as there are 1,099,511,627,775 combinations from selecting just 1 filter, right through to selecting all 40. Each of those would need to be tested for generating SQL containing what you expect, but also run correctly.

Sometimes unit testing everything is impossible. Sometimes you've got to just test "enough", however you measure that.