Skip to content

Commit

Permalink
JPRO-3315 Add Additional Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-brown-jamf committed Dec 27, 2023
1 parent 4306164 commit 36766ea
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysqldump
import (
"bytes"
"database/sql"
"errors"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestGetServerVersionOk(t *testing.T) {
assert.Equal(t, "test_version", meta.ServerVersion)
}

func TestCreateTableSQLOk(t *testing.T) {
func TestCreateSQLSQLOk(t *testing.T) {
data, mock, err := getMockData()
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
defer data.Close()
Expand All @@ -147,6 +148,83 @@ func TestCreateTableSQLOk(t *testing.T) {
}
}

func TestCreateSQLQueryFail(t *testing.T) {
data, mock, err := getMockData()
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
defer data.Close()

queryError := errors.New("query failure")
mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnError(queryError)

table := data.createTable("Test_Table")

result, err := table.CreateSQL()
assert.Error(t, err)
assert.Equal(t, queryError, err)

// we make sure that all expectations were met
assert.NoError(t, mock.ExpectationsWereMet(), "there were unfulfilled expections")

expectedResult := ""

if !reflect.DeepEqual(result, expectedResult) {
t.Fatalf("expected %#v, got %#v", expectedResult, result)
}
}

func TestCreateSQLWrongTable(t *testing.T) {
data, mock, err := getMockData()
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
defer data.Close()

rows := sqlmock.NewRows([]string{"Table", "Create Table"}).
AddRow("Diff_Table", "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`s` char(60) DEFAULT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1")

mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(rows)

table := data.createTable("Test_Table")

result, err := table.CreateSQL()
assert.Error(t, err)
expectedError := errors.New("Returned table is not the same as requested table")
assert.Equal(t, expectedError, err)

// we make sure that all expectations were met
assert.NoError(t, mock.ExpectationsWereMet(), "there were unfulfilled expections")

expectedResult := ""

if !reflect.DeepEqual(result, expectedResult) {
t.Fatalf("expected %#v, got %#v", expectedResult, result)
}
}

func TestCreateTableInvalidColumns(t *testing.T) {
data, mock, err := getMockData()
assert.NoError(t, err, "an error was not expected when opening a stub database connection")
defer data.Close()

rows := sqlmock.NewRows([]string{"Table"}).
AddRow("Test_Table")

mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(rows)

table := data.createTable("Test_Table")

result, err := table.CreateSQL()
assert.Error(t, err)
assert.Equal(t, errors.New("database column information is malformed"), err)

// we make sure that all expectations were met
assert.NoError(t, mock.ExpectationsWereMet(), "there were unfulfilled expections")

expectedResult := ""

if !reflect.DeepEqual(result, expectedResult) {
t.Fatalf("expected %#v, got %#v", expectedResult, result)
}
}

func mockTableSelect(mock sqlmock.Sqlmock, name string) {
cols := sqlmock.NewRows([]string{"Field", "Extra"}).
AddRow("id", "").
Expand Down

0 comments on commit 36766ea

Please sign in to comment.