This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
enterprise/internal/insights: add basic store package + testing infrastructure #17733
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func testInsights(t *testing.T, ctx context.Context, s *Store, clock func() time.Time) { | ||
| // TODO: write tests against the store once it is implemented | ||
| // https://github.com/sourcegraph/sourcegraph/issues/17218 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "os" | ||
| "os/user" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/internal/database/dbconn" | ||
| "github.com/sourcegraph/sourcegraph/internal/database/dbutil" | ||
| "github.com/sourcegraph/sourcegraph/internal/timeutil" | ||
| ) | ||
|
|
||
| func TestIntegration(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip() | ||
| } | ||
|
|
||
| t.Parallel() | ||
|
|
||
| getTimescaleDB := func(t testing.TB) *sql.DB { | ||
| // Setup TimescaleDB for testing. | ||
| username := "" | ||
| if user, err := user.Current(); err == nil { | ||
| username = user.Username | ||
| } | ||
| timescaleDSN := dbutil.PostgresDSN("codeinsights", username, os.Getenv) | ||
| db, err := dbconn.New(timescaleDSN, "insights-test-"+strings.Replace(t.Name(), "/", "_", -1)) | ||
| if err != nil { | ||
| t.Log("") | ||
| t.Log("README: To run these tests you need to have the codeinsights TimescaleDB running:") | ||
| t.Log("") | ||
| t.Log("$ ./dev/codeinsights-db.sh &") | ||
| t.Log("$ export CODEINSIGHTS_PGDATASOURCE=postgres://postgres:password@127.0.0.1:5435") | ||
| t.Log("") | ||
| t.Log("Or skip them with 'go test -short'") | ||
| t.Log("") | ||
| t.Fatalf("Failed to connect to codeinsights database: %s", err) | ||
| } | ||
| if err := dbconn.MigrateDB(db, dbconn.CodeInsights); err != nil { | ||
| t.Fatalf("Failed to perform codeinsights database migration: %s", err) | ||
| } | ||
| return db | ||
| } | ||
|
|
||
| t.Run("Integration", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| clock := timeutil.Now | ||
| store := NewWithClock(getTimescaleDB(t), clock) | ||
| t.Run("Insights", func(t *testing.T) { testInsights(t, ctx, store, clock) }) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "time" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/internal/database/basestore" | ||
| "github.com/sourcegraph/sourcegraph/internal/database/dbutil" | ||
| "github.com/sourcegraph/sourcegraph/internal/timeutil" | ||
| ) | ||
|
|
||
| // Store exposes methods to read and write code insights domain models from | ||
| // persistent storage. | ||
| type Store struct { | ||
| *basestore.Store | ||
| now func() time.Time | ||
| } | ||
|
|
||
| // New returns a new Store backed by the given Timescale db. | ||
| func New(db dbutil.DB) *Store { | ||
| return NewWithClock(db, timeutil.Now) | ||
| } | ||
|
|
||
| // NewWithClock returns a new Store backed by the given db and | ||
| // clock for timestamps. | ||
| func NewWithClock(db dbutil.DB, clock func() time.Time) *Store { | ||
| return &Store{Store: basestore.NewWithDB(db, sql.TxOptions{}), now: clock} | ||
| } | ||
|
|
||
| var _ basestore.ShareableStore = &Store{} | ||
|
|
||
| // Handle returns the underlying transactable database handle. | ||
| // Needed to implement the ShareableStore interface. | ||
| func (s *Store) Handle() *basestore.TransactableHandle { return s.Store.Handle() } | ||
|
|
||
| // With creates a new Store with the given basestore.Shareable store as the | ||
| // underlying basestore.Store. | ||
| // Needed to implement the basestore.Store interface | ||
| func (s *Store) With(other basestore.ShareableStore) *Store { | ||
| return &Store{Store: s.Store.With(other), now: s.now} | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.