Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: change where mocks are generated #88

Merged
merged 3 commits into from
Jan 15, 2025
Merged

Conversation

volmedo
Copy link
Member

@volmedo volmedo commented Jan 14, 2025

#87 required the introduction of MockProviderIndex, which imports the providerindex package. At the same time, the tests in the providerindex package itself import the mocks package, where all mocks are generated currently. This causes an import cycle. More import cycles can be created in the future as we generate mocks for other interfaces.

There are two ways of implementing a future-proof solution for these cycles:

  • keep all mocks in the same package and move tests to their own packages. This has the advantage that mock code lives in a single location and doesn't clutter actual business logic code. The downside is that non-exported members and functions cannot be tested directly as the tests now call the code under testing as an external package. We currently have some white box tests that call non-exported functions and I don't think that's bad (if not mis-used).
  • generate mocks in the same package the interfaces they are mocking live in (except for external interfaces, i.e. those defined in dependencies used by the project). This spreads mock code files across the codebase, but allows test code to be in the same package than the code under test. It's not completely weird that mocks are offered by the same packages that export the relevant interfaces.

I decided to go with the second option in this case cause it allows the most flexibility when writing tests. That implied changing mock generation configuration, moving mocks around and updating the imports in test files using them.

@volmedo volmedo requested a review from hannahhoward January 14, 2025 17:45
@volmedo volmedo self-assigned this Jan 14, 2025
@volmedo volmedo merged commit 02a3619 into main Jan 15, 2025
8 checks passed
@volmedo volmedo deleted the vic/chore/reorganize-mocks branch January 15, 2025 15:27
volmedo added a commit that referenced this pull request Jan 16, 2025
Resolves storacha/project-tracking#171

Adds tests for the main `IndexingService.Query` function. The happy path
test is useful to understand the behavior of the service fetching the
different types of claims.

**Note for reviewers:** the PR is not as big as it seems. I changed some
interfaces dealing with `url.URL` to use `*url.URL`, which makes client
code a bit more readable and is more idiomatic. A lot of files were
touched just to update the type of the parameter/variable.

## Considerations

I think the happy path test is the most valuable. I also added some
basic tests for error conditions in the main dependencies. I thought of
adding a test confirming the function filters non-relevant claims, but
that is implemented by the `providerindex` package and tested there.

Please let me know if you think I'm missing any other interesting tests.

## Known issues

The addition of `MockProviderIndex` causes an import cycle in the tests
of the `providerindex` package. These tests import the `mocks` package
and the new mock imports the `providerindex` package to implement the
mock.

I think the best solution would be to change the mock generation
configuration so that mocks are created in the same package as the
interfaces they are mocking. That would avoid the cycle while allowing
tests to live in the same package as the functions they are testing
(which in turn enables testing non-exported members).

I'll take care of that in a separate PR (#88).
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants