The problem with a failing test was driving me nuts yesterday. And I made a classic Engineer mistake, I didn't take a step back to clear my head and recharge. I continued to work on the same problem till 1 AM, of course without a result, before I gave up. After some sleep and getting back to work with a structured approach, it took me less than 15 minutes today to validate my theory and figure out what the problem was... Damn, I'm happy I found it but all the time I wasted last night... Whatever, chasing the white rabbit and getting a bit lost happens to the best sometimes.
So, what was the issue? I want to know if a method was called with a specific set of args by using
assert_called_once_with
. Visually, the expected and actual values match. But I pass an object of
model.Project
and Mock checks if it's the same instance of the object, not if the content matches.
I creaet a project
object in my tests, and pagure.lib.tasks_services.trigger_ci_build()
does
the same. Even though the keys and values look identical, there are two different objects and the assertion fails.
To solve it, I have to Mock pagure.lib.query.get_authorized_project()
and inject the project
object
that's created in the test. Then the whole thing works as I want it. Next step: Refactoring to use model.PullRequest
object with access to all pull request related information instead of pull_request_id
and pull_request_uid
.
That worked pretty well, writing the tests before the actual code feels weird at first but today that was a producitivy booster. It's much easier to focus on the next feature or refactoring, while I don't have to worry that silently breaks something else. As long the other tests continue to pass, I'm still on track. I tried Test-Driven-Development before, but I guess bad habbits came back and I never went all-in with it for a longer session.
This Pull Request contains the current state and the outcome of my Hack Week project. I finished around 2/3 of what was planned, I hope to get the rest done in the next week or two.
I wrote a lot Python code before, including tests, but a couple days with an intensive focus helped a lot to brush up my knowledge. Good chances that I not waste time with such a stupid problem next time ;) This week re-affirmed that my passion is solving problems and writing code. I hope I can do that more regularly in future and have the opportunity to free up time and attend SUSE Hack Week 25 as well.