|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/kndndrj/nvim-dbee/dbee/core" |
| 9 | + th "github.com/kndndrj/nvim-dbee/dbee/tests/testhelpers" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + tsuite "github.com/stretchr/testify/suite" |
| 12 | + tc "github.com/testcontainers/testcontainers-go" |
| 13 | +) |
| 14 | + |
| 15 | +// SQLiteTestSuite is the test suite for the sqlite adapter. |
| 16 | +type SQLiteTestSuite struct { |
| 17 | + tsuite.Suite |
| 18 | + ctr *th.SQLiteContainer |
| 19 | + ctx context.Context |
| 20 | + d *core.Connection |
| 21 | +} |
| 22 | + |
| 23 | +func TestSQLiteTestSuite(t *testing.T) { |
| 24 | + tsuite.Run(t, new(SQLiteTestSuite)) |
| 25 | +} |
| 26 | + |
| 27 | +func (suite *SQLiteTestSuite) SetupSuite() { |
| 28 | + suite.ctx = context.Background() |
| 29 | + tempDir := suite.T().TempDir() |
| 30 | + |
| 31 | + params := &core.ConnectionParams{ID: "test-sqlite", Name: "test-sqlite"} |
| 32 | + ctr, err := th.NewSQLiteContainer(suite.ctx, params, tempDir) |
| 33 | + if err != nil { |
| 34 | + log.Fatal(err) |
| 35 | + } |
| 36 | + |
| 37 | + suite.ctr, suite.d = ctr, ctr.Driver |
| 38 | +} |
| 39 | + |
| 40 | +func (suite *SQLiteTestSuite) TeardownSuite() { |
| 41 | + tc.CleanupContainer(suite.T(), suite.ctr) |
| 42 | +} |
| 43 | + |
| 44 | +func (suite *SQLiteTestSuite) TestShouldErrorInvalidQuery() { |
| 45 | + t := suite.T() |
| 46 | + |
| 47 | + want := "syntax error" |
| 48 | + |
| 49 | + call := suite.d.Execute("invalid sql", func(cs core.CallState, c *core.Call) { |
| 50 | + if cs == core.CallStateExecutingFailed { |
| 51 | + assert.ErrorContains(t, c.Err(), want) |
| 52 | + } |
| 53 | + }) |
| 54 | + assert.NotNil(t, call) |
| 55 | +} |
| 56 | + |
| 57 | +func (suite *SQLiteTestSuite) TestShouldCancelQuery() { |
| 58 | + t := suite.T() |
| 59 | + want := []core.CallState{core.CallStateExecuting, core.CallStateCanceled} |
| 60 | + |
| 61 | + _, got, err := th.GetResultWithCancel(t, suite.d, "SELECT 1") |
| 62 | + assert.NoError(t, err) |
| 63 | + |
| 64 | + assert.Equal(t, want, got) |
| 65 | +} |
| 66 | + |
| 67 | +func (suite *SQLiteTestSuite) TestShouldReturnManyRows() { |
| 68 | + t := suite.T() |
| 69 | + |
| 70 | + wantStates := []core.CallState{ |
| 71 | + core.CallStateExecuting, core.CallStateRetrieving, core.CallStateArchived, |
| 72 | + } |
| 73 | + wantCols := []string{"id", "username"} |
| 74 | + wantRows := []core.Row{ |
| 75 | + {int64(1), "john_doe"}, |
| 76 | + {int64(2), "jane_smith"}, |
| 77 | + {int64(3), "bob_wilson"}, |
| 78 | + } |
| 79 | + |
| 80 | + query := "SELECT id, username FROM test_table" |
| 81 | + |
| 82 | + gotRows, gotCols, gotStates, err := th.GetResult(t, suite.d, query) |
| 83 | + assert.NoError(t, err) |
| 84 | + |
| 85 | + assert.ElementsMatch(t, wantCols, gotCols) |
| 86 | + assert.ElementsMatch(t, wantStates, gotStates) |
| 87 | + assert.Equal(t, wantRows, gotRows) |
| 88 | +} |
| 89 | + |
| 90 | +func (suite *SQLiteTestSuite) TestShouldReturnOneRow() { |
| 91 | + t := suite.T() |
| 92 | + |
| 93 | + wantStates := []core.CallState{ |
| 94 | + core.CallStateExecuting, core.CallStateRetrieving, core.CallStateArchived, |
| 95 | + } |
| 96 | + wantCols := []string{"id", "username"} |
| 97 | + wantRows := []core.Row{{int64(2), "jane_smith"}} |
| 98 | + |
| 99 | + query := "SELECT id, username FROM test_view" |
| 100 | + |
| 101 | + gotRows, gotCols, gotStates, err := th.GetResult(t, suite.d, query) |
| 102 | + assert.NoError(t, err) |
| 103 | + |
| 104 | + assert.ElementsMatch(t, wantCols, gotCols) |
| 105 | + assert.ElementsMatch(t, wantStates, gotStates) |
| 106 | + assert.Equal(t, wantRows, gotRows) |
| 107 | +} |
| 108 | + |
| 109 | +func (suite *SQLiteTestSuite) TestShouldReturnStructure() { |
| 110 | + t := suite.T() |
| 111 | + |
| 112 | + var ( |
| 113 | + wantSchema = "sqlite_schema" |
| 114 | + wantSomeTable = "test_table" |
| 115 | + wantSomeView = "test_view" |
| 116 | + ) |
| 117 | + |
| 118 | + structure, err := suite.d.GetStructure() |
| 119 | + assert.NoError(t, err) |
| 120 | + |
| 121 | + gotSchemas := th.GetSchemas(t, structure) |
| 122 | + assert.Contains(t, gotSchemas, wantSchema) |
| 123 | + |
| 124 | + gotTables := th.GetModels(t, structure, core.StructureTypeTable) |
| 125 | + assert.Contains(t, gotTables, wantSomeTable) |
| 126 | + |
| 127 | + gotViews := th.GetModels(t, structure, core.StructureTypeView) |
| 128 | + assert.Contains(t, gotViews, wantSomeView) |
| 129 | +} |
| 130 | + |
| 131 | +func (suite *SQLiteTestSuite) TestShouldReturnColumns() { |
| 132 | + t := suite.T() |
| 133 | + |
| 134 | + want := []*core.Column{ |
| 135 | + {Name: "id", Type: "INTEGER"}, |
| 136 | + {Name: "username", Type: "TEXT"}, |
| 137 | + {Name: "email", Type: "TEXT"}, |
| 138 | + } |
| 139 | + |
| 140 | + got, err := suite.d.GetColumns(&core.TableOptions{ |
| 141 | + Table: "test_table", |
| 142 | + Schema: "sqlite_schema", |
| 143 | + Materialization: core.StructureTypeTable, |
| 144 | + }) |
| 145 | + |
| 146 | + assert.NoError(t, err) |
| 147 | + assert.Equal(t, want, got) |
| 148 | +} |
0 commit comments