Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(postgres): cover workaround for gorm record not found
  • Loading branch information
jbrockopp committed May 27, 2021
commit b10ca33d48f890ac2770538dcd829ac2b0612a5a
35 changes: 29 additions & 6 deletions database/postgres/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func TestPostgres_Client_GetBuild(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -62,6 +64,10 @@ func TestPostgres_Client_GetBuild(t *testing.T) {
failure: false,
want: _build,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -120,8 +126,10 @@ func TestPostgres_Client_GetLastBuild(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -132,6 +140,10 @@ func TestPostgres_Client_GetLastBuild(t *testing.T) {
failure: false,
want: _build,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -190,8 +202,10 @@ func TestPostgres_Client_GetLastBuildByBranch(t *testing.T) {
[]string{"id", "repo_id", "number", "parent", "event", "status", "error", "enqueued", "created", "started", "finished", "deploy", "deploy_payload", "clone", "source", "title", "message", "commit", "sender", "author", "email", "link", "branch", "ref", "base_ref", "head_ref", "host", "runtime", "distribution", "timestamp"},
).AddRow(1, 1, 1, 0, "", "", "", 0, 0, 0, 0, "", nil, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 0)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -202,6 +216,10 @@ func TestPostgres_Client_GetLastBuildByBranch(t *testing.T) {
failure: false,
want: _build,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -254,11 +272,12 @@ func TestPostgres_Client_GetPendingAndRunningBuilds(t *testing.T) {

// create expected return in mock
_rows := sqlmock.NewRows([]string{"created", "full_name", "number", "status"}).
AddRow(0, "", 1, "").
AddRow(0, "", 2, "")
AddRow(0, "", 1, "").AddRow(0, "", 2, "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -269,6 +288,10 @@ func TestPostgres_Client_GetPendingAndRunningBuilds(t *testing.T) {
failure: false,
want: []*library.BuildQueue{_buildOne, _buildTwo},
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
16 changes: 14 additions & 2 deletions database/postgres/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func TestPostgres_Client_GetHook(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "source_id", "created", "host", "event", "branch", "error", "status", "link"},
).AddRow(1, 1, 1, 1, "c8da1302-07d6-11ea-882f-4893bca275b8", 0, "", "", "", "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -61,6 +63,10 @@ func TestPostgres_Client_GetHook(t *testing.T) {
failure: false,
want: _hook,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -118,8 +124,10 @@ func TestPostgres_Client_GetLastHook(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "source_id", "created", "host", "event", "branch", "error", "status", "link"},
).AddRow(1, 1, 1, 1, "c8da1302-07d6-11ea-882f-4893bca275b8", 0, "", "", "", "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -130,6 +138,10 @@ func TestPostgres_Client_GetLastHook(t *testing.T) {
failure: false,
want: _hook,
},
{
failure: false,
want: nil,
},
}

// run tests
Expand Down
16 changes: 14 additions & 2 deletions database/postgres/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ func TestPostgres_Client_GetStepLog(t *testing.T) {
[]string{"id", "build_id", "repo_id", "service_id", "step_id", "data"},
).AddRow(1, 1, 1, 0, 1, []byte{})

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -123,6 +125,10 @@ func TestPostgres_Client_GetStepLog(t *testing.T) {
failure: false,
want: _log,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down Expand Up @@ -173,8 +179,10 @@ func TestPostgres_Client_GetServiceLog(t *testing.T) {
[]string{"id", "build_id", "repo_id", "service_id", "step_id", "data"},
).AddRow(1, 1, 1, 1, 0, []byte{})

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -185,6 +193,10 @@ func TestPostgres_Client_GetServiceLog(t *testing.T) {
failure: false,
want: _log,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
8 changes: 7 additions & 1 deletion database/postgres/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func TestPostgres_Client_GetRepo(t *testing.T) {
[]string{"id", "user_id", "hash", "org", "name", "full_name", "link", "clone", "branch", "timeout", "counter", "visibility", "private", "trusted", "active", "allow_pull", "allow_push", "allow_deploy", "allow_tag", "allow_comment"},
).AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", 0, 0, "public", false, false, false, false, false, false, false, false)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -56,6 +58,10 @@ func TestPostgres_Client_GetRepo(t *testing.T) {
failure: false,
want: _repo,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
33 changes: 24 additions & 9 deletions database/postgres/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func TestPostgres_Client_GetSecret_Org(t *testing.T) {
[]string{"id", "type", "org", "repo", "team", "name", "value", "images", "events", "allow_command"},
).AddRow(1, "org", "foo", "*", "", "bar", "baz", "{}", "{}", false)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -53,8 +55,11 @@ func TestPostgres_Client_GetSecret_Org(t *testing.T) {
}{
{
failure: false,

want: _secret,
want: _secret,
},
{
failure: true,
want: nil,
},
}

Expand Down Expand Up @@ -107,8 +112,10 @@ func TestPostgres_Client_GetSecret_Repo(t *testing.T) {
[]string{"id", "type", "org", "repo", "team", "name", "value", "images", "events", "allow_command"},
).AddRow(1, "repo", "foo", "bar", "", "baz", "foob", "{}", "{}", false)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -117,8 +124,11 @@ func TestPostgres_Client_GetSecret_Repo(t *testing.T) {
}{
{
failure: false,

want: _secret,
want: _secret,
},
{
failure: true,
want: nil,
},
}

Expand Down Expand Up @@ -171,8 +181,10 @@ func TestPostgres_Client_GetSecret_Shared(t *testing.T) {
[]string{"id", "type", "org", "repo", "team", "name", "value", "images", "events", "allow_command"},
).AddRow(1, "shared", "foo", "", "bar", "baz", "foob", "{}", "{}", false)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -181,8 +193,11 @@ func TestPostgres_Client_GetSecret_Shared(t *testing.T) {
}{
{
failure: false,

want: _secret,
want: _secret,
},
{
failure: true,
want: nil,
},
}

Expand Down
8 changes: 7 additions & 1 deletion database/postgres/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func TestPostgres_Client_GetService(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "name", "image", "status", "error", "exit_code", "created", "started", "finished", "host", "runtime", "distribution"},
).AddRow(1, 1, 1, 1, "foo", "bar", "", "", 0, 0, 0, 0, "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -60,6 +62,10 @@ func TestPostgres_Client_GetService(t *testing.T) {
failure: false,
want: _service,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
8 changes: 7 additions & 1 deletion database/postgres/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func TestPostgres_Client_GetStep(t *testing.T) {
[]string{"id", "repo_id", "build_id", "number", "name", "image", "stage", "status", "error", "exit_code", "created", "started", "finished", "host", "runtime", "distribution"},
).AddRow(1, 1, 1, 1, "foo", "bar", "", "", "", 0, 0, 0, 0, "", "", "")

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -60,6 +62,10 @@ func TestPostgres_Client_GetStep(t *testing.T) {
failure: false,
want: _step,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
8 changes: 7 additions & 1 deletion database/postgres/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func TestPostgres_Client_GetUser(t *testing.T) {
[]string{"id", "name", "refresh_token", "token", "hash", "favorites", "active", "admin"},
).AddRow(1, "foo", "", "bar", "baz", "{}", false, false)

// ensure the mock expects the query
// ensure the mock expects the query for test case 1
_mock.ExpectQuery(_query.SQL.String()).WillReturnRows(_rows)
// ensure the mock expects the error for test case 2
_mock.ExpectQuery(_query.SQL.String()).WillReturnError(gorm.ErrRecordNotFound)

// setup tests
tests := []struct {
Expand All @@ -53,6 +55,10 @@ func TestPostgres_Client_GetUser(t *testing.T) {
failure: false,
want: _user,
},
{
failure: true,
want: nil,
},
}

// run tests
Expand Down
Loading