The main reason I went without testing event- and command-handling when I implemented them is that they operated only on the main repositories (defined in
main.py and not redefined in any of the handling functions). This makes testing them awkward: either I'd have to mock out those repositories or test on the main repositories. Neither is a good option.
As so often happens, the problem is in a sort of inappropriate coupling: those handlers should not be tied to the choice of repositories in
main.py. In order to correct that, we need to build out some infrastructure that encodes the idea of there being different environments for things.
If you're not familiar with production vs. QA environments, the idea of test databases, or similar, now is a good time to Google around a bit for those.
For now our
Environments will be simple: just a bundle (dataclass) consisting of a
TaskRepository and a
CoordinatorRepository, both optional. (One reason to make them optional is that we might want to build out a test environment with just a
TaskRepository, having no need in the context of certain tests for a
This installment's work is to define that object, rewrite all the URLs to have the environment as a parameter, rewrite the various other functions (and tests downstream of these) to take in an environment as a parameter, and make sure the site still works.
Next post: Python task manager from scratch, part 46: Getting handlers under test