-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(sourcerer database functions): Add tests for db calls
Figured out how to mock the db and added some initial test coverage for sourcerer.
- Loading branch information
1 parent
29b8fe8
commit 8b399ec
Showing
15 changed files
with
370 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -15,5 +15,9 @@ jobs: | |
uses: actions/[email protected] | ||
with: | ||
go-version: ">=1.22.1" | ||
- name: Run tests | ||
- name: Run main tests | ||
run: go test | ||
- name: Run `sourcerer` tests | ||
run: | | ||
cd sourcerer | ||
go test |
This file contains 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 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 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 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 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 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,39 @@ | ||
package sourcerer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/gwenwindflower/tbd/shared" | ||
) | ||
|
||
func TestConnectToDb(t *testing.T) { | ||
cd := shared.ConnectionDetails{ | ||
ConnType: "snowflake", | ||
Account: "dunedain.snowflakecomputing.com", | ||
Username: "aragorn", | ||
Database: "gondor", | ||
Schema: "minas-tirith", | ||
} | ||
conn, err := GetConn(cd) | ||
if err != nil { | ||
t.Errorf("GetConn failed: %v", err) | ||
} | ||
SfConn, ok := conn.(*SfConn) | ||
if !ok { | ||
t.Errorf("conn not of type SfConn: %v", err) | ||
} | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
SfConn.Db = db | ||
defer SfConn.Db.Close() | ||
mock.ExpectBegin() | ||
if _, err := SfConn.Db.Begin(); err != nil { | ||
t.Errorf("error '%s' was not expected, while pinging db", err) | ||
} | ||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} |
This file contains 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,57 @@ | ||
package sourcerer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/gwenwindflower/tbd/shared" | ||
) | ||
|
||
func TestGetColumnsSnowflake(t *testing.T) { | ||
t.SkipNow() | ||
ctx := context.Background() | ||
st := shared.SourceTable{ | ||
Name: "table1", | ||
} | ||
cd := shared.ConnectionDetails{ | ||
ConnType: "snowflake", | ||
Account: "dunedain.snowflakecomputing.com", | ||
Username: "aragorn", | ||
Database: "gondor", | ||
Schema: "minas-tirith", | ||
} | ||
conn, err := GetConn(cd) | ||
if err != nil { | ||
t.Errorf("GetConn failed: %v", err) | ||
} | ||
if conn == nil { | ||
t.Errorf("GetConn failed: conn is nil") | ||
} | ||
SfConn, ok := conn.(*SfConn) | ||
if !ok { | ||
t.Errorf("GetConn failed: conn is not of type SfConn") | ||
} | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
SfConn.Db = db | ||
defer SfConn.Db.Close() | ||
q := fmt.Sprintf("SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = '%s' AND table_name = '%s'", SfConn.Schema, st.Name) | ||
mock.ExpectQuery(q).WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type"}).AddRow("column1", "varchar").AddRow("column2", "varchar").AddRow("column3", "int")) | ||
cols, err := SfConn.GetColumns(ctx, st) | ||
if err != nil { | ||
t.Errorf("GetColumns failed: %v", err) | ||
} | ||
if len(cols) != 1 { | ||
t.Errorf("GetColumns failed: expected 1 column, got %d", len(cols)) | ||
} | ||
if cols[0].Name != "column1" { | ||
t.Errorf("GetColumns failed: expected column name %s, got %s", "column1", cols[0].Name) | ||
} | ||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.