-
Notifications
You must be signed in to change notification settings - Fork 90
test: integration tests for sqlite
#195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MattiasMTS
merged 3 commits into
kndndrj:master
from
MattiasMTS:ms/integration-test-sqlite
Mar 12, 2025
Merged
Changes from all commits
Commits
Show all changes
3 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,161 @@ | ||
| package integration | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
| "testing" | ||
|
|
||
| "github.com/kndndrj/nvim-dbee/dbee/core" | ||
| th "github.com/kndndrj/nvim-dbee/dbee/tests/testhelpers" | ||
| "github.com/stretchr/testify/assert" | ||
| tsuite "github.com/stretchr/testify/suite" | ||
| tc "github.com/testcontainers/testcontainers-go" | ||
| ) | ||
|
|
||
| // SQLiteTestSuite is the test suite for the sqlite adapter. | ||
| type SQLiteTestSuite struct { | ||
| tsuite.Suite | ||
| ctr *th.SQLiteContainer | ||
| ctx context.Context | ||
| d *core.Connection | ||
| } | ||
|
|
||
| func TestSQLiteTestSuite(t *testing.T) { | ||
| tsuite.Run(t, new(SQLiteTestSuite)) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) SetupSuite() { | ||
| suite.ctx = context.Background() | ||
| tempDir := suite.T().TempDir() | ||
|
|
||
| params := &core.ConnectionParams{ID: "test-sqlite", Name: "test-sqlite"} | ||
| ctr, err := th.NewSQLiteContainer(suite.ctx, params, tempDir) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| suite.ctr, suite.d = ctr, ctr.Driver | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TeardownSuite() { | ||
| tc.CleanupContainer(suite.T(), suite.ctr) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldErrorInvalidQuery() { | ||
| t := suite.T() | ||
|
|
||
| want := "syntax error" | ||
|
|
||
| call := suite.d.Execute("invalid sql", func(cs core.CallState, c *core.Call) { | ||
| if cs == core.CallStateExecutingFailed { | ||
| assert.ErrorContains(t, c.Err(), want) | ||
| } | ||
| }) | ||
| assert.NotNil(t, call) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldCancelQuery() { | ||
| t := suite.T() | ||
| want := []core.CallState{core.CallStateExecuting, core.CallStateCanceled} | ||
|
|
||
| _, got, err := th.GetResultWithCancel(t, suite.d, "SELECT 1") | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, want, got) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldReturnManyRows() { | ||
| t := suite.T() | ||
|
|
||
| wantStates := []core.CallState{ | ||
| core.CallStateExecuting, core.CallStateRetrieving, core.CallStateArchived, | ||
| } | ||
| wantCols := []string{"id", "username"} | ||
| wantRows := []core.Row{ | ||
| {int64(1), "john_doe"}, | ||
| {int64(2), "jane_smith"}, | ||
| {int64(3), "bob_wilson"}, | ||
| } | ||
|
|
||
| query := "SELECT id, username FROM test_table" | ||
|
|
||
| gotRows, gotCols, gotStates, err := th.GetResult(t, suite.d, query) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.ElementsMatch(t, wantCols, gotCols) | ||
| assert.ElementsMatch(t, wantStates, gotStates) | ||
| assert.Equal(t, wantRows, gotRows) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldReturnOneRow() { | ||
| t := suite.T() | ||
|
|
||
| wantStates := []core.CallState{ | ||
| core.CallStateExecuting, core.CallStateRetrieving, core.CallStateArchived, | ||
| } | ||
| wantCols := []string{"id", "username"} | ||
| wantRows := []core.Row{{int64(2), "jane_smith"}} | ||
|
|
||
| query := "SELECT id, username FROM test_view" | ||
MattiasMTS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| gotRows, gotCols, gotStates, err := th.GetResult(t, suite.d, query) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.ElementsMatch(t, wantCols, gotCols) | ||
| assert.ElementsMatch(t, wantStates, gotStates) | ||
| assert.Equal(t, wantRows, gotRows) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldReturnStructure() { | ||
| t := suite.T() | ||
|
|
||
| var ( | ||
| wantSchema = "sqlite_schema" | ||
| wantSomeTable = "test_table" | ||
| wantSomeView = "test_view" | ||
| ) | ||
|
|
||
| structure, err := suite.d.GetStructure() | ||
| assert.NoError(t, err) | ||
|
|
||
| gotSchemas := th.GetSchemas(t, structure) | ||
| assert.Contains(t, gotSchemas, wantSchema) | ||
|
|
||
| gotTables := th.GetModels(t, structure, core.StructureTypeTable) | ||
| assert.Contains(t, gotTables, wantSomeTable) | ||
|
|
||
| gotViews := th.GetModels(t, structure, core.StructureTypeView) | ||
| assert.Contains(t, gotViews, wantSomeView) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldReturnColumns() { | ||
| t := suite.T() | ||
|
|
||
| want := []*core.Column{ | ||
| {Name: "id", Type: "INTEGER"}, | ||
| {Name: "username", Type: "TEXT"}, | ||
| {Name: "email", Type: "TEXT"}, | ||
| } | ||
|
|
||
| got, err := suite.d.GetColumns(&core.TableOptions{ | ||
| Table: "test_table", | ||
| Schema: "sqlite_schema", | ||
| Materialization: core.StructureTypeTable, | ||
| }) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, want, got) | ||
| } | ||
|
|
||
| func (suite *SQLiteTestSuite) TestShouldNoOperationSwitchDatabase() { | ||
| t := suite.T() | ||
|
|
||
| driver, err := suite.ctr.NewDriver(&core.ConnectionParams{ | ||
| ID: "test-sqlite-2", | ||
| Name: "test-sqlite-2", | ||
| }) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = driver.SelectDatabase("no-op") | ||
| assert.Nil(t, err) | ||
| } | ||
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,16 @@ | ||
| CREATE TABLE IF NOT EXISTS test_table ( | ||
| id INTEGER PRIMARY KEY, | ||
| username TEXT, | ||
| email TEXT | ||
| ); | ||
|
|
||
| INSERT INTO test_table (id, username, email) VALUES | ||
| (1, 'john_doe', 'john@example.com'), | ||
| (2, 'jane_smith', 'jane@example.com'), | ||
| (3, 'bob_wilson', 'bob@example.com'); | ||
|
|
||
| CREATE VIEW IF NOT EXISTS test_view AS | ||
| SELECT id, username, email | ||
| FROM test_table | ||
| WHERE id = 2; | ||
|
|
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,100 @@ | ||
| package testhelpers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/docker/docker/api/types/container" | ||
| "github.com/kndndrj/nvim-dbee/dbee/adapters" | ||
| "github.com/kndndrj/nvim-dbee/dbee/core" | ||
| tc "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/wait" | ||
| ) | ||
|
|
||
| type SQLiteContainer struct { | ||
| tc.Container | ||
| ConnURL string | ||
| Driver *core.Connection | ||
| TempDir string | ||
| } | ||
|
|
||
| // NewSQLiteContainer creates a new sqlite container with | ||
| // default adapter and connection. The params.URL is overwritten. | ||
| // It uses a temporary directory (usually the test suite tempDir) to store the db file. | ||
| // The tmpDir is then mounted to the container and all the dependencies are installed | ||
| // in the container file, while still being able to connect to the db file in the host. | ||
| func NewSQLiteContainer(ctx context.Context, params *core.ConnectionParams, tmpDir string) (*SQLiteContainer, error) { | ||
| seedFile, err := GetTestDataFile("sqlite_seed.sql") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| dbName, containerDBPath := "test.db", "/container/db" | ||
| entrypointCmd := []string{ | ||
| "apk add sqlite=3.48.0-r0", | ||
| fmt.Sprintf("sqlite3 %s/%s < %s", containerDBPath, dbName, seedFile.Name()), | ||
| "echo 'ready'", | ||
| "tail -f /dev/null", // hack to keep the container running indefinitely | ||
| } | ||
|
|
||
| req := tc.ContainerRequest{ | ||
| Image: "alpine:3.21", | ||
| Files: []tc.ContainerFile{ | ||
| { | ||
| Reader: seedFile, | ||
| ContainerFilePath: seedFile.Name(), | ||
| FileMode: 0o755, | ||
| }, | ||
| }, | ||
| HostConfigModifier: func(hc *container.HostConfig) { | ||
| hc.Binds = append(hc.Binds, fmt.Sprintf("%s:%s", tmpDir, containerDBPath)) | ||
| }, | ||
| Cmd: []string{"sh", "-c", strings.Join(entrypointCmd, " && ")}, | ||
| WaitingFor: wait.ForLog("ready").WithStartupTimeout(5 * time.Second), | ||
| } | ||
|
|
||
| ctr, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{ | ||
| ContainerRequest: req, | ||
| ProviderType: GetContainerProvider(), | ||
| Started: true, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if params.Type == "" { | ||
| params.Type = "sqlite" | ||
| } | ||
|
|
||
| connURL := filepath.Join(tmpDir, dbName) | ||
| if params.URL == "" { | ||
| params.URL = connURL | ||
| } | ||
|
|
||
| driver, err := adapters.NewConnection(params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &SQLiteContainer{ | ||
| Container: ctr, | ||
| ConnURL: connURL, | ||
| Driver: driver, | ||
| TempDir: tmpDir, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewDriver helper function to create a new driver with the connection URL. | ||
| func (p *SQLiteContainer) NewDriver(params *core.ConnectionParams) (*core.Connection, error) { | ||
| if params.URL == "" { | ||
| params.URL = p.ConnURL | ||
| } | ||
| if params.Type == "" { | ||
| params.Type = "sqlite" | ||
| } | ||
|
|
||
| return adapters.NewConnection(params) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.