Query Builder Testing

I am creating a query builder that I want unit test.
I do not know how to do this, though.

It (currently) consists of two parts: itself QueryBuilder, which provides a free interface for building queries. And SqlConstructorthat takes care of building the actual SQL.

So basically, how can I check for "correctness"? Should I just check for keywords? (For example, selectis the first keyword in a select-type query?) I think that there are many important things for validation, such as the order in which the keywords appear, etc.

+3
source share
1 answer

You verify that the expected result is expected for this entry.

If I understand correctly, your QueryBuilder collects the details of the query, so make sure that the data structure containing these parts actually contains them when you add them through the QueryBuilder method. If he has a method addWhereClauseor something like that, check if this method is really what you entered into the body of the method, for example. write a test for example

public function testWhereMethodAddsExpressionToPartsArray()
{
    $expression = 'foo = "bar"';
    $this->sut->where($expression);
    $parts = $this->sut->getParts('where');
    $this->assertContains($expression, $parts);
}

For SqlConstructor, do the same, verify that the input it receives from the data structure filled with QueryBuilder (you may want to mock it) gives the expected result.

SQL, . , UnitTest, SQL, SQLGenerator SQL , , .

SQL - SQL. . ? SQL , .

:

+1

Source: https://habr.com/ru/post/1763748/


All Articles