Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Adds a test for search-based usages#63610

Merged
kritzcreek merged 4 commits into
mainfrom
christoph/test-syntactic-usages
Jul 10, 2024
Merged

Adds a test for search-based usages#63610
kritzcreek merged 4 commits into
mainfrom
christoph/test-syntactic-usages

Conversation

@kritzcreek

@kritzcreek kritzcreek commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

Closes https://linear.app/sourcegraph/issue/GRAPH-726/test-syntactic-and-search-based-usages

Testing just the search-based usages just requires mocking the SearchClient, which works out nicely.

Test plan

The whole PR is just a test

@kritzcreek kritzcreek requested a review from keynmol July 3, 2024 11:21
@cla-bot cla-bot Bot added the cla-signed label Jul 3, 2024
@github-actions github-actions Bot added team/graph Graph Team (previously Code Intel/Language Tools/Language Platform) team/product-platform labels Jul 3, 2024
@kritzcreek kritzcreek force-pushed the christoph/test-syntactic-usages branch from b4728e3 to 40c5f0d Compare July 3, 2024 11:22
Comment on lines +60 to +64
mockRepoStore := defaultMockRepoStore()
mockLsifStore := NewMockLsifStore()
mockUploadSvc := NewMockUploadService()
mockGitserverClient := gitserver.NewMockClient()
mockSearchClient := client.NewMockSearchClient()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO if we are to have useful tests (or even one), we should think more carefully about what we mock.

For example, I don't think mocking repostore is necessary - it can be easily created from test DB, and it'd be good to pre-seed it with actual data: https://github.com/sourcegraph/sourcegraph/blob/main/internal/codeintel/syntactic_indexing/internal/testutils/testutils.go#L15

This will aid debugging greatly, and reduce the amount of mocked code we run through.

Of the things you mock here I would only mock gitserver, as it relies on external state and network to clone repositories - the rest I believe can be constructed using available New* methods.

The process of filling out those constructors is very annoying but as this PR stands I think it will be hard to maintain and doesn't actually give much confidence in tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the testutils you linked, but that seems like a bad idea? By writing INSERT statements you're actually tying the test to the concrete database schema, when the whole point of the Repo pattern is to abstract from that, so that you can test a service against just the Repo interface and evolve the underlying schema if you have to.

FTR I don't think the mockRepoStore in here is the problem, because all we're mocking is a single GetByID call which is very unlikely to break in the future.
The mocked gitserver and lsifstore are more problematic because they're mocking things 4-5 calls deep from getSearchBasedUsages in service.go.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the actual way the database state is set up is not the important bit here. The important bit is that we use service implementations that interact with that state.

If it's easier to rewrite the InsertRepo in terms of just purely using RepoStore - that's great, as long as when the tests for this code run they actually use the real RepoStore talking to the real database.

when the whole point of the Repo pattern is to abstract from that, so that you can test a service against just the Repo interface and

Despite us using SQL to hydrate the state, the code under test still goes through the RepoStore interface. If the SQL scripts are broken and violate Repo's own internal logic, it will show up in the tests.

Using SQL helps circumvent the potentially much larger "full" state that RepoStore requires for each of its methods – but if we can get rid of it, that'd be even better.

I would suggest starting with setting up lsifstore – gitserver will be much more annoying to fake

@kritzcreek kritzcreek force-pushed the christoph/test-syntactic-usages branch 2 times, most recently from 685c291 to d416a1b Compare July 9, 2024 03:19
@kritzcreek

Copy link
Copy Markdown
Contributor Author

I've decided to only test search-based usages in this PR, as syntactic search will require even more mocking after I finish and merge https://github.com/sourcegraph/sourcegraph/pull/63630
After pulling apart search-based usages a little bit I was able to test the core logic with only mocking search client, which I think turned out quite nicely.
PTAL

@kritzcreek kritzcreek requested a review from keynmol July 9, 2024 03:30
@kritzcreek kritzcreek changed the title Adds a test for syntactic and search-based usages Adds a test for search-based usages Jul 10, 2024
@kritzcreek kritzcreek force-pushed the christoph/test-syntactic-usages branch from d416a1b to 9113d38 Compare July 10, 2024 06:11
Comment on lines 97 to 99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to our convo about testing libraries - in a lot of other code this block would become require.NoError(t, searchErr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builder warms my crusty JVM-infested heart

@kritzcreek kritzcreek force-pushed the christoph/test-syntactic-usages branch from b500206 to 84853aa Compare July 10, 2024 13:12
@kritzcreek kritzcreek enabled auto-merge (squash) July 10, 2024 13:12
@kritzcreek kritzcreek merged commit d3df71e into main Jul 10, 2024
@kritzcreek kritzcreek deleted the christoph/test-syntactic-usages branch July 10, 2024 13:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed team/graph Graph Team (previously Code Intel/Language Tools/Language Platform) team/product-platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants