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

UnqueryableQueryMock::all() returns Exception but 0 count is expected #41

Open
fcaldarelli opened this issue Feb 6, 2019 · 14 comments
Open
Labels

Comments

@fcaldarelli
Copy link
Member

$this->assertCount(0, $provider->getModels());

Here we expect that count is 0. But UnqueryableQueryMock::all() returns an Exception mandatory.

https://github.com/FabrizioCaldarelli/db/blob/b1bb19a4bafbe5a84aa62388cd3f0e81b5db809f/tests/unit/UnqueryableQueryMock.php#L28

@fcaldarelli fcaldarelli changed the title Query::all() returns Exception but 0 count is expected UnqueryableQueryMock::all() returns Exception but 0 count is expected Feb 6, 2019
@fcaldarelli
Copy link
Member Author

fcaldarelli commented Feb 7, 2019

I found the problem, it is about how prepareModels() has changed.

In current ActiveDataProvider version, prepareModels() calls prepareQuery(). When there are not records, $query->emulateExecution(); is called and anyway the methods returns $query object.

$query->emulateExecution();

In previous implementation, prepareModels() returned an empty array where there are not records:

This is from last commit, where @GHopperMSK wanted to explicit $query content to simplify debug activities.

bed8118

We can solve checking emulateExecution property that it has been used to track this case, from last commit:

    protected function prepareModels()
    {
        $query = $this->prepareQuery();
        return ($query->emulateExecution) ? [] : $query->all($this->db);
    }

@samdark samdark added the type:bug Bug label Feb 7, 2019
@np25071984
Copy link
Contributor

I don't think it is a good idea to return an empty array. Now we return $query object and it is quite convenient in all cases. If the query is empty, it sets emulate mode witch protects the DB from unnecessary requests. If the query isn't empty, it works as usual. We don't need to check the results every time, we just work with obtained object.

Maybe we should just adapt the test to new conditions?

@fcaldarelli
Copy link
Member Author

fcaldarelli commented Feb 7, 2019

I don't think it is a good idea to return an empty array. Now we return $query object and it is

In previous implementation, prepareModels() did the same work that you now do with two methods, so it made sense that it returned an empty array, in that condition.

@np25071984
Copy link
Contributor

In previous implementation, prepareModels() did the same work that you now do with two methods

If so, then how test $this->assertCount(0, $provider->getModels()); passed successfully?

@fcaldarelli
Copy link
Member Author

I have explained in previous post, because prepareModels() returned an empty array:

and test succeded.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2019

This should be fixed in UnqueryableQueryMock - it should not throw exception if emulateExecution is enabled.

@fcaldarelli
Copy link
Member Author

In this case it is not about emulateExecution , because it has been added from last commit.

UnqueryableQueryMock throw an exception independently from emulateExecution.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2019

UnqueryableQueryMock throw an exception independently from emulateExecution.

Yes, and that is the problem.

@fcaldarelli
Copy link
Member Author

fcaldarelli commented Feb 7, 2019

Yes, and that is the problem.

Why? In previous release it worked fine, because as class name says it has to test query not queryable.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2019

The purpose of this mock is to ensure that no actual query will be performed. With emulateExecution there will be not actual query, there should be no exception in this case.

@fcaldarelli
Copy link
Member Author

Take a look at differences from last commit (not working) and previous (working).

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2019

@FabrizioCaldarelli I don't get your point. These changes are correct, the tests should be fixed to not throw exception when there is no need for it. It was already done in yiisoft/yii2#17073

@fcaldarelli
Copy link
Member Author

I showed how keep current tests and current changes together.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2019

Why would you want add additional conditions to (correct) code instead of fixing (incorrect) tests? It is actually a bug that UnqueryableQueryMock throws exception for query with emulateExecution, it should be changed anyway.

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

No branches or pull requests

4 participants